forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge changes from master for service/ec2: Add Resource & Data Source…
… aws_ec2_transit_gateway_peering_attachment (hashicorp#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
1 parent
01be436
commit 4cee185
Showing
15 changed files
with
1,191 additions
and
4 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
aws/data_source_aws_ec2_transit_gateway_peering_attachment.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
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 | ||
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig | ||
|
||
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().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { | ||
return fmt.Errorf("error setting tags: %s", err) | ||
} | ||
|
||
d.SetId(aws.StringValue(transitGatewayPeeringAttachment.TransitGatewayAttachmentId)) | ||
|
||
return nil | ||
} |
211 changes: 211 additions & 0 deletions
211
aws/data_source_aws_ec2_transit_gateway_peering_attachment_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
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 testAccAWSEc2TransitGatewayPeeringAttachmentDataSourceConfigFilter_sameAccount(rName string) string { | ||
return composeConfig( | ||
testAccAWSEc2TransitGatewayPeeringAttachmentConfigBasic_sameAccount(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 composeConfig( | ||
testAccAWSEc2TransitGatewayPeeringAttachmentConfigBasic_sameAccount(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 composeConfig( | ||
testAccAWSEc2TransitGatewayPeeringAttachmentConfigTags1_sameAccount(rName, "Name", 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 composeConfig( | ||
testAccAWSEc2TransitGatewayPeeringAttachmentConfigBasic_differentAccount(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 composeConfig( | ||
testAccAWSEc2TransitGatewayPeeringAttachmentConfigBasic_differentAccount(rName), | ||
fmt.Sprintf(` | ||
data "aws_ec2_transit_gateway_peering_attachment" "test" { | ||
provider = "aws.alternate" | ||
id = "${aws_ec2_transit_gateway_peering_attachment.test.id}" | ||
} | ||
`)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.