From e69a7f359e311d95cf581d0de06a4cf503676a49 Mon Sep 17 00:00:00 2001 From: drexler Date: Sat, 12 Sep 2020 09:25:00 -0400 Subject: [PATCH] feat: add data source aws_db_parameter_group --- aws/data_source_aws_db_parameter_group.go | 78 +++++++++++++++++++ ...data_source_aws_db_parameter_group_test.go | 62 +++++++++++++++ aws/provider.go | 1 + .../docs/d/db_parameter_group.html.markdown | 34 ++++++++ 4 files changed, 175 insertions(+) create mode 100644 aws/data_source_aws_db_parameter_group.go create mode 100644 aws/data_source_aws_db_parameter_group_test.go create mode 100644 website/docs/d/db_parameter_group.html.markdown diff --git a/aws/data_source_aws_db_parameter_group.go b/aws/data_source_aws_db_parameter_group.go new file mode 100644 index 000000000000..25adb092f693 --- /dev/null +++ b/aws/data_source_aws_db_parameter_group.go @@ -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 +} diff --git a/aws/data_source_aws_db_parameter_group_test.go b/aws/data_source_aws_db_parameter_group_test.go new file mode 100644 index 000000000000..333ed0522fba --- /dev/null +++ b/aws/data_source_aws_db_parameter_group_test.go @@ -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) +} diff --git a/aws/provider.go b/aws/provider.go index 0311d1c66f7b..98cfb82becdf 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -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(), diff --git a/website/docs/d/db_parameter_group.html.markdown b/website/docs/d/db_parameter_group.html.markdown new file mode 100644 index 000000000000..96cc26362229 --- /dev/null +++ b/website/docs/d/db_parameter_group.html.markdown @@ -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. \ No newline at end of file