From 6bc640666d62e7c6ea598cb0a4bb5b1db1124f90 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 20 May 2021 11:15:16 -0400 Subject: [PATCH] r/aws_amplify_app: Add test sweeper. Acceptance test output: % TEST=./aws SWEEP=us-west-2 SWEEPARGS=-sweep-run=aws_amplify_app make sweep WARNING: This will destroy infrastructure. Use only in development accounts. go test ./aws -v -sweep=us-west-2 -sweep-run=aws_amplify_app -timeout 60m 2021/05/20 11:13:49 [DEBUG] Running Sweepers for region (us-west-2): 2021/05/20 11:13:49 [DEBUG] Running Sweeper (aws_amplify_app) in region (us-west-2) 2021/05/20 11:13:49 [INFO] AWS Auth provider used: "EnvProvider" 2021/05/20 11:13:49 [DEBUG] Trying to get account information via sts:GetCallerIdentity 2021/05/20 11:13:50 [DEBUG] Trying to get account information via sts:GetCallerIdentity 2021/05/20 11:13:50 [DEBUG] Deleting Amplify App (d3e2rpqo82owxg) 2021/05/20 11:13:51 Sweeper Tests ran successfully: - aws_amplify_app ok github.com/terraform-providers/terraform-provider-aws/aws 4.452s --- aws/internal/service/amplify/lister/list.go | 3 ++ .../service/amplify/lister/list_pages_gen.go | 31 +++++++++++ aws/resource_aws_amplify_app_test.go | 52 ++++++++++++++++++- 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 aws/internal/service/amplify/lister/list.go create mode 100644 aws/internal/service/amplify/lister/list_pages_gen.go diff --git a/aws/internal/service/amplify/lister/list.go b/aws/internal/service/amplify/lister/list.go new file mode 100644 index 00000000000..3b7007c9979 --- /dev/null +++ b/aws/internal/service/amplify/lister/list.go @@ -0,0 +1,3 @@ +//go:generate go run ../../../generators/listpages/main.go -function=ListApps github.com/aws/aws-sdk-go/service/amplify + +package lister diff --git a/aws/internal/service/amplify/lister/list_pages_gen.go b/aws/internal/service/amplify/lister/list_pages_gen.go new file mode 100644 index 00000000000..30fa4b12930 --- /dev/null +++ b/aws/internal/service/amplify/lister/list_pages_gen.go @@ -0,0 +1,31 @@ +// Code generated by "aws/internal/generators/listpages/main.go -function=ListApps github.com/aws/aws-sdk-go/service/amplify"; DO NOT EDIT. + +package lister + +import ( + "context" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/amplify" +) + +func ListAppsPages(conn *amplify.Amplify, input *amplify.ListAppsInput, fn func(*amplify.ListAppsOutput, bool) bool) error { + return ListAppsPagesWithContext(context.Background(), conn, input, fn) +} + +func ListAppsPagesWithContext(ctx context.Context, conn *amplify.Amplify, input *amplify.ListAppsInput, fn func(*amplify.ListAppsOutput, bool) bool) error { + for { + output, err := conn.ListAppsWithContext(ctx, input) + if err != nil { + return err + } + + lastPage := aws.StringValue(output.NextToken) == "" + if !fn(output, lastPage) || lastPage { + break + } + + input.NextToken = output.NextToken + } + return nil +} diff --git a/aws/resource_aws_amplify_app_test.go b/aws/resource_aws_amplify_app_test.go index a61704d2c3f..e5fa6643b9b 100644 --- a/aws/resource_aws_amplify_app_test.go +++ b/aws/resource_aws_amplify_app_test.go @@ -3,20 +3,70 @@ package aws import ( "encoding/base64" "fmt" + "log" "os" "regexp" "testing" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/amplify" + "github.com/hashicorp/go-multierror" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/amplify/finder" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/amplify/lister" "github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource" ) -// TODO sweeper +func init() { + resource.AddTestSweepers("aws_amplify_app", &resource.Sweeper{ + Name: "aws_amplify_app", + F: testSweepAmplifyApps, + }) +} + +func testSweepAmplifyApps(region string) error { + client, err := sharedClientForRegion(region) + if err != nil { + return fmt.Errorf("error getting client: %s", err) + } + conn := client.(*AWSClient).amplifyconn + input := &lify.ListAppsInput{} + var sweeperErrs *multierror.Error + + err = lister.ListAppsPages(conn, input, func(page *amplify.ListAppsOutput, lastPage bool) bool { + if page == nil { + return !lastPage + } + + for _, app := range page.Apps { + r := resourceAwsAmplifyApp() + d := r.Data(nil) + d.SetId(aws.StringValue(app.AppId)) + err = r.Delete(d, client) + + if err != nil { + log.Printf("[ERROR] %s", err) + sweeperErrs = multierror.Append(sweeperErrs, err) + continue + } + } + + return !lastPage + }) + + if testSweepSkipSweepError(err) { + log.Printf("[WARN] Skipping Amplify Apps sweep for %s: %s", region, err) + return sweeperErrs.ErrorOrNil() // In case we have completed some pages, but had errors + } + + if err != nil { + sweeperErrs = multierror.Append(sweeperErrs, fmt.Errorf("error listing Amplify Apps: %w", err)) + } + + return sweeperErrs.ErrorOrNil() +} func TestAccAWSAmplifyApp_basic(t *testing.T) { var app amplify.App