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

Add Resource & Data Source aws_ec2_transit_gateway_peering_attachment #11162

Merged
merged 33 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
44291e0
Resource aws_ec2_transit_gateway_peering_attachment
Dec 5, 2019
6b2b24a
Resource aws_ec2_transit_gateway_peering_attachment
Omarimcblack Dec 5, 2019
54b21e5
Remove references to tgw peering attachment acceptance
Dec 6, 2019
c261ada
Resource aws_ec2_transit_gateway_peering_attachment
Dec 5, 2019
f8552d3
Resource aws_ec2_transit_gateway_peering_attachment
Omarimcblack Dec 5, 2019
a37f145
Remove references to tgw peering attachment acceptance
Dec 6, 2019
5094ff5
Refactor tagging logic to keyvaluetags package
Omarimcblack Dec 7, 2019
348da35
Add tgw-peer-attachment docs
Omarimcblack Dec 7, 2019
e4eb279
Merge branch 'add_transit_gw_peering' into add_transit_gw_peering_docs
Omarimcblack Dec 23, 2019
932a775
Merge pull request #2 from j-nix/add_transit_gw_peering_docs
Omarimcblack Dec 23, 2019
8a373a2
WIP: Tests for TGW peering attachment
Dec 9, 2019
0fddd81
Merge branch 'add_transit_gw_peering' into tgw_peer_tests
Omarimcblack Dec 24, 2019
20f5872
Merge pull request #3 from j-nix/tgw_peer_tests
Omarimcblack Dec 24, 2019
73e0a1c
Add data source for transit gateway peering attachment
Omarimcblack Dec 30, 2019
7c3e613
r/aws_ec2_transit_gateway_peering_attachment: Get acceptance tests pa…
Jan 9, 2020
4e48f9f
Merge pull request #4 from ewbankkit/add_transit_gw_peering
Omarimcblack Jan 10, 2020
07c8839
d/aws_ec2_transit_gateway_peering_attachment: Get acceptance tests pa…
ewbankkit Jan 10, 2020
b483433
r/aws_ec2_transit_gateway_peering_attachment: Add cross-account accep…
ewbankkit Jan 10, 2020
e521a8b
Merge pull request #5 from ewbankkit/add_transit_gw_peering
j-nix Jan 11, 2020
7a91af2
Fixes broken CI website build
jnvaldron-skillsoft Mar 13, 2020
292ae11
Adds AWS account ID validation to transit gateway peering attachmen
jnvaldron-skillsoft Mar 13, 2020
4ee22e4
Merge pull request #6 from jValdron/add_transit_gw_peering
j-nix Mar 17, 2020
b92b727
Ran make website-lint-fix
Omarimcblack Mar 21, 2020
bdd9c12
Delay time for peering attachment delete moved to aws_ec2_transit_gat…
ewbankkit Apr 8, 2020
86b3468
Use hashicorp/go-multierror in sweeper for aws_ec2_transit_gateway_pe…
ewbankkit Apr 8, 2020
a7674c8
Merge pull request #8 from ewbankkit/issue-11117.aws_ec2_transit_gate…
j-nix Apr 9, 2020
6f7a696
Refer to AWS Docs for TGW peering supported regions
Omarimcblack Apr 9, 2020
1a2af88
aws_ec2_transit_gateway_peering_attachment: local/peer terminology
j-nix Apr 20, 2020
1c32337
aws_ec2_transit_gateway_peering_attachment: simplify test logic
j-nix Apr 20, 2020
3321080
aws_ec2_transit_gateway_peering_attachment: amend docs
j-nix Apr 20, 2020
04b8a62
aws_ec2_transit_gateway_peering_attachment: Change test sweep to use …
Apr 24, 2020
b2e4d5d
aws_ec2_transit_gateway_peering_attachment: Add comment for string ra…
Apr 24, 2020
9221ed7
aws_ec2_transit_gateway_peering_attachment: test datasource using
Omarimcblack Apr 24, 2020
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
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