Skip to content

Commit

Permalink
Add 'aws_ec2_transit_gateway_dx_gateway_attachment' data source.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed May 24, 2019
1 parent 4161488 commit 665ef14
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 125 deletions.
78 changes: 78 additions & 0 deletions aws/data_source_aws_ec2_transit_gateway_dx_gateway_attachment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package aws

import (
"errors"
"fmt"
"log"

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

func dataSourceAwsEc2TransitGatewayDxGatewayAttachment() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsEc2TransitGatewayDxGatewayAttachmentRead,

Schema: map[string]*schema.Schema{
"dx_gateway_id": {
Type: schema.TypeString,
Required: true,
},
"tags": tagsSchemaComputed(),
"transit_gateway_id": {
Type: schema.TypeString,
Required: true,
},
},
}
}

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

input := &ec2.DescribeTransitGatewayAttachmentsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("resource-id"),
Values: []*string{aws.String(d.Get("dx_gateway_id").(string))},
},
{
Name: aws.String("resource-type"),
Values: []*string{aws.String("direct-connect-gateway")}, // Not yet defined in ec2/api.go.
},
{
Name: aws.String("transit-gateway-id"),
Values: []*string{aws.String(d.Get("transit_gateway_id").(string))},
},
},
}

log.Printf("[DEBUG] Reading EC2 Transit Gateway Direct Connect Gateway Attachments: %s", input)
output, err := conn.DescribeTransitGatewayAttachments(input)

if err != nil {
return fmt.Errorf("error reading EC2 Transit Gateway Direct Connect Gateway Attachment: %s", err)
}

if output == nil || len(output.TransitGatewayAttachments) == 0 || output.TransitGatewayAttachments[0] == nil {
return errors.New("error reading EC2 Transit Gateway Direct Connect Gateway Attachment: no results found")
}

if len(output.TransitGatewayAttachments) > 1 {
return errors.New("error reading EC2 Transit Gateway Direct Connect Gateway Attachment: multiple results found, try adjusting search criteria")
}

transitGatewayAttachment := output.TransitGatewayAttachments[0]

if err := d.Set("tags", tagsToMap(transitGatewayAttachment.Tags)); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

d.Set("transit_gateway_id", aws.StringValue(transitGatewayAttachment.TransitGatewayId))
d.Set("dx_gateway_id", aws.StringValue(transitGatewayAttachment.ResourceId))

d.SetId(aws.StringValue(transitGatewayAttachment.TransitGatewayAttachmentId))

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package aws

import (
"fmt"
"testing"

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

func TestAccAWSEc2TransitGatewayDxGatewayAttachmentDataSource_TransitGatewayIdAndDxGatewayId(t *testing.T) {
rName := fmt.Sprintf("terraform-testacc-tgwdxgwattach-%d", acctest.RandInt())
rBgpAsn := randIntRange(64512, 65534)
dataSourceName := "data.aws_ec2_transit_gateway_dx_gateway_attachment.test"
transitGatewayResourceName := "aws_ec2_transit_gateway.test"
dxGatewayResourceName := "aws_dx_gateway.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAWSEc2TransitGateway(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEc2TransitGatewayVpnAttachmentDataSourceConfigTransitGatewayIdAndDxGatewayId(rName, rBgpAsn),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"),
resource.TestCheckResourceAttrPair(dataSourceName, "transit_gateway_id", transitGatewayResourceName, "id"),
resource.TestCheckResourceAttrPair(dataSourceName, "dx_gateway_id", dxGatewayResourceName, "id"),
),
},
},
})
}

func testAccAWSEc2TransitGatewayVpnAttachmentDataSourceConfigTransitGatewayIdAndDxGatewayId(rName string, rBgpAsn int) string {
return fmt.Sprintf(`
resource "aws_dx_gateway" "test" {
name = %[1]q
amazon_side_asn = "%[2]d"
}
resource "aws_ec2_transit_gateway" "test" {
tags = {
Name = %[1]q
}
}
resource "aws_dx_gateway_association" "test" {
dx_gateway_id = "${aws_dx_gateway.test.id}"
associated_gateway_id = "${aws_ec2_transit_gateway.test.id}"
allowed_prefixes = [
"10.255.255.0/30",
"10.255.255.8/30",
]
}
data "aws_ec2_transit_gateway_dx_gateway_attachment" "test" {
transit_gateway_id = "${aws_dx_gateway_association.test.associated_gateway_id}"
dx_gateway_id = "${aws_dx_gateway_association.test.dx_gateway_id}"
}
`, rName, rBgpAsn)
}
Loading

0 comments on commit 665ef14

Please sign in to comment.