2015-02-12 10:45:29 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2015-04-14 18:23:26 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
|
|
|
"github.com/awslabs/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,
|
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,
|
|
|
|
},
|
2015-02-12 10:45:29 +01:00
|
|
|
"tags": tagsSchema(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-14 18:23:26 +02:00
|
|
|
createOpts := &ec2.CreateVPCPeeringConnectionInput{
|
2015-03-10 21:18:30 +01:00
|
|
|
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
|
|
|
}
|
2015-03-17 11:34:22 +01:00
|
|
|
log.Printf("[DEBUG] VPCPeeringCreate create config: %#v", createOpts)
|
2015-05-05 17:45:30 +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-03-10 21:18:30 +01: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
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] Waiting for vpc peering connection (%s) to become available",
|
|
|
|
d.Id())
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"pending"},
|
2015-03-17 11:27:33 +01:00
|
|
|
Target: "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(
|
|
|
|
"Error waiting for vpc peering (%s) to become available: %s",
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if pcRaw == nil {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-10 21:18:30 +01:00
|
|
|
pc := pcRaw.(*ec2.VPCPeeringConnection)
|
2015-02-12 10:45:29 +01:00
|
|
|
|
2015-03-17 11:27:33 +01:00
|
|
|
d.Set("accept_status", *pc.Status.Code)
|
|
|
|
d.Set("peer_owner_id", pc.AccepterVPCInfo.OwnerID)
|
|
|
|
d.Set("peer_vpc_id", pc.AccepterVPCInfo.VPCID)
|
|
|
|
d.Set("vpc_id", pc.RequesterVPCInfo.VPCID)
|
|
|
|
d.Set("tags", tagsToMapSDK(pc.Tags))
|
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) {
|
2015-03-17 11:27:33 +01:00
|
|
|
|
2015-03-17 11:34:22 +01:00
|
|
|
log.Printf("[INFO] Accept VPC Peering Connection with id: %s", id)
|
2015-03-09 09:59:52 +01:00
|
|
|
|
2015-05-05 17:45:30 +02:00
|
|
|
req := &ec2.AcceptVPCPeeringConnectionInput{
|
2015-03-17 11:27:33 +01:00
|
|
|
VPCPeeringConnectionID: aws.String(id),
|
2015-03-09 09:59:52 +01:00
|
|
|
}
|
2015-02-12 10:45:29 +01:00
|
|
|
|
2015-03-17 11:27:33 +01:00
|
|
|
resp, err := conn.AcceptVPCPeeringConnection(req)
|
|
|
|
pc := resp.VPCPeeringConnection
|
|
|
|
return *pc.Status.Code, err
|
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-04-14 18:23:26 +02:00
|
|
|
if err := setTagsSDK(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
|
|
|
|
}
|
|
|
|
if pcRaw == nil {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pc := pcRaw.(*ec2.VPCPeeringConnection)
|
|
|
|
|
|
|
|
if *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
|
|
|
|
|
|
|
log.Printf(
|
2015-03-17 11:34:22 +01:00
|
|
|
"[DEBUG] VPC Peering connection accept status %s",
|
2015-03-17 11:27:33 +01:00
|
|
|
status)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-14 18:23:26 +02:00
|
|
|
_, err := conn.DeleteVPCPeeringConnection(
|
|
|
|
&ec2.DeleteVPCPeeringConnectionInput{
|
2015-03-10 21:18:30 +01:00
|
|
|
VPCPeeringConnectionID: aws.String(d.Id()),
|
|
|
|
})
|
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-04-14 18:23:26 +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-03-10 21:18:30 +01:00
|
|
|
if ec2err, ok := err.(aws.APIError); 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-04-14 18:23:26 +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
|
|
|
}
|
|
|
|
}
|