Skip to content

Commit

Permalink
Add sweeper; fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharon Nam authored and Sharon Nam committed Apr 16, 2024
1 parent b4a8aae commit 4c658c6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 49 deletions.
46 changes: 5 additions & 41 deletions internal/service/bcmdataexports/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ func (r *resourceExport) Create(ctx context.Context, req resource.CreateRequest,
return
}

// resp.Diagnostics.Append(flex.Flatten(ctx, out, &plan)...)
resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
}

Expand All @@ -259,18 +258,6 @@ func (r *resourceExport) Read(ctx context.Context, req resource.ReadRequest, res
return
}

// exp := ExportData{}
// exp.Description = flex.StringToFramework(ctx, out.Export.Description)

// dq := fwtypes.NewListNestedObjectValueOfPtrMust(ctx, &DataQueryData{
// QueryStatement: flex.StringToFramework(ctx, out.Export.DataQuery.QueryStatement),
// })
// resp.Diagnostics.Append(flex.Flatten(ctx, out.Export, &exp)...)
// if resp.Diagnostics.HasError() {
// return
// }

// state.Export = fwtypes.NewListNestedObjectValueOfPtrMust(ctx, &exp)
state.ID = flex.StringToFramework(ctx, out.Export.ExportArn)

resp.Diagnostics.Append(flex.Flatten(ctx, out, &state)...)
Expand Down Expand Up @@ -343,25 +330,18 @@ func (r *resourceExport) Delete(ctx context.Context, req resource.DeleteRequest,
}

_, err := conn.DeleteExport(ctx, in)

if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return
}

if err != nil {
if errs.IsA[*retry.NotFoundError](err) {
return
}
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.BCMDataExports, create.ErrActionDeleting, ResNameExport, state.ID.String(), err),
err.Error(),
)
return
}
// deleteTimeout := r.DeleteTimeout(ctx, state.Timeouts)
// _, err = waitExportDeleted(ctx, conn, state.ID.ValueString(), deleteTimeout)
// if err != nil {
// resp.Diagnostics.AddError(
// create.ProblemStandardMessage(names.BCMDataExports, create.ErrActionWaitingForDeletion, ResNameExport, state.ID.String(), err),
// err.Error(),
// )
// return
// }
}

func (r *resourceExport) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
Expand Down Expand Up @@ -409,22 +389,6 @@ func waitExportUpdated(ctx context.Context, conn *bcmdataexports.Client, id stri
return nil, err
}

// func waitExportDeleted(ctx context.Context, conn *bcmdataexports.Client, id string, timeout time.Duration) (*awstypes.Export, error) {
// stateConf := &retry.StateChangeConf{
// Pending: []string{statusDeleting, statusHealthy},
// Target: []string{},
// Refresh: statusExport(ctx, conn, id),
// Timeout: timeout,
// }

// outputRaw, err := stateConf.WaitForStateContext(ctx)
// if out, ok := outputRaw.(*bcmdataexports.Export); ok {
// return out, err
// }

// return nil, err
// }

func statusExport(ctx context.Context, conn *bcmdataexports.Client, id string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
out, err := findExportByID(ctx, conn, id)
Expand Down
18 changes: 10 additions & 8 deletions internal/service/bcmdataexports/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ func TestAccBCMDataExportsExport_disappears(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, names.BCMDataExportsEndpointID)
},
ErrorCheck: acctest.ErrorCheck(t, names.BCMDataExportsServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Expand Down Expand Up @@ -153,11 +152,12 @@ resource "aws_s3_bucket_policy" "bucket" {
"billingreports.amazonaws.com",
"bcm-data-exports.amazonaws.com"
]
}
Resource = [
aws_s3_bucket.test.arn,
"${aws_s3_bucket.test.arn}/*",
]
}
Resource = [
aws_s3_bucket.test.arn,
"${aws_s3_bucket.test.arn}/*",
]
}]
Version = "2012-10-17"
})
}
Expand All @@ -174,11 +174,13 @@ resource "aws_bcmdataexports_export" "test" {
query_statement = "SELECT identity_line_item_id, identity_time_interval, line_item_product_code,line_item_unblended_cost FROM COST_AND_USAGE_REPORT"
table_configurations = {
COST_AND_USAGE_REPORT = {
TIME_GRANULARITY = "DAILY"
TIME_GRANULARITY = "HOURLY",
INCLUDE_RESOURCES = "FALSE",
INCLUDE_MANUAL_DISCOUNT_COMPATIBILITY = "FALSE",
INCLUDE_SPLIT_COST_ALLOCATION_DATA = "FALSE",
}
}
}
destination_configurations {
s3_destination {
s3_bucket = aws_s3_bucket.test.bucket
Expand Down
75 changes: 75 additions & 0 deletions internal/service/bcmdataexports/sweep.go
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
}

0 comments on commit 4c658c6

Please sign in to comment.