Skip to content

Commit

Permalink
Adds listPages() function for aws_rds_cluster_parameter_group
Browse files Browse the repository at this point in the history
Needed to add a paginator field name to the generator function, since not all services use `NextToken`. Removed the waiter for `aws_rds_cluster_parameter_group` deletion, since it always exited on the first call.
  • Loading branch information
gdavison committed Jun 8, 2020
1 parent 5101128 commit a4da051
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 71 deletions.
17 changes: 11 additions & 6 deletions aws/internal/generators/listpages/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (

var (
functionNames = flag.String("function", "", "comma-separated list of API List functions; required")
paginatorName = flag.String("paginator", "NextToken", "name of the pagination token field")
packageName = flag.String("package", "", "override package name for generated code")
)

Expand Down Expand Up @@ -52,7 +53,8 @@ func main() {
sort.Strings(functions)

g := Generator{
tmpl: template.Must(template.New("function").Parse(functionTemplate)),
paginator: *paginatorName,
tmpl: template.Must(template.New("function").Parse(functionTemplate)),
}
g.parsePackage(sourcePackage)

Expand Down Expand Up @@ -81,9 +83,10 @@ type HeaderInfo struct {
}

type Generator struct {
buf bytes.Buffer
pkg *Package
tmpl *template.Template
buf bytes.Buffer
pkg *Package
tmpl *template.Template
paginator string
}

func (g *Generator) Printf(format string, args ...interface{}) {
Expand Down Expand Up @@ -139,6 +142,7 @@ type FuncSpec struct {
RecvType string
ParamType string
ResultType string
Paginator string
}

func (g *Generator) generateFunction(functionName string) {
Expand Down Expand Up @@ -170,6 +174,7 @@ func (g *Generator) generateFunction(functionName string) {
RecvType: g.expandTypeField(function.Recv),
ParamType: g.expandTypeField(function.Type.Params), // Assumes there is a single input parameter
ResultType: g.expandTypeField(function.Type.Results), // Assumes we can take the first return parameter
Paginator: g.paginator,
}

err := g.tmpl.Execute(&g.buf, funcSpec)
Expand Down Expand Up @@ -216,12 +221,12 @@ func {{ .Name }}Pages(conn {{ .RecvType }}, input {{ .ParamType }}, fn func({{ .
return err
}
lastPage := aws.StringValue(output.NextToken) == ""
lastPage := aws.StringValue(output.{{ .Paginator }}) == ""
if !fn(output, lastPage) || lastPage {
break
}
input.NextToken = output.NextToken
input.{{ .Paginator }} = output.{{ .Paginator }}
}
return nil
}
Expand Down
13 changes: 13 additions & 0 deletions aws/internal/service/rds/lister/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:generate go run ../../../generators/listpages/main.go -function=DescribeDBClusterParameterGroups -paginator Marker github.com/aws/aws-sdk-go/service/rds

package lister

import (
"github.com/aws/aws-sdk-go/service/rds"
)

func ListAllClusterParameterGroups(conn *rds.RDS, fn func(*rds.DescribeDBClusterParameterGroupsOutput, bool) bool) error {
input := &rds.DescribeDBClusterParameterGroupsInput{}

return DescribeDBClusterParameterGroupsPages(conn, input, fn)
}
25 changes: 25 additions & 0 deletions aws/internal/service/rds/lister/list_pages_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 11 additions & 30 deletions aws/resource_aws_rds_cluster_parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,39 +276,20 @@ func resourceAwsRDSClusterParameterGroupUpdate(d *schema.ResourceData, meta inte
}

func resourceAwsRDSClusterParameterGroupDelete(d *schema.ResourceData, meta interface{}) error {
stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: []string{"destroyed"},
Refresh: resourceAwsRDSClusterParameterGroupDeleteRefreshFunc(d, meta),
Timeout: 3 * time.Minute,
MinTimeout: 1 * time.Second,
}
_, err := stateConf.WaitForState()
return err
}

func resourceAwsRDSClusterParameterGroupDeleteRefreshFunc(
d *schema.ResourceData,
meta interface{}) resource.StateRefreshFunc {
rdsconn := meta.(*AWSClient).rdsconn
return deleteRDSClusterParameterGroup(rdsconn, d.Id())
}

return func() (interface{}, string, error) {

deleteOpts := rds.DeleteDBClusterParameterGroupInput{
DBClusterParameterGroupName: aws.String(d.Id()),
}

if _, err := rdsconn.DeleteDBClusterParameterGroup(&deleteOpts); err != nil {
rdserr, ok := err.(awserr.Error)
if !ok {
return d, "error", err
}
func deleteRDSClusterParameterGroup(conn *rds.RDS, id string) error {
input := rds.DeleteDBClusterParameterGroupInput{
DBClusterParameterGroupName: aws.String(id),
}

if rdserr.Code() != "DBParameterGroupNotFound" {
return d, "error", err
}
if _, err := conn.DeleteDBClusterParameterGroup(&input); err != nil {
if isAWSErr(err, rds.ErrCodeDBParameterGroupNotFoundFault, "") {
return nil
}

return d, "destroyed", nil
return err
}
return nil
}
56 changes: 21 additions & 35 deletions aws/resource_aws_rds_cluster_parameter_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/rds/lister"
)

func init() {
Expand All @@ -34,53 +36,43 @@ func testSweepRdsClusterParameterGroups(region string) error {
}
conn := client.(*AWSClient).rdsconn

input := &rds.DescribeDBClusterParameterGroupsInput{}
var sweeperErrs *multierror.Error

for {
output, err := conn.DescribeDBClusterParameterGroups(input)

if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping RDS DB Cluster Parameter Group sweep for %s: %s", region, err)
return nil
}

if err != nil {
return fmt.Errorf("error retrieving DB Cluster Parameter Groups: %s", err)
err = lister.ListAllClusterParameterGroups(conn, func(page *rds.DescribeDBClusterParameterGroupsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, dbcpg := range output.DBClusterParameterGroups {
if dbcpg == nil {
continue
}

input := &rds.DeleteDBClusterParameterGroupInput{
DBClusterParameterGroupName: dbcpg.DBClusterParameterGroupName,
}
for _, dbcpg := range page.DBClusterParameterGroups {
name := aws.StringValue(dbcpg.DBClusterParameterGroupName)

if strings.HasPrefix(name, "default.") {
log.Printf("[INFO] Skipping DB Cluster Parameter Group: %s", name)
log.Printf("[INFO] Skipping DB Cluster Parameter Group %s", name)
continue
}

log.Printf("[INFO] Deleting DB Cluster Parameter Group: %s", name)

_, err := conn.DeleteDBClusterParameterGroup(input)

err := deleteRDSClusterParameterGroup(conn, aws.StringValue(dbcpg.DBClusterParameterGroupName))
if err != nil {
log.Printf("[ERROR] Failed to delete DB Cluster Parameter Group %s: %s", name, err)
sweeperErrs = multierror.Append(sweeperErrs, err)
continue
}
}

if aws.StringValue(output.Marker) == "" {
break
}
return !lastPage
})

input.Marker = output.Marker
if testSweepSkipSweepError(err) {
log.Printf("[WARN] Skipping DB Cluster Parameter Group sweep for %q: %s", region, err)
return nil
}

return nil
if err != nil {
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error retrieving DB Cluster Parameter Groups: %w", err))
}

return sweeperErrs.ErrorOrNil()
}

func TestAccAWSDBClusterParameterGroup_basic(t *testing.T) {
Expand Down Expand Up @@ -366,13 +358,7 @@ func testAccCheckAWSDBClusterParameterGroupDestroy(s *terraform.State) error {
return errors.New("DB Cluster Parameter Group still exists")
}
}

// Verify the error
newerr, ok := err.(awserr.Error)
if !ok {
return err
}
if newerr.Code() != "DBParameterGroupNotFound" {
if !isAWSErr(err, rds.ErrCodeDBParameterGroupNotFoundFault, "") {
return err
}
}
Expand Down

0 comments on commit a4da051

Please sign in to comment.