From 52459172f8947a8f8a33079d69b0a1169d6a68d6 Mon Sep 17 00:00:00 2001 From: Farhan Angullia Date: Tue, 13 Apr 2021 02:39:50 +0800 Subject: [PATCH 1/7] Added new data source for subnets and acceptance tests - based on data_source_aws_subnets_id --- aws/data_source_aws_subnets.go | 71 +++++++++ aws/data_source_aws_subnets_test.go | 224 ++++++++++++++++++++++++++++ 2 files changed, 295 insertions(+) create mode 100644 aws/data_source_aws_subnets.go create mode 100644 aws/data_source_aws_subnets_test.go diff --git a/aws/data_source_aws_subnets.go b/aws/data_source_aws_subnets.go new file mode 100644 index 00000000000..820c5bcba53 --- /dev/null +++ b/aws/data_source_aws_subnets.go @@ -0,0 +1,71 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" +) + +func dataSourceAwsSubnets() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsSubnetsRead, + Schema: map[string]*schema.Schema{ + "filter": ec2CustomFiltersSchema(), + + "tags": tagsSchemaComputed(), + + "ids": { + Type: schema.TypeSet, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + }, + } +} + +func dataSourceAwsSubnetsRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ec2conn + + req := &ec2.DescribeSubnetsInput{} + + if tags, tagsOk := d.GetOk("tags"); tagsOk { + req.Filters = append(req.Filters, buildEC2TagFilterList( + keyvaluetags.New(tags.(map[string]interface{})).Ec2Tags(), + )...) + } + + if filters, filtersOk := d.GetOk("filter"); filtersOk { + req.Filters = append(req.Filters, buildEC2CustomFilterList( + filters.(*schema.Set), + )...) + } + + if len(req.Filters) == 0 { + req.Filters = nil + } + + log.Printf("[DEBUG] DescribeSubnets %s\n", req) + resp, err := conn.DescribeSubnets(req) + if err != nil { + return err + } + + if resp == nil || len(resp.Subnets) == 0 { + return fmt.Errorf("no matching subnets found") + } + + subnets := make([]string, 0) + + for _, subnet := range resp.Subnets { + subnets = append(subnets, *subnet.SubnetId) + } + + d.SetId(meta.(*AWSClient).region) + d.Set("ids", subnets) + + return nil +} diff --git a/aws/data_source_aws_subnets_test.go b/aws/data_source_aws_subnets_test.go new file mode 100644 index 00000000000..b623b68d9f2 --- /dev/null +++ b/aws/data_source_aws_subnets_test.go @@ -0,0 +1,224 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccDataSourceAwsSubnets_basic(t *testing.T) { + rInt := acctest.RandIntRange(0, 256) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID), + Providers: testAccProviders, + CheckDestroy: testAccCheckVpcDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAwsSubnetsConfig(rInt), + }, + { + Config: testAccDataSourceAwsSubnetsConfigWithDataSource(rInt), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_subnets.selected", "ids.#", "3"), + resource.TestCheckResourceAttr("data.aws_subnets.private", "ids.#", "2"), + testCheckResourceAttrGreaterThanValue("data.aws_subnets.all", "ids.#", "0"), + ), + }, + }, + }) +} + +func TestAccDataSourceAwsSubnets_filter(t *testing.T) { + rInt := acctest.RandIntRange(0, 256) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID), + Providers: testAccProviders, + CheckDestroy: testAccCheckVpcDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAwsSubnets_filter(rInt), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_subnets.test_vpc_id", "ids.#", "2"), + testCheckResourceAttrGreaterThanValue("data.aws_subnets.test_no_vpc_id", "ids.#", "0"), + ), + }, + }, + }) +} + +func testAccDataSourceAwsSubnetsConfigWithDataSource(rInt int) string { + return composeConfig(testAccAvailableAZsNoOptInConfig(), fmt.Sprintf(` +resource "aws_vpc" "test" { + cidr_block = "172.%[1]d.0.0/16" + + tags = { + Name = "terraform-testacc-subnets-data-source" + } +} + +resource "aws_subnet" "test_public_a" { + vpc_id = aws_vpc.test.id + cidr_block = "172.%[1]d.123.0/24" + availability_zone = data.aws_availability_zones.available.names[0] + + tags = { + Name = "tf-acc-subnets-data-source-public-a" + Tier = "Public" + } +} + +resource "aws_subnet" "test_public_b" { + vpc_id = aws_vpc.test.id + cidr_block = "172.%[1]d.124.0/24" + availability_zone = data.aws_availability_zones.available.names[0] + + tags = { + Name = "tf-acc-subnets-data-source-public-b" + Tier = "Public" + } +} + +resource "aws_subnet" "test_private_a" { + vpc_id = aws_vpc.test.id + cidr_block = "172.%[1]d.125.0/24" + availability_zone = data.aws_availability_zones.available.names[0] + + tags = { + Name = "tf-acc-subnets-data-source-private-a" + Tier = "Private" + } +} + +resource "aws_subnet" "test_private_b" { + vpc_id = aws_vpc.test.id + cidr_block = "172.%[1]d.126.0/24" + availability_zone = data.aws_availability_zones.available.names[1] + + tags = { + Name = "tf-acc-subnets-data-source-private-b" + Tier = "Private" + } +} + +data "aws_subnets" "selected" { + filter { + name = "vpc-id" + values = [aws_vpc.test.id] + } +} + +data "aws_subnets" "private" { + filter { + name = "vpc-id" + values = [aws_vpc.test.id] + } + + tags = { + Tier = "Private" + } +} + +data "aws_subnets" "all" {} +`, rInt)) +} + +func testAccDataSourceAwsSubnetsConfig(rInt int) string { + return composeConfig(testAccAvailableAZsNoOptInConfig(), fmt.Sprintf(` +resource "aws_vpc" "test" { + cidr_block = "172.%[1]d.0.0/16" + + tags = { + Name = "terraform-testacc-subnets-data-source" + } +} + +resource "aws_subnet" "test_public_a" { + vpc_id = aws_vpc.test.id + cidr_block = "172.%[1]d.123.0/24" + availability_zone = data.aws_availability_zones.available.names[0] + + tags = { + Name = "tf-acc-subnets-data-source-public-a" + Tier = "Public" + } +} + +resource "aws_subnet" "test_private_a" { + vpc_id = aws_vpc.test.id + cidr_block = "172.%[1]d.125.0/24" + availability_zone = data.aws_availability_zones.available.names[0] + + tags = { + Name = "tf-acc-subnets-data-source-private-a" + Tier = "Private" + } +} + +resource "aws_subnet" "test_private_b" { + vpc_id = aws_vpc.test.id + cidr_block = "172.%[1]d.126.0/24" + availability_zone = data.aws_availability_zones.available.names[1] + + tags = { + Name = "tf-acc-subnets-data-source-private-b" + Tier = "Private" + } +} +`, rInt)) +} + +func testAccDataSourceAwsSubnets_filter(rInt int) string { + return composeConfig(testAccAvailableAZsNoOptInConfig(), fmt.Sprintf(` +resource "aws_vpc" "test" { + cidr_block = "172.%[1]d.0.0/16" + + tags = { + Name = "terraform-testacc-subnets-data-source" + } +} + +resource "aws_subnet" "test_a_one" { + vpc_id = aws_vpc.test.id + cidr_block = "172.%[1]d.1.0/24" + availability_zone = data.aws_availability_zones.available.names[0] +} + +resource "aws_subnet" "test_a_two" { + vpc_id = aws_vpc.test.id + cidr_block = "172.%[1]d.2.0/24" + availability_zone = data.aws_availability_zones.available.names[0] +} + +resource "aws_subnet" "test_b" { + vpc_id = aws_vpc.test.id + cidr_block = "172.%[1]d.3.0/24" + availability_zone = data.aws_availability_zones.available.names[1] +} + +data "aws_subnets" "test_vpc_id" { + filter { + name = "availabilityZone" + values = [aws_subnet.test_a_one.availability_zone] + } + + filter { + name = "vpc-id" + values = [aws_subnet.test_a_two.vpc_id] + } +} + +data "aws_subnets" "test_no_vpc_id" { + filter { + name = "availabilityZone" + values = [aws_subnet.test_b.availability_zone] + } +} +`, rInt)) +} From 3ff04c35705b471daf9388a3a19b2b2215f4a161 Mon Sep 17 00:00:00 2001 From: Farhan Angullia Date: Tue, 13 Apr 2021 02:40:39 +0800 Subject: [PATCH 2/7] Added aws_subnets data source to DataSourcesMap schema --- aws/provider.go | 1 + 1 file changed, 1 insertion(+) diff --git a/aws/provider.go b/aws/provider.go index 39138bbdb13..20e12045679 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -413,6 +413,7 @@ func Provider() *schema.Provider { "aws_ssoadmin_permission_set": dataSourceAwsSsoAdminPermissionSet(), "aws_storagegateway_local_disk": dataSourceAwsStorageGatewayLocalDisk(), "aws_subnet": dataSourceAwsSubnet(), + "aws_subnets": dataSourceAwsSubnets(), "aws_subnet_ids": dataSourceAwsSubnetIDs(), "aws_transfer_server": dataSourceAwsTransferServer(), "aws_vpcs": dataSourceAwsVpcs(), From f3cb79ba12382451e0fb092f9281b40dcf21f749 Mon Sep 17 00:00:00 2001 From: Farhan Angullia Date: Tue, 13 Apr 2021 02:41:01 +0800 Subject: [PATCH 3/7] Docs - Added site for subnets --- website/docs/d/subnets.html.markdown | 89 ++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 website/docs/d/subnets.html.markdown diff --git a/website/docs/d/subnets.html.markdown b/website/docs/d/subnets.html.markdown new file mode 100644 index 00000000000..f326475ae6c --- /dev/null +++ b/website/docs/d/subnets.html.markdown @@ -0,0 +1,89 @@ +--- +subcategory: "VPC" +layout: "aws" +page_title: "AWS: aws_subnets" +description: |- + Provides a set of subnet Ids +--- + +# Data Source: aws_subnets + +`aws_subnets` provides a set of ids + +This resource can be useful for getting back a set of subnet ids. + +## Example Usage + +The following shows outputing all cidr blocks for every subnet id in a vpc. + +```terraform +data "aws_subnets" "example" { + filter { + name = "vpc-id" + values = [var.vpc_id] + } +} + +data "aws_subnet" "example" { + for_each = data.aws_subnets.example.ids + id = each.value +} + +output "subnet_cidr_blocks" { + value = [for s in data.aws_subnet.example : s.cidr_block] +} +``` + +The following example retrieves a set of all subnets in a VPC with a custom +tag of `Tier` set to a value of "Private" so that the `aws_instance` resource +can loop through the subnets, putting instances across availability zones. + +```terraform +data "aws_subnets" "private" { + filter { + name = "vpc-id" + values = [var.vpc_id] + } + + tags = { + Tier = "Private" + } +} + +resource "aws_instance" "app" { + for_each = data.aws_subnets.example.ids + ami = var.ami + instance_type = "t2.micro" + subnet_id = each.value +} +``` + +## Argument Reference + +* `filter` - (Optional) Custom filter block as described below. + +* `tags` - (Optional) A map of tags, each pair of which must exactly match + a pair on the desired subnets. + +More complex filters can be expressed using one or more `filter` sub-blocks, +which take the following arguments: + +* `name` - (Required) The name of the field to filter by, as defined by + [the underlying AWS API](http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeSubnets.html). + For example, if matching against tag `Name`, use: + +```terraform +data "aws_subnets" "selected" { + filter { + name = "tag:Name" + values = [""] # insert values here + } +} +``` + +* `values` - (Required) Set of values that are accepted for the given field. + Subnet IDs will be selected if any one of the given values match. + +## Attributes Reference + +* `ids` - A set of all the subnet ids found. This data source will fail if none are found. From af45f68c38d68465a153845ca78edf020e100e0d Mon Sep 17 00:00:00 2001 From: Farhan Angullia Date: Tue, 13 Apr 2021 03:41:18 +0800 Subject: [PATCH 4/7] Removed unnecessary check in subnets filter acc test --- aws/data_source_aws_subnets_test.go | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/aws/data_source_aws_subnets_test.go b/aws/data_source_aws_subnets_test.go index b623b68d9f2..17af00dd019 100644 --- a/aws/data_source_aws_subnets_test.go +++ b/aws/data_source_aws_subnets_test.go @@ -45,8 +45,7 @@ func TestAccDataSourceAwsSubnets_filter(t *testing.T) { { Config: testAccDataSourceAwsSubnets_filter(rInt), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.aws_subnets.test_vpc_id", "ids.#", "2"), - testCheckResourceAttrGreaterThanValue("data.aws_subnets.test_no_vpc_id", "ids.#", "0"), + resource.TestCheckResourceAttr("data.aws_subnets.test", "ids.#", "2"), ), }, }, @@ -202,7 +201,7 @@ resource "aws_subnet" "test_b" { availability_zone = data.aws_availability_zones.available.names[1] } -data "aws_subnets" "test_vpc_id" { +data "aws_subnets" "test" { filter { name = "availabilityZone" values = [aws_subnet.test_a_one.availability_zone] @@ -213,12 +212,5 @@ data "aws_subnets" "test_vpc_id" { values = [aws_subnet.test_a_two.vpc_id] } } - -data "aws_subnets" "test_no_vpc_id" { - filter { - name = "availabilityZone" - values = [aws_subnet.test_b.availability_zone] - } -} `, rInt)) } From 44175c075c8490438efad4e19990572145356edf Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 17 Aug 2021 09:41:38 -0400 Subject: [PATCH 5/7] Add CHANGELOG entry. --- .changelog/18803.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/18803.txt diff --git a/.changelog/18803.txt b/.changelog/18803.txt new file mode 100644 index 00000000000..d948e6d6d82 --- /dev/null +++ b/.changelog/18803.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +aws_subnets +``` \ No newline at end of file From 5495c57fc488efebd93e5d89cf8eb7121c1ca695 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 17 Aug 2021 10:38:00 -0400 Subject: [PATCH 6/7] d/aws_subnets: Allow zero results. --- aws/data_source_aws_subnets.go | 48 +++++----- aws/data_source_aws_subnets_test.go | 128 ++++++++++++--------------- website/docs/d/subnets.html.markdown | 11 +-- 3 files changed, 82 insertions(+), 105 deletions(-) diff --git a/aws/data_source_aws_subnets.go b/aws/data_source_aws_subnets.go index 820c5bcba53..053d24ef601 100644 --- a/aws/data_source_aws_subnets.go +++ b/aws/data_source_aws_subnets.go @@ -2,8 +2,8 @@ package aws import ( "fmt" - "log" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" @@ -13,15 +13,13 @@ func dataSourceAwsSubnets() *schema.Resource { return &schema.Resource{ Read: dataSourceAwsSubnetsRead, Schema: map[string]*schema.Schema{ - "filter": ec2CustomFiltersSchema(), - - "tags": tagsSchemaComputed(), + "filter": dataSourceFiltersSchema(), + "tags": tagsSchemaComputed(), "ids": { - Type: schema.TypeSet, + Type: schema.TypeList, Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, - Set: schema.HashString, }, }, } @@ -30,42 +28,42 @@ func dataSourceAwsSubnets() *schema.Resource { func dataSourceAwsSubnetsRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ec2conn - req := &ec2.DescribeSubnetsInput{} + input := &ec2.DescribeSubnetsInput{} if tags, tagsOk := d.GetOk("tags"); tagsOk { - req.Filters = append(req.Filters, buildEC2TagFilterList( + input.Filters = append(input.Filters, buildEC2TagFilterList( keyvaluetags.New(tags.(map[string]interface{})).Ec2Tags(), )...) } if filters, filtersOk := d.GetOk("filter"); filtersOk { - req.Filters = append(req.Filters, buildEC2CustomFilterList( - filters.(*schema.Set), - )...) + input.Filters = append(input.Filters, + buildAwsDataSourceFilters(filters.(*schema.Set))...) } - if len(req.Filters) == 0 { - req.Filters = nil + if len(input.Filters) == 0 { + input.Filters = nil } - log.Printf("[DEBUG] DescribeSubnets %s\n", req) - resp, err := conn.DescribeSubnets(req) - if err != nil { - return err - } + var subnetIDs []*string + err := conn.DescribeSubnetsPages(input, func(page *ec2.DescribeSubnetsOutput, lastPage bool) bool { + if page == nil { + return !lastPage + } - if resp == nil || len(resp.Subnets) == 0 { - return fmt.Errorf("no matching subnets found") - } + for _, subnet := range page.Subnets { + subnetIDs = append(subnetIDs, subnet.SubnetId) + } - subnets := make([]string, 0) + return !lastPage + }) - for _, subnet := range resp.Subnets { - subnets = append(subnets, *subnet.SubnetId) + if err != nil { + return fmt.Errorf("error reading Subnets: %w", err) } d.SetId(meta.(*AWSClient).region) - d.Set("ids", subnets) + d.Set("ids", aws.StringValueSlice(subnetIDs)) return nil } diff --git a/aws/data_source_aws_subnets_test.go b/aws/data_source_aws_subnets_test.go index 17af00dd019..ccfcdc9cc18 100644 --- a/aws/data_source_aws_subnets_test.go +++ b/aws/data_source_aws_subnets_test.go @@ -10,23 +10,23 @@ import ( ) func TestAccDataSourceAwsSubnets_basic(t *testing.T) { - rInt := acctest.RandIntRange(0, 256) + rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID), - Providers: testAccProviders, - CheckDestroy: testAccCheckVpcDestroy, + PreCheck: func() { testAccPreCheck(t) }, + ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID), + Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccDataSourceAwsSubnetsConfig(rInt), + Config: testAccDataSourceAwsSubnetsConfig(rName), }, { - Config: testAccDataSourceAwsSubnetsConfigWithDataSource(rInt), + Config: testAccDataSourceAwsSubnetsConfigWithDataSource(rName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.aws_subnets.selected", "ids.#", "3"), + resource.TestCheckResourceAttr("data.aws_subnets.selected", "ids.#", "4"), resource.TestCheckResourceAttr("data.aws_subnets.private", "ids.#", "2"), testCheckResourceAttrGreaterThanValue("data.aws_subnets.all", "ids.#", "0"), + resource.TestCheckResourceAttr("data.aws_subnets.none", "ids.#", "0"), ), }, }, @@ -34,16 +34,15 @@ func TestAccDataSourceAwsSubnets_basic(t *testing.T) { } func TestAccDataSourceAwsSubnets_filter(t *testing.T) { - rInt := acctest.RandIntRange(0, 256) + rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID), - Providers: testAccProviders, - CheckDestroy: testAccCheckVpcDestroy, + PreCheck: func() { testAccPreCheck(t) }, + ErrorCheck: testAccErrorCheck(t, ec2.EndpointsID), + Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccDataSourceAwsSubnets_filter(rInt), + Config: testAccDataSourceAwsSubnets_filter(rName), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("data.aws_subnets.test", "ids.#", "2"), ), @@ -52,60 +51,64 @@ func TestAccDataSourceAwsSubnets_filter(t *testing.T) { }) } -func testAccDataSourceAwsSubnetsConfigWithDataSource(rInt int) string { +func testAccDataSourceAwsSubnetsConfig(rName string) string { return composeConfig(testAccAvailableAZsNoOptInConfig(), fmt.Sprintf(` resource "aws_vpc" "test" { - cidr_block = "172.%[1]d.0.0/16" + cidr_block = "172.16.0.0/16" tags = { - Name = "terraform-testacc-subnets-data-source" + Name = %[1]q } } resource "aws_subnet" "test_public_a" { vpc_id = aws_vpc.test.id - cidr_block = "172.%[1]d.123.0/24" + cidr_block = "172.16.123.0/24" availability_zone = data.aws_availability_zones.available.names[0] tags = { - Name = "tf-acc-subnets-data-source-public-a" + Name = "%[1]s-public-a" Tier = "Public" } } resource "aws_subnet" "test_public_b" { vpc_id = aws_vpc.test.id - cidr_block = "172.%[1]d.124.0/24" + cidr_block = "172.16.124.0/24" availability_zone = data.aws_availability_zones.available.names[0] tags = { - Name = "tf-acc-subnets-data-source-public-b" + Name = "%[1]s-public-b" Tier = "Public" } } resource "aws_subnet" "test_private_a" { vpc_id = aws_vpc.test.id - cidr_block = "172.%[1]d.125.0/24" + cidr_block = "172.16.125.0/24" availability_zone = data.aws_availability_zones.available.names[0] tags = { - Name = "tf-acc-subnets-data-source-private-a" + Name = "%[1]s-private-a" Tier = "Private" } } resource "aws_subnet" "test_private_b" { vpc_id = aws_vpc.test.id - cidr_block = "172.%[1]d.126.0/24" + cidr_block = "172.16.126.0/24" availability_zone = data.aws_availability_zones.available.names[1] tags = { - Name = "tf-acc-subnets-data-source-private-b" + Name = "%[1]s-private-b" Tier = "Private" } } +`, rName)) +} +func testAccDataSourceAwsSubnetsConfigWithDataSource(rName string) string { + return composeConfig(testAccDataSourceAwsSubnetsConfig(rName), ` data "aws_subnets" "selected" { filter { name = "vpc-id" @@ -125,82 +128,61 @@ data "aws_subnets" "private" { } data "aws_subnets" "all" {} -`, rInt)) + +data "aws_subnets" "none" { + filter { + name = "vpc-id" + values = [aws_vpc.test.id] + } + + filter { + name = "cidr-block" + values = ["172.16.127.0/24"] + } +} +`) } -func testAccDataSourceAwsSubnetsConfig(rInt int) string { +func testAccDataSourceAwsSubnets_filter(rName string) string { return composeConfig(testAccAvailableAZsNoOptInConfig(), fmt.Sprintf(` resource "aws_vpc" "test" { - cidr_block = "172.%[1]d.0.0/16" + cidr_block = "172.16.0.0/16" tags = { - Name = "terraform-testacc-subnets-data-source" + Name = %[1]q } } -resource "aws_subnet" "test_public_a" { +resource "aws_subnet" "test_a_one" { vpc_id = aws_vpc.test.id - cidr_block = "172.%[1]d.123.0/24" + cidr_block = "172.16.1.0/24" availability_zone = data.aws_availability_zones.available.names[0] tags = { - Name = "tf-acc-subnets-data-source-public-a" - Tier = "Public" + Name = "%[1]s-a1" } } -resource "aws_subnet" "test_private_a" { - vpc_id = aws_vpc.test.id - cidr_block = "172.%[1]d.125.0/24" +resource "aws_subnet" "test_a_two" { + vpc_id = aws_subnet.test_b.vpc_id + cidr_block = "172.16.2.0/24" availability_zone = data.aws_availability_zones.available.names[0] tags = { - Name = "tf-acc-subnets-data-source-private-a" - Tier = "Private" + Name = "%[1]s-a2" } } -resource "aws_subnet" "test_private_b" { +resource "aws_subnet" "test_b" { vpc_id = aws_vpc.test.id - cidr_block = "172.%[1]d.126.0/24" + cidr_block = "172.16.3.0/24" availability_zone = data.aws_availability_zones.available.names[1] tags = { - Name = "tf-acc-subnets-data-source-private-b" - Tier = "Private" - } -} -`, rInt)) -} - -func testAccDataSourceAwsSubnets_filter(rInt int) string { - return composeConfig(testAccAvailableAZsNoOptInConfig(), fmt.Sprintf(` -resource "aws_vpc" "test" { - cidr_block = "172.%[1]d.0.0/16" - - tags = { - Name = "terraform-testacc-subnets-data-source" + Name = "%[1]s-b" } } -resource "aws_subnet" "test_a_one" { - vpc_id = aws_vpc.test.id - cidr_block = "172.%[1]d.1.0/24" - availability_zone = data.aws_availability_zones.available.names[0] -} - -resource "aws_subnet" "test_a_two" { - vpc_id = aws_vpc.test.id - cidr_block = "172.%[1]d.2.0/24" - availability_zone = data.aws_availability_zones.available.names[0] -} - -resource "aws_subnet" "test_b" { - vpc_id = aws_vpc.test.id - cidr_block = "172.%[1]d.3.0/24" - availability_zone = data.aws_availability_zones.available.names[1] -} - data "aws_subnets" "test" { filter { name = "availabilityZone" @@ -212,5 +194,5 @@ data "aws_subnets" "test" { values = [aws_subnet.test_a_two.vpc_id] } } -`, rInt)) +`, rName)) } diff --git a/website/docs/d/subnets.html.markdown b/website/docs/d/subnets.html.markdown index f326475ae6c..22cda24b5d9 100644 --- a/website/docs/d/subnets.html.markdown +++ b/website/docs/d/subnets.html.markdown @@ -3,18 +3,16 @@ subcategory: "VPC" layout: "aws" page_title: "AWS: aws_subnets" description: |- - Provides a set of subnet Ids + Get information about a set of subnets. --- # Data Source: aws_subnets -`aws_subnets` provides a set of ids - -This resource can be useful for getting back a set of subnet ids. +This resource can be useful for getting back a set of subnet IDs. ## Example Usage -The following shows outputing all cidr blocks for every subnet id in a vpc. +The following shows outputing all CIDR blocks for every subnet ID in a VPC. ```terraform data "aws_subnets" "example" { @@ -61,7 +59,6 @@ resource "aws_instance" "app" { ## Argument Reference * `filter` - (Optional) Custom filter block as described below. - * `tags` - (Optional) A map of tags, each pair of which must exactly match a pair on the desired subnets. @@ -86,4 +83,4 @@ data "aws_subnets" "selected" { ## Attributes Reference -* `ids` - A set of all the subnet ids found. This data source will fail if none are found. +* `ids` - A list of all the subnet ids found. From 92b53bc39f4e2e8c539ce8d23807f9385c95c403 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 17 Aug 2021 11:07:19 -0400 Subject: [PATCH 7/7] Fix terrafmt errors. --- aws/data_source_aws_subnets_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/data_source_aws_subnets_test.go b/aws/data_source_aws_subnets_test.go index ccfcdc9cc18..baff9a13433 100644 --- a/aws/data_source_aws_subnets_test.go +++ b/aws/data_source_aws_subnets_test.go @@ -138,7 +138,7 @@ data "aws_subnets" "none" { filter { name = "cidr-block" values = ["172.16.127.0/24"] - } + } } `) }