Skip to content

Commit

Permalink
Merge pull request #5 from ewbankkit/add_transit_gw_peering
Browse files Browse the repository at this point in the history
improved acceptance tests
  • Loading branch information
j-nix authored Jan 11, 2020
2 parents 4e48f9f + b483433 commit e521a8b
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 90 deletions.
35 changes: 20 additions & 15 deletions aws/data_source_aws_ec2_transit_gateway_peering_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func dataSourceAwsEc2TransitGatewayPeeringAttachment() *schema.Resource {
Read: dataSourceAwsEc2TransitGatewayPeeringAttachmentRead,

Schema: map[string]*schema.Schema{
"filter": dataSourceFiltersSchema(),
"filter": ec2CustomFiltersSchema(),
"id": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -47,44 +47,49 @@ func dataSourceAwsEc2TransitGatewayPeeringAttachmentRead(d *schema.ResourceData,

input := &ec2.DescribeTransitGatewayPeeringAttachmentsInput{}

if v, ok := d.GetOk("filter"); ok {
input.Filters = buildAwsDataSourceFilters(v.(*schema.Set))
if v, ok := d.GetOk("id"); ok {
input.TransitGatewayAttachmentIds = aws.StringSlice([]string{v.(string)})
}

if v, ok := d.GetOk("id"); ok {
input.TransitGatewayAttachmentIds = []*string{aws.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 Gateways: %s", input)
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 Route Table: %s", err)
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 Route Table: no results found")
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 Route Table: multiple results found, try adjusting search criteria")
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 Route Table: empty result")
return errors.New("error reading EC2 Transit Gateway Peering Attachment: empty result")
}

d.Set("peer_account_id", transitGatewayPeeringAttachment.AccepterTgwInfo.OwnerId)
d.Set("peer_region", transitGatewayPeeringAttachment.AccepterTgwInfo.Region)
d.Set("peer_transit_gateway_id", transitGatewayPeeringAttachment.AccepterTgwInfo.TransitGatewayId)
d.Set("transit_gateway_id", transitGatewayPeeringAttachment.RequesterTgwInfo.TransitGatewayId)

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

d.Set("peer_account_id", aws.StringValue(transitGatewayPeeringAttachment.AccepterTgwInfo.OwnerId))
d.Set("peer_region", aws.StringValue(transitGatewayPeeringAttachment.AccepterTgwInfo.Region))
d.Set("peer_transit_gateway_id", aws.StringValue(transitGatewayPeeringAttachment.AccepterTgwInfo.TransitGatewayId))
d.Set("transit_gateway_id", aws.StringValue(transitGatewayPeeringAttachment.RequesterTgwInfo.TransitGatewayId))

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

return nil
Expand Down
138 changes: 72 additions & 66 deletions aws/data_source_aws_ec2_transit_gateway_peering_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@ 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(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) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy,
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAWSEc2TransitGateway(t)
testAccMultipleRegionsPreCheck(t)
testAccAlternateRegionPreCheck(t)
},
ProviderFactories: testAccProviderFactories(&providers),
CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigFilter(),
Config: testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigFilter(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "peer_account_id", dataSourceName, "peer_account_id"),
resource.TestCheckResourceAttrPair(resourceName, "peer_region", dataSourceName, "peer_region"),
Expand All @@ -31,16 +40,23 @@ func TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_Filter(t *testing.T)
}

func TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_ID(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) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy,
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAWSEc2TransitGateway(t)
testAccMultipleRegionsPreCheck(t)
testAccAlternateRegionPreCheck(t)
},
ProviderFactories: testAccProviderFactories(&providers),
CheckDestroy: testAccCheckAWSEc2TransitGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigID(),
Config: testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigID(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "peer_account_id", dataSourceName, "peer_account_id"),
resource.TestCheckResourceAttrPair(resourceName, "peer_region", dataSourceName, "peer_region"),
Expand All @@ -53,85 +69,75 @@ func TestAccAWSEc2TransitGatewayPeeringAttachmentDataSource_ID(t *testing.T) {
})
}

func testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigFilter() string {
return testAccAlternateRegionProviderConfig() + fmt.Sprintf(`
data "aws_caller_identity" "second" {
provider = "aws.alternate"
}
resource "aws_ec2_transit_gateway" "first" {
provider = "aws"
tags = {
Name = "tf-acc-test-ec2-transit-gateway-first-Filter"
}
}
resource "aws_ec2_transit_gateway" "second" {
provider = "aws.alternate"
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"

tags = {
Name = "tf-acc-test-ec2-transit-gateway-second-Filter"
}
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(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"),
),
},
},
})
}

// Create the Peering attachment in the first account...
func testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfig_base(rName string) string {
return testAccAWSEc2TransitGatewayPeeringAttachmentConfig_sameAccount_base(rName) + fmt.Sprintf(`
resource "aws_ec2_transit_gateway_peering_attachment" "test" {
peer_account_id = "${data.aws_caller_identity.second.account_id}"
peer_region = %[1]q
peer_transit_gateway_id = "${aws_ec2_transit_gateway.second.id}"
transit_gateway_id = "${aws_ec2_transit_gateway.first.id}"
peer_transit_gateway_id = "${aws_ec2_transit_gateway.peer.id}"
transit_gateway_id = "${aws_ec2_transit_gateway.test.id}"
tags = {
Name = "tf-acc-test-ec2-transit-gateway-peering-attachment-Filter"
Name = %[2]q
}
}
`, testAccGetAlternateRegion(), rName)
}

func testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigFilter(rName string) string {
return testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfig_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}"]
}
}
`, testAccGetAlternateRegion())
}

func testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigID() string {
return testAccAlternateRegionProviderConfig() + fmt.Sprintf(`
data "aws_caller_identity" "second" {
provider = "aws.alternate"
`)
}

resource "aws_ec2_transit_gateway" "first" {
provider = "aws"
tags = {
Name = "tf-acc-test-ec2-transit-gateway-first-ID"
}
func testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigID(rName string) string {
return testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfig_base(rName) + fmt.Sprintf(`
data "aws_ec2_transit_gateway_peering_attachment" "test" {
id = "${aws_ec2_transit_gateway_peering_attachment.test.id}"
}
resource "aws_ec2_transit_gateway" "second" {
provider = "aws.alternate"
tags = {
Name = "tf-acc-test-ec2-transit-gateway-second-ID"
}
`)
}

// Create the Peering attachment in the first account...
resource "aws_ec2_transit_gateway_peering_attachment" "test" {
peer_account_id = "${data.aws_caller_identity.second.account_id}"
peer_region = %[1]q
peer_transit_gateway_id = "${aws_ec2_transit_gateway.second.id}"
transit_gateway_id = "${aws_ec2_transit_gateway.first.id}"
func testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigTags(rName string) string {
return testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfig_base(rName) + fmt.Sprintf(`
data "aws_ec2_transit_gateway_peering_attachment" "test" {
tags = {
Name = "tf-acc-test-ec2-transit-gateway-peering-attachment-ID"
Name = "${aws_ec2_transit_gateway_peering_attachment.test.tags["Name"]}"
}
}
data "aws_ec2_transit_gateway_peering_attachment" "test" {
id = "${aws_ec2_transit_gateway_peering_attachment.test.id}"
}
`, testAccGetAlternateRegion())
`)
}
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
Loading

0 comments on commit e521a8b

Please sign in to comment.