Skip to content

Commit

Permalink
data-source/aws_db_subnet_group: create aws_db_subnet_group data-source
Browse files Browse the repository at this point in the history
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
  • Loading branch information
maxenglander committed Jul 26, 2019
1 parent beb8080 commit 73f8be1
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 0 deletions.
93 changes: 93 additions & 0 deletions aws/data_source_aws_db_subnet_group.go
Original file line number Diff line number Diff line change
@@ -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
}
59 changes: 59 additions & 0 deletions aws/data_source_aws_db_subnet_group_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2258,6 +2258,9 @@
<li>
<a href="/docs/providers/aws/d/db_snapshot.html">aws_db_snapshot</a>
</li>
<li>
<a href="/docs/providers/aws/d/db_subnet_group.html">aws_db_subnet_group</a>
</li>
<li>
<a href="/docs/providers/aws/d/rds_cluster.html">aws_rds_cluster</a>
</li>
Expand Down
35 changes: 35 additions & 0 deletions website/docs/d/db_subnet_group.html.markdown
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 73f8be1

Please sign in to comment.