Skip to content

Commit

Permalink
sweep
Browse files Browse the repository at this point in the history
  • Loading branch information
DrFaust92 committed Jun 18, 2022
1 parent a2f632e commit cde6d96
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
68 changes: 68 additions & 0 deletions internal/service/applicationinsights/sweep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//go:build sweep
// +build sweep

package applicationinsights

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/applicationinsights"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
)

func init() {
resource.AddTestSweepers("aws_applicationinsights_application", &resource.Sweeper{
Name: "aws_applicationinsights_application",
F: sweepApplications,
})
}

func sweepApplications(region string) error {
client, err := sweep.SharedRegionalSweepClient(region)

if err != nil {
return fmt.Errorf("error getting client: %s", err)
}

conn := client.(*conns.AWSClient).ApplicationInsightsConn
sweepResources := make([]*sweep.SweepResource, 0)
var errs *multierror.Error

err = conn.ListApplicationsPages(&applicationinsights.ListApplicationsInput{}, func(resp *applicationinsights.ListApplicationsOutput, lastPage bool) bool {
if len(resp.ApplicationInfoList) == 0 {
log.Print("[DEBUG] No ApplicationInsights Applications to sweep")
return !lastPage
}

for _, c := range resp.ApplicationInfoList {
r := ResourceApplication()
d := r.Data(nil)
d.SetId(aws.StringValue(c.ResourceGroupName))

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

return !lastPage
})

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error describing ApplicationInsights Applications: %w", err))
// in case work can be done, don't jump out yet
}

if err = sweep.SweepOrchestrator(sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping ApplicationInsights Applications for %s: %w", region, err))
}

if sweep.SkipSweepError(errs.ErrorOrNil()) {
log.Printf("[WARN] Skipping ApplicationInsights Application sweep for %s: %s", region, err)
return nil
}

return errs.ErrorOrNil()
}
1 change: 1 addition & 0 deletions internal/sweep/sweep_test.go

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

0 comments on commit cde6d96

Please sign in to comment.