From 5b7ca87541b8e5c2ca2775da11722f750ba03b5d Mon Sep 17 00:00:00 2001 From: Brad Sickles Date: Mon, 23 May 2016 18:24:28 -0400 Subject: [PATCH] Implementing vpc_peering_connection_accept. --- builtin/providers/aws/provider.go | 1 + ...ource_aws_vpc_peering_connection_accept.go | 64 +++++++++++++++++ ..._aws_vpc_peering_connection_accept_test.go | 69 +++++++++++++++++++ .../providers/aws/r/vpc_peering.html.markdown | 4 +- .../aws/r/vpc_peering_accept.html.markdown | 57 +++++++++++++++ website/source/layouts/aws.erb | 4 ++ 6 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 builtin/providers/aws/resource_aws_vpc_peering_connection_accept.go create mode 100644 builtin/providers/aws/resource_aws_vpc_peering_connection_accept_test.go create mode 100644 website/source/docs/providers/aws/r/vpc_peering_accept.html.markdown diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index 9f7f557973ad..3ab7737b1520 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -260,6 +260,7 @@ func Provider() terraform.ResourceProvider { "aws_vpc_dhcp_options_association": resourceAwsVpcDhcpOptionsAssociation(), "aws_vpc_dhcp_options": resourceAwsVpcDhcpOptions(), "aws_vpc_peering_connection": resourceAwsVpcPeeringConnection(), + "aws_vpc_peering_connection_accept": resourceAwsVpcPeeringConnectionAccept(), "aws_vpc": resourceAwsVpc(), "aws_vpc_endpoint": resourceAwsVpcEndpoint(), "aws_vpn_connection": resourceAwsVpnConnection(), diff --git a/builtin/providers/aws/resource_aws_vpc_peering_connection_accept.go b/builtin/providers/aws/resource_aws_vpc_peering_connection_accept.go new file mode 100644 index 000000000000..cb732a87c051 --- /dev/null +++ b/builtin/providers/aws/resource_aws_vpc_peering_connection_accept.go @@ -0,0 +1,64 @@ +package aws + +import ( + "fmt" + "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceAwsVpcPeeringConnectionAccept() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsVPCPeeringAcceptCreate, + Read: resourceAwsVPCPeeringAcceptRead, + Delete: resourceAwsVPCPeeringAcceptDelete, + + Schema: map[string]*schema.Schema{ + "peering_connection_id": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "accept_status": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func resourceAwsVPCPeeringAcceptCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ec2conn + + if cur, ok := d.Get("accept_status").(string); ok && cur == ec2.VpcPeeringConnectionStateReasonCodeActive { + // already accepted + return nil + } + + status, err := resourceVPCPeeringConnectionAccept(conn, d.Id()) + if err != nil { + return err + } + d.Set("accept_status", status) + + // TODO: should we poll until this resolves? VpcPeeringConnectionStateReasonCodePendingAcceptance + + if status != ec2.VpcPeeringConnectionStateReasonCodeActive { + return fmt.Errorf("Error accepting connection, state: %s", status) + } + return nil +} + +func resourceAwsVPCPeeringAcceptRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ec2conn + _, status, err := resourceAwsVPCPeeringConnectionStateRefreshFunc(conn, d.Id())() + if err != nil { + return err + } + d.Set("accept_status", status) + d.SetId(d.Get("peering_connection_id").(string)) + return nil +} + +func resourceAwsVPCPeeringAcceptDelete(d *schema.ResourceData, meta interface{}) error { + return nil +} diff --git a/builtin/providers/aws/resource_aws_vpc_peering_connection_accept_test.go b/builtin/providers/aws/resource_aws_vpc_peering_connection_accept_test.go new file mode 100644 index 000000000000..1d0015e91877 --- /dev/null +++ b/builtin/providers/aws/resource_aws_vpc_peering_connection_accept_test.go @@ -0,0 +1,69 @@ +package aws + +import ( + "fmt" + "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "os" + "testing" +) + +func TestAccAWSVPCPeeringConnectionAccept_basic(t *testing.T) { + var connection ec2.VpcPeeringConnection + + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + if os.Getenv("AWS_ACCOUNT_ID") == "" { + t.Fatal("AWS_ACCOUNT_ID must be set") + } + }, + + IDRefreshName: "aws_vpc_peering_connection.foo", + IDRefreshIgnore: []string{"auto_accept"}, + + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccVpcPeeringAcceptConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSVpcPeeringConnectionExists("aws_vpc_peering_connection.foo", &connection), + testAccCheckAWSVpcPeeringConnectionAccepted(&connection), + ), + }, + }, + }) +} + +func testAccCheckAWSVpcPeeringConnectionAccepted(conn *ec2.VpcPeeringConnection) resource.TestCheckFunc { + return func(s *terraform.State) error { + if conn.Status == nil { + return fmt.Errorf("No vpc peering connection status") + } + if *conn.Status.Code != ec2.VpcPeeringConnectionStateReasonCodeActive { + return fmt.Errorf("Vpc peering connection not accepted: %s", conn.Status.Code) + } + return nil + } +} + +const testAccVpcPeeringAcceptConfig = ` +resource "aws_vpc" "foo" { + cidr_block = "10.0.0.0/16" +} + +resource "aws_vpc" "bar" { + cidr_block = "10.1.0.0/16" +} + +resource "aws_vpc_peering_connection" "foo" { + vpc_id = "${aws_vpc.foo.id}" + peer_vpc_id = "${aws_vpc.bar.id}" + auto_accept = false +} + +resource "aws_vpc_peering_connection_accept" "foo" { + peering_connection_id = "${aws_vpc_peering_connection.foo.id}" +} +` 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 cf3e16eb06e9..6b48e8bc4757 100644 --- a/website/source/docs/providers/aws/r/vpc_peering.html.markdown +++ b/website/source/docs/providers/aws/r/vpc_peering.html.markdown @@ -3,12 +3,12 @@ layout: "aws" page_title: "AWS: aws_vpc_peering_connection" sidebar_current: "docs-aws-resource-vpc-peering" description: |- - Provides an VPC Peering Connection resource. + Provides a VPC Peering Connection resource. --- # aws\_vpc\_peering\_connection -Provides an VPC Peering Connection resource. +Provides a VPC Peering Connection resource. ## Example Usage diff --git a/website/source/docs/providers/aws/r/vpc_peering_accept.html.markdown b/website/source/docs/providers/aws/r/vpc_peering_accept.html.markdown new file mode 100644 index 000000000000..6dc274dfbecc --- /dev/null +++ b/website/source/docs/providers/aws/r/vpc_peering_accept.html.markdown @@ -0,0 +1,57 @@ +--- +layout: "aws" +page_title: "AWS: aws_vpc_peering_connection_accept" +sidebar_current: "docs-aws-resource-vpc-peering-accept" +description: |- + Provides a VPC Peering Connection Accept resource. +--- + +# aws\_vpc\_peering\_connection\_accept + +Provides a VPC Peering Connection Accept resource. + +## Example Usage + +Basic usage: + +``` +resource "aws_vpc" "main" { + cidr_block = "10.0.0.0/16" +} + +provider "aws" { + // another AWS account creds + access_key = "..." + secret_key = "..." + alias = "peer" +} + +resource "aws_vpc" "peer" { + provider = "aws.peer" + cidr_block = "10.1.0.0/16" +} + +resource "aws_vpc_peering_connection" "peer" { + vpc_id = "${aws_vpc.main.id}" + peer_vpc_id = "${aws_vpc.peer.id}" + auto_accept = false +} + +resource "aws_vpc_peering_connection_accept" "peer" { + provider = "aws.peer" + peering_connection_id = "${aws_vpc_peering_connection.peer.id}" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `peering_connection_id` - (Required) The VPC Peering Connection ID to accept. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ID of the VPC Peering Connection. +* `accept_status` - The Status of the VPC peering connection request. diff --git a/website/source/layouts/aws.erb b/website/source/layouts/aws.erb index a4c24890758c..27fe9315702c 100644 --- a/website/source/layouts/aws.erb +++ b/website/source/layouts/aws.erb @@ -786,6 +786,10 @@ aws_vpc_peering_connection + > + aws_vpc_peering_connection_accept + + > aws_vpn_connection