Skip to content

Commit

Permalink
r/aws_ec2_transit_gateway_peering_attachment: Add cross-account accep…
Browse files Browse the repository at this point in the history
…tance test.
  • Loading branch information
ewbankkit committed Jan 10, 2020
1 parent 07c8839 commit b483433
Showing 1 changed file with 72 additions and 2 deletions.
74 changes: 72 additions & 2 deletions aws/resource_aws_ec2_transit_gateway_peering_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,51 @@ func TestAccAWSEc2TransitGatewayPeeringAttachment_Tags_sameAccount(t *testing.T)
})
}

func TestAccAWSEc2TransitGatewayPeeringAttachment_differentAccount(t *testing.T) {
var transitGatewayPeeringAttachment ec2.TransitGatewayPeeringAttachment
var providers []*schema.Provider
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_ec2_transit_gateway_peering_attachment.test"
transitGatewayResourceName := "aws_ec2_transit_gateway.test"
transitGatewayResourceNamePeer := "aws_ec2_transit_gateway.peer"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAWSEc2TransitGateway(t)
testAccMultipleRegionsPreCheck(t)
testAccAlternateRegionPreCheck(t)
},
ProviderFactories: testAccProviderFactories(&providers),
CheckDestroy: testAccCheckAWSEc2TransitGatewayPeeringAttachmentDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEc2TransitGatewayPeeringAttachmentConfigBasic_differentAccount(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSEc2TransitGatewayPeeringAttachmentExists(resourceName, &transitGatewayPeeringAttachment),
// Test that the peer account ID != the primary (request) account ID
func(s *terraform.State) error {
if testAccCheckResourceAttrAccountID(resourceName, "peer_account_id") == nil {
return fmt.Errorf("peer_account_id attribute incorrectly to the requester's account ID")
}
return nil
},
resource.TestCheckResourceAttr(resourceName, "peer_region", testAccGetAlternateRegion()),
resource.TestCheckResourceAttrPair(resourceName, "peer_transit_gateway_id", transitGatewayResourceNamePeer, "id"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestCheckResourceAttrPair(resourceName, "transit_gateway_id", transitGatewayResourceName, "id"),
),
},
{
Config: testAccAWSEc2TransitGatewayPeeringAttachmentConfigBasic_differentAccount(rName),
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckAWSEc2TransitGatewayPeeringAttachmentExists(resourceName string, transitGatewayPeeringAttachment *ec2.TransitGatewayPeeringAttachment) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
Expand Down Expand Up @@ -275,8 +320,8 @@ func testAccCheckAWSEc2TransitGatewayPeeringAttachmentDisappears(transitGatewayP
}
}

func testAccAWSEc2TransitGatewayPeeringAttachmentConfig_sameAccount_base(rName string) string {
return testAccAlternateRegionProviderConfig() + fmt.Sprintf(`
func testAccAWSEc2TransitGatewayPeeringAttachmentConfig_base(rName string) string {
return fmt.Sprintf(`
resource "aws_ec2_transit_gateway" "test" {
tags = {
Name = %[1]q
Expand All @@ -293,6 +338,20 @@ resource "aws_ec2_transit_gateway" "peer" {
`, rName)
}

func testAccAWSEc2TransitGatewayPeeringAttachmentConfig_sameAccount_base(rName string) string {
return testAccAlternateRegionProviderConfig() + testAccAWSEc2TransitGatewayPeeringAttachmentConfig_base(rName)
}

func testAccAWSEc2TransitGatewayPeeringAttachmentConfig_differentAccount_base(rName string) string {
return testAccAlternateAccountAlternateRegionProviderConfig() +
testAccAWSEc2TransitGatewayPeeringAttachmentConfig_base(rName) +
fmt.Sprintf(`
data "aws_caller_identity" "peer" {
provider = "aws.alternate"
}
`)
}

func testAccAWSEc2TransitGatewayPeeringAttachmentConfigBasic_sameAccount(rName string) string {
return testAccAWSEc2TransitGatewayPeeringAttachmentConfig_sameAccount_base(rName) + fmt.Sprintf(`
resource "aws_ec2_transit_gateway_peering_attachment" "test" {
Expand All @@ -303,6 +362,17 @@ resource "aws_ec2_transit_gateway_peering_attachment" "test" {
`, testAccGetAlternateRegion())
}

func testAccAWSEc2TransitGatewayPeeringAttachmentConfigBasic_differentAccount(rName string) string {
return testAccAWSEc2TransitGatewayPeeringAttachmentConfig_differentAccount_base(rName) + fmt.Sprintf(`
resource "aws_ec2_transit_gateway_peering_attachment" "test" {
peer_account_id = "${data.aws_caller_identity.peer.account_id}"
peer_region = %[1]q
peer_transit_gateway_id = "${aws_ec2_transit_gateway.peer.id}"
transit_gateway_id = "${aws_ec2_transit_gateway.test.id}"
}
`, testAccGetAlternateRegion())
}

func testAccAWSEc2TransitGatewayPeeringAttachmentConfigTags1_sameAccount(rName, tagKey1, tagValue1 string) string {
return testAccAWSEc2TransitGatewayPeeringAttachmentConfig_sameAccount_base(rName) + fmt.Sprintf(`
resource "aws_ec2_transit_gateway_peering_attachment" "test" {
Expand Down

0 comments on commit b483433

Please sign in to comment.