Skip to content

Commit

Permalink
Merge pull request #5038 from kl4w/filter-subnet-ids-data-source
Browse files Browse the repository at this point in the history
d/aws_subnet_ids: Add filtering
  • Loading branch information
bflad authored Jun 29, 2018
2 parents 7c3c613 + 994e919 commit e73a028
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 8 deletions.
31 changes: 23 additions & 8 deletions aws/data_source_aws_subnet_ids.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func dataSourceAwsSubnetIDs() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsSubnetIDsRead,
Schema: map[string]*schema.Schema{
"filter": ec2CustomFiltersSchema(),

"tags": tagsSchemaComputed(),

Expand All @@ -35,15 +36,29 @@ func dataSourceAwsSubnetIDsRead(d *schema.ResourceData, meta interface{}) error

req := &ec2.DescribeSubnetsInput{}

req.Filters = buildEC2AttributeFilterList(
map[string]string{
"vpc-id": d.Get("vpc_id").(string),
},
)
if vpc, vpcOk := d.GetOk("vpc_id"); vpcOk {
req.Filters = buildEC2AttributeFilterList(
map[string]string{
"vpc-id": vpc.(string),
},
)
}

if tags, tagsOk := d.GetOk("tags"); tagsOk {
req.Filters = append(req.Filters, buildEC2TagFilterList(
tagsFromMap(tags.(map[string]interface{})),
)...)
}

if filters, filtersOk := d.GetOk("filter"); filtersOk {
req.Filters = append(req.Filters, buildEC2CustomFilterList(
filters.(*schema.Set),
)...)
}

req.Filters = append(req.Filters, buildEC2TagFilterList(
tagsFromMap(d.Get("tags").(map[string]interface{})),
)...)
if len(req.Filters) == 0 {
req.Filters = nil
}

log.Printf("[DEBUG] DescribeSubnets %s\n", req)
resp, err := conn.DescribeSubnets(req)
Expand Down
56 changes: 56 additions & 0 deletions aws/data_source_aws_subnet_ids_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ func TestAccDataSourceAwsSubnetIDs(t *testing.T) {
})
}

func TestAccDataSourceAwsSubnetIDs_filter(t *testing.T) {
rInt := acctest.RandIntRange(0, 256)
rName := "data.aws_subnet_ids.test"

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

func testAccDataSourceAwsSubnetIDsConfigWithDataSource(rInt int) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
Expand Down Expand Up @@ -129,3 +148,40 @@ resource "aws_subnet" "test_private_b" {
}
`, rInt, rInt, rInt, rInt)
}

func testAccDataSourceAwsSubnetIDs_filter(rInt int) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "172.%d.0.0/16"
tags {
Name = "terraform-testacc-subnet-ids-data-source"
}
}
resource "aws_subnet" "test_a_one" {
vpc_id = "${aws_vpc.test.id}"
cidr_block = "172.%d.1.0/24"
availability_zone = "us-west-2a"
}
resource "aws_subnet" "test_a_two" {
vpc_id = "${aws_vpc.test.id}"
cidr_block = "172.%d.2.0/24"
availability_zone = "us-west-2a"
}
resource "aws_subnet" "test_b" {
vpc_id = "${aws_vpc.test.id}"
cidr_block = "172.%d.3.0/24"
availability_zone = "us-west-2b"
}
data "aws_subnet_ids" "test" {
vpc_id = "${aws_subnet.test_a_two.vpc_id}"
filter {
name = "availabilityZone"
values = ["${aws_subnet.test_a_one.availability_zone}"]
}
}
`, rInt, rInt, rInt, rInt)
}

0 comments on commit e73a028

Please sign in to comment.