Skip to content

Commit

Permalink
Merge branch 'feature/add_cidr_block_set_to_datasource_vpc_peering' o…
Browse files Browse the repository at this point in the history
…f ssh://github.com/AlbertoSH/terraform-provider-aws into AlbertoSH-feature/add_cidr_block_set_to_datasource_vpc_peering
  • Loading branch information
bflad committed Jan 25, 2021
2 parents 4425957 + 62b9b1c commit bfe78e0
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
46 changes: 46 additions & 0 deletions aws/data_source_aws_vpc_peering_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ func dataSourceAwsVpcPeeringConnection() *schema.Resource {
Optional: true,
Computed: true,
},
"cidr_block_set": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cidr_block": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"region": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -60,6 +73,19 @@ func dataSourceAwsVpcPeeringConnection() *schema.Resource {
Optional: true,
Computed: true,
},
"peer_cidr_block_set": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cidr_block": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"peer_region": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -138,10 +164,30 @@ func dataSourceAwsVpcPeeringConnectionRead(d *schema.ResourceData, meta interfac
d.Set("vpc_id", pcx.RequesterVpcInfo.VpcId)
d.Set("owner_id", pcx.RequesterVpcInfo.OwnerId)
d.Set("cidr_block", pcx.RequesterVpcInfo.CidrBlock)
cidrBlockSet := []interface{}{}
for _, associationSet := range pcx.RequesterVpcInfo.CidrBlockSet {
association := map[string]interface{}{
"cidr_block": aws.StringValue(associationSet.CidrBlock),
}
cidrBlockSet = append(cidrBlockSet, association)
}
if err := d.Set("cidr_block_set", cidrBlockSet); err != nil {
return fmt.Errorf("error setting cidr_block_set: %s", err)
}
d.Set("region", pcx.RequesterVpcInfo.Region)
d.Set("peer_vpc_id", pcx.AccepterVpcInfo.VpcId)
d.Set("peer_owner_id", pcx.AccepterVpcInfo.OwnerId)
d.Set("peer_cidr_block", pcx.AccepterVpcInfo.CidrBlock)
peerCidrBlockSet := []interface{}{}
for _, associationSet := range pcx.AccepterVpcInfo.CidrBlockSet {
association := map[string]interface{}{
"cidr_block": aws.StringValue(associationSet.CidrBlock),
}
peerCidrBlockSet = append(peerCidrBlockSet, association)
}
if err := d.Set("peer_cidr_block_set", peerCidrBlockSet); err != nil {
return fmt.Errorf("error setting peer_cidr_block_set: %s", err)
}
d.Set("peer_region", pcx.AccepterVpcInfo.Region)
if err := d.Set("tags", keyvaluetags.Ec2KeyValueTags(pcx.Tags).IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
Expand Down
63 changes: 63 additions & 0 deletions aws/data_source_aws_vpc_peering_connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ func TestAccDataSourceAwsVpcPeeringConnection_basic(t *testing.T) {
})
}

func TestAccDataSourceAwsVpcPeeringConnection_cidrBlockSets(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsVpcPeeringConnectionCidrBlockSetConfig,
Check: resource.ComposeTestCheckFunc(
testAccDataSourceAwsVpcPeeringConnectionCheck("data.aws_vpc_peering_connection.test_by_id"),
resource.TestCheckResourceAttrSet("data.aws_vpc_peering_connection.test_by_id", "cidr_block_set.0.cidr_block"),
resource.TestCheckResourceAttrSet("data.aws_vpc_peering_connection.test_by_id", "cidr_block_set.1.cidr_block"),
resource.TestCheckResourceAttrSet("data.aws_vpc_peering_connection.test_by_id", "peer_cidr_block_set.0.cidr_block"),
resource.TestCheckResourceAttrSet("data.aws_vpc_peering_connection.test_by_id", "peer_cidr_block_set.1.cidr_block"),
),
},
},
})
}

func testAccDataSourceAwsVpcPeeringConnectionCheck(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
Expand Down Expand Up @@ -134,3 +153,47 @@ data "aws_vpc_peering_connection" "test_by_owner_ids" {
depends_on = [aws_vpc_peering_connection.test]
}
`

const testAccDataSourceAwsVpcPeeringConnectionCidrBlockSetConfig = `
resource "aws_vpc" "foo" {
cidr_block = "10.4.0.0/16"
tags = {
Name = "terraform-testacc-vpc-peering-connection-data-source-foo-cidr-block-set"
}
}
resource "aws_vpc_ipv4_cidr_block_association" "foo_secondary_cidr" {
vpc_id = aws_vpc.foo.id
cidr_block = "10.5.0.0/16"
}
resource "aws_vpc" "bar" {
cidr_block = "10.6.0.0/16"
tags = {
Name = "terraform-testacc-vpc-peering-connection-data-source-bar-cidr-block-set"
}
}
resource "aws_vpc_ipv4_cidr_block_association" "bar_secondary_cidr" {
vpc_id = aws_vpc.bar.id
cidr_block = "10.7.0.0/16"
}
resource "aws_vpc_peering_connection" "test" {
vpc_id = aws_vpc.foo.id
peer_vpc_id = aws_vpc.bar.id
auto_accept = true
tags = {
Name = "terraform-testacc-vpc-peering-connection-data-source-foo-to-bar-cidr-block-set"
}
depends_on = ["aws_vpc_ipv4_cidr_block_association.foo_secondary_cidr", "aws_vpc_ipv4_cidr_block_association.bar_secondary_cidr"]
}
data "aws_vpc_peering_connection" "test_by_id" {
id = aws_vpc_peering_connection.test.id
}
`
12 changes: 10 additions & 2 deletions website/docs/d/vpc_peering_connection.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ The given filters must match exactly one VPC peering connection whose data will

* `owner_id` - (Optional) The AWS account ID of the owner of the requester VPC of the specific VPC Peering Connection to retrieve.

* `cidr_block` - (Optional) The CIDR block of the requester VPC of the specific VPC Peering Connection to retrieve.
* `cidr_block` - (Optional) The primary CIDR block of the requester VPC of the specific VPC Peering Connection to retrieve.

* `region` - (Optional) The region of the requester VPC of the specific VPC Peering Connection to retrieve.

* `peer_vpc_id` - (Optional) The ID of the accepter VPC of the specific VPC Peering Connection to retrieve.

* `peer_owner_id` - (Optional) The AWS account ID of the owner of the accepter VPC of the specific VPC Peering Connection to retrieve.

* `peer_cidr_block` - (Optional) The CIDR block of the accepter VPC of the specific VPC Peering Connection to retrieve.
* `peer_cidr_block` - (Optional) The primary CIDR block of the accepter VPC of the specific VPC Peering Connection to retrieve.

* `peer_region` - (Optional) The region of the accepter VPC of the specific VPC Peering Connection to retrieve.

Expand Down Expand Up @@ -82,6 +82,10 @@ All of the argument attributes except `filter` are also exported as result attri
* `requester` - A configuration block that describes [VPC Peering Connection]
(https://docs.aws.amazon.com/vpc/latest/peering/what-is-vpc-peering.html) options set for the requester VPC.

* `cidr_block_set` - (Optional) The list of all CIDR blocks of the requester VPC of the specific VPC Peering Connection to retrieve.

* `peer_cidr_block_set` - (Optional) The list of all CIDR blocks of the accepter VPC of the specific VPC Peering Connection to retrieve.

#### Accepter and Requester Attributes Reference

* `allow_remote_vpc_dns_resolution` - Indicates whether a local VPC can resolve public DNS hostnames to
Expand All @@ -92,3 +96,7 @@ with the peer VPC over the VPC peering connection.

* `allow_vpc_to_remote_classic_link` - Indicates whether a local VPC can communicate with a ClassicLink
connection in the peer VPC over the VPC peering connection.

#### CIDR block set Attributes Reference

* `cidr_block` - A CIDR block associated to the VPC of the specific VPC Peering Connection.

0 comments on commit bfe78e0

Please sign in to comment.