Skip to content

Commit

Permalink
feat: add data source aws_db_parameter_group
Browse files Browse the repository at this point in the history
  • Loading branch information
drexler committed Sep 12, 2020
1 parent 4e7e9fb commit e69a7f3
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 0 deletions.
78 changes: 78 additions & 0 deletions aws/data_source_aws_db_parameter_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package aws

import (
"fmt"

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

func dataSourceAwsDbParameterGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsDbParameterGroupRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"arn": {
Type: schema.TypeString,
Computed: true,
},

"family": {
Type: schema.TypeString,
Computed: true,
},

"description": {
Type: schema.TypeString,
Computed: true,
},

"tags": tagsSchemaComputed(),
},
}
}

func dataSourceAwsDbParameterGroupRead(d *schema.ResourceData, meta interface{}) error {
rdsconn := meta.(*AWSClient).rdsconn
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

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

describeOpts := rds.DescribeDBParameterGroupsInput{
DBParameterGroupName: aws.String(groupName),
}

describeResp, err := rdsconn.DescribeDBParameterGroups(&describeOpts)
if err != nil {
return fmt.Errorf("Error getting DB Parameter Groups: %v", err)
}

if len(describeResp.DBParameterGroups) != 1 ||
*describeResp.DBParameterGroups[0].DBParameterGroupName != groupName {
return fmt.Errorf("Unable to find Parameter Group: %#v", describeResp.DBParameterGroups)
}

d.SetId(aws.StringValue(describeResp.DBParameterGroups[0].DBParameterGroupName))
d.Set("name", describeResp.DBParameterGroups[0].DBParameterGroupName)
d.Set("arn", describeResp.DBParameterGroups[0].DBParameterGroupArn)
d.Set("family", describeResp.DBParameterGroups[0].DBParameterGroupFamily)
d.Set("description", describeResp.DBParameterGroups[0].Description)

tags, err := keyvaluetags.RdsListTags(rdsconn, d.Get("arn").(string))

if err != nil {
return fmt.Errorf("error listing tags for RDS DB Parameter Group (%s): %s", d.Get("arn").(string), err)
}

if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

return nil
}
62 changes: 62 additions & 0 deletions aws/data_source_aws_db_parameter_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package aws

import (
"fmt"
"regexp"
"testing"

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

func TestAccAWSDbParameterGroupDataSource_basic(t *testing.T) {
datasourceName := "data.aws_db_parameter_group.test"
resourceName := "aws_db_parameter_group.test"
rInt := acctest.RandInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccAwsDbParameterGroupDataSourceConfig_nonExistent,
ExpectError: regexp.MustCompile(`Error getting DB Parameter Groups`),
},
{
Config: testAccAwsDbParameterGroupDataSourceConfig_basic(rInt),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(datasourceName, "name", resourceName, "name"),
resource.TestCheckResourceAttrPair(datasourceName, "arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceName, "family", resourceName, "family"),
resource.TestCheckResourceAttrPair(datasourceName, "description", resourceName, "description"),
resource.TestCheckResourceAttrPair(datasourceName, "tags.%", resourceName, "tags.%"),
),
},
},
})
}

const testAccAwsDbParameterGroupDataSourceConfig_nonExistent = `
data "aws_db_parameter_group" "test" {
name = "tf-acc-test-does-not-exist"
}
`

func testAccAwsDbParameterGroupDataSourceConfig_basic(rInt int) string {
return fmt.Sprintf(`
resource "aws_db_parameter_group" "test" {
name = "tf-acc-test-%d"
family = "postgres12"
parameter {
name = "client_encoding"
value = "UTF8"
apply_method = "pending-reboot"
}
}
data "aws_db_parameter_group" "test" {
name = aws_db_parameter_group.test.name
}
`, rInt)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func Provider() *schema.Provider {
"aws_db_cluster_snapshot": dataSourceAwsDbClusterSnapshot(),
"aws_db_event_categories": dataSourceAwsDbEventCategories(),
"aws_db_instance": dataSourceAwsDbInstance(),
"aws_db_parameter_group": dataSourceAwsDbParameterGroup(),
"aws_db_snapshot": dataSourceAwsDbSnapshot(),
"aws_db_subnet_group": dataSourceAwsDbSubnetGroup(),
"aws_directory_service_directory": dataSourceAwsDirectoryServiceDirectory(),
Expand Down
34 changes: 34 additions & 0 deletions website/docs/d/db_parameter_group.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
subcategory: "RDS"
layout: "aws"
page_title: "AWS: aws_db_parameter_group"
description: |-
Provides details about an RDS Database Parameter Group.
---

# Data Source: aws_backup_vault

Use this data source to get information on an existing database parameter group.

## Example Usage

```hcl
data "aws_db_parameter_group" "example" {
name = "name-of-parameter"
}
```

## Argument Reference

The following arguments are supported:

* `name` - (Required) The name of the database parameter group.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `arn` - The ARN of the DB parameter group.
* `family` - The name of the DB parameter group family that this DB parameter is compatible with.
* `description` - The customer specified description for the DB parameter group.
* `tags` - Metadata that you can assign to help organize the resources that you create.

0 comments on commit e69a7f3

Please sign in to comment.