Skip to content

Commit

Permalink
Merge pull request #17852 from yzguy/data_source_aws_dx_connection
Browse files Browse the repository at this point in the history
New Data Source: aws_dx_connection
  • Loading branch information
ewbankkit authored Aug 25, 2021
2 parents a99fcfb + 7b99f14 commit 709e4a0
Show file tree
Hide file tree
Showing 23 changed files with 1,227 additions and 376 deletions.
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

0 comments on commit 709e4a0

Please sign in to comment.