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_gateway #4988

Merged
merged 1 commit into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
66 changes: 66 additions & 0 deletions aws/data_source_aws_dx_gateway.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package aws

import (
"fmt"
"strconv"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/directconnect"
"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceAwsDxGateway() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsDxGatewayRead,

Schema: map[string]*schema.Schema{
"amazon_side_asn": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
},
}
}

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

gateways := make([]*directconnect.Gateway, 0)
// DescribeDirectConnectGatewaysInput does not have a name parameter for filtering
input := &directconnect.DescribeDirectConnectGatewaysInput{}
for {
output, err := conn.DescribeDirectConnectGateways(input)
if err != nil {
return fmt.Errorf("error reading Direct Connect Gateway: %s", err)
}
for _, gateway := range output.DirectConnectGateways {
if aws.StringValue(gateway.DirectConnectGatewayName) == name {
gateways = append(gateways, gateway)
}
}
if output.NextToken == nil {
break
}
input.NextToken = output.NextToken
}

if len(gateways) == 0 {
return fmt.Errorf("Direct Connect Gateway not found for name: %s", name)
}

if len(gateways) > 1 {
return fmt.Errorf("Multiple Direct Connect Gateways found for name: %s", name)
}

gateway := gateways[0]

d.SetId(aws.StringValue(gateway.DirectConnectGatewayId))
d.Set("amazon_side_asn", strconv.FormatInt(aws.Int64Value(gateway.AmazonSideAsn), 10))

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

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

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

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsDxGatewayConfig_NonExistent,
ExpectError: regexp.MustCompile(`Direct Connect Gateway not found`),
},
{
Config: testAccDataSourceAwsDxGatewayConfig_Name(rName, randIntRange(64512, 65534)),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "amazon_side_asn", resourceName, "amazon_side_asn"),
resource.TestCheckResourceAttrPair(datasourceName, "id", resourceName, "id"),
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
),
},
},
})
}

func testAccDataSourceAwsDxGatewayConfig_Name(rName string, rBgpAsn int) string {
return fmt.Sprintf(`
resource "aws_dx_gateway" "wrong" {
amazon_side_asn = "%d"
name = "%s-wrong"
}
resource "aws_dx_gateway" "test" {
amazon_side_asn = "%d"
name = "%s"
}

data "aws_dx_gateway" "test" {
name = "${aws_dx_gateway.test.name}"
}
`, rBgpAsn+1, rName, rBgpAsn, rName)
}

const testAccDataSourceAwsDxGatewayConfig_NonExistent = `
data "aws_dx_gateway" "test" {
name = "tf-acc-test-does-not-exist"
}
`
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func Provider() terraform.ResourceProvider {
"aws_codecommit_repository": dataSourceAwsCodeCommitRepository(),
"aws_db_instance": dataSourceAwsDbInstance(),
"aws_db_snapshot": dataSourceAwsDbSnapshot(),
"aws_dx_gateway": dataSourceAwsDxGateway(),
"aws_dynamodb_table": dataSourceAwsDynamoDbTable(),
"aws_ebs_snapshot": dataSourceAwsEbsSnapshot(),
"aws_ebs_snapshot_ids": dataSourceAwsEbsSnapshotIds(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@
<li<%= sidebar_current("docs-aws-datasource-db-snapshot") %>>
<a href="/docs/providers/aws/d/db_snapshot.html">aws_db_snapshot</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-dx-gateway") %>>
<a href="/docs/providers/aws/d/dx_gateway.html">aws_dx_gateway</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-dynamodb-table") %>>
<a href="/docs/providers/aws/d/dynamodb_table.html">aws_dynamodb_table</a>
</li>
Expand Down
28 changes: 28 additions & 0 deletions website/docs/d/dx_gateway.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
layout: "aws"
page_title: "AWS: aws_dx_gateway"
sidebar_current: "docs-aws-datasource-dx-gateway"
description: |-
Retrieve information about a Direct Connect Gateway
---

# Data Source: aws_dx_gateway

Retrieve information about a Direct Connect Gateway.

## Example Usage

```hcl
data "aws_dx_gateway" "example" {
name = "example"
}
```

## Argument Reference

* `name` - (Required) The name of the gateway to retrieve.

## Attributes Reference

* `amazon_side_asn` - The ASN on the Amazon side of the connection.
* `id` - The ID of the gateway.