From 73f8be16be40563eef661f58cd0c540b35bb9b68 Mon Sep 17 00:00:00 2001 From: Max Englander Date: Fri, 26 Jul 2019 11:19:02 -0400 Subject: [PATCH] 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 802c49b1dc5..d274e541e52 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -171,6 +171,7 @@ func Provider() terraform.ResourceProvider { "aws_db_event_categories": dataSourceAwsDbEventCategories(), "aws_db_instance": dataSourceAwsDbInstance(), "aws_db_snapshot": dataSourceAwsDbSnapshot(), + "aws_db_subnet_group": dataSourceAwsDbSubnetGroup(), "aws_dx_gateway": dataSourceAwsDxGateway(), "aws_dynamodb_table": dataSourceAwsDynamoDbTable(), "aws_ebs_default_kms_key": dataSourceAwsEbsDefaultKmsKey(), diff --git a/website/aws.erb b/website/aws.erb index eae02116af9..f91f3750bbf 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -2258,6 +2258,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.