Skip to content

Commit

Permalink
gamelift: helpers: migrate to AWS SDK v2
Browse files Browse the repository at this point in the history
  • Loading branch information
mattburgess committed Jul 16, 2024
1 parent 4927ab2 commit d5e4438
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 52 deletions.
17 changes: 1 addition & 16 deletions internal/service/gamelift/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 3 additions & 5 deletions internal/service/gamelift/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}
}

Expand All @@ -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
}
}

Expand All @@ -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
}
}
23 changes: 11 additions & 12 deletions internal/service/gamelift/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
38 changes: 19 additions & 19 deletions internal/service/gamelift/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
}
Expand All @@ -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,
}
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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
}
}
Expand All @@ -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,
}
Expand All @@ -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,
Expand Down

0 comments on commit d5e4438

Please sign in to comment.