diff --git a/internal/service/xray/sweep.go b/internal/service/xray/sweep.go new file mode 100644 index 00000000000..96d49882911 --- /dev/null +++ b/internal/service/xray/sweep.go @@ -0,0 +1,57 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package xray + +import ( + "context" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/xray" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/internal/sweep" + "github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2" +) + +func RegisterSweepers() { + sweep.Register("aws_xray_group", sweepGroups) +} + +func sweepGroups(ctx context.Context, client *conns.AWSClient) ([]sweep.Sweepable, error) { + conn := client.XRayClient(ctx) + + var sweepResources []sweep.Sweepable + r := resourceGroup() + + pages := xray.NewGetGroupsPaginator(conn, &xray.GetGroupsInput{}) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if awsv2.SkipSweepError(err) { + tflog.Warn(ctx, "Skipping sweeper", map[string]any{ + "error": err.Error(), + }) + return nil, nil + } + if err != nil { + return nil, err + } + + for _, v := range page.Groups { + if aws.ToString(v.GroupName) == "Default" { + tflog.Debug(ctx, "Skipping resource", map[string]any{ + "skip_reason": `Cannot delete "Default"`, + "arn": aws.ToString(v.GroupARN), + }) + continue + } + d := r.Data(nil) + d.SetId(aws.ToString(v.GroupARN)) + + sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client)) + } + } + + return sweepResources, nil +} diff --git a/internal/sweep/register_gen_test.go b/internal/sweep/register_gen_test.go index 1c6785c779d..d0179c8a068 100644 --- a/internal/sweep/register_gen_test.go +++ b/internal/sweep/register_gen_test.go @@ -154,6 +154,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/service/wafregional" "github.com/hashicorp/terraform-provider-aws/internal/service/wafv2" "github.com/hashicorp/terraform-provider-aws/internal/service/workspaces" + "github.com/hashicorp/terraform-provider-aws/internal/service/xray" ) func registerSweepers() { @@ -308,4 +309,5 @@ func registerSweepers() { wafregional.RegisterSweepers() wafv2.RegisterSweepers() workspaces.RegisterSweepers() + xray.RegisterSweepers() }