From bf20428fa100f858275e592478240951af8e867c Mon Sep 17 00:00:00 2001 From: Max Englander Date: Fri, 26 Jul 2019 11:19:02 -0400 Subject: [PATCH 1/3] data-source/aws_db_subnet_group: create aws_db_subnet_group data-source Adds a data source for aws_db_subnet_group. Used aws_db_instance as a model for this work. Currently only allows looking up exactly one database subnet group using `name` as the argument, although the AWS RDS API also supports using `filters`. Returns all of the attributes listed on the AWS docs for the DBSubnetGroup data type. The `subnets` attribute contains a list of subnet identifiers. Resolves #3326 --- aws/data_source_aws_db_subnet_group.go | 93 ++++++++++++++++++++ aws/data_source_aws_db_subnet_group_test.go | 59 +++++++++++++ aws/provider.go | 1 + website/aws.erb | 3 + website/docs/d/db_subnet_group.html.markdown | 35 ++++++++ 5 files changed, 191 insertions(+) create mode 100644 aws/data_source_aws_db_subnet_group.go create mode 100644 aws/data_source_aws_db_subnet_group_test.go create mode 100644 website/docs/d/db_subnet_group.html.markdown diff --git a/aws/data_source_aws_db_subnet_group.go b/aws/data_source_aws_db_subnet_group.go new file mode 100644 index 00000000000..81ed721baec --- /dev/null +++ b/aws/data_source_aws_db_subnet_group.go @@ -0,0 +1,93 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/rds" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceAwsDbSubnetGroup() *schema.Resource { + return &schema.Resource{ + Read: dataSourceAwsDbSubnetGroupRead, + + Schema: map[string]*schema.Schema{ + "arn": { + Type: schema.TypeString, + Computed: true, + }, + + "description": { + Type: schema.TypeString, + Computed: true, + }, + + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "status": { + Type: schema.TypeString, + Computed: true, + }, + + "subnets": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + + "vpc_id": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceAwsDbSubnetGroupRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).rdsconn + + opts := &rds.DescribeDBSubnetGroupsInput{ + DBSubnetGroupName: aws.String(d.Get("name").(string)), + } + + log.Printf("[DEBUG] Reading DB SubnetGroup: %s", opts) + + resp, err := conn.DescribeDBSubnetGroups(opts) + if err != nil { + return err + } + + if len(resp.DBSubnetGroups) < 1 { + return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.") + } + if len(resp.DBSubnetGroups) > 1 { + return fmt.Errorf("Your query returned more than one result. Please try a more specific search criteria.") + } + + dbSubnetGroup := *resp.DBSubnetGroups[0] + + d.SetId(d.Get("name").(string)) + + d.Set("arn", dbSubnetGroup.DBSubnetGroupArn) + d.Set("description", dbSubnetGroup.DBSubnetGroupDescription) + d.Set("name", dbSubnetGroup.DBSubnetGroupName) + d.Set("status", dbSubnetGroup.SubnetGroupStatus) + + var subnets []string + for _, v := range dbSubnetGroup.Subnets { + subnets = append(subnets, *v.SubnetIdentifier) + } + if err := d.Set("subnets", subnets); err != nil { + return fmt.Errorf("Error setting subnets attribute: %#v, error: %#v", subnets, err) + } + + d.Set("vpc_id", dbSubnetGroup.VpcId) + + return nil +} diff --git a/aws/data_source_aws_db_subnet_group_test.go b/aws/data_source_aws_db_subnet_group_test.go new file mode 100644 index 00000000000..489ef4f02a6 --- /dev/null +++ b/aws/data_source_aws_db_subnet_group_test.go @@ -0,0 +1,59 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccAWSDbSubnetGroupDataSource_basic(t *testing.T) { + rInt := acctest.RandInt() + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccAWSDBSubnetGroupDataSourceConfig(rInt), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("data.aws_db_subnet_group.bar", "arn"), + resource.TestCheckResourceAttrSet("data.aws_db_subnet_group.bar", "description"), + resource.TestCheckResourceAttrSet("data.aws_db_subnet_group.bar", "name"), + resource.TestCheckResourceAttrSet("data.aws_db_subnet_group.bar", "status"), + resource.TestCheckResourceAttrSet("data.aws_db_subnet_group.bar", "subnets.0"), + resource.TestCheckResourceAttrSet("data.aws_db_subnet_group.bar", "subnets.1"), + resource.TestCheckResourceAttrSet("data.aws_db_subnet_group.bar", "vpc_id"), + ), + }, + }, + }) +} + +func testAccAWSDBSubnetGroupDataSourceConfig(rInt int) string { + return fmt.Sprintf(` +data "aws_availability_zones" "available" { + state = "available" +} + +resource "aws_vpc" "foo" { + cidr_block = "10.0.0.0/16" +} + +resource "aws_subnet" "foo" { + count = 2 + availability_zone = "${data.aws_availability_zones.available.names[count.index]}" + cidr_block = "10.0.${count.index}.0/24" + vpc_id = "${aws_vpc.foo.id}" +} + +resource "aws_db_subnet_group" "bar" { + name = "datasource-test-terraform-%d" + subnet_ids = "${aws_subnet.foo.*.id}" +} + +data "aws_db_subnet_group" "bar" { + name = "${aws_db_subnet_group.bar.name}" +} +`, rInt) +} diff --git a/aws/provider.go b/aws/provider.go index 96834ce7345..95cc65884eb 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -202,6 +202,7 @@ func Provider() terraform.ResourceProvider { "aws_db_event_categories": dataSourceAwsDbEventCategories(), "aws_db_instance": dataSourceAwsDbInstance(), "aws_db_snapshot": dataSourceAwsDbSnapshot(), + "aws_db_subnet_group": dataSourceAwsDbSubnetGroup(), "aws_directory_service_directory": dataSourceAwsDirectoryServiceDirectory(), "aws_dx_gateway": dataSourceAwsDxGateway(), "aws_dynamodb_table": dataSourceAwsDynamoDbTable(), diff --git a/website/aws.erb b/website/aws.erb index 008f986c920..212939c7f8e 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -2619,6 +2619,9 @@
  • aws_db_snapshot
  • +
  • + aws_db_subnet_group +
  • aws_rds_cluster
  • diff --git a/website/docs/d/db_subnet_group.html.markdown b/website/docs/d/db_subnet_group.html.markdown new file mode 100644 index 00000000000..ba71e1daf28 --- /dev/null +++ b/website/docs/d/db_subnet_group.html.markdown @@ -0,0 +1,35 @@ +--- +layout: "aws" +page_title: "AWS: aws_db_subnet_group" +sidebar_current: "docs-aws-datasource-db-subnet-group" +description: |- + Get information on an RDS Database Subnet Group. +--- + +# Data Source: aws_db_subnet_group + +Use this data source to get information about an RDS subnet group + +## Example Usage + +```hcl +data "aws_db_subnet_group" "database" { + name = "my-test-database-subnet-group" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the RDS database subnet group + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `arn` - The Amazon Resource Name (ARN) for the DB subnet group.. +* `description` - Provides the description of the DB subnet group. +* `status` - Provides the status of the DB subnet group. +* `subnets` - Contains a list of subnet identifiers. +* `vpc_id` - Provides the VPC ID of the subnet group. From fd15f383d309f9b2fe04a0013b88b16eae78afb5 Mon Sep 17 00:00:00 2001 From: Angie Pinilla Date: Tue, 30 Jun 2020 00:41:05 -0400 Subject: [PATCH 2/3] CR updates --- aws/data_source_aws_db_subnet_group.go | 40 +++++++------ aws/data_source_aws_db_subnet_group_test.go | 60 +++++++++++++------- website/docs/d/db_subnet_group.html.markdown | 12 ++-- 3 files changed, 64 insertions(+), 48 deletions(-) diff --git a/aws/data_source_aws_db_subnet_group.go b/aws/data_source_aws_db_subnet_group.go index 81ed721baec..9e5a7f10008 100644 --- a/aws/data_source_aws_db_subnet_group.go +++ b/aws/data_source_aws_db_subnet_group.go @@ -6,7 +6,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/rds" - "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) func dataSourceAwsDbSubnetGroup() *schema.Resource { @@ -18,29 +18,23 @@ func dataSourceAwsDbSubnetGroup() *schema.Resource { Type: schema.TypeString, Computed: true, }, - "description": { Type: schema.TypeString, Computed: true, }, - "name": { Type: schema.TypeString, Required: true, - ForceNew: true, }, - "status": { Type: schema.TypeString, Computed: true, }, - - "subnets": { - Type: schema.TypeList, + "subnet_ids": { + Type: schema.TypeSet, Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, }, - "vpc_id": { Type: schema.TypeString, Computed: true, @@ -52,39 +46,43 @@ func dataSourceAwsDbSubnetGroup() *schema.Resource { func dataSourceAwsDbSubnetGroupRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).rdsconn + name := d.Get("name").(string) + opts := &rds.DescribeDBSubnetGroupsInput{ - DBSubnetGroupName: aws.String(d.Get("name").(string)), + DBSubnetGroupName: aws.String(name), } - log.Printf("[DEBUG] Reading DB SubnetGroup: %s", opts) + log.Printf("[DEBUG] Reading DB SubnetGroup: %s", name) resp, err := conn.DescribeDBSubnetGroups(opts) if err != nil { - return err + if isAWSErr(err, rds.ErrCodeDBSubnetGroupNotFoundFault, "") { + return fmt.Errorf("DB SubnetGroup (%s) not found", name) + } + return fmt.Errorf("error reading DB SubnetGroup (%s): %w", name, err) } - if len(resp.DBSubnetGroups) < 1 { - return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.") + if resp == nil || resp.DBSubnetGroups == nil { + return fmt.Errorf("error reading DB SubnetGroup (%s): empty response", name) } if len(resp.DBSubnetGroups) > 1 { return fmt.Errorf("Your query returned more than one result. Please try a more specific search criteria.") } - dbSubnetGroup := *resp.DBSubnetGroups[0] - - d.SetId(d.Get("name").(string)) + dbSubnetGroup := resp.DBSubnetGroups[0] + d.SetId(aws.StringValue(dbSubnetGroup.DBSubnetGroupName)) d.Set("arn", dbSubnetGroup.DBSubnetGroupArn) d.Set("description", dbSubnetGroup.DBSubnetGroupDescription) d.Set("name", dbSubnetGroup.DBSubnetGroupName) d.Set("status", dbSubnetGroup.SubnetGroupStatus) - var subnets []string + subnets := make([]string, 0, len(dbSubnetGroup.Subnets)) for _, v := range dbSubnetGroup.Subnets { - subnets = append(subnets, *v.SubnetIdentifier) + subnets = append(subnets, aws.StringValue(v.SubnetIdentifier)) } - if err := d.Set("subnets", subnets); err != nil { - return fmt.Errorf("Error setting subnets attribute: %#v, error: %#v", subnets, err) + if err := d.Set("subnet_ids", subnets); err != nil { + return fmt.Errorf("error setting subnet_ids: %w", err) } d.Set("vpc_id", dbSubnetGroup.VpcId) diff --git a/aws/data_source_aws_db_subnet_group_test.go b/aws/data_source_aws_db_subnet_group_test.go index 489ef4f02a6..c14ffeff2a7 100644 --- a/aws/data_source_aws_db_subnet_group_test.go +++ b/aws/data_source_aws_db_subnet_group_test.go @@ -2,58 +2,76 @@ package aws import ( "fmt" + "regexp" "testing" - "github.com/hashicorp/terraform/helper/acctest" - "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" ) func TestAccAWSDbSubnetGroupDataSource_basic(t *testing.T) { - rInt := acctest.RandInt() + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_db_subnet_group.test" + dataSourceName := "data.aws_db_subnet_group.test" + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccAWSDBSubnetGroupDataSourceConfig(rInt), + Config: testAccAWSDBSubnetGroupDataSourceConfig_NonExistent, + ExpectError: regexp.MustCompile(`DB SubnetGroup \(tf-acc-test-does-not-exist\) not found$`), + }, + { + Config: testAccAWSDBSubnetGroupDataSourceConfig(rName), Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttrSet("data.aws_db_subnet_group.bar", "arn"), - resource.TestCheckResourceAttrSet("data.aws_db_subnet_group.bar", "description"), - resource.TestCheckResourceAttrSet("data.aws_db_subnet_group.bar", "name"), - resource.TestCheckResourceAttrSet("data.aws_db_subnet_group.bar", "status"), - resource.TestCheckResourceAttrSet("data.aws_db_subnet_group.bar", "subnets.0"), - resource.TestCheckResourceAttrSet("data.aws_db_subnet_group.bar", "subnets.1"), - resource.TestCheckResourceAttrSet("data.aws_db_subnet_group.bar", "vpc_id"), + resource.TestCheckResourceAttrPair(resourceName, "arn", dataSourceName, "arn"), + resource.TestCheckResourceAttrPair(resourceName, "description", dataSourceName, "description"), + resource.TestCheckResourceAttrPair(resourceName, "name", dataSourceName, "name"), + resource.TestCheckResourceAttrPair(resourceName, "subnet_ids", dataSourceName, "subnet_ids"), + resource.TestCheckResourceAttrSet(dataSourceName, "status"), + resource.TestCheckResourceAttrSet(dataSourceName, "vpc_id"), ), }, }, }) } -func testAccAWSDBSubnetGroupDataSourceConfig(rInt int) string { +const testAccAWSDBSubnetGroupDataSourceConfig_NonExistent = ` +data "aws_db_subnet_group" "test" { + name = "tf-acc-test-does-not-exist" +} +` + +func testAccAWSDBSubnetGroupDataSourceConfig(rName string) string { return fmt.Sprintf(` data "aws_availability_zones" "available" { state = "available" + + filter { + name = "opt-in-status" + values = ["opt-in-not-required"] + } } -resource "aws_vpc" "foo" { +resource "aws_vpc" "test" { cidr_block = "10.0.0.0/16" } -resource "aws_subnet" "foo" { +resource "aws_subnet" "test" { count = 2 availability_zone = "${data.aws_availability_zones.available.names[count.index]}" cidr_block = "10.0.${count.index}.0/24" - vpc_id = "${aws_vpc.foo.id}" + vpc_id = "${aws_vpc.test.id}" } -resource "aws_db_subnet_group" "bar" { - name = "datasource-test-terraform-%d" - subnet_ids = "${aws_subnet.foo.*.id}" +resource "aws_db_subnet_group" "test" { + name = "%s" + subnet_ids = "${aws_subnet.test.*.id}" } -data "aws_db_subnet_group" "bar" { - name = "${aws_db_subnet_group.bar.name}" +data "aws_db_subnet_group" "test" { + name = "${aws_db_subnet_group.test.name}" } -`, rInt) +`, rName) } diff --git a/website/docs/d/db_subnet_group.html.markdown b/website/docs/d/db_subnet_group.html.markdown index ba71e1daf28..f3d02b3cdba 100644 --- a/website/docs/d/db_subnet_group.html.markdown +++ b/website/docs/d/db_subnet_group.html.markdown @@ -1,14 +1,14 @@ --- +subcategory: "RDS" layout: "aws" page_title: "AWS: aws_db_subnet_group" -sidebar_current: "docs-aws-datasource-db-subnet-group" description: |- Get information on an RDS Database Subnet Group. --- # Data Source: aws_db_subnet_group -Use this data source to get information about an RDS subnet group +Use this data source to get information about an RDS subnet group. ## Example Usage @@ -22,14 +22,14 @@ data "aws_db_subnet_group" "database" { The following arguments are supported: -* `name` - (Required) The name of the RDS database subnet group +* `name` - (Required) The name of the RDS database subnet group. ## Attributes Reference In addition to all arguments above, the following attributes are exported: -* `arn` - The Amazon Resource Name (ARN) for the DB subnet group.. +* `arn` - The Amazon Resource Name (ARN) for the DB subnet group. * `description` - Provides the description of the DB subnet group. * `status` - Provides the status of the DB subnet group. -* `subnets` - Contains a list of subnet identifiers. -* `vpc_id` - Provides the VPC ID of the subnet group. +* `subnet_ids` - Contains a list of subnet identifiers. +* `vpc_id` - Provides the VPC ID of the subnet group. \ No newline at end of file From 643e35e1acb907d3acd4e2c546a12e6a9344dbe0 Mon Sep 17 00:00:00 2001 From: Angie Pinilla Date: Wed, 26 Aug 2020 12:04:14 -0400 Subject: [PATCH 3/3] remove aws webite ref --- website/aws.erb | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 website/aws.erb diff --git a/website/aws.erb b/website/aws.erb deleted file mode 100644 index e69de29bb2d..00000000000