Skip to content

Commit

Permalink
New Resource: aws_dx_connection_association (#2360)
Browse files Browse the repository at this point in the history
* New Resource: aws_dx_connection_association

* Reflect reviews

* fix

* fix

* Reflect 2nd reviews

* Add acctest
  • Loading branch information
atsushi-ishibashi authored and radeksimko committed Dec 4, 2017
1 parent 4b3ff9e commit dcb44db
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 0 deletions.
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,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(),
Expand Down
89 changes: 89 additions & 0 deletions aws/resource_aws_dx_connection_association.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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

input := &directconnect.DescribeConnectionsInput{
ConnectionId: aws.String(d.Id()),
}

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("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
}

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)),
}

_, err := conn.DisassociateConnectionFromLag(input)
if err != nil {
return err
}

d.SetId("")
return nil
}
138 changes: 138 additions & 0 deletions aws/resource_aws_dx_connection_association_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
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 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

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 dissociated 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)
}

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)
}
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,9 @@
<li<%= sidebar_current("docs-aws-resource-dx-connection") %>>
<a href="/docs/providers/aws/r/dx_connection.html">aws_dx_connection</a>
</li>
<li<%= sidebar_current("docs-aws-resource-dx-connection-association") %>>
<a href="/docs/providers/aws/r/dx_connection_association.html">aws_dx_connection_association</a>
</li>
<li<%= sidebar_current("docs-aws-resource-dx-lag") %>>
<a href="/docs/providers/aws/r/dx_lag.html">aws_dx_lag</a>
</li>
Expand Down
40 changes: 40 additions & 0 deletions website/docs/r/dx_connection_association.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
layout: "aws"
page_title: "AWS: aws_dx_connection_association"
sidebar_current: "docs-aws-resource-dx-connection-association"
description: |-
Associates a Direct Connect Connection with a LAG.
---

# aws_dx_connection_association

Associates a Direct Connect Connection with a 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.

0 comments on commit dcb44db

Please sign in to comment.