-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sharon Nam
authored and
Sharon Nam
committed
Apr 16, 2024
1 parent
b4a8aae
commit 4c658c6
Showing
3 changed files
with
90 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package bcmdataexports | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/auditmanager/types" | ||
"github.com/aws/aws-sdk-go-v2/service/bcmdataexports" | ||
"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" | ||
) | ||
|
||
func RegisterSweepers() { | ||
resource.AddTestSweepers("aws_bcmdataexports", &resource.Sweeper{ | ||
Name: "aws_bcmdataexports_export", | ||
F: sweepExports, | ||
}) | ||
} | ||
|
||
// isCompleteSetupError checks whether the returned error message indicates | ||
// AuditManager isn't yet enabled in the current region. | ||
// | ||
// For example: | ||
// AccessDeniedException: Please complete AWS Audit Manager setup from home page to enable this action in this account. | ||
func isCompleteSetupError(err error) bool { | ||
var ade *types.AccessDeniedException | ||
return errors.As(err, &ade) | ||
} | ||
|
||
func sweepExports(region string) error { | ||
ctx := sweep.Context(region) | ||
client, err := sweep.SharedRegionalSweepClient(ctx, region) | ||
if err != nil { | ||
return fmt.Errorf("error getting client: %s", err) | ||
} | ||
|
||
conn := client.BCMDataExportsClient(ctx) | ||
sweepResources := make([]sweep.Sweepable, 0) | ||
in := &bcmdataexports.ListExportsInput{} | ||
|
||
pages := bcmdataexports.NewListExportsPaginator(conn, in) | ||
|
||
for pages.HasMorePages() { | ||
page, err := pages.NextPage(ctx) | ||
if awsv2.SkipSweepError(err) || isCompleteSetupError(err) { | ||
log.Printf("[WARN] Skipping BCM Data Exports export sweep for %s: %s", region, err) | ||
return nil | ||
} | ||
if err != nil { | ||
return fmt.Errorf("error retrieving BCM Data Exports Export: %w", err) | ||
} | ||
|
||
for _, b := range page.Exports { | ||
id := aws.ToString(b.ExportArn) | ||
|
||
log.Printf("[INFO] Deleting AuditManager Assessment: %s", id) | ||
sweepResources = append(sweepResources, framework.NewSweepResource(newResourceExport, client, | ||
framework.NewAttribute("id", id), | ||
)) | ||
} | ||
} | ||
|
||
if err := sweep.SweepOrchestrator(ctx, sweepResources); err != nil { | ||
return fmt.Errorf("error sweeping AuditManager Assessments for %s: %w", region, err) | ||
} | ||
|
||
return nil | ||
} |