Skip to content

Commit

Permalink
Adds sweeper for aws_fms_admin_account
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavison committed Aug 29, 2024
1 parent 5da7378 commit efcb3d5
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
104 changes: 104 additions & 0 deletions internal/service/fms/sweep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package fms

import (
"context"
"fmt"
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/service/fms"
awstypes "github.com/aws/aws-sdk-go-v2/service/fms/types"
"github.com/hashicorp/aws-sdk-go-base/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/sdk"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

func RegisterSweepers() {
resource.AddTestSweepers("aws_fms_admin_account", &resource.Sweeper{
Name: "aws_fms_admin_account",
F: sweepAdminAccount,
})
}

// TODO: This sweeper has custom skip logic, so can't use a `sweep.SweeperFn`
func sweepAdminAccount(region string) error {
ctx := sweep.Context(region)
client, err := sweep.SharedRegionalSweepClient(ctx, region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}
conn := client.FMSClient(ctx)

var sweepResources []sweep.Sweepable

output, err := conn.GetAdminAccount(ctx, &fms.GetAdminAccountInput{})
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
tflog.Info(ctx, "No resources to sweep")
return nil
}
if awsv2.SkipSweepError(err) || tfawserr.ErrCodeEquals(err, "AccessDeniedException") {
tflog.Warn(ctx, "Skipping sweeper", map[string]any{
"error": err.Error(),
})
return nil
}
if err != nil {
return err
}

if output == nil {
tflog.Warn(ctx, "Skipping sweeper", map[string]any{
"skip_reason": "Empty result",
})
return nil
}

r := resourceAdminAccount()
d := r.Data(nil)
d.Set(names.AttrAccountID, output.AdminAccount)

sweepResources = append(sweepResources, newAdminAccountSweeper(r, d, client))

err = sweep.SweepOrchestrator(ctx, sweepResources)

if err != nil {
return fmt.Errorf("error sweeping FMS Admin Account (%s): %w", region, err)
}

return nil
}

type adminAccountSweeper struct {
d *schema.ResourceData
sweepable sweep.Sweepable
}

func newAdminAccountSweeper(resource *schema.Resource, d *schema.ResourceData, client *conns.AWSClient) *adminAccountSweeper {
return &adminAccountSweeper{
d: d,
sweepable: sdk.NewSweepResource(resource, d, client),
}
}

func (aas adminAccountSweeper) Delete(ctx context.Context, timeout time.Duration, optFns ...tfresource.OptionsFunc) error {
err := aas.sweepable.Delete(ctx, timeout, optFns...)
if err != nil && strings.Contains(err.Error(), "AccessDeniedException") {
tflog.Warn(ctx, "Skipping resource", map[string]any{
"attr.account_id": aas.d.Get(names.AttrAccountID),
"error": err.Error(),
})
return nil
}
return err
}
2 changes: 2 additions & 0 deletions internal/sweep/register_gen_test.go

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

0 comments on commit efcb3d5

Please sign in to comment.