From d5e44385af2432cdf6be1d62f65ac50a17abaaba Mon Sep 17 00:00:00 2001 From: Matt Burgess <549318+mattburgess@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:55:02 +0100 Subject: [PATCH] gamelift: helpers: migrate to AWS SDK v2 --- internal/service/gamelift/find.go | 17 +------------ internal/service/gamelift/status.go | 8 +++--- internal/service/gamelift/sweep.go | 23 +++++++++-------- internal/service/gamelift/wait.go | 38 ++++++++++++++--------------- 4 files changed, 34 insertions(+), 52 deletions(-) diff --git a/internal/service/gamelift/find.go b/internal/service/gamelift/find.go index 2547414879d..9850fbe1922 100644 --- a/internal/service/gamelift/find.go +++ b/internal/service/gamelift/find.go @@ -9,7 +9,6 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/gamelift" awstypes "github.com/aws/aws-sdk-go-v2/service/gamelift/types" - "github.com/hashicorp/aws-sdk-go-base/v2/tfawserr" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-provider-aws/internal/errs" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" @@ -58,21 +57,7 @@ func FindFleetByID(ctx context.Context, conn *gamelift.Client, id string) (*awst return nil, err } - if len(output.FleetAttributes) == 0 || output.FleetAttributes[0] == nil { - return nil, tfresource.NewEmptyResultError(output) - } - - if count := len(output.FleetAttributes); count > 1 { - return nil, tfresource.NewTooManyResultsError(count, output) - } - - fleet := output.FleetAttributes[0] - - if aws.ToString(fleet.FleetId) != id { - return nil, tfresource.NewEmptyResultError(id) - } - - return fleet, nil + return tfresource.AssertSingleValueResult(output.FleetAttributes) } func FindGameServerGroupByName(ctx context.Context, conn *gamelift.Client, name string) (*awstypes.GameServerGroup, error) { diff --git a/internal/service/gamelift/status.go b/internal/service/gamelift/status.go index c797e777e56..3c912f01779 100644 --- a/internal/service/gamelift/status.go +++ b/internal/service/gamelift/status.go @@ -6,9 +6,7 @@ package gamelift import ( "context" - "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/gamelift" - awstypes "github.com/aws/aws-sdk-go-v2/service/gamelift/types" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) @@ -25,7 +23,7 @@ func statusBuild(ctx context.Context, conn *gamelift.Client, id string) retry.St return nil, "", err } - return output, aws.ToString(output.Status), nil + return output, string(output.Status), nil } } @@ -41,7 +39,7 @@ func statusFleet(ctx context.Context, conn *gamelift.Client, id string) retry.St return nil, "", err } - return output, aws.ToString(output.Status), nil + return output, string(output.Status), nil } } @@ -57,6 +55,6 @@ func statusGameServerGroup(ctx context.Context, conn *gamelift.Client, name stri return nil, "", err } - return output, aws.ToString(output.Status), nil + return output, string(output.Status), nil } } diff --git a/internal/service/gamelift/sweep.go b/internal/service/gamelift/sweep.go index a9079a778b7..ef5c1a8dd07 100644 --- a/internal/service/gamelift/sweep.go +++ b/internal/service/gamelift/sweep.go @@ -10,12 +10,11 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/gamelift" - awstypes "github.com/aws/aws-sdk-go-v2/service/gamelift/types" "github.com/hashicorp/go-multierror" "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-provider-aws/internal/errs" "github.com/hashicorp/terraform-provider-aws/internal/sweep" "github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv1" + "github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2" ) func RegisterSweepers() { @@ -186,11 +185,15 @@ func sweepFleets(region string) error { for { output, err := conn.ListFleets(ctx, input) + if err != nil { + errs = multierror.Append(errs, fmt.Errorf("listing GameLift Fleet for %s: %w", region, err)) + } + for _, fleet := range output.FleetIds { r := ResourceFleet() d := r.Data(nil) - id := aws.ToString(fleet) + id := fleet d.SetId(id) if err != nil { @@ -210,10 +213,6 @@ func sweepFleets(region string) error { input.NextToken = output.NextToken } - if err != nil { - errs = multierror.Append(errs, fmt.Errorf("listing GameLift Fleet for %s: %w", region, err)) - } - if err := sweep.SweepOrchestrator(ctx, sweepResources); err != nil { errs = multierror.Append(errs, fmt.Errorf("sweeping GameLift Fleet for %s: %w", region, err)) } @@ -241,6 +240,10 @@ func sweepGameServerGroups(region string) error { for { output, err := conn.ListGameServerGroups(ctx, input) + if err != nil { + errs = multierror.Append(errs, fmt.Errorf("listing GameLift Game Server Group for %s: %w", region, err)) + } + for _, gameServerGroup := range output.GameServerGroups { r := ResourceGameServerGroup() d := r.Data(nil) @@ -265,15 +268,11 @@ func sweepGameServerGroups(region string) error { input.NextToken = output.NextToken } - if err != nil { - errs = multierror.Append(errs, fmt.Errorf("listing GameLift Game Server Group for %s: %w", region, err)) - } - if err := sweep.SweepOrchestrator(ctx, sweepResources); err != nil { errs = multierror.Append(errs, fmt.Errorf("sweeping GameLift Game Server Group for %s: %w", region, err)) } - if awsv1.SkipSweepError(err) { + if awsv2.SkipSweepError(err) { log.Printf("[WARN] Skipping GameLift Game Server Group sweep for %s: %s", region, errs) return nil } diff --git a/internal/service/gamelift/wait.go b/internal/service/gamelift/wait.go index b4bf209f306..c9dbe1055bc 100644 --- a/internal/service/gamelift/wait.go +++ b/internal/service/gamelift/wait.go @@ -12,8 +12,8 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/gamelift" awstypes "github.com/aws/aws-sdk-go-v2/service/gamelift/types" - "github.com/hashicorp/aws-sdk-go-base/v2/tfawserr" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" + "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs" ) @@ -23,8 +23,8 @@ const ( func waitBuildReady(ctx context.Context, conn *gamelift.Client, id string) (*awstypes.Build, error) { stateConf := &retry.StateChangeConf{ - Pending: []string{awstypes.BuildStatusInitialized}, - Target: []string{awstypes.BuildStatusReady}, + Pending: enum.Slice(awstypes.BuildStatusInitialized), + Target: enum.Slice(awstypes.BuildStatusReady), Refresh: statusBuild(ctx, conn, id), Timeout: buildReadyTimeout, } @@ -40,14 +40,14 @@ func waitBuildReady(ctx context.Context, conn *gamelift.Client, id string) (*aws func waitFleetActive(ctx context.Context, conn *gamelift.Client, id string, timeout time.Duration) (*awstypes.FleetAttributes, error) { stateConf := &retry.StateChangeConf{ - Pending: []string{ + Pending: enum.Slice( awstypes.FleetStatusActivating, awstypes.FleetStatusBuilding, awstypes.FleetStatusDownloading, awstypes.FleetStatusNew, awstypes.FleetStatusValidating, - }, - Target: []string{awstypes.FleetStatusActive}, + ), + Target: enum.Slice(awstypes.FleetStatusActive), Refresh: statusFleet(ctx, conn, id), Timeout: timeout, } @@ -63,12 +63,12 @@ func waitFleetActive(ctx context.Context, conn *gamelift.Client, id string, time func waitFleetTerminated(ctx context.Context, conn *gamelift.Client, id string, timeout time.Duration) (*awstypes.FleetAttributes, error) { stateConf := &retry.StateChangeConf{ - Pending: []string{ + Pending: enum.Slice( awstypes.FleetStatusActive, awstypes.FleetStatusDeleting, awstypes.FleetStatusError, awstypes.FleetStatusTerminated, - }, + ), Target: []string{}, Refresh: statusFleet(ctx, conn, id), Timeout: timeout, @@ -93,13 +93,13 @@ func waitFleetTerminated(ctx context.Context, conn *gamelift.Client, id string, return nil, err } -func getFleetFailures(ctx context.Context, conn *gamelift.Client, id string) ([]*awstypes.Event, error) { - var events []*awstypes.Event +func getFleetFailures(ctx context.Context, conn *gamelift.Client, id string) ([]awstypes.Event, error) { + var events []awstypes.Event err := _getFleetFailures(ctx, conn, id, nil, &events) return events, err } -func _getFleetFailures(ctx context.Context, conn *gamelift.Client, id string, nextToken *string, events *[]*awstypes.Event) error { +func _getFleetFailures(ctx context.Context, conn *gamelift.Client, id string, nextToken *string, events *[]awstypes.Event) error { eOut, err := conn.DescribeFleetEvents(ctx, &gamelift.DescribeFleetEventsInput{ FleetId: aws.String(id), NextToken: nextToken, @@ -124,8 +124,8 @@ func _getFleetFailures(ctx context.Context, conn *gamelift.Client, id string, ne return nil } -func isEventFailure(event *awstypes.Event) bool { - failureCodes := []string{ +func isEventFailure(event awstypes.Event) bool { + failureCodes := []awstypes.EventCode{ awstypes.EventCodeFleetActivationFailed, awstypes.EventCodeFleetActivationFailedNoInstances, awstypes.EventCodeFleetBinaryDownloadFailed, @@ -145,7 +145,7 @@ func isEventFailure(event *awstypes.Event) bool { awstypes.EventCodeServerProcessTerminatedUnhealthy, } for _, fc := range failureCodes { - if string(event.EventCode) == fc { + if event.EventCode == fc { return true } } @@ -154,11 +154,11 @@ func isEventFailure(event *awstypes.Event) bool { func waitGameServerGroupActive(ctx context.Context, conn *gamelift.Client, name string, timeout time.Duration) (*awstypes.GameServerGroup, error) { stateConf := &retry.StateChangeConf{ - Pending: []string{ + Pending: enum.Slice( awstypes.GameServerGroupStatusNew, awstypes.GameServerGroupStatusActivating, - }, - Target: []string{awstypes.GameServerGroupStatusActive}, + ), + Target: enum.Slice(awstypes.GameServerGroupStatusActive), Refresh: statusGameServerGroup(ctx, conn, name), Timeout: timeout, } @@ -174,10 +174,10 @@ func waitGameServerGroupActive(ctx context.Context, conn *gamelift.Client, name func waitGameServerGroupTerminated(ctx context.Context, conn *gamelift.Client, name string, timeout time.Duration) error { stateConf := &retry.StateChangeConf{ - Pending: []string{ + Pending: enum.Slice( awstypes.GameServerGroupStatusDeleteScheduled, awstypes.GameServerGroupStatusDeleting, - }, + ), Target: []string{}, Refresh: statusGameServerGroup(ctx, conn, name), Timeout: timeout,