-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
beb8080
commit 73f8be1
Showing
5 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |