Skip to content

Commit

Permalink
Adds functions to generate the DeleteInput structures from resource d…
Browse files Browse the repository at this point in the history
…ata and API resource
  • Loading branch information
gdavison committed Jun 8, 2020
1 parent a4da051 commit f63f342
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 25 deletions.
24 changes: 19 additions & 5 deletions aws/resource_aws_cloudformation_stack_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,18 +251,32 @@ func resourceAwsCloudFormationStackSetDelete(d *schema.ResourceData, meta interf
conn := meta.(*AWSClient).cfconn

log.Printf("[DEBUG] Deleting CloudFormation StackSet: %s", d.Id())
err := deleteCloudFormationStackSet(conn, d.Id())
input, err := deleteCloudFormationStackSetInputFromResourceData(d)
if err != nil {
return err
}
err = deleteCloudFormationStackSet(conn, input)
if err != nil {
return fmt.Errorf("error deleting CloudFormation StackSet (%s): %s", d.Id(), err)
}

return nil
}

func deleteCloudFormationStackSet(conn *cloudformation.CloudFormation, name string) error {
_, err := conn.DeleteStackSet(&cloudformation.DeleteStackSetInput{
StackSetName: aws.String(name),
})
func deleteCloudFormationStackSetInputFromResourceData(d *schema.ResourceData) (*cloudformation.DeleteStackSetInput, error) {
return &cloudformation.DeleteStackSetInput{
StackSetName: aws.String(d.Id()),
}, nil
}

func deleteCloudFormationStackSetInputFromAPIResource(r *cloudformation.StackSetSummary) *cloudformation.DeleteStackSetInput {
return &cloudformation.DeleteStackSetInput{
StackSetName: r.StackSetName,
}
}

func deleteCloudFormationStackSet(conn *cloudformation.CloudFormation, input *cloudformation.DeleteStackSetInput) error {
_, err := conn.DeleteStackSet(input)
if isAWSErr(err, cloudformation.ErrCodeStackSetNotFoundException, "") {
return nil
}
Expand Down
35 changes: 28 additions & 7 deletions aws/resource_aws_cloudformation_stack_set_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,43 @@ func resourceAwsCloudFormationStackSetInstanceDelete(d *schema.ResourceData, met
conn := meta.(*AWSClient).cfconn

log.Printf("[DEBUG] Deleting CloudFormation StackSet Instance: %s", d.Id())
return deleteCloudFormationStackSetInstance(conn, d.Id(), d.Get("retain_stack").(bool), d.Timeout(schema.TimeoutDelete))
input, err := deleteCloudFormationStackSetInstanceInputFromResourceData(d)
if err != nil {
return err
}
return deleteCloudFormationStackSetInstance(conn, input, d.Timeout(schema.TimeoutDelete))
}

func deleteCloudFormationStackSetInstance(conn *cloudformation.CloudFormation, id string, retainStack bool, timeout time.Duration) error {
stackSetName, accountID, region, err := resourceAwsCloudFormationStackSetInstanceParseId(id)
func deleteCloudFormationStackSetInstanceInputFromResourceData(d *schema.ResourceData) (*cloudformation.DeleteStackInstancesInput, error) {
stackSetName, accountID, region, err := resourceAwsCloudFormationStackSetInstanceParseId(d.Id())
if err != nil {
return err
return nil, err
}

input := &cloudformation.DeleteStackInstancesInput{
Accounts: aws.StringSlice([]string{accountID}),
return &cloudformation.DeleteStackInstancesInput{
OperationId: aws.String(resource.UniqueId()),
Accounts: aws.StringSlice([]string{accountID}),
Regions: aws.StringSlice([]string{region}),
RetainStacks: aws.Bool(retainStack),
StackSetName: aws.String(stackSetName),
RetainStacks: aws.Bool(d.Get("retain_stack").(bool)),
}, nil
}

func deleteCloudFormationStackSetInstanceInputFromAPIResource(p *cloudformation.StackSetSummary, r *cloudformation.StackInstanceSummary) *cloudformation.DeleteStackInstancesInput {
return &cloudformation.DeleteStackInstancesInput{
OperationId: aws.String(resource.UniqueId()),
Accounts: []*string{r.Account},
Regions: []*string{r.Region},
StackSetName: p.StackSetName,
RetainStacks: aws.Bool(false),
}
}

func deleteCloudFormationStackSetInstance(conn *cloudformation.CloudFormation, input *cloudformation.DeleteStackInstancesInput, timeout time.Duration) error {
stackSetName := aws.StringValue(input.StackSetName)
accountID := aws.StringValue(input.Accounts[0])
region := aws.StringValue(input.Regions[0])
id := resourceAwsCloudFormationStackSetInstanceCreateId(stackSetName, accountID, region)

output, err := conn.DeleteStackInstances(input)
if isAWSErr(err, cloudformation.ErrCodeStackInstanceNotFoundException, "") {
Expand Down
2 changes: 1 addition & 1 deletion aws/resource_aws_cloudformation_stack_set_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func testSweepCloudformationStackSetInstances(region string) error {
id := resourceAwsCloudFormationStackSetInstanceCreateId(stackSetName, accountID, region)

log.Printf("[INFO] Deleting CloudFormation StackSet Instance: %s", id)
err := deleteCloudFormationStackSetInstance(conn, id, false, StackSetInstanceDeletedTimeout)
err := deleteCloudFormationStackSetInstance(conn, deleteCloudFormationStackSetInstanceInputFromAPIResource(set, instance), StackSetInstanceDeletedTimeout)

if err != nil {
log.Printf("[ERROR] %s", err)
Expand Down
2 changes: 1 addition & 1 deletion aws/resource_aws_cloudformation_stack_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func testSweepCloudformationStackSets(region string) error {
name := aws.StringValue(stackSet.StackSetName)

log.Printf("[INFO] Deleting CloudFormation StackSet: %s", name)
err := deleteCloudFormationStackSet(conn, name)
err := deleteCloudFormationStackSet(conn, deleteCloudFormationStackSetInputFromAPIResource(stackSet))
if err != nil {
sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error deleting CloudFormation StackSet (%s): %w", name, err))
continue
Expand Down
31 changes: 21 additions & 10 deletions aws/resource_aws_rds_cluster_parameter_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,30 @@ func resourceAwsRDSClusterParameterGroupUpdate(d *schema.ResourceData, meta inte

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

input, err := deleteRDSClusterParameterGroupInputFromResourceData(d)
if err != nil {
return err
}
return deleteRDSClusterParameterGroup(rdsconn, input)
}

func deleteRDSClusterParameterGroupInputFromResourceData(d *schema.ResourceData) (*rds.DeleteDBClusterParameterGroupInput, error) {
return &rds.DeleteDBClusterParameterGroupInput{
DBClusterParameterGroupName: aws.String(d.Id()),
}, nil
}

func deleteRDSClusterParameterGroup(conn *rds.RDS, id string) error {
input := rds.DeleteDBClusterParameterGroupInput{
DBClusterParameterGroupName: aws.String(id),
func deleteRDSClusterParameterGroupInputFromAPIResource(r *rds.DBClusterParameterGroup) *rds.DeleteDBClusterParameterGroupInput {
return &rds.DeleteDBClusterParameterGroupInput{
DBClusterParameterGroupName: r.DBClusterParameterGroupName,
}
}

if _, err := conn.DeleteDBClusterParameterGroup(&input); err != nil {
if isAWSErr(err, rds.ErrCodeDBParameterGroupNotFoundFault, "") {
return nil
}
return err
func deleteRDSClusterParameterGroup(conn *rds.RDS, input *rds.DeleteDBClusterParameterGroupInput) error {
_, err := conn.DeleteDBClusterParameterGroup(input)
if isAWSErr(err, rds.ErrCodeDBParameterGroupNotFoundFault, "") {
return nil
}
return nil
return err
}
2 changes: 1 addition & 1 deletion aws/resource_aws_rds_cluster_parameter_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func testSweepRdsClusterParameterGroups(region string) error {

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

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

0 comments on commit f63f342

Please sign in to comment.