Skip to content
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 Data Source: aws_dx_connection #17852

Merged
merged 32 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
230dc6a
New data sources 'aws_dx_location' and 'aws_dx_locations'.
Aug 12, 2019
437dcef
Use 'aws_dx_locations' data source in 'aws_dx_connection' resource te…
Aug 12, 2019
2072769
Use 'aws_dx_locations' data source in 'aws_dx_lag' resource tests.
Aug 12, 2019
b764c25
Terraform Plugin SDK migration.
ewbankkit Oct 4, 2019
706a7bf
Add 'available_providers' attribute to aws_dx_location data source.
Oct 11, 2019
e12f5a6
Add 'subcategory' and remove 'sidebar_current' in documentation.
Dec 17, 2019
7ed2bcb
Corrections after rebase.
ewbankkit Mar 1, 2021
678a112
Correct file name.
ewbankkit Mar 1, 2021
d9fc99d
Add CHANGELOG entries.
ewbankkit Mar 1, 2021
1ad2727
Fix 'tfproviderdocs' error.
ewbankkit Mar 1, 2021
2b27427
Fix 'awsproviderlint' error.
ewbankkit Mar 1, 2021
aadfb1c
d/aws_dx_location(s): '%s' -> '%w' and 'TypeList' -> 'TypeSet'.
ewbankkit Mar 1, 2021
2901c8d
d/aws_dx_location(s): Use internal finder package.
ewbankkit Mar 1, 2021
73a6252
r/aws_dx_lag: Use internal finder package. Add '_disappears' test (#1…
ewbankkit Mar 1, 2021
f30a8d7
r/aws_dx_connection: Use internal finder package. Add '_disappears' t…
ewbankkit Mar 1, 2021
f52c8ff
Fix 'golangci-lint' errors.
ewbankkit Mar 1, 2021
4f32c2b
Fix rebase errors.
ewbankkit Mar 9, 2021
2a780c3
Fix 'XAT001: missing ErrorCheck' errors.
ewbankkit Mar 20, 2021
0021686
Fix review comments.
ewbankkit Apr 29, 2021
71cbd81
Tweak DirectConnect finders.
ewbankkit Aug 24, 2021
1274562
Use 'tfresource.NewTooManyResultsError'.
ewbankkit Aug 24, 2021
e32ffd8
data_source_aws_dx_connection
Feb 26, 2021
95dc48b
Add CHANGELOG entry.
ewbankkit Aug 24, 2021
9f22903
r/aws_dx_connection: Use internal finder and waiter packages.
ewbankkit Aug 24, 2021
5705bab
r/aws_dx_lag: Use internal finder and waiter packages.
ewbankkit Aug 25, 2021
a97aa3c
r/aws_dx_connection_asociation: Use internal finder and waiter packages.
ewbankkit Aug 25, 2021
be11698
r/aws_dx_connection: Add 'owner_account_id' attribute.
ewbankkit Aug 25, 2021
5e1c708
d/aws_dx_connection: Complete implementation.
ewbankkit Aug 25, 2021
8ec4898
r/aws_dx_connection: Add 'provider_name' argument.
ewbankkit Aug 25, 2021
1ea9a08
d/aws_dx_connection: Additional attributes.
ewbankkit Aug 25, 2021
20a66e9
Fix 'XAT001: missing ErrorCheck'.
ewbankkit Aug 25, 2021
7b99f14
Remove trailing whitespace.
ewbankkit Aug 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .changelog/17852.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
```release-note:new-data-source
aws_dx_connection
```

```release-note:enhancement
resource/aws_dx_connection: Add `owner_account_id` attribute
```

```release-note:enhancement
resource/aws_dx_lag: Add `owner_account_id` attribute
```

```release-note:enhancement
resource/aws_dx_connection: Add `provider_name` argument
```

```release-note:enhancement
resource/aws_dx_lag: Add `provider_name` argument
```
7 changes: 7 additions & 0 deletions .changelog/9735.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-data-source
aws_dx_location
```

```release-note:new-data-source
aws_dx_locations
```
110 changes: 110 additions & 0 deletions aws/data_source_aws_dx_connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/directconnect"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsDxConnection() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsDxConnectionRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"aws_device": {
Type: schema.TypeString,
Computed: true,
},
"bandwidth": {
Type: schema.TypeString,
Computed: true,
},
"location": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"owner_account_id": {
Type: schema.TypeString,
Computed: true,
},
"provider_name": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaComputed(),
},
}
}

func dataSourceAwsDxConnectionRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

var connections []*directconnect.Connection
input := &directconnect.DescribeConnectionsInput{}
name := d.Get("name").(string)

// DescribeConnections is not paginated.
output, err := conn.DescribeConnections(input)

if err != nil {
return fmt.Errorf("error reading Direct Connect Connections: %w", err)
}

for _, connection := range output.Connections {
if aws.StringValue(connection.ConnectionName) == name {
connections = append(connections, connection)
}
}

switch count := len(connections); count {
case 0:
return fmt.Errorf("no matching Direct Connect Connection found")
case 1:
default:
return fmt.Errorf("%d Direct Connect Connections matched; use additional constraints to reduce matches to a single Direct Connect Connection", count)
}

connection := connections[0]

d.SetId(aws.StringValue(connection.ConnectionId))

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: aws.StringValue(connection.Region),
Service: "directconnect",
AccountID: aws.StringValue(connection.OwnerAccount),
Resource: fmt.Sprintf("dxcon/%s", d.Id()),
}.String()
d.Set("arn", arn)
d.Set("aws_device", connection.AwsDeviceV2)
d.Set("bandwidth", connection.Bandwidth)
d.Set("location", connection.Location)
d.Set("name", connection.ConnectionName)
d.Set("owner_account_id", connection.OwnerAccount)
d.Set("provider_name", connection.ProviderName)

tags, err := keyvaluetags.DirectconnectListTags(conn, arn)

if err != nil {
return fmt.Errorf("error listing tags for Direct Connect Connection (%s): %w", arn, err)
}

if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

return nil
}
53 changes: 53 additions & 0 deletions aws/data_source_aws_dx_connection_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package aws

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/directconnect"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceAwsDxConnection_basic(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_dx_connection.test"
datasourceName := "data.aws_dx_connection.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, directconnect.EndpointsID),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsDxConnectionConfig(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceName, "aws_device", resourceName, "aws_device"),
resource.TestCheckResourceAttrPair(datasourceName, "bandwidth", resourceName, "bandwidth"),
resource.TestCheckResourceAttrPair(datasourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(datasourceName, "location", resourceName, "location"),
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(datasourceName, "owner_account_id", resourceName, "owner_account_id"),
resource.TestCheckResourceAttrPair(datasourceName, "provider_name", resourceName, "provider_name"),
),
},
},
})
}

func testAccDataSourceAwsDxConnectionConfig(rName string) string {
return fmt.Sprintf(`
data "aws_dx_locations" "test" {}

resource "aws_dx_connection" "test" {
name = %[1]q
bandwidth = "1Gbps"
location = tolist(data.aws_dx_locations.test.location_codes)[0]
}

data "aws_dx_connection" "test" {
name = aws_dx_connection.test.name
}
`, rName)
}
63 changes: 63 additions & 0 deletions aws/data_source_aws_dx_location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/directconnect/finder"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func dataSourceAwsDxLocation() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsDxLocationRead,

Schema: map[string]*schema.Schema{
"available_port_speeds": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"available_providers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"location_code": {
Type: schema.TypeString,
Required: true,
},

"location_name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceAwsDxLocationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn
locationCode := d.Get("location_code").(string)

location, err := finder.LocationByCode(conn, locationCode)

if tfresource.NotFound(err) {
return fmt.Errorf("no Direct Connect location matched; change the search criteria and try again")
}

if err != nil {
return fmt.Errorf("error reading Direct Connect location (%s): %w", locationCode, err)
}

d.SetId(locationCode)
d.Set("available_port_speeds", aws.StringValueSlice(location.AvailablePortSpeeds))
d.Set("available_providers", aws.StringValueSlice(location.AvailableProviders))
d.Set("location_code", location.LocationCode)
d.Set("location_name", location.LocationName)

return nil
}
37 changes: 37 additions & 0 deletions aws/data_source_aws_dx_location_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package aws

import (
"testing"

"github.com/aws/aws-sdk-go/service/directconnect"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceAwsDxLocation_basic(t *testing.T) {
dsResourceName := "data.aws_dx_location.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, directconnect.EndpointsID),
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceDxLocationConfig_basic,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dsResourceName, "available_port_speeds.#"),
resource.TestCheckResourceAttrSet(dsResourceName, "available_providers.#"),
resource.TestCheckResourceAttrSet(dsResourceName, "location_code"),
resource.TestCheckResourceAttrSet(dsResourceName, "location_name"),
),
},
},
})
}

const testAccDataSourceDxLocationConfig_basic = `
data "aws_dx_locations" "test" {}

data "aws_dx_location" "test" {
location_code = tolist(data.aws_dx_locations.test.location_codes)[0]
}
`
45 changes: 45 additions & 0 deletions aws/data_source_aws_dx_locations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package aws

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/directconnect"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/directconnect/finder"
)

func dataSourceAwsDxLocations() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsDxLocationsRead,

Schema: map[string]*schema.Schema{
"location_codes": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceAwsDxLocationsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).dxconn

locations, err := finder.Locations(conn, &directconnect.DescribeLocationsInput{})

if err != nil {
return fmt.Errorf("error reading Direct Connect locations: %w", err)
}

var locationCodes []*string

for _, location := range locations {
locationCodes = append(locationCodes, location.LocationCode)
}

d.SetId(meta.(*AWSClient).region)
d.Set("location_codes", aws.StringValueSlice(locationCodes))

return nil
}
Loading