Skip to content

Commit

Permalink
Merge pull request hashicorp#40108 from hashicorp/td-sweep-appregistry
Browse files Browse the repository at this point in the history
Adds `aws_servicecatalogappregistry_application` sweeper
  • Loading branch information
gdavison authored Nov 14, 2024
2 parents f412f37 + 0988908 commit 59d791a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 31 deletions.
62 changes: 31 additions & 31 deletions internal/service/resourcegroups/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,60 @@
package resourcegroups

import (
"fmt"
"log"
"context"
"slices"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/resourcegroups"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"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"
"github.com/hashicorp/terraform-provider-aws/names"
)

func RegisterSweepers() {
resource.AddTestSweepers("aws_resourcegroups_group", &resource.Sweeper{
Name: "aws_resourcegroups_group",
F: sweepGroups,
})
awsv2.Register("aws_resourcegroups_group", sweepGroups,
"aws_servicecatalogappregistry_application",
)
}

func sweepGroups(region string) error {
ctx := sweep.Context(region)
client, err := sweep.SharedRegionalSweepClient(ctx, region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}
func sweepGroups(ctx context.Context, client *conns.AWSClient) ([]sweep.Sweepable, error) {
conn := client.ResourceGroupsClient(ctx)
input := &resourcegroups.ListGroupsInput{}
sweepResources := make([]sweep.Sweepable, 0)

pages := resourcegroups.NewListGroupsPaginator(conn, input)
var sweepResources []sweep.Sweepable

r := resourceGroup()
pages := resourcegroups.NewListGroupsPaginator(conn, &resourcegroups.ListGroupsInput{})
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if awsv2.SkipSweepError(err) {
log.Printf("[WARN] Skipping Resource Groups Group sweep for %s: %s", region, err)
return nil
}

if err != nil {
return fmt.Errorf("error listing Resource Groups Groups (%s): %w", region, err)
return nil, err
}

for _, v := range page.GroupIdentifiers {
r := resourceGroup()
tags, err := listTags(ctx, conn, aws.ToString(v.GroupArn))
if err != nil {
tflog.Warn(ctx, "Skipping resource", map[string]any{
"error": err.Error(),
})
continue
}

if slices.Contains(tags.Keys(), "aws:servicecatalog:applicationId") {
tflog.Warn(ctx, "Skipping resource", map[string]any{
"skip_reason": "managed by AppRegistry",
names.AttrGroupName: aws.ToString(v.GroupName),
})
continue
}

d := r.Data(nil)
d.SetId(aws.ToString(v.GroupName))

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}
}

err = sweep.SweepOrchestrator(ctx, sweepResources)

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

return nil
return sweepResources, nil
}
42 changes: 42 additions & 0 deletions internal/service/servicecatalogappregistry/sweep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package servicecatalogappregistry

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/servicecatalogappregistry"
"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"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/framework"
"github.com/hashicorp/terraform-provider-aws/names"
)

func RegisterSweepers() {
awsv2.Register("aws_servicecatalogappregistry_application", sweepScraper)
}

func sweepScraper(ctx context.Context, client *conns.AWSClient) ([]sweep.Sweepable, error) {
conn := client.ServiceCatalogAppRegistryClient(ctx)

var sweepResources []sweep.Sweepable

pages := servicecatalogappregistry.NewListApplicationsPaginator(conn, &servicecatalogappregistry.ListApplicationsInput{})
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)
if err != nil {
return nil, err
}

for _, application := range page.Applications {
sweepResources = append(sweepResources, framework.NewSweepResource(newResourceApplication, client,
framework.NewAttribute(names.AttrID, aws.ToString(application.Id)),
))
}
}

return sweepResources, nil
}
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 59d791a

Please sign in to comment.