Skip to content

Commit

Permalink
CR updates
Browse files Browse the repository at this point in the history
  • Loading branch information
anGie44 committed Jun 30, 2020
1 parent bf20428 commit fd15f38
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 48 deletions.
40 changes: 19 additions & 21 deletions aws/data_source_aws_db_subnet_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataSourceAwsDbSubnetGroup() *schema.Resource {
Expand All @@ -18,29 +18,23 @@ func dataSourceAwsDbSubnetGroup() *schema.Resource {
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,
"subnet_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"vpc_id": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -52,39 +46,43 @@ func dataSourceAwsDbSubnetGroup() *schema.Resource {
func dataSourceAwsDbSubnetGroupRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).rdsconn

name := d.Get("name").(string)

opts := &rds.DescribeDBSubnetGroupsInput{
DBSubnetGroupName: aws.String(d.Get("name").(string)),
DBSubnetGroupName: aws.String(name),
}

log.Printf("[DEBUG] Reading DB SubnetGroup: %s", opts)
log.Printf("[DEBUG] Reading DB SubnetGroup: %s", name)

resp, err := conn.DescribeDBSubnetGroups(opts)
if err != nil {
return err
if isAWSErr(err, rds.ErrCodeDBSubnetGroupNotFoundFault, "") {
return fmt.Errorf("DB SubnetGroup (%s) not found", name)
}
return fmt.Errorf("error reading DB SubnetGroup (%s): %w", name, err)
}

if len(resp.DBSubnetGroups) < 1 {
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
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(d.Get("name").(string))
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)

var subnets []string
subnets := make([]string, 0, len(dbSubnetGroup.Subnets))
for _, v := range dbSubnetGroup.Subnets {
subnets = append(subnets, *v.SubnetIdentifier)
subnets = append(subnets, aws.StringValue(v.SubnetIdentifier))
}
if err := d.Set("subnets", subnets); err != nil {
return fmt.Errorf("Error setting subnets attribute: %#v, error: %#v", subnets, err)
if err := d.Set("subnet_ids", subnets); err != nil {
return fmt.Errorf("error setting subnet_ids: %w", err)
}

d.Set("vpc_id", dbSubnetGroup.VpcId)
Expand Down
60 changes: 39 additions & 21 deletions aws/data_source_aws_db_subnet_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,76 @@ package aws

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccAWSDbSubnetGroupDataSource_basic(t *testing.T) {
rInt := acctest.RandInt()
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) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAWSDBSubnetGroupDataSourceConfig(rInt),
Config: testAccAWSDBSubnetGroupDataSourceConfig_NonExistent,
ExpectError: regexp.MustCompile(`DB SubnetGroup \(tf-acc-test-does-not-exist\) not found$`),
},
{
Config: testAccAWSDBSubnetGroupDataSourceConfig(rName),
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"),
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 testAccAWSDBSubnetGroupDataSourceConfig(rInt int) string {
const testAccAWSDBSubnetGroupDataSourceConfig_NonExistent = `
data "aws_db_subnet_group" "test" {
name = "tf-acc-test-does-not-exist"
}
`

func testAccAWSDBSubnetGroupDataSourceConfig(rName string) string {
return fmt.Sprintf(`
data "aws_availability_zones" "available" {
state = "available"
filter {
name = "opt-in-status"
values = ["opt-in-not-required"]
}
}
resource "aws_vpc" "foo" {
resource "aws_vpc" "test" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "foo" {
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.foo.id}"
vpc_id = "${aws_vpc.test.id}"
}
resource "aws_db_subnet_group" "bar" {
name = "datasource-test-terraform-%d"
subnet_ids = "${aws_subnet.foo.*.id}"
resource "aws_db_subnet_group" "test" {
name = "%s"
subnet_ids = "${aws_subnet.test.*.id}"
}
data "aws_db_subnet_group" "bar" {
name = "${aws_db_subnet_group.bar.name}"
data "aws_db_subnet_group" "test" {
name = "${aws_db_subnet_group.test.name}"
}
`, rInt)
`, rName)
}
12 changes: 6 additions & 6 deletions website/docs/d/db_subnet_group.html.markdown
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
subcategory: "RDS"
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
Use this data source to get information about an RDS subnet group.

## Example Usage

Expand All @@ -22,14 +22,14 @@ data "aws_db_subnet_group" "database" {

The following arguments are supported:

* `name` - (Required) The name of the RDS database subnet group
* `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..
* `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.
* `subnet_ids` - Contains a list of subnet identifiers.
* `vpc_id` - Provides the VPC ID of the subnet group.

0 comments on commit fd15f38

Please sign in to comment.