Skip to content

Commit

Permalink
resource/aws_batch_job_queue: Prevent panic when ComputeEnvironmentOr…
Browse files Browse the repository at this point in the history
…der is updated outside Terraform (#12632)

Reference: #8083

Also modernizes the testing.

Previously:

```
=== CONT  TestAccAWSBatchJobQueue_ComputeEnvironments_ExternalOrderUpdate
panic: runtime error: index out of range [1] with length 1

goroutine 1306 [running]:
github.com/terraform-providers/terraform-provider-aws/aws.resourceAwsBatchJobQueueRead(0xc001117c70, 0x62e4cc0, 0xc0003bcf00, 0xc001117c70, 0x0)
  /Users/bflad/src/github.com/terraform-providers/terraform-provider-aws-deux/aws/resource_aws_batch_job_queue.go:110 +0x55a
github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Resource).RefreshWithoutUpgrade(0xc0005df580, 0xc00104ae60, 0x62e4cc0, 0xc0003bcf00, 0xc000c7ec30, 0x0, 0x0)
  /Users/bflad/go/pkg/mod/github.com/hashicorp/terraform-plugin-sdk@v1.8.0/helper/schema/resource.go:455 +0x119
```

Output from acceptance testing:

```
--- PASS: TestAccAWSBatchJobQueue_basic (85.28s)
--- PASS: TestAccAWSBatchJobQueue_disappears (87.06s)
--- PASS: TestAccAWSBatchJobQueue_ComputeEnvironments_ExternalOrderUpdate (98.67s)
--- PASS: TestAccAWSBatchJobQueue_Priority (120.72s)
--- PASS: TestAccAWSBatchJobQueue_State (127.15s)
```
  • Loading branch information
bflad authored Apr 29, 2020
1 parent 2742de4 commit 6facb73
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 126 deletions.
11 changes: 9 additions & 2 deletions aws/resource_aws_batch_job_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"sort"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -105,10 +106,16 @@ func resourceAwsBatchJobQueueRead(d *schema.ResourceData, meta interface{}) erro

d.Set("arn", jq.JobQueueArn)

computeEnvironments := make([]string, len(jq.ComputeEnvironmentOrder))
computeEnvironments := make([]string, 0, len(jq.ComputeEnvironmentOrder))

sort.Slice(jq.ComputeEnvironmentOrder, func(i, j int) bool {
return aws.Int64Value(jq.ComputeEnvironmentOrder[i].Order) < aws.Int64Value(jq.ComputeEnvironmentOrder[j].Order)
})

for _, computeEnvironmentOrder := range jq.ComputeEnvironmentOrder {
computeEnvironments[aws.Int64Value(computeEnvironmentOrder.Order)] = aws.StringValue(computeEnvironmentOrder.ComputeEnvironment)
computeEnvironments = append(computeEnvironments, aws.StringValue(computeEnvironmentOrder.ComputeEnvironment))
}

if err := d.Set("compute_environments", computeEnvironments); err != nil {
return fmt.Errorf("error setting compute_environments: %s", err)
}
Expand Down
Loading

0 comments on commit 6facb73

Please sign in to comment.