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 {
return &schema.Resource{
Create: resourceAwsVpcPeeringCreate,
Read: resourceAwsVpcPeeringRead,
Update: resourceAwsVpcPeeringUpdate,
Delete: resourceAwsVpcPeeringDelete,
Create: resourceAwsVPCPeeringCreate,
Read: resourceAwsVPCPeeringRead,
Update: resourceAwsVPCPeeringUpdate,
Delete: resourceAwsVPCPeeringDelete,
Schema: map[string]*schema.Schema{
"peer_owner_id": &schema.Schema{
@ -35,12 +35,20 @@ func resourceAwsVpcPeeringConnection() *schema.Resource {
Required: true,
ForceNew: true,
},
"auto_accept": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
},
"accept_status": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
func resourceAwsVpcPeeringCreate(d *schema.ResourceData, meta interface{}) error {
func resourceAwsVPCPeeringCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
// 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)),
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)
if err != nil {
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
rt := resp.VPCPeeringConnection
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
log.Printf(
@ -66,8 +74,8 @@ func resourceAwsVpcPeeringCreate(d *schema.ResourceData, meta interface{}) error
d.Id())
stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: "ready",
Refresh: resourceAwsVpcPeeringConnectionStateRefreshFunc(conn, d.Id()),
Target: "pending-acceptance",
Refresh: resourceAwsVPCPeeringConnectionStateRefreshFunc(conn, d.Id()),
Timeout: 1 * time.Minute,
}
if _, err := stateConf.WaitForState(); err != nil {
@ -76,13 +84,12 @@ func resourceAwsVpcPeeringCreate(d *schema.ResourceData, meta interface{}) error
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
pcRaw, _, err := resourceAwsVpcPeeringConnectionStateRefreshFunc(conn, d.Id())()
pcRaw, _, err := resourceAwsVPCPeeringConnectionStateRefreshFunc(conn, d.Id())()
if err != nil {
return err
}
@ -93,6 +100,7 @@ func resourceAwsVpcPeeringRead(d *schema.ResourceData, meta interface{}) error {
pc := pcRaw.(*ec2.VPCPeeringConnection)
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)
@ -101,7 +109,20 @@ func resourceAwsVpcPeeringRead(d *schema.ResourceData, meta interface{}) error {
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
if err := setTagsSDK(conn, d); err != nil {
@ -110,10 +131,36 @@ func resourceAwsVpcPeeringUpdate(d *schema.ResourceData, meta interface{}) error
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
_, err := conn.DeleteVPCPeeringConnection(
@ -123,9 +170,9 @@ func resourceAwsVpcPeeringDelete(d *schema.ResourceData, meta interface{}) error
return err
}
// resourceAwsVpcPeeringConnectionStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// a VpcPeeringConnection.
func resourceAwsVpcPeeringConnectionStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
// resourceAwsVPCPeeringConnectionStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch
// a VPCPeeringConnection.
func resourceAwsVPCPeeringConnectionStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
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" {
resp = nil
} else {
log.Printf("Error on VpcPeeringConnectionStateRefresh: %s", err)
log.Printf("Error on VPCPeeringConnectionStateRefresh: %s", err)
return nil, "", err
}
}
@ -148,6 +195,6 @@ func resourceAwsVpcPeeringConnectionStateRefreshFunc(conn *ec2.EC2, id string) r
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_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.
* `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.
## Attributes Reference
@ -53,6 +54,7 @@ The following arguments are supported:
The following attributes are exported:
* `id` - The ID of the VPC Peering Connectiona
* `accept_status` - The Status of the VPC peering connection request.
## Notes