-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9525 from maxenglander/datasource-aws-db-subnet-g…
…roup data-source/aws_db_subnet_group: create aws_db_subnet_group data-source
- Loading branch information
Showing
4 changed files
with
207 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,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 | ||
} |
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,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)) | ||
} |
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 @@ | ||
--- | ||
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. |