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..aeee80d5df4 --- /dev/null +++ b/aws/data_source_aws_db_subnet_group.go @@ -0,0 +1,88 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/rds" + "github.com/hashicorp/terraform-plugin-sdk/v2/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, + }, + "status": { + Type: schema.TypeString, + Computed: true, + }, + "subnet_ids": { + Type: schema.TypeSet, + 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 + + name := d.Get("name").(string) + + opts := &rds.DescribeDBSubnetGroupsInput{ + DBSubnetGroupName: aws.String(name), + } + + log.Printf("[DEBUG] Reading DB SubnetGroup: %s", name) + + resp, err := conn.DescribeDBSubnetGroups(opts) + if err != nil { + return fmt.Errorf("error reading DB SubnetGroup (%s): %w", name, err) + } + + 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(aws.StringValue(dbSubnetGroup.DBSubnetGroupName)) + d.Set("arn", dbSubnetGroup.DBSubnetGroupArn) + d.Set("description", dbSubnetGroup.DBSubnetGroupDescription) + d.Set("name", dbSubnetGroup.DBSubnetGroupName) + d.Set("status", dbSubnetGroup.SubnetGroupStatus) + + subnets := make([]string, 0, len(dbSubnetGroup.Subnets)) + for _, v := range dbSubnetGroup.Subnets { + subnets = append(subnets, aws.StringValue(v.SubnetIdentifier)) + } + if err := d.Set("subnet_ids", subnets); err != nil { + return fmt.Errorf("error setting subnet_ids: %w", 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..00a59f06ef4 --- /dev/null +++ b/aws/data_source_aws_db_subnet_group_test.go @@ -0,0 +1,83 @@ +package aws + +import ( + "fmt" + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func TestAccAWSDbSubnetGroupDataSource_basic(t *testing.T) { + var providers []*schema.Provider + 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) }, + ProviderFactories: testAccProviderFactories(&providers), + Steps: []resource.TestStep{ + { + Config: testAccAWSDBSubnetGroupDataSourceConfig(rName), + Check: resource.ComposeAggregateTestCheckFunc( + 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 TestAccAWSDbSubnetGroupDataSource_nonexistent(t *testing.T) { + var providers []*schema.Provider + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + ProviderFactories: testAccProviderFactories(&providers), + Steps: []resource.TestStep{ + { + Config: testAccAWSDBSubnetGroupDataSourceConfig_NonExistent, + ExpectError: regexp.MustCompile(`error reading DB SubnetGroup \(tf-acc-test-does-not-exist\)`), + }, + }, + }) +} + +const testAccAWSDBSubnetGroupDataSourceConfig_NonExistent = ` +data "aws_db_subnet_group" "test" { + name = "tf-acc-test-does-not-exist" +} +` + +func testAccAWSDBSubnetGroupDataSourceConfig(rName string) string { + return composeConfig( + testAccAvailableAZsNoOptInConfig(), + fmt.Sprintf(` +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" +} + +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.test.id +} + +resource "aws_db_subnet_group" "test" { + name = "%s" + subnet_ids = aws_subnet.test.*.id +} + +data "aws_db_subnet_group" "test" { + name = aws_db_subnet_group.test.name +} +`, rName)) +} diff --git a/aws/provider.go b/aws/provider.go index 550b235a5dd..038ca380c38 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -200,6 +200,7 @@ func Provider() *schema.Provider { "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/docs/d/db_subnet_group.html.markdown b/website/docs/d/db_subnet_group.html.markdown new file mode 100644 index 00000000000..f3d02b3cdba --- /dev/null +++ b/website/docs/d/db_subnet_group.html.markdown @@ -0,0 +1,35 @@ +--- +subcategory: "RDS" +layout: "aws" +page_title: "AWS: aws_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. +* `subnet_ids` - Contains a list of subnet identifiers. +* `vpc_id` - Provides the VPC ID of the subnet group. \ No newline at end of file