From cde6d967ba4fc108df1959fda38def1d63dfbe98 Mon Sep 17 00:00:00 2001 From: drfaust92 Date: Sat, 18 Jun 2022 21:47:14 +0300 Subject: [PATCH] sweep --- internal/service/applicationinsights/sweep.go | 68 +++++++++++++++++++ internal/sweep/sweep_test.go | 1 + 2 files changed, 69 insertions(+) create mode 100644 internal/service/applicationinsights/sweep.go diff --git a/internal/service/applicationinsights/sweep.go b/internal/service/applicationinsights/sweep.go new file mode 100644 index 000000000000..675ba3978f0b --- /dev/null +++ b/internal/service/applicationinsights/sweep.go @@ -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() +} diff --git a/internal/sweep/sweep_test.go b/internal/sweep/sweep_test.go index 7ed1fa6fff9e..2ab46abe2c85 100644 --- a/internal/sweep/sweep_test.go +++ b/internal/sweep/sweep_test.go @@ -16,6 +16,7 @@ import ( _ "github.com/hashicorp/terraform-provider-aws/internal/service/apigateway" _ "github.com/hashicorp/terraform-provider-aws/internal/service/apigatewayv2" _ "github.com/hashicorp/terraform-provider-aws/internal/service/appconfig" + _ "github.com/hashicorp/terraform-provider-aws/internal/service/applicationinsights" _ "github.com/hashicorp/terraform-provider-aws/internal/service/appmesh" _ "github.com/hashicorp/terraform-provider-aws/internal/service/apprunner" _ "github.com/hashicorp/terraform-provider-aws/internal/service/appstream"