From 159e21f1ae4e294fb6cb367d15d2c3b616a7fe31 Mon Sep 17 00:00:00 2001 From: Atsushi Ishibashi Date: Sat, 18 Nov 2017 23:15:02 +0900 Subject: [PATCH 1/6] New Resource: aws_dx_connection_association --- aws/provider.go | 1 + aws/resource_aws_dx_connection_association.go | 98 +++++++++++++++++++ ...urce_aws_dx_connection_association_test.go | 87 ++++++++++++++++ website/aws.erb | 3 + .../r/dx_connection_association.html.markdown | 40 ++++++++ 5 files changed, 229 insertions(+) create mode 100644 aws/resource_aws_dx_connection_association.go create mode 100644 aws/resource_aws_dx_connection_association_test.go create mode 100644 website/docs/r/dx_connection_association.html.markdown diff --git a/aws/provider.go b/aws/provider.go index f5bb4bf6743..1f648bae17e 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -315,6 +315,7 @@ func Provider() terraform.ResourceProvider { "aws_dms_replication_task": resourceAwsDmsReplicationTask(), "aws_dx_lag": resourceAwsDxLag(), "aws_dx_connection": resourceAwsDxConnection(), + "aws_dx_connection_association": resourceAwsDxConnectionAssociation(), "aws_dynamodb_table": resourceAwsDynamoDbTable(), "aws_ebs_snapshot": resourceAwsEbsSnapshot(), "aws_ebs_volume": resourceAwsEbsVolume(), diff --git a/aws/resource_aws_dx_connection_association.go b/aws/resource_aws_dx_connection_association.go new file mode 100644 index 00000000000..a8237929d77 --- /dev/null +++ b/aws/resource_aws_dx_connection_association.go @@ -0,0 +1,98 @@ +package aws + +import ( + "fmt" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceAwsDxConnectionAssociation() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsDxConnectionAssociationCreate, + Read: resourceAwsDxConnectionAssociationRead, + Delete: resourceAwsDxConnectionAssociationDelete, + + Schema: map[string]*schema.Schema{ + "connection_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "lag_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + }, + } +} + +func resourceAwsDxConnectionAssociationCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).dxconn + + input := &directconnect.AssociateConnectionWithLagInput{ + ConnectionId: aws.String(d.Get("connection_id").(string)), + LagId: aws.String(d.Get("lag_id").(string)), + } + resp, err := conn.AssociateConnectionWithLag(input) + if err != nil { + return err + } + + d.SetId(*resp.ConnectionId) + return nil +} + +func resourceAwsDxConnectionAssociationRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).dxconn + + connectionId := d.Id() + input := &directconnect.DescribeConnectionsInput{ + ConnectionId: aws.String(connectionId), + } + + resp, err := conn.DescribeConnections(input) + if err != nil { + return err + } + if len(resp.Connections) < 1 { + d.SetId("") + return nil + } + if len(resp.Connections) != 1 { + return fmt.Errorf("Number of DX Connection (%s) isn't one, got %d", connectionId, len(resp.Connections)) + } + if d.Id() != *resp.Connections[0].ConnectionId { + return fmt.Errorf("DX Connection (%s) not found", connectionId) + } + if resp.Connections[0].LagId == nil { + d.SetId("") + return nil + } + if *resp.Connections[0].LagId != d.Get("lag_id").(string) { + return fmt.Errorf("DX Connection (%s) is associated with unexpected LAG (%s)", connectionId, *resp.Connections[0].LagId) + } + return nil +} + +func resourceAwsDxConnectionAssociationDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).dxconn + + input := &directconnect.DisassociateConnectionFromLagInput{ + ConnectionId: aws.String(d.Id()), + LagId: aws.String(d.Get("lag_id").(string)), + } + + resp, err := conn.DisassociateConnectionFromLag(input) + if err != nil { + return err + } + if resp.LagId != nil { + return fmt.Errorf("DX Connection (%s) is still associated with LAG", d.Id()) + } + + d.SetId("") + return nil +} diff --git a/aws/resource_aws_dx_connection_association_test.go b/aws/resource_aws_dx_connection_association_test.go new file mode 100644 index 00000000000..7c61f32f64c --- /dev/null +++ b/aws/resource_aws_dx_connection_association_test.go @@ -0,0 +1,87 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/directconnect" + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAwsDxConnectionAssociation_basic(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAwsDxConnectionAssociationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDxConnectionAssociationConfig(acctest.RandString(5)), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsDxConnectionAssociationExists("aws_dx_connection_association.test"), + ), + }, + }, + }) +} + +func testAccCheckAwsDxConnectionAssociationDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).dxconn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_dx_connection_association" { + continue + } + + input := &directconnect.DescribeConnectionsInput{ + ConnectionId: aws.String(rs.Primary.ID), + } + + resp, err := conn.DescribeConnections(input) + if err != nil { + return err + } + for _, v := range resp.Connections { + if *v.ConnectionId == rs.Primary.ID && v.LagId != nil { + return fmt.Errorf("Dx Connection (%s) is not diasociated with Lag", rs.Primary.ID) + } + } + } + return nil +} + +func testAccCheckAwsDxConnectionAssociationExists(name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + _, ok := s.RootModule().Resources[name] + if !ok { + return fmt.Errorf("Not found: %s", name) + } + + return nil + } +} + +func testAccDxConnectionAssociationConfig(rName string) string { + return fmt.Sprintf(` +resource "aws_dx_connection" "test" { + name = "tf-dx-%s" + bandwidth = "1Gbps" + location = "EqSe2" +} + +resource "aws_dx_lag" "test" { + name = "tf-dx-%s" + connections_bandwidth = "1Gbps" + location = "EqSe2" + number_of_connections = 1 + force_destroy = true +} + +resource "aws_dx_connection_association" "test" { + connection_id = "${aws_dx_connection.test.id}" + lag_id = "${aws_dx_lag.test.id}" +} +`, rName, rName) +} diff --git a/website/aws.erb b/website/aws.erb index d82eb2734f6..f8363e48e48 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -553,6 +553,9 @@ > aws_dx_connection + > + aws_dx_connection_association + > aws_dx_lag diff --git a/website/docs/r/dx_connection_association.html.markdown b/website/docs/r/dx_connection_association.html.markdown new file mode 100644 index 00000000000..6ecc2d2f33b --- /dev/null +++ b/website/docs/r/dx_connection_association.html.markdown @@ -0,0 +1,40 @@ +--- +layout: "aws" +page_title: "AWS: aws_dx_connection_association" +sidebar_current: "docs-aws-resource-dx-connection-association" +description: |- + Associates a Connection with LAG. +--- + +# aws_dx_connection_association + +Associates a Connection with LAG. + +## Example Usage + +```hcl +resource "aws_dx_connection" "example" { + name = "example" + bandwidth = "1Gbps" + location = "EqSe2" +} + +resource "aws_dx_lag" "example" { + name = "example" + connections_bandwidth = "1Gbps" + location = "EqSe2" + number_of_connections = 1 +} + +resource "aws_dx_connection_association" "example" { + connection_id = "${aws_dx_connection.example.id}" + lag_id = "${aws_dx_lag.example.id}" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `connection_id` - (Required) The ID of the connection. +* `lag_id` - (Required) The ID of the LAG with which to associate the connection. From 87dd343a3876d3364cfb4f75bd06dc4f58821dbd Mon Sep 17 00:00:00 2001 From: Atsushi Ishibashi Date: Sat, 2 Dec 2017 23:29:19 +0900 Subject: [PATCH 2/6] Reflect reviews --- aws/resource_aws_dx_connection_association.go | 16 ++-------------- .../r/dx_connection_association.html.markdown | 4 ++-- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/aws/resource_aws_dx_connection_association.go b/aws/resource_aws_dx_connection_association.go index a8237929d77..a06d6206edb 100644 --- a/aws/resource_aws_dx_connection_association.go +++ b/aws/resource_aws_dx_connection_association.go @@ -62,18 +62,9 @@ func resourceAwsDxConnectionAssociationRead(d *schema.ResourceData, meta interfa return nil } if len(resp.Connections) != 1 { - return fmt.Errorf("Number of DX Connection (%s) isn't one, got %d", connectionId, len(resp.Connections)) - } - if d.Id() != *resp.Connections[0].ConnectionId { - return fmt.Errorf("DX Connection (%s) not found", connectionId) - } - if resp.Connections[0].LagId == nil { - d.SetId("") - return nil - } - if *resp.Connections[0].LagId != d.Get("lag_id").(string) { - return fmt.Errorf("DX Connection (%s) is associated with unexpected LAG (%s)", connectionId, *resp.Connections[0].LagId) + return fmt.Errorf("Found %d DX connections for %s, expected 1", len(resp.Connections)) } + return nil } @@ -89,9 +80,6 @@ func resourceAwsDxConnectionAssociationDelete(d *schema.ResourceData, meta inter if err != nil { return err } - if resp.LagId != nil { - return fmt.Errorf("DX Connection (%s) is still associated with LAG", d.Id()) - } d.SetId("") return nil diff --git a/website/docs/r/dx_connection_association.html.markdown b/website/docs/r/dx_connection_association.html.markdown index 6ecc2d2f33b..5b76e4516b7 100644 --- a/website/docs/r/dx_connection_association.html.markdown +++ b/website/docs/r/dx_connection_association.html.markdown @@ -3,12 +3,12 @@ layout: "aws" page_title: "AWS: aws_dx_connection_association" sidebar_current: "docs-aws-resource-dx-connection-association" description: |- - Associates a Connection with LAG. + Associates a Direct Connect Connection with a LAG. --- # aws_dx_connection_association -Associates a Connection with LAG. +Associates a Direct Connect Connection with a LAG. ## Example Usage From e8fad3949a0319f053cfd5178639cd281ba86bdc Mon Sep 17 00:00:00 2001 From: Atsushi Ishibashi Date: Sun, 3 Dec 2017 12:13:54 +0900 Subject: [PATCH 3/6] fix --- aws/resource_aws_dx_connection_association.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_dx_connection_association.go b/aws/resource_aws_dx_connection_association.go index a06d6206edb..3e20cff2225 100644 --- a/aws/resource_aws_dx_connection_association.go +++ b/aws/resource_aws_dx_connection_association.go @@ -76,7 +76,7 @@ func resourceAwsDxConnectionAssociationDelete(d *schema.ResourceData, meta inter LagId: aws.String(d.Get("lag_id").(string)), } - resp, err := conn.DisassociateConnectionFromLag(input) + _, err := conn.DisassociateConnectionFromLag(input) if err != nil { return err } From 52fcd8b7cf368a22b42d5d1563d3c816ecd83b90 Mon Sep 17 00:00:00 2001 From: Atsushi Ishibashi Date: Sun, 3 Dec 2017 14:52:24 +0900 Subject: [PATCH 4/6] fix --- aws/resource_aws_dx_connection_association.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_dx_connection_association.go b/aws/resource_aws_dx_connection_association.go index 3e20cff2225..9f75d3e9821 100644 --- a/aws/resource_aws_dx_connection_association.go +++ b/aws/resource_aws_dx_connection_association.go @@ -48,9 +48,8 @@ func resourceAwsDxConnectionAssociationCreate(d *schema.ResourceData, meta inter func resourceAwsDxConnectionAssociationRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).dxconn - connectionId := d.Id() input := &directconnect.DescribeConnectionsInput{ - ConnectionId: aws.String(connectionId), + ConnectionId: aws.String(d.Id()), } resp, err := conn.DescribeConnections(input) @@ -62,7 +61,7 @@ func resourceAwsDxConnectionAssociationRead(d *schema.ResourceData, meta interfa return nil } if len(resp.Connections) != 1 { - return fmt.Errorf("Found %d DX connections for %s, expected 1", len(resp.Connections)) + return fmt.Errorf("Found %d DX connections for %s, expected 1", len(resp.Connections), d.Id()) } return nil From 67ea88af3cc18201f3f853813653d57ae0cb8092 Mon Sep 17 00:00:00 2001 From: Atsushi Ishibashi Date: Mon, 4 Dec 2017 19:25:57 +0900 Subject: [PATCH 5/6] Reflect 2nd reviews --- aws/resource_aws_dx_connection_association.go | 4 ++++ aws/resource_aws_dx_connection_association_test.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_dx_connection_association.go b/aws/resource_aws_dx_connection_association.go index 9f75d3e9821..9f478ce9b7e 100644 --- a/aws/resource_aws_dx_connection_association.go +++ b/aws/resource_aws_dx_connection_association.go @@ -63,6 +63,10 @@ func resourceAwsDxConnectionAssociationRead(d *schema.ResourceData, meta interfa if len(resp.Connections) != 1 { return fmt.Errorf("Found %d DX connections for %s, expected 1", len(resp.Connections), d.Id()) } + if *resp.Connections[0].LagId != d.Get("lag_id").(string) { + d.SetId("") + return nil + } return nil } diff --git a/aws/resource_aws_dx_connection_association_test.go b/aws/resource_aws_dx_connection_association_test.go index 7c61f32f64c..4bcb66c5339 100644 --- a/aws/resource_aws_dx_connection_association_test.go +++ b/aws/resource_aws_dx_connection_association_test.go @@ -45,7 +45,7 @@ func testAccCheckAwsDxConnectionAssociationDestroy(s *terraform.State) error { } for _, v := range resp.Connections { if *v.ConnectionId == rs.Primary.ID && v.LagId != nil { - return fmt.Errorf("Dx Connection (%s) is not diasociated with Lag", rs.Primary.ID) + return fmt.Errorf("Dx Connection (%s) is not dissociated with Lag", rs.Primary.ID) } } } From 53df9330606c34b98da69ab6140087a8a344779f Mon Sep 17 00:00:00 2001 From: Atsushi Ishibashi Date: Mon, 4 Dec 2017 20:11:24 +0900 Subject: [PATCH 6/6] Add acctest --- ...urce_aws_dx_connection_association_test.go | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/aws/resource_aws_dx_connection_association_test.go b/aws/resource_aws_dx_connection_association_test.go index 4bcb66c5339..504d43b4b39 100644 --- a/aws/resource_aws_dx_connection_association_test.go +++ b/aws/resource_aws_dx_connection_association_test.go @@ -27,6 +27,23 @@ func TestAccAwsDxConnectionAssociation_basic(t *testing.T) { }) } +func TestAccAwsDxConnectionAssociation_multiConns(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAwsDxConnectionAssociationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDxConnectionAssociationConfig_multiConns(acctest.RandString(5)), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsDxConnectionAssociationExists("aws_dx_connection_association.test1"), + testAccCheckAwsDxConnectionAssociationExists("aws_dx_connection_association.test2"), + ), + }, + }, + }) +} + func testAccCheckAwsDxConnectionAssociationDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).dxconn @@ -85,3 +102,37 @@ resource "aws_dx_connection_association" "test" { } `, rName, rName) } + +func testAccDxConnectionAssociationConfig_multiConns(rName string) string { + return fmt.Sprintf(` +resource "aws_dx_connection" "test1" { + name = "tf-dxconn1-%s" + bandwidth = "1Gbps" + location = "EqSe2" +} + +resource "aws_dx_connection" "test2" { + name = "tf-dxconn2-%s" + bandwidth = "1Gbps" + location = "EqSe2" +} + +resource "aws_dx_lag" "test" { + name = "tf-dx-%s" + connections_bandwidth = "1Gbps" + location = "EqSe2" + number_of_connections = 1 + force_destroy = true +} + +resource "aws_dx_connection_association" "test1" { + connection_id = "${aws_dx_connection.test1.id}" + lag_id = "${aws_dx_lag.test.id}" +} + +resource "aws_dx_connection_association" "test2" { + connection_id = "${aws_dx_connection.test2.id}" + lag_id = "${aws_dx_lag.test.id}" +} +`, rName, rName, rName) +}