From 11ba9b10421ce1f1febb39993226568ed46d59d3 Mon Sep 17 00:00:00 2001 From: jba Date: Mon, 9 Mar 2015 09:59:52 +0100 Subject: [PATCH] add the auto_accecpt option --- .../resource_aws_vpc_peering_connection.go | 50 ++++++++++++++++--- .../providers/aws/r/vpc_peering.html.markdown | 2 + 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/builtin/providers/aws/resource_aws_vpc_peering_connection.go b/builtin/providers/aws/resource_aws_vpc_peering_connection.go index 5a3d8bda1..cbf80331d 100644 --- a/builtin/providers/aws/resource_aws_vpc_peering_connection.go +++ b/builtin/providers/aws/resource_aws_vpc_peering_connection.go @@ -35,6 +35,14 @@ 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(), }, } @@ -76,7 +84,6 @@ func resourceAwsVpcPeeringCreate(d *schema.ResourceData, meta interface{}) error d.Id(), err) } - // Update our attributes and return return resourceAwsVpcPeeringUpdate(d, meta) } @@ -93,10 +100,41 @@ func resourceAwsVpcPeeringRead(d *schema.ResourceData, meta interface{}) error { pc := pcRaw.(*ec2.VPCPeeringConnection) - 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)) + if d.Get("auto_accept").(bool) { + resourceVpcPeeringConnectionAccept(ec2conn, pc, d) + } else { + 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", tagsToMap(pc.Tags)) + + return nil +} + +func resourceVpcPeeringConnectionAccept(conn *ec2.EC2, oldPc *ec2.VpcPeeringConnection, d *schema.ResourceData) error { + if oldPc.Status.Code == "pending-acceptance" { + log.Printf("[INFO] Accept Vpc Peering Connection with id: %s", d.Id()) + _, err := conn.AcceptVpcPeeringConnection(d.Id()) + if err != nil { + return fmt.Errorf("Error accepting vpc peering connection: %s", err) + } + + pcRaw, _, err := resourceAwsVpcPeeringConnectionStateRefreshFunc(conn, d.Id())() + if err != nil { + return err + } + if pcRaw == nil { + d.SetId("") + return nil + } + + pc := pcRaw.(*ec2.VpcPeeringConnection) + d.Set("accept_status", pc.Status.Code) + + } return nil } @@ -110,7 +148,7 @@ func resourceAwsVpcPeeringUpdate(d *schema.ResourceData, meta interface{}) error d.SetPartial("tags") } - return resourceAwsRouteTableRead(d, meta) + return resourceAwsVpcPeeringRead(d, meta) } func resourceAwsVpcPeeringDelete(d *schema.ResourceData, meta interface{}) error { diff --git a/website/source/docs/providers/aws/r/vpc_peering.html.markdown b/website/source/docs/providers/aws/r/vpc_peering.html.markdown index 1d396a584..56e75b0bf 100644 --- a/website/source/docs/providers/aws/r/vpc_peering.html.markdown +++ b/website/source/docs/providers/aws/r/vpc_peering.html.markdown @@ -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