Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data-source/aws_db_subnet_group: create aws_db_subnet_group data-source #9525

Merged
merged 4 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions aws/data_source_aws_db_subnet_group.go
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
}
83 changes: 83 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,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(
anGie44 marked this conversation as resolved.
Show resolved Hide resolved
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))
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
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 @@
---
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.