-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Resource: aws_dx_connection_association #2360
Changes from 1 commit
159e21f
87dd343
e8fad39
52fcd8b
67ea88a
53df933
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just grammar nitpick - do you mind changing this to |
||
} | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I understand there's 1-to-1 relationship between a connection and a LAG which this resource should represent, so shouldn't we instead treat this as if the resource (i.e. unique pair) is gone? |
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you ever experienced this in reality? Is it just a matter of time until it becomes actually disassociated or is it an erroneous/unrecoverable state? If it's the former then I'd suggest we just put a retry logic in place and wait until it's disassociated. If it's the latter then I guess this is a correct behaviour - I'm just trying to ensure that we know what we're doing here. |
||
return fmt.Errorf("DX Connection (%s) is still associated with LAG", d.Id()) | ||
} | ||
|
||
d.SetId("") | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: |
||
} | ||
} | ||
} | ||
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) | ||
} |
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 Connection with LAG. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick, but do you mind changing this to |
||
--- | ||
|
||
# aws_dx_connection_association | ||
|
||
Associates a Connection with LAG. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick, but do you mind changing this to |
||
|
||
## 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there was a bit of misunderstanding about what I meant by my question about LAG ID mismatch - I wasn't implying it should be removed. I meant we should treat it the same way as if there is no connection - i.e.
d.SetId(""); return nil
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I got it👍