From 101f801846a9baf239a2e0edf93dbdd0f96941a0 Mon Sep 17 00:00:00 2001 From: jba Date: Thu, 12 Feb 2015 10:45:29 +0100 Subject: [PATCH] add aws_vpc_peering_connection --- builtin/providers/aws/provider.go | 1 + .../resource_aws_vpc_peering_connection.go | 129 ++++++++++++++++++ ...esource_aws_vpc_peering_connection_test.go | 79 +++++++++++ .../providers/aws/r/vpc_peering.html.markdown | 58 ++++++++ 4 files changed, 267 insertions(+) create mode 100644 builtin/providers/aws/resource_aws_vpc_peering_connection.go create mode 100644 builtin/providers/aws/resource_aws_vpc_peering_connection_test.go create mode 100644 website/source/docs/providers/aws/r/vpc_peering.html.markdown diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index 90e43011a..0ab2919fd 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -66,6 +66,7 @@ func Provider() terraform.ResourceProvider { "aws_security_group": resourceAwsSecurityGroup(), "aws_subnet": resourceAwsSubnet(), "aws_vpc": resourceAwsVpc(), + "aws_vpc_peering_connection": resourceAwsVpcPeeringConnection(), }, ConfigureFunc: providerConfigure, diff --git a/builtin/providers/aws/resource_aws_vpc_peering_connection.go b/builtin/providers/aws/resource_aws_vpc_peering_connection.go new file mode 100644 index 000000000..8d9711d7b --- /dev/null +++ b/builtin/providers/aws/resource_aws_vpc_peering_connection.go @@ -0,0 +1,129 @@ +package aws + +import ( + "fmt" + "log" + "time" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/helper/schema" + "github.com/mitchellh/goamz/ec2" +) + +func resourceAwsVpcPeeringConnection() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsVpcPeeringCreate, + Read: resourceAwsVpcPeeringConnectionRead, + Update: nil, + Delete: resourceAwsVpcPeeringDelete, + + Schema: map[string]*schema.Schema{ + "peer_owner_id": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + "peer_vpc_id": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + "vpc_id": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + "tags": tagsSchema(), + }, + } +} + +func resourceAwsVpcPeeringCreate(d *schema.ResourceData, meta interface{}) error { + ec2conn := meta.(*AWSClient).ec2conn + + // Create the vpc peering connection + createOpts := &ec2.CreateVpcPeeringConnection{ + PeerOwnerId: d.Get("peer_owner_id").(string), + PeerVpcId: d.Get("peer_vpc_id").(string), + VpcId: d.Get("vpc_id").(string), + } + log.Printf("[DEBUG] VpcPeeringCreate create config: %#v", createOpts) + resp, err := ec2conn.CreateVpcPeeringConnection(createOpts) + if err != nil { + return fmt.Errorf("Error creating vpc peering connection: %s", err) + } + + // Get the ID and store it + rt := &resp.VpcPeeringConnection + d.SetId(rt.VpcPeeringConnectionId) + log.Printf("[INFO] Vpc Peering Connection ID: %s", d.Id()) + + // 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"}, + Target: "ready", + Refresh: resourceAwsVpcPeeringConnectionStateRefreshFunc(ec2conn, d.Id()), + 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) + } + + return nil +} + +func resourceAwsVpcPeeringConnectionRead(d *schema.ResourceData, meta interface{}) error { + ec2conn := meta.(*AWSClient).ec2conn + pcRaw, _, err := resourceAwsVpcPeeringConnectionStateRefreshFunc(ec2conn, d.Id())() + if err != nil { + return err + } + if pcRaw == nil { + d.SetId("") + return nil + } + + 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", tagsToMap(pc.Tags)) + + return nil +} +func resourceAwsVpcPeeringDelete(d *schema.ResourceData, meta interface{}) error { + ec2conn := meta.(*AWSClient).ec2conn + + _, err := ec2conn.DeleteVpcPeeringConnection(d.Id()) + return err +} + +// 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.DescribeVpcPeeringConnection([]string{id}, ec2.NewFilter()) + if err != nil { + if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidVpcPeeringConnectionID.NotFound" { + resp = nil + } else { + log.Printf("Error on VpcPeeringConnectionStateRefresh: %s", err) + 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 + } + + pc := &resp.VpcPeeringConnections[0] + + return pc, "ready", nil + } +} diff --git a/builtin/providers/aws/resource_aws_vpc_peering_connection_test.go b/builtin/providers/aws/resource_aws_vpc_peering_connection_test.go new file mode 100644 index 000000000..2b4b71e33 --- /dev/null +++ b/builtin/providers/aws/resource_aws_vpc_peering_connection_test.go @@ -0,0 +1,79 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" + "github.com/mitchellh/goamz/ec2" +) + +func TestAccAWSVPCPeeringConnection_normal(t *testing.T) { + var conf ec2.Address + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSVpcPeeringConnectionDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccVpcPeeringConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSVpcPeeringConnectionExists("aws_vpc_peering_connection.foo", &conf), + ), + }, + }, + }) +} + +func testAccCheckAWSVpcPeeringConnectionDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).ec2conn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_vpc_peering_connection" { + continue + } + + describe, err := conn.DescribeVpcPeeringConnection([]string{rs.Primary.ID}, ec2.NewFilter()) + + if err == nil { + if len(describe.VpcPeeringConnections) != 0 { + return fmt.Errorf("vpc peering connection still exists") + } + } + } + + return nil +} + +func testAccCheckAWSVpcPeeringConnectionExists(n string, res *ec2.Address) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No vpc peering connection id is set") + } + + return nil + } +} + +const testAccVpcPeeringConfig = ` +resource "aws_vpc" "foo" { + cidr_block = "10.0.0.0/16" +} + +resource "aws_vpc" "bar" { + cidr_block = "10.0.1.0/16" +} + +resource "aws_vpc_peering_connection" "foo" { + peer_owner_id = "12345" + vpc_id = "${aws_vpc.foo.id}" + peer_vpc_id = "${aws_vpc.bar.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 new file mode 100644 index 000000000..9fa2fa60d --- /dev/null +++ b/website/source/docs/providers/aws/r/vpc_peering.html.markdown @@ -0,0 +1,58 @@ +--- +layout: "aws" +page_title: "AWS: aws_vpc_peering_connection" +sidebar_current: "docs-aws-resource-vpc-peering-connection" +description: |- + Provides an VPC Peering Connection resource. +--- + +# aws\_vpc\_peering\_connection + +Provides an VPC Peering Connection resource. + +## Example Usage + +Basic usage: + +``` +resource "aws_vpc" "main" { + cidr_block = "10.0.0.0/16" +} +``` + +Basic usage with tags: + +``` + +resource "aws_vpc_peering_connection" "foo" { + peer_owner_id = "${var.peer_owner_id}" + peer_vpc_id = "${aws_vpc.bar.id}" + vpc_id = "${aws_vpc.foo.id}" +} + +resource "aws_vpc" "foo" { + cidr_block = "10.1.0.0/16" +} + +resource "aws_vpc" "bar" { + cidr_block = "10.2.0.0/16" +} +``` + +## Argument Reference + +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. +* `tags` - (Optional) A mapping of tags to assign to the resource. + + +Note: You still have to accept the peering with the aws console, aws-cli or goamz + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ID of the VPC Peering Connection