Merge pull request #1809 from hashicorp/f-aws-auto-vpc-peer-julienba

provider/aws: Add auto accept option for VPC peering
This commit is contained in:
Clint 2015-05-05 10:49:56 -05:00
commit 7c62329506
2 changed files with 70 additions and 21 deletions

View File

@ -13,10 +13,10 @@ import (
func resourceAwsVpcPeeringConnection() *schema.Resource { func resourceAwsVpcPeeringConnection() *schema.Resource {
return &schema.Resource{ return &schema.Resource{
Create: resourceAwsVpcPeeringCreate, Create: resourceAwsVPCPeeringCreate,
Read: resourceAwsVpcPeeringRead, Read: resourceAwsVPCPeeringRead,
Update: resourceAwsVpcPeeringUpdate, Update: resourceAwsVPCPeeringUpdate,
Delete: resourceAwsVpcPeeringDelete, Delete: resourceAwsVPCPeeringDelete,
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"peer_owner_id": &schema.Schema{ "peer_owner_id": &schema.Schema{
@ -35,12 +35,20 @@ func resourceAwsVpcPeeringConnection() *schema.Resource {
Required: true, Required: true,
ForceNew: true, ForceNew: true,
}, },
"auto_accept": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"accept_status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(), "tags": tagsSchema(),
}, },
} }
} }
func resourceAwsVpcPeeringCreate(d *schema.ResourceData, meta interface{}) error { func resourceAwsVPCPeeringCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn conn := meta.(*AWSClient).ec2conn
// Create the vpc peering connection // Create the vpc peering connection
@ -49,7 +57,7 @@ func resourceAwsVpcPeeringCreate(d *schema.ResourceData, meta interface{}) error
PeerVPCID: aws.String(d.Get("peer_vpc_id").(string)), PeerVPCID: aws.String(d.Get("peer_vpc_id").(string)),
VPCID: aws.String(d.Get("vpc_id").(string)), VPCID: aws.String(d.Get("vpc_id").(string)),
} }
log.Printf("[DEBUG] VpcPeeringCreate create config: %#v", createOpts) log.Printf("[DEBUG] VPCPeeringCreate create config: %#v", createOpts)
resp, err := conn.CreateVPCPeeringConnection(createOpts) resp, err := conn.CreateVPCPeeringConnection(createOpts)
if err != nil { if err != nil {
return fmt.Errorf("Error creating vpc peering connection: %s", err) return fmt.Errorf("Error creating vpc peering connection: %s", err)
@ -58,7 +66,7 @@ func resourceAwsVpcPeeringCreate(d *schema.ResourceData, meta interface{}) error
// Get the ID and store it // Get the ID and store it
rt := resp.VPCPeeringConnection rt := resp.VPCPeeringConnection
d.SetId(*rt.VPCPeeringConnectionID) d.SetId(*rt.VPCPeeringConnectionID)
log.Printf("[INFO] Vpc Peering Connection ID: %s", d.Id()) log.Printf("[INFO] VPC Peering Connection ID: %s", d.Id())
// Wait for the vpc peering connection to become available // Wait for the vpc peering connection to become available
log.Printf( log.Printf(
@ -66,8 +74,8 @@ func resourceAwsVpcPeeringCreate(d *schema.ResourceData, meta interface{}) error
d.Id()) d.Id())
stateConf := &resource.StateChangeConf{ stateConf := &resource.StateChangeConf{
Pending: []string{"pending"}, Pending: []string{"pending"},
Target: "ready", Target: "pending-acceptance",
Refresh: resourceAwsVpcPeeringConnectionStateRefreshFunc(conn, d.Id()), Refresh: resourceAwsVPCPeeringConnectionStateRefreshFunc(conn, d.Id()),
Timeout: 1 * time.Minute, Timeout: 1 * time.Minute,
} }
if _, err := stateConf.WaitForState(); err != nil { if _, err := stateConf.WaitForState(); err != nil {
@ -76,13 +84,12 @@ func resourceAwsVpcPeeringCreate(d *schema.ResourceData, meta interface{}) error
d.Id(), err) d.Id(), err)
} }
// Update our attributes and return return resourceAwsVPCPeeringUpdate(d, meta)
return resourceAwsVpcPeeringUpdate(d, meta)
} }
func resourceAwsVpcPeeringRead(d *schema.ResourceData, meta interface{}) error { func resourceAwsVPCPeeringRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn conn := meta.(*AWSClient).ec2conn
pcRaw, _, err := resourceAwsVpcPeeringConnectionStateRefreshFunc(conn, d.Id())() pcRaw, _, err := resourceAwsVPCPeeringConnectionStateRefreshFunc(conn, d.Id())()
if err != nil { if err != nil {
return err return err
} }
@ -93,6 +100,7 @@ func resourceAwsVpcPeeringRead(d *schema.ResourceData, meta interface{}) error {
pc := pcRaw.(*ec2.VPCPeeringConnection) pc := pcRaw.(*ec2.VPCPeeringConnection)
d.Set("accept_status", *pc.Status.Code)
d.Set("peer_owner_id", pc.AccepterVPCInfo.OwnerID) d.Set("peer_owner_id", pc.AccepterVPCInfo.OwnerID)
d.Set("peer_vpc_id", pc.AccepterVPCInfo.VPCID) d.Set("peer_vpc_id", pc.AccepterVPCInfo.VPCID)
d.Set("vpc_id", pc.RequesterVPCInfo.VPCID) d.Set("vpc_id", pc.RequesterVPCInfo.VPCID)
@ -101,7 +109,20 @@ func resourceAwsVpcPeeringRead(d *schema.ResourceData, meta interface{}) error {
return nil return nil
} }
func resourceAwsVpcPeeringUpdate(d *schema.ResourceData, meta interface{}) error { func resourceVPCPeeringConnectionAccept(conn *ec2.EC2, id string) (string, error) {
log.Printf("[INFO] Accept VPC Peering Connection with id: %s", id)
req := &ec2.AcceptVPCPeeringConnectionInput{
VPCPeeringConnectionID: aws.String(id),
}
resp, err := conn.AcceptVPCPeeringConnection(req)
pc := resp.VPCPeeringConnection
return *pc.Status.Code, err
}
func resourceAwsVPCPeeringUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn conn := meta.(*AWSClient).ec2conn
if err := setTagsSDK(conn, d); err != nil { if err := setTagsSDK(conn, d); err != nil {
@ -110,10 +131,36 @@ func resourceAwsVpcPeeringUpdate(d *schema.ResourceData, meta interface{}) error
d.SetPartial("tags") d.SetPartial("tags")
} }
return resourceAwsRouteTableRead(d, meta) if _, ok := d.GetOk("auto_accept"); ok {
pcRaw, _, err := resourceAwsVPCPeeringConnectionStateRefreshFunc(conn, d.Id())()
if err != nil {
return err
}
if pcRaw == nil {
d.SetId("")
return nil
}
pc := pcRaw.(*ec2.VPCPeeringConnection)
if *pc.Status.Code == "pending-acceptance" {
status, err := resourceVPCPeeringConnectionAccept(conn, d.Id())
log.Printf(
"[DEBUG] VPC Peering connection accept status %s",
status)
if err != nil {
return err
}
}
}
return resourceAwsVPCPeeringRead(d, meta)
} }
func resourceAwsVpcPeeringDelete(d *schema.ResourceData, meta interface{}) error { func resourceAwsVPCPeeringDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn conn := meta.(*AWSClient).ec2conn
_, err := conn.DeleteVPCPeeringConnection( _, err := conn.DeleteVPCPeeringConnection(
@ -123,9 +170,9 @@ func resourceAwsVpcPeeringDelete(d *schema.ResourceData, meta interface{}) error
return err return err
} }
// resourceAwsVpcPeeringConnectionStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch // resourceAwsVPCPeeringConnectionStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// a VpcPeeringConnection. // a VPCPeeringConnection.
func resourceAwsVpcPeeringConnectionStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc { func resourceAwsVPCPeeringConnectionStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) { return func() (interface{}, string, error) {
resp, err := conn.DescribeVPCPeeringConnections(&ec2.DescribeVPCPeeringConnectionsInput{ resp, err := conn.DescribeVPCPeeringConnections(&ec2.DescribeVPCPeeringConnectionsInput{
@ -135,7 +182,7 @@ func resourceAwsVpcPeeringConnectionStateRefreshFunc(conn *ec2.EC2, id string) r
if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidVpcPeeringConnectionID.NotFound" { if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidVpcPeeringConnectionID.NotFound" {
resp = nil resp = nil
} else { } else {
log.Printf("Error on VpcPeeringConnectionStateRefresh: %s", err) log.Printf("Error on VPCPeeringConnectionStateRefresh: %s", err)
return nil, "", err return nil, "", err
} }
} }
@ -148,6 +195,6 @@ func resourceAwsVpcPeeringConnectionStateRefreshFunc(conn *ec2.EC2, id string) r
pc := resp.VPCPeeringConnections[0] pc := resp.VPCPeeringConnections[0]
return pc, "ready", nil return pc, *pc.Status.Code, nil
} }
} }

View File

@ -46,6 +46,7 @@ The following arguments are supported:
* `peer_owner_id` - (Required) The AWS account ID of the owner of the peer VPC. * `peer_owner_id` - (Required) The AWS account ID of the owner of the peer VPC.
* `peer_vpc_id` - (Required) The ID of the VPC with which you are creating the VPC peering connection. * `peer_vpc_id` - (Required) The ID of the VPC with which you are creating the VPC peering connection.
* `vpc_id` - (Required) The ID of the requester VPC. * `vpc_id` - (Required) The ID of the requester VPC.
* `auto_accept` - (Optional) Accept the peering ( you need to be the owner of both vpc)
* `tags` - (Optional) A mapping of tags to assign to the resource. * `tags` - (Optional) A mapping of tags to assign to the resource.
## Attributes Reference ## Attributes Reference
@ -53,6 +54,7 @@ The following arguments are supported:
The following attributes are exported: The following attributes are exported:
* `id` - The ID of the VPC Peering Connectiona * `id` - The ID of the VPC Peering Connectiona
* `accept_status` - The Status of the VPC peering connection request.
## Notes ## Notes