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

d/aws_vpc: allow for multiple cidr blocks #5098

Merged
merged 2 commits into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 32 additions & 0 deletions aws/data_source_aws_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@ func dataSourceAwsVpc() *schema.Resource {
Computed: true,
},

"cidr_block_associations": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"association_id": {
Type: schema.TypeString,
Computed: true,
},
"cidr_block": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"dhcp_options_id": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -141,6 +162,17 @@ func dataSourceAwsVpcRead(d *schema.ResourceData, meta interface{}) error {
d.Set("state", vpc.State)
d.Set("tags", tagsToMap(vpc.Tags))

cidrAssociations := []interface{}{}
for _, associationSet := range vpc.CidrBlockAssociationSet {
association := map[string]interface{}{
"association_id": aws.StringValue(associationSet.AssociationId),
"cidr_block": aws.StringValue(associationSet.CidrBlock),
"state": aws.StringValue(associationSet.CidrBlockState.State),
}
cidrAssociations = append(cidrAssociations, association)
}
d.Set("cidr_block_associations", cidrAssociations)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When using d.Set() with aggregate types (TypeList, TypeSet, TypeMap), we should perform error checking to prevent issues where the code is not properly able to set the Terraform state. e.g.

if err := d.Set("cidr_block_associations", cidrAssociations); err != nil {
  return fmt.Errorf("error setting cidr_block_associations: %s", err)
}

Luckily this was working just fine before so will adjust this on merge. 👍


if vpc.Ipv6CidrBlockAssociationSet != nil {
d.Set("ipv6_association_id", vpc.Ipv6CidrBlockAssociationSet[0].AssociationId)
d.Set("ipv6_cidr_block", vpc.Ipv6CidrBlockAssociationSet[0].Ipv6CidrBlock)
Expand Down
39 changes: 39 additions & 0 deletions aws/data_source_aws_vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ func TestAccDataSourceAwsVpc_ipv6Associated(t *testing.T) {
})
}

func TestAccDataSourceAwsVpc_multipleCidr(t *testing.T) {
rInt := rand.Intn(16)
rName := "data.aws_vpc.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVpcDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataSourceAwsVpcConfigMultipleCidr(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(rName, "cidr_block_associations.#", "2"),
),
},
},
})
}

func testAccDataSourceAwsVpcCheck(name, cidr, tag string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
Expand Down Expand Up @@ -147,3 +166,23 @@ data "aws_vpc" "by_filter" {
}
}`, cidr, tag)
}

func testAccDataSourceAwsVpcConfigMultipleCidr(octet int) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "10.%d.0.0/16"
}

resource "aws_vpc_ipv4_cidr_block_association" "test" {
vpc_id = "${aws_vpc.test.id}"
cidr_block = "172.%d.0.0/16"
}

data "aws_vpc" "test" {
filter {
name = "cidr-block-association.cidr-block"
values = ["${aws_vpc_ipv4_cidr_block_association.test.cidr_block}"]
}
}
`, octet, octet)
}
6 changes: 6 additions & 0 deletions website/docs/d/vpc.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,9 @@ The following attribute is additionally exported:
* `ipv6_cidr_block` - The IPv6 CIDR block.
* `enable_dns_support` - Whether or not the VPC has DNS support
* `enable_dns_hostnames` - Whether or not the VPC has DNS hostname support

`cidr_block_associations` is also exported with the following attributes:

* `association_id` - The association ID for the the IPv4 CIDR block.
* `cidr_block` - The CIDR block for the association.
* `state` - The State of the association.