2015-02-12 10:45:29 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2015-02-12 10:45:29 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceAwsVpcPeeringConnection() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
2015-03-17 11:34:22 +01:00
|
|
|
Create: resourceAwsVPCPeeringCreate,
|
|
|
|
Read: resourceAwsVPCPeeringRead,
|
|
|
|
Update: resourceAwsVPCPeeringUpdate,
|
|
|
|
Delete: resourceAwsVPCPeeringDelete,
|
2016-06-28 17:04:11 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2015-02-12 10:45:29 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"peer_owner_id": &schema.Schema{
|
2015-03-10 20:44:07 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("AWS_ACCOUNT_ID", nil),
|
2015-02-12 10:45:29 +01:00
|
|
|
},
|
|
|
|
"peer_vpc_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2015-02-18 10:45:12 +01:00
|
|
|
ForceNew: true,
|
2015-02-12 10:45:29 +01:00
|
|
|
},
|
|
|
|
"vpc_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2015-02-18 10:45:12 +01:00
|
|
|
ForceNew: true,
|
2015-02-12 10:45:29 +01:00
|
|
|
},
|
2015-03-09 09:59:52 +01:00
|
|
|
"auto_accept": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"accept_status": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2016-08-19 12:19:49 +02:00
|
|
|
"accepter": vpcPeeringConnectionOptionsSchema(),
|
|
|
|
"requester": vpcPeeringConnectionOptionsSchema(),
|
|
|
|
"tags": tagsSchema(),
|
2015-02-12 10:45:29 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 11:34:22 +01:00
|
|
|
func resourceAwsVPCPeeringCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-05-05 17:45:30 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-02-12 10:45:29 +01:00
|
|
|
|
|
|
|
// Create the vpc peering connection
|
2015-08-17 20:27:16 +02:00
|
|
|
createOpts := &ec2.CreateVpcPeeringConnectionInput{
|
|
|
|
PeerOwnerId: aws.String(d.Get("peer_owner_id").(string)),
|
|
|
|
PeerVpcId: aws.String(d.Get("peer_vpc_id").(string)),
|
|
|
|
VpcId: aws.String(d.Get("vpc_id").(string)),
|
2015-02-12 10:45:29 +01:00
|
|
|
}
|
2016-08-19 12:19:49 +02:00
|
|
|
log.Printf("[DEBUG] VPC Peering Create options: %#v", createOpts)
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
resp, err := conn.CreateVpcPeeringConnection(createOpts)
|
2015-02-12 10:45:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating vpc peering connection: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ID and store it
|
2015-08-17 20:27:16 +02:00
|
|
|
rt := resp.VpcPeeringConnection
|
|
|
|
d.SetId(*rt.VpcPeeringConnectionId)
|
2015-03-17 11:34:22 +01:00
|
|
|
log.Printf("[INFO] VPC Peering Connection ID: %s", d.Id())
|
2015-02-12 10:45:29 +01:00
|
|
|
|
|
|
|
// Wait for the vpc peering connection to become available
|
2016-08-19 12:19:49 +02:00
|
|
|
log.Printf("[DEBUG] Waiting for VPC Peering Connection (%s) to become available.", d.Id())
|
2015-02-12 10:45:29 +01:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"pending"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"pending-acceptance"},
|
2015-05-05 17:45:30 +02:00
|
|
|
Refresh: resourceAwsVPCPeeringConnectionStateRefreshFunc(conn, d.Id()),
|
2015-02-12 10:45:29 +01:00
|
|
|
Timeout: 1 * time.Minute,
|
|
|
|
}
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
return fmt.Errorf(
|
2016-08-19 12:19:49 +02:00
|
|
|
"Error waiting for VPC Peering Connection (%s) to become available: %s",
|
2015-02-12 10:45:29 +01:00
|
|
|
d.Id(), err)
|
|
|
|
}
|
|
|
|
|
2015-03-17 11:34:22 +01:00
|
|
|
return resourceAwsVPCPeeringUpdate(d, meta)
|
2015-02-12 10:45:29 +01:00
|
|
|
}
|
|
|
|
|
2015-03-17 11:34:22 +01:00
|
|
|
func resourceAwsVPCPeeringRead(d *schema.ResourceData, meta interface{}) error {
|
2015-05-05 17:45:30 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
|
|
|
pcRaw, _, err := resourceAwsVPCPeeringConnectionStateRefreshFunc(conn, d.Id())()
|
2015-02-12 10:45:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-19 12:19:49 +02:00
|
|
|
|
2015-02-12 10:45:29 +01:00
|
|
|
if pcRaw == nil {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
pc := pcRaw.(*ec2.VpcPeeringConnection)
|
2015-02-12 10:45:29 +01:00
|
|
|
|
2015-06-29 07:35:02 +02:00
|
|
|
// The failed status is a status that we can assume just means the
|
|
|
|
// connection is gone. Destruction isn't allowed, and it eventually
|
|
|
|
// just "falls off" the console. See GH-2322
|
2016-03-31 22:05:10 +02:00
|
|
|
if pc.Status != nil {
|
2016-08-19 12:19:49 +02:00
|
|
|
status := map[string]bool{
|
|
|
|
"deleted": true,
|
|
|
|
"deleting": true,
|
|
|
|
"expired": true,
|
|
|
|
"failed": true,
|
|
|
|
"rejected": true,
|
|
|
|
}
|
|
|
|
if _, ok := status[*pc.Status.Code]; ok {
|
|
|
|
log.Printf("[DEBUG] VPC Peering Connection (%s) in state (%s), removing.",
|
|
|
|
d.Id(), *pc.Status.Code)
|
2016-03-31 22:05:10 +02:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-29 07:35:02 +02:00
|
|
|
}
|
|
|
|
|
2015-03-17 11:27:33 +01:00
|
|
|
d.Set("accept_status", *pc.Status.Code)
|
2016-08-19 12:19:49 +02:00
|
|
|
d.Set("peer_owner_id", *pc.AccepterVpcInfo.OwnerId)
|
|
|
|
d.Set("peer_vpc_id", *pc.AccepterVpcInfo.VpcId)
|
|
|
|
d.Set("vpc_id", *pc.RequesterVpcInfo.VpcId)
|
|
|
|
|
|
|
|
err = d.Set("accepter", flattenPeeringOptions(pc.AccepterVpcInfo.PeeringOptions))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.Set("requester", flattenPeeringOptions(pc.RequesterVpcInfo.PeeringOptions))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.Set("tags", tagsToMap(pc.Tags))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-09 09:59:52 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-17 11:34:22 +01:00
|
|
|
func resourceVPCPeeringConnectionAccept(conn *ec2.EC2, id string) (string, error) {
|
2016-08-19 12:19:49 +02:00
|
|
|
log.Printf("[INFO] Accept VPC Peering Connection with ID: %s", id)
|
2015-03-09 09:59:52 +01:00
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
req := &ec2.AcceptVpcPeeringConnectionInput{
|
|
|
|
VpcPeeringConnectionId: aws.String(id),
|
2015-03-09 09:59:52 +01:00
|
|
|
}
|
2015-02-12 10:45:29 +01:00
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
resp, err := conn.AcceptVpcPeeringConnection(req)
|
2015-10-13 13:20:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
pc := resp.VpcPeeringConnection
|
2016-08-19 12:19:49 +02:00
|
|
|
|
|
|
|
return *pc.Status.Code, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceVPCPeeringConnectionOptionsModify(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ec2conn
|
|
|
|
|
|
|
|
modifyOpts := &ec2.ModifyVpcPeeringConnectionOptionsInput{
|
|
|
|
VpcPeeringConnectionId: aws.String(d.Id()),
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("accepter"); ok {
|
|
|
|
if s := v.([]interface{}); len(s) > 0 {
|
|
|
|
modifyOpts.AccepterPeeringConnectionOptions = expandPeeringOptions(
|
|
|
|
s[0].(map[string]interface{}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("requester"); ok {
|
|
|
|
if s := v.([]interface{}); len(s) > 0 {
|
|
|
|
modifyOpts.RequesterPeeringConnectionOptions = expandPeeringOptions(
|
|
|
|
s[0].(map[string]interface{}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] VPC Peering Connection modify options: %#v", modifyOpts)
|
|
|
|
if _, err := conn.ModifyVpcPeeringConnectionOptions(modifyOpts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2015-02-12 10:45:29 +01:00
|
|
|
}
|
2015-02-18 10:45:12 +01:00
|
|
|
|
2015-03-17 11:34:22 +01:00
|
|
|
func resourceAwsVPCPeeringUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-05-05 17:45:30 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-02-18 10:45:12 +01:00
|
|
|
|
2015-05-12 21:58:10 +02:00
|
|
|
if err := setTags(conn, d); err != nil {
|
2015-02-18 10:45:12 +01:00
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
d.SetPartial("tags")
|
|
|
|
}
|
|
|
|
|
2015-03-17 11:27:33 +01:00
|
|
|
if _, ok := d.GetOk("auto_accept"); ok {
|
2015-05-05 17:45:30 +02:00
|
|
|
pcRaw, _, err := resourceAwsVPCPeeringConnectionStateRefreshFunc(conn, d.Id())()
|
2015-03-17 11:27:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-19 12:19:49 +02:00
|
|
|
|
2015-03-17 11:27:33 +01:00
|
|
|
if pcRaw == nil {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2015-08-17 20:27:16 +02:00
|
|
|
pc := pcRaw.(*ec2.VpcPeeringConnection)
|
2015-03-17 11:27:33 +01:00
|
|
|
|
2015-10-13 13:20:46 +02:00
|
|
|
if pc.Status != nil && *pc.Status.Code == "pending-acceptance" {
|
2015-05-05 17:45:30 +02:00
|
|
|
status, err := resourceVPCPeeringConnectionAccept(conn, d.Id())
|
2015-03-17 11:27:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-19 12:19:49 +02:00
|
|
|
log.Printf("[DEBUG] VPC Peering Connection accept status: %s", status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("accepter") || d.HasChange("requester") {
|
|
|
|
if err := resourceVPCPeeringConnectionOptionsModify(d, meta); err != nil {
|
|
|
|
return err
|
2015-03-17 11:27:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 11:34:22 +01:00
|
|
|
return resourceAwsVPCPeeringRead(d, meta)
|
2015-02-18 10:45:12 +01:00
|
|
|
}
|
|
|
|
|
2015-03-17 11:34:22 +01:00
|
|
|
func resourceAwsVPCPeeringDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-05-05 17:45:30 +02:00
|
|
|
conn := meta.(*AWSClient).ec2conn
|
2015-02-12 10:45:29 +01:00
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
_, err := conn.DeleteVpcPeeringConnection(
|
|
|
|
&ec2.DeleteVpcPeeringConnectionInput{
|
|
|
|
VpcPeeringConnectionId: aws.String(d.Id()),
|
2015-03-10 21:18:30 +01:00
|
|
|
})
|
2016-08-19 12:19:49 +02:00
|
|
|
|
2015-02-12 10:45:29 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-17 11:34:22 +01:00
|
|
|
// resourceAwsVPCPeeringConnectionStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
|
|
|
|
// a VPCPeeringConnection.
|
|
|
|
func resourceAwsVPCPeeringConnectionStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
2015-02-12 10:45:29 +01:00
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
resp, err := conn.DescribeVpcPeeringConnections(&ec2.DescribeVpcPeeringConnectionsInput{
|
|
|
|
VpcPeeringConnectionIds: []*string{aws.String(id)},
|
2015-03-10 21:18:30 +01:00
|
|
|
})
|
2015-02-12 10:45:29 +01:00
|
|
|
if err != nil {
|
2015-05-20 13:21:23 +02:00
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpcPeeringConnectionID.NotFound" {
|
2015-02-12 10:45:29 +01:00
|
|
|
resp = nil
|
|
|
|
} else {
|
2015-03-17 11:34:22 +01:00
|
|
|
log.Printf("Error on VPCPeeringConnectionStateRefresh: %s", err)
|
2015-02-12 10:45:29 +01:00
|
|
|
return nil, "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp == nil {
|
|
|
|
// Sometimes AWS just has consistency issues and doesn't see
|
|
|
|
// our instance yet. Return an empty state.
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
pc := resp.VpcPeeringConnections[0]
|
2015-02-12 10:45:29 +01:00
|
|
|
|
2015-03-17 11:27:33 +01:00
|
|
|
return pc, *pc.Status.Code, nil
|
2015-02-12 10:45:29 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-19 12:19:49 +02:00
|
|
|
|
|
|
|
func vpcPeeringConnectionOptionsSchema() *schema.Schema {
|
|
|
|
return &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
MaxItems: 1,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"allow_remote_vpc_dns_resolution": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"allow_classic_link_to_remote_vpc": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
"allow_vpc_to_remote_classic_link": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func flattenPeeringOptions(options *ec2.VpcPeeringConnectionOptionsDescription) (results []map[string]interface{}) {
|
|
|
|
m := map[string]interface{}{
|
|
|
|
"allow_remote_vpc_dns_resolution": *options.AllowDnsResolutionFromRemoteVpc,
|
|
|
|
"allow_classic_link_to_remote_vpc": *options.AllowEgressFromLocalClassicLinkToRemoteVpc,
|
|
|
|
"allow_vpc_to_remote_classic_link": *options.AllowEgressFromLocalVpcToRemoteClassicLink,
|
|
|
|
}
|
|
|
|
results = append(results, m)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandPeeringOptions(m map[string]interface{}) *ec2.PeeringConnectionOptionsRequest {
|
|
|
|
return &ec2.PeeringConnectionOptionsRequest{
|
|
|
|
AllowDnsResolutionFromRemoteVpc: aws.Bool(m["allow_remote_vpc_dns_resolution"].(bool)),
|
|
|
|
AllowEgressFromLocalClassicLinkToRemoteVpc: aws.Bool(m["allow_classic_link_to_remote_vpc"].(bool)),
|
|
|
|
AllowEgressFromLocalVpcToRemoteClassicLink: aws.Bool(m["allow_vpc_to_remote_classic_link"].(bool)),
|
|
|
|
}
|
|
|
|
}
|