Skip to content

Commit

Permalink
r/aws_gamelift_game_server_group: fix crash reading group with nil AS…
Browse files Browse the repository at this point in the history
…G ARN

The read operation assumed a non-`nil` value for the `AutoScalingGroupArn`
field in the Describe API response. This field may be `nil` if
the scaling group cannot be created or is deleted out of
band. Previously, when a `nil` value was returned, the provider attempted to split the
result to retrieve the name of the auto scaling group from the full ARN,
resulting in an `index out of range` panic when referencing the second
item of the split function.

The read operation now ensures the `AutoScalingGroupArn` field is not
`nil` and can be split into the appropriate number of parts before
attempting to complete subsequent read operations on the auto scaling
group. When `nil`, these read operations are skipped entirely.

```console
% make testacc PKG=gamelift TESTS=TestAccGameLiftGameServerGroup_
make: Verifying source code with gofmt...
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go1.22.6 test ./internal/service/gamelift/... -v -count 1 -parallel 20 -run='TestAccGameLiftGameServerGroup_'  -timeout 360m

--- PASS: TestAccGameLiftGameServerGroup_InstanceDefinition (211.70s)
--- PASS: TestAccGameLiftGameServerGroup_InstanceDefinition_WeightedCapacity (223.88s)
--- PASS: TestAccGameLiftGameServerGroup_LaunchTemplate_Id (246.89s)
--- PASS: TestAccGameLiftGameServerGroup_AutoScalingPolicy_EstimatedInstanceWarmup (248.01s)
--- PASS: TestAccGameLiftGameServerGroup_GameServerProtectionPolicy (274.38s)
--- PASS: TestAccGameLiftGameServerGroup_BalancingStrategy (277.45s)
--- PASS: TestAccGameLiftGameServerGroup_basic (279.24s)
--- PASS: TestAccGameLiftGameServerGroup_LaunchTemplate_Version (282.54s)
--- PASS: TestAccGameLiftGameServerGroup_roleARN (287.50s)
--- PASS: TestAccGameLiftGameServerGroup_LaunchTemplate_Name (293.63s)
--- PASS: TestAccGameLiftGameServerGroup_AutoScalingPolicy (333.94s)
--- PASS: TestAccGameLiftGameServerGroup_MinSize (362.83s)
--- PASS: TestAccGameLiftGameServerGroup_vpcSubnets (402.18s)
--- PASS: TestAccGameLiftGameServerGroup_GameServerGroupName (465.44s)
--- PASS: TestAccGameLiftGameServerGroup_MaxSize (676.82s)
PASS
ok      github.com/hashicorp/terraform-provider-aws/internal/service/gamelift   682.783s
```
  • Loading branch information
jar-b committed Aug 23, 2024
1 parent d455e3f commit b6ff60c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .changelog/39022.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_gamelift_game_server_group: Fix crash while reading server group with a nil auto scaling group ARN
```
48 changes: 26 additions & 22 deletions internal/service/gamelift/game_server_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,41 +257,45 @@ func resourceGameServerGroupRead(ctx context.Context, d *schema.ResourceData, me
return sdkdiag.AppendErrorf(diags, "reading GameLift Game Server Group (%s): %s", d.Id(), err)
}

autoScalingGroupName := strings.Split(aws.ToString(gameServerGroup.AutoScalingGroupArn), "/")[1]
autoScalingGroup, err := tfautoscaling.FindGroupByName(ctx, autoscalingConn, autoScalingGroupName)
if asgArnParts := strings.Split(aws.ToString(gameServerGroup.AutoScalingGroupArn), "/"); len(asgArnParts) == 2 {
asgName := asgArnParts[1]
asg, err := tfautoscaling.FindGroupByName(ctx, autoscalingConn, asgName)

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading Auto Scaling Group (%s): %s", autoScalingGroupName, err)
}
if err != nil {
return sdkdiag.AppendErrorf(diags, "reading Auto Scaling Group (%s): %s", asgName, err)
}

autoScalingPolicy, err := tfautoscaling.FindScalingPolicyByTwoPartKey(ctx, autoscalingConn, autoScalingGroupName, d.Id())
asgPolicy, err := tfautoscaling.FindScalingPolicyByTwoPartKey(ctx, autoscalingConn, asgName, d.Id())

switch {
case tfresource.NotFound(err):
case err != nil:
return sdkdiag.AppendErrorf(diags, "reading Auto Scaling Policy (%s/%s): %s", autoScalingGroupName, d.Id(), err)
switch {
case tfresource.NotFound(err):
case err != nil:
return sdkdiag.AppendErrorf(diags, "reading Auto Scaling Policy (%s/%s): %s", asgName, d.Id(), err)
}

if asgPolicy != nil {
if err := d.Set("auto_scaling_policy", []interface{}{flattenGameServerGroupAutoScalingPolicy(asgPolicy)}); err != nil {
return sdkdiag.AppendErrorf(diags, "setting auto_scaling_policy: %s", err)
}
} else {
d.Set("auto_scaling_policy", nil)
}

if err := d.Set(names.AttrLaunchTemplate, flattenAutoScalingLaunchTemplateSpecification(asg.MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting launch_template: %s", err)
}
d.Set("max_size", asg.MaxSize)
d.Set("min_size", asg.MinSize)
}

d.Set(names.AttrARN, gameServerGroup.GameServerGroupArn)
d.Set("auto_scaling_group_arn", gameServerGroup.AutoScalingGroupArn)
if autoScalingPolicy != nil {
if err := d.Set("auto_scaling_policy", []interface{}{flattenGameServerGroupAutoScalingPolicy(autoScalingPolicy)}); err != nil {
return sdkdiag.AppendErrorf(diags, "setting auto_scaling_policy: %s", err)
}
} else {
d.Set("auto_scaling_policy", nil)
}
d.Set("balancing_strategy", gameServerGroup.BalancingStrategy)
d.Set("game_server_group_name", gameServerGroup.GameServerGroupName)
d.Set("game_server_protection_policy", gameServerGroup.GameServerProtectionPolicy)
if err := d.Set("instance_definition", flattenInstanceDefinitions(gameServerGroup.InstanceDefinitions)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting instance_definition: %s", err)
}
if err := d.Set(names.AttrLaunchTemplate, flattenAutoScalingLaunchTemplateSpecification(autoScalingGroup.MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting launch_template: %s", err)
}
d.Set("max_size", autoScalingGroup.MaxSize)
d.Set("min_size", autoScalingGroup.MinSize)
d.Set(names.AttrRoleARN, gameServerGroup.RoleArn)

return diags
Expand Down

0 comments on commit b6ff60c

Please sign in to comment.