Skip to content

Commit

Permalink
service/ec2: Add Resource & Data Source aws_ec2_transit_gateway_peeri…
Browse files Browse the repository at this point in the history
…ng_attachment (#11162)

Output from acceptance testing:

```
--- PASS: TestAccAWSEc2TransitGatewayPeeringAttachment_basic (279.30s)
--- PASS: TestAccAWSEc2TransitGatewayPeeringAttachment_differentAccount (274.76s)
--- PASS: TestAccAWSEc2TransitGatewayPeeringAttachment_disappears (298.80s)
--- PASS: TestAccAWSEc2TransitGatewayPeeringAttachment_Tags_sameAccount (341.73s)

--- PASS: TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_Filter_differentAccount (290.68s)
--- PASS: TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_Filter_sameAccount (281.60s)
--- PASS: TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_ID_differentAccount (291.70s)
--- PASS: TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_ID_sameAccount (308.69s)
```
  • Loading branch information
Omarimcblack authored Apr 30, 2020
1 parent a138eed commit 70279b8
Show file tree
Hide file tree
Showing 15 changed files with 1,269 additions and 0 deletions.
104 changes: 104 additions & 0 deletions aws/data_source_aws_ec2_transit_gateway_peering_attachment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
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-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func dataSourceAwsEc2TransitGatewayPeeringAttachment() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsEc2TransitGatewayPeeringAttachmentRead,

Schema: map[string]*schema.Schema{
"filter": ec2CustomFiltersSchema(),
"id": {
Type: schema.TypeString,
Optional: true,
},
"peer_account_id": {
Type: schema.TypeString,
Computed: true,
},
"peer_region": {
Type: schema.TypeString,
Computed: true,
},
"peer_transit_gateway_id": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchemaComputed(),
"transit_gateway_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

input := &ec2.DescribeTransitGatewayPeeringAttachmentsInput{}

if v, ok := d.GetOk("id"); ok {
input.TransitGatewayAttachmentIds = aws.StringSlice([]string{v.(string)})
}

input.Filters = buildEC2CustomFilterList(d.Get("filter").(*schema.Set))
if v := d.Get("tags").(map[string]interface{}); len(v) > 0 {
input.Filters = append(input.Filters, ec2TagFiltersFromMap(v)...)
}
if len(input.Filters) == 0 {
// Don't send an empty filters list; the EC2 API won't accept it.
input.Filters = nil
}

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

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

if output == nil || len(output.TransitGatewayPeeringAttachments) == 0 {
return errors.New("error reading EC2 Transit Gateway Peering Attachment: no results found")
}

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

transitGatewayPeeringAttachment := output.TransitGatewayPeeringAttachments[0]

if transitGatewayPeeringAttachment == nil {
return errors.New("error reading EC2 Transit Gateway Peering Attachment: empty result")
}

local := transitGatewayPeeringAttachment.RequesterTgwInfo
peer := transitGatewayPeeringAttachment.AccepterTgwInfo

if aws.StringValue(transitGatewayPeeringAttachment.AccepterTgwInfo.OwnerId) == meta.(*AWSClient).accountid && aws.StringValue(transitGatewayPeeringAttachment.AccepterTgwInfo.Region) == meta.(*AWSClient).region {
local = transitGatewayPeeringAttachment.AccepterTgwInfo
peer = transitGatewayPeeringAttachment.RequesterTgwInfo
}

d.Set("peer_account_id", peer.OwnerId)
d.Set("peer_region", peer.Region)
d.Set("peer_transit_gateway_id", peer.TransitGatewayId)
d.Set("transit_gateway_id", local.TransitGatewayId)

if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(transitGatewayPeeringAttachment.Tags).IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

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

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

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_Filter_sameAccount(t *testing.T) {
var providers []*schema.Provider
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_ec2_transit_gateway_peering_attachment.test"
resourceName := "aws_ec2_transit_gateway_peering_attachment.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAWSEc2TransitGateway(t)
testAccMultipleRegionsPreCheck(t)
testAccAlternateRegionPreCheck(t)
},
ProviderFactories: testAccProviderFactories(&providers),
CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigFilter_sameAccount(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "peer_account_id", dataSourceName, "peer_account_id"),
resource.TestCheckResourceAttrPair(resourceName, "peer_region", dataSourceName, "peer_region"),
resource.TestCheckResourceAttrPair(resourceName, "peer_transit_gateway_id", dataSourceName, "peer_transit_gateway_id"),
resource.TestCheckResourceAttrPair(resourceName, "tags.%", dataSourceName, "tags.%"),
resource.TestCheckResourceAttrPair(resourceName, "transit_gateway_id", dataSourceName, "transit_gateway_id"),
),
},
},
})
}
func TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_Filter_differentAccount(t *testing.T) {
var providers []*schema.Provider
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_ec2_transit_gateway_peering_attachment.test"
resourceName := "aws_ec2_transit_gateway_peering_attachment.test"
transitGatewayResourceName := "aws_ec2_transit_gateway.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAWSEc2TransitGateway(t)
testAccMultipleRegionsPreCheck(t)
testAccAlternateRegionPreCheck(t)
},
ProviderFactories: testAccProviderFactories(&providers),
CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigFilter_differentAccount(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "peer_region", testAccGetRegion()),
resource.TestCheckResourceAttrPair(transitGatewayResourceName, "owner_id", dataSourceName, "peer_account_id"),
resource.TestCheckResourceAttrPair(resourceName, "transit_gateway_id", dataSourceName, "peer_transit_gateway_id"),
resource.TestCheckResourceAttrPair(resourceName, "peer_transit_gateway_id", dataSourceName, "transit_gateway_id"),
),
},
},
})
}
func TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_ID_sameAccount(t *testing.T) {
var providers []*schema.Provider
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_ec2_transit_gateway_peering_attachment.test"
resourceName := "aws_ec2_transit_gateway_peering_attachment.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAWSEc2TransitGateway(t)
testAccMultipleRegionsPreCheck(t)
testAccAlternateRegionPreCheck(t)
},
ProviderFactories: testAccProviderFactories(&providers),
CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigID_sameAccount(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "peer_account_id", dataSourceName, "peer_account_id"),
resource.TestCheckResourceAttrPair(resourceName, "peer_region", dataSourceName, "peer_region"),
resource.TestCheckResourceAttrPair(resourceName, "peer_transit_gateway_id", dataSourceName, "peer_transit_gateway_id"),
resource.TestCheckResourceAttrPair(resourceName, "tags.%", dataSourceName, "tags.%"),
resource.TestCheckResourceAttrPair(resourceName, "transit_gateway_id", dataSourceName, "transit_gateway_id"),
),
},
},
})
}
func TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_ID_differentAccount(t *testing.T) {
var providers []*schema.Provider
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_ec2_transit_gateway_peering_attachment.test"
resourceName := "aws_ec2_transit_gateway_peering_attachment.test"
transitGatewayResourceName := "aws_ec2_transit_gateway.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAWSEc2TransitGateway(t)
testAccMultipleRegionsPreCheck(t)
testAccAlternateRegionPreCheck(t)
},
ProviderFactories: testAccProviderFactories(&providers),
CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigID_differentAccount(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "peer_region", testAccGetRegion()),
resource.TestCheckResourceAttrPair(transitGatewayResourceName, "owner_id", dataSourceName, "peer_account_id"),
resource.TestCheckResourceAttrPair(resourceName, "transit_gateway_id", dataSourceName, "peer_transit_gateway_id"),
resource.TestCheckResourceAttrPair(resourceName, "peer_transit_gateway_id", dataSourceName, "transit_gateway_id"),
),
},
},
})
}
func TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_Tags(t *testing.T) {
var providers []*schema.Provider
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_ec2_transit_gateway_peering_attachment.test"
resourceName := "aws_ec2_transit_gateway_peering_attachment.test"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAWSEc2TransitGateway(t)
testAccMultipleRegionsPreCheck(t)
testAccAlternateRegionPreCheck(t)
},
ProviderFactories: testAccProviderFactories(&providers),
CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigTags_sameAccount(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "peer_account_id", dataSourceName, "peer_account_id"),
resource.TestCheckResourceAttrPair(resourceName, "peer_region", dataSourceName, "peer_region"),
resource.TestCheckResourceAttrPair(resourceName, "peer_transit_gateway_id", dataSourceName, "peer_transit_gateway_id"),
resource.TestCheckResourceAttrPair(resourceName, "tags.%", dataSourceName, "tags.%"),
resource.TestCheckResourceAttrPair(resourceName, "transit_gateway_id", dataSourceName, "transit_gateway_id"),
),
},
},
})
}

func testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfig_sameAccount_base(rName string) string {
return testAccAWSEc2TransitGatewayPeeringAttachmentConfig_sameAccount_base(rName) + fmt.Sprintf(`
resource "aws_ec2_transit_gateway_peering_attachment" "test" {
peer_region = %[1]q
peer_transit_gateway_id = "${aws_ec2_transit_gateway.peer.id}"
transit_gateway_id = "${aws_ec2_transit_gateway.test.id}"
tags = {
Name = %[2]q
}
}
`, testAccGetAlternateRegion(), rName)
}
func testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfig_differentAccount_base(rName string) string {
return testAccAWSEc2TransitGatewayPeeringAttachmentConfig_differentAccount_base(rName) + fmt.Sprintf(`
resource "aws_ec2_transit_gateway_peering_attachment" "test" {
peer_region = %[1]q
peer_transit_gateway_id = "${aws_ec2_transit_gateway.peer.id}"
transit_gateway_id = "${aws_ec2_transit_gateway.test.id}"
tags = {
Name = %[2]q
}
}
`, testAccGetAlternateRegion(), rName)
}
func testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigFilter_sameAccount(rName string) string {
return testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfig_sameAccount_base(rName) + fmt.Sprintf(`
data "aws_ec2_transit_gateway_peering_attachment" "test" {
filter {
name = "transit-gateway-attachment-id"
values = ["${aws_ec2_transit_gateway_peering_attachment.test.id}"]
}
}
`)
}
func testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigID_sameAccount(rName string) string {
return testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfig_sameAccount_base(rName) + fmt.Sprintf(`
data "aws_ec2_transit_gateway_peering_attachment" "test" {
id = "${aws_ec2_transit_gateway_peering_attachment.test.id}"
}
`)
}
func testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigTags_sameAccount(rName string) string {
return testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfig_sameAccount_base(rName) + fmt.Sprintf(`
data "aws_ec2_transit_gateway_peering_attachment" "test" {
tags = {
Name = "${aws_ec2_transit_gateway_peering_attachment.test.tags["Name"]}"
}
}
`)
}
func testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigFilter_differentAccount(rName string) string {
return testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfig_differentAccount_base(rName) + fmt.Sprintf(`
data "aws_ec2_transit_gateway_peering_attachment" "test" {
provider = "aws.alternate"
filter {
name = "transit-gateway-attachment-id"
values = ["${aws_ec2_transit_gateway_peering_attachment.test.id}"]
}
}
`)
}
func testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigID_differentAccount(rName string) string {
return testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfig_differentAccount_base(rName) + fmt.Sprintf(`
data "aws_ec2_transit_gateway_peering_attachment" "test" {
provider = "aws.alternate"
id = "${aws_ec2_transit_gateway_peering_attachment.test.id}"
}
`)
}
Loading

0 comments on commit 70279b8

Please sign in to comment.