Skip to content

[Enhancement] Adding parameters to the datasource aws_db_parameter_group #42765

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .changelog/42765.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
datasource/aws_db_parameter_group: added parameter block
```
79 changes: 79 additions & 0 deletions internal/service/rds/parameter_group_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ package rds

import (
"context"
"log"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/rds"
"github.com/aws/aws-sdk-go-v2/service/rds/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)
Expand All @@ -37,6 +42,29 @@ func dataSourceParameterGroup() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
names.AttrParameter: {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"apply_method": {
Type: schema.TypeString,
Optional: true,
Default: types.ApplyMethodImmediate,
ValidateDiagFunc: enum.ValidateIgnoreCase[types.ApplyMethod](),
},
names.AttrName: {
Type: schema.TypeString,
Required: true,
},
names.AttrValue: {
Type: schema.TypeString,
Required: true,
},
},
},
Set: parameterHash,
},
},
}
}
Expand All @@ -57,5 +85,56 @@ func dataSourceParameterGroupRead(ctx context.Context, d *schema.ResourceData, m
d.Set(names.AttrFamily, output.DBParameterGroupFamily)
d.Set(names.AttrName, output.DBParameterGroupName)

input := rds.DescribeDBParametersInput{
DBParameterGroupName: aws.String(d.Id()),
}

configParams := d.Get(names.AttrParameter).(*schema.Set)
if configParams.Len() < 1 {
input.Source = aws.String(parameterSourceUser)
}

parameters, err := findDBParameters(ctx, conn, &input, tfslices.PredicateTrue[*types.Parameter]())

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading RDS DB Parameter Group (%s) parameters: %s", d.Id(), err)
}

var userParams []types.Parameter
if configParams.Len() < 1 {
userParams = parameters
} else {
for _, parameter := range parameters {
if parameter.Source == nil || parameter.ParameterName == nil {
continue
}

if aws.ToString(parameter.Source) == parameterSourceUser {
userParams = append(userParams, parameter)
continue
}

var paramFound bool
for _, cp := range expandParameters(configParams.List()) {
if cp.ParameterName == nil {
continue
}

if aws.ToString(cp.ParameterName) == aws.ToString(parameter.ParameterName) {
userParams = append(userParams, parameter)
paramFound = true
break
}
}
if !paramFound {
log.Printf("[DEBUG] Not getting %s, as its source is %q and it isn't in the config", aws.ToString(parameter.ParameterName), aws.ToString(parameter.Source))
}
}
}

if err := d.Set(names.AttrParameter, flattenParameters(userParams)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting parameter: %s", err)
}

return diags
}
5 changes: 5 additions & 0 deletions internal/service/rds/parameter_group_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func TestAccRDSParameterGroupDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(datasourceName, names.AttrDescription, resourceName, names.AttrDescription),
resource.TestCheckResourceAttrPair(datasourceName, names.AttrFamily, resourceName, names.AttrFamily),
resource.TestCheckResourceAttrPair(datasourceName, names.AttrName, resourceName, names.AttrName),
resource.TestCheckResourceAttr(resourceName, "parameter.#", "1"),
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "parameter.*", map[string]string{
names.AttrName: "client_encoding",
names.AttrValue: "UTF8",
}),
),
},
},
Expand Down
9 changes: 9 additions & 0 deletions website/docs/d/db_parameter_group.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ This data source exports the following attributes in addition to the arguments a
* `arn` - ARN of the parameter group.
* `family` - Family of the parameter group.
* `description` - Description of the parameter group.
* `parameter` - The DB parameters block. See [`parameter` Block](#parameter-block) below for more details.

### `parameter` Block

The `parameter` blocks support the following arguments:

* `name` - The name of the DB parameter.
* `value` - The value of the DB parameter.
* `apply_method` - "immediate" or "pending-reboot".
Loading