Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sweepers: Create AWS SDK version-specific skip error handlers for sweepers #32488

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/service/accessanalyzer/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/accessanalyzer"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2"
)

func init() {
Expand All @@ -37,7 +38,7 @@ func sweepAnalyzers(region string) error {
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if sweep.SkipSweepError(err) {
if awsv2.SkipSweepError(err) {
log.Printf("[WARN] Skipping IAM Access Analyzer Analyzer sweep for %s: %s", region, err)
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion internal/service/acm/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/acm"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2"
)

func init() {
Expand Down Expand Up @@ -54,7 +55,7 @@ func sweepCertificates(region string) error {
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if sweep.SkipSweepError(err) {
if awsv2.SkipSweepError(err) {
log.Printf("[WARN] Skipping ACM Certificate sweep for %s: %s", region, err)
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion internal/service/acmpca/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv1"
)

func init() {
Expand Down Expand Up @@ -72,7 +73,7 @@ func sweepCertificateAuthorities(region string) error {
errs = multierror.Append(errs, fmt.Errorf("sweeping ACM PCA Certificate Authorities for %s: %w", region, err))
}

if sweep.SkipSweepError(errs.ErrorOrNil()) {
if awsv1.SkipSweepError(errs.ErrorOrNil()) {
log.Printf("[WARN] Skipping ACM PCA Certificate Authorities sweep for %s: %s", region, errs)
return nil
}
Expand Down
80 changes: 25 additions & 55 deletions internal/service/auditmanager/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/auditmanager"
"github.com/aws/aws-sdk-go-v2/service/auditmanager/types"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"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"
)

Expand Down Expand Up @@ -70,19 +70,18 @@ func sweepAssessments(region string) error {
ctx := sweep.Context(region)
client, err := sweep.SharedRegionalSweepClient(ctx, region)
if err != nil {
fmt.Errorf("error getting client: %s", err)
return fmt.Errorf("error getting client: %s", err)
}

conn := client.AuditManagerClient(ctx)
sweepResources := make([]sweep.Sweepable, 0)
in := &auditmanager.ListAssessmentsInput{}
var errs *multierror.Error

pages := auditmanager.NewListAssessmentsPaginator(conn, in)

for pages.HasMorePages() {
page, err := pages.NextPage(ctx)
if sweep.SkipSweepError(err) || isCompleteSetupError(err) {
if awsv2.SkipSweepError(err) || isCompleteSetupError(err) {
log.Printf("[WARN] Skipping AuditManager Assessments sweep for %s: %s", region, err)
return nil
}
Expand All @@ -101,33 +100,28 @@ func sweepAssessments(region string) error {
}

if err := sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping AuditManager Assessments for %s: %w", region, err))
}
if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping AuditManager Assessments sweep for %s: %s", region, errs)
return nil
return fmt.Errorf("error sweeping AuditManager Assessments for %s: %w", region, err)
}

return errs.ErrorOrNil()
return nil
}

func sweepAssessmentDelegations(region string) error {
ctx := sweep.Context(region)
client, err := sweep.SharedRegionalSweepClient(ctx, region)
if err != nil {
fmt.Errorf("error getting client: %s", err)
return fmt.Errorf("error getting client: %s", err)
}

conn := client.AuditManagerClient(ctx)
sweepResources := make([]sweep.Sweepable, 0)
in := &auditmanager.GetDelegationsInput{}
var errs *multierror.Error

pages := auditmanager.NewGetDelegationsPaginator(conn, in)

for pages.HasMorePages() {
page, err := pages.NextPage(ctx)
if sweep.SkipSweepError(err) || isCompleteSetupError(err) {
if awsv2.SkipSweepError(err) || isCompleteSetupError(err) {
log.Printf("[WARN] Skipping AuditManager Assesment Delegations sweep for %s: %s", region, err)
return nil
}
Expand All @@ -145,33 +139,28 @@ func sweepAssessmentDelegations(region string) error {
}

if err := sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping AuditManager Assessment Delegations for %s: %w", region, err))
}
if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping AuditManager Assessment Delegations sweep for %s: %s", region, errs)
return nil
return fmt.Errorf("error sweeping AuditManager Assessment Delegations for %s: %w", region, err)
}

return errs.ErrorOrNil()
return nil
}

func sweepAssessmentReports(region string) error {
ctx := sweep.Context(region)
client, err := sweep.SharedRegionalSweepClient(ctx, region)
if err != nil {
fmt.Errorf("error getting client: %s", err)
return fmt.Errorf("error getting client: %s", err)
}

conn := client.AuditManagerClient(ctx)
sweepResources := make([]sweep.Sweepable, 0)
in := &auditmanager.ListAssessmentReportsInput{}
var errs *multierror.Error

pages := auditmanager.NewListAssessmentReportsPaginator(conn, in)

for pages.HasMorePages() {
page, err := pages.NextPage(ctx)
if sweep.SkipSweepError(err) || isCompleteSetupError(err) {
if awsv2.SkipSweepError(err) || isCompleteSetupError(err) {
log.Printf("[WARN] Skipping AuditManager Assesment Reports sweep for %s: %s", region, err)
return nil
}
Expand All @@ -191,33 +180,28 @@ func sweepAssessmentReports(region string) error {
}

if err := sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping AuditManager Assessment Reports for %s: %w", region, err))
}
if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping AuditManager Assessment Reports sweep for %s: %s", region, errs)
return nil
return fmt.Errorf("error sweeping AuditManager Assessment Reports for %s: %w", region, err)
}

return errs.ErrorOrNil()
return nil
}

func sweepControls(region string) error {
ctx := sweep.Context(region)
client, err := sweep.SharedRegionalSweepClient(ctx, region)
if err != nil {
fmt.Errorf("error getting client: %s", err)
return fmt.Errorf("error getting client: %s", err)
}

conn := client.AuditManagerClient(ctx)
sweepResources := make([]sweep.Sweepable, 0)
in := &auditmanager.ListControlsInput{ControlType: types.ControlTypeCustom}
var errs *multierror.Error

pages := auditmanager.NewListControlsPaginator(conn, in)

for pages.HasMorePages() {
page, err := pages.NextPage(ctx)
if sweep.SkipSweepError(err) || isCompleteSetupError(err) {
if awsv2.SkipSweepError(err) || isCompleteSetupError(err) {
log.Printf("[WARN] Skipping AuditManager Controls sweep for %s: %s", region, err)
return nil
}
Expand All @@ -236,33 +220,28 @@ func sweepControls(region string) error {
}

if err := sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping AuditManager Controls for %s: %w", region, err))
}
if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping AuditManager Controls sweep for %s: %s", region, errs)
return nil
return fmt.Errorf("error sweeping AuditManager Controls for %s: %w", region, err)
}

return errs.ErrorOrNil()
return nil
}

func sweepFrameworks(region string) error {
ctx := sweep.Context(region)
client, err := sweep.SharedRegionalSweepClient(ctx, region)
if err != nil {
fmt.Errorf("error getting client: %s", err)
return fmt.Errorf("error getting client: %s", err)
}

conn := client.AuditManagerClient(ctx)
sweepResources := make([]sweep.Sweepable, 0)
in := &auditmanager.ListAssessmentFrameworksInput{FrameworkType: types.FrameworkTypeCustom}
var errs *multierror.Error

pages := auditmanager.NewListAssessmentFrameworksPaginator(conn, in)

for pages.HasMorePages() {
page, err := pages.NextPage(ctx)
if sweep.SkipSweepError(err) || isCompleteSetupError(err) {
if awsv2.SkipSweepError(err) || isCompleteSetupError(err) {
log.Printf("[WARN] Skipping AuditManager Frameworks sweep for %s: %s", region, err)
return nil
}
Expand All @@ -281,33 +260,28 @@ func sweepFrameworks(region string) error {
}

if err := sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping AuditManager Frameworks for %s: %w", region, err))
}
if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping AuditManager Frameworks sweep for %s: %s", region, errs)
return nil
return fmt.Errorf("error sweeping AuditManager Frameworks for %s: %w", region, err)
}

return errs.ErrorOrNil()
return nil
}

func sweepFrameworkShares(region string) error {
ctx := sweep.Context(region)
client, err := sweep.SharedRegionalSweepClient(ctx, region)
if err != nil {
fmt.Errorf("error getting client: %s", err)
return fmt.Errorf("error getting client: %s", err)
}

conn := client.AuditManagerClient(ctx)
sweepResources := make([]sweep.Sweepable, 0)
in := &auditmanager.ListAssessmentFrameworkShareRequestsInput{RequestType: types.ShareRequestTypeSent}
var errs *multierror.Error

pages := auditmanager.NewListAssessmentFrameworkShareRequestsPaginator(conn, in)

for pages.HasMorePages() {
page, err := pages.NextPage(ctx)
if sweep.SkipSweepError(err) || isCompleteSetupError(err) {
if awsv2.SkipSweepError(err) || isCompleteSetupError(err) {
log.Printf("[WARN] Skipping AuditManager Framework Shares sweep for %s: %s", region, err)
return nil
}
Expand All @@ -326,12 +300,8 @@ func sweepFrameworkShares(region string) error {
}

if err := sweep.SweepOrchestrator(ctx, sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping AuditManager Framework Shares for %s: %w", region, err))
}
if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping AuditManager Framework Shares sweep for %s: %s", region, errs)
return nil
return fmt.Errorf("error sweeping AuditManager Framework Shares for %s: %w", region, err)
}

return errs.ErrorOrNil()
return nil
}
3 changes: 1 addition & 2 deletions internal/service/ec2/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"
"time"

aws_sdkv2 "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
Expand Down Expand Up @@ -2628,7 +2627,7 @@ func sweepInstanceConnectEndpoints(region string) error {
}

sweepResources = append(sweepResources, framework.NewSweepResource(newResourceInstanceConnectEndpoint, client,
framework.NewAttribute("id", aws_sdkv2.ToString(v.InstanceConnectEndpointId)),
framework.NewAttribute("id", aws.StringValue(v.InstanceConnectEndpointId)),
))
}

Expand Down
3 changes: 2 additions & 1 deletion internal/service/glacier/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/glacier"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2"
)

func init() {
Expand All @@ -37,7 +38,7 @@ func sweepVaults(region string) error {
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if sweep.SkipSweepError(err) {
if awsv2.SkipSweepError(err) {
log.Printf("[WARN] Skipping Glacier Vault sweep for %s: %s", region, err)
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion internal/service/internetmonitor/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/internetmonitor"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2"
)

func init() {
Expand All @@ -37,7 +38,7 @@ func sweepMonitors(region string) error {
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if sweep.SkipSweepError(err) {
if awsv2.SkipSweepError(err) {
log.Printf("[WARN] Skipping Internet Monitor Monitor sweep for %s: %s", region, err)
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion internal/service/kendra/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2"
)

func init() {
Expand All @@ -41,7 +42,7 @@ func sweepIndex(region string) error {
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if sweep.SkipSweepError(err) {
if awsv2.SkipSweepError(err) {
log.Printf("[WARN] Skipping Kendra Indices sweep for %s: %s", region, err)
return errs.ErrorOrNil()
}
Expand Down
3 changes: 2 additions & 1 deletion internal/service/keyspaces/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/keyspaces"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/sweep"
"github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2"
)

func init() {
Expand All @@ -38,7 +39,7 @@ func sweepKeyspaces(region string) error { // nosemgrep:ci.keyspaces-in-func-nam
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if sweep.SkipSweepError(err) {
if awsv2.SkipSweepError(err) {
log.Printf("[WARN] Skipping Keyspaces Keyspace sweep for %s: %s", region, err)
return nil
}
Expand Down
Loading