Skip to content

Commit

Permalink
Merge pull request hashicorp#19505 from hashicorp/b-aws_batch_job_def…
Browse files Browse the repository at this point in the history
…inition-attempt_duration_seconds.null

r/aws_batch_job_definition: Don't crash when setting `timeout.attempt_duration_seconds` to `null`
  • Loading branch information
ewbankkit authored May 26, 2021
2 parents ed1883d + 7fb917e commit bb23961
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .changelog/19505.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_batch_job_definition: Don't crash when setting `timeout.attempt_duration_seconds` to `null`
```
45 changes: 28 additions & 17 deletions aws/resource_aws_batch_job_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ func resourceAwsBatchJobDefinitionCreate(d *schema.ResourceData, meta interface{
input.Tags = tags.IgnoreAws().BatchTags()
}

if v, ok := d.GetOk("timeout"); ok {
input.Timeout = expandJobDefinitionTimeout(v.([]interface{}))
if v, ok := d.GetOk("timeout"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input.Timeout = expandBatchJobTimeout(v.([]interface{})[0].(map[string]interface{}))
}

output, err := conn.RegisterJobDefinition(input)
Expand Down Expand Up @@ -278,8 +278,12 @@ func resourceAwsBatchJobDefinitionRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("error setting tags_all: %w", err)
}

if err := d.Set("timeout", flattenBatchJobTimeout(jobDefinition.Timeout)); err != nil {
return fmt.Errorf("error setting timeout: %w", err)
if jobDefinition.Timeout != nil {
if err := d.Set("timeout", []interface{}{flattenBatchJobTimeout(jobDefinition.Timeout)}); err != nil {
return fmt.Errorf("error setting timeout: %w", err)
}
} else {
d.Set("timeout", nil)
}

d.Set("revision", jobDefinition.Revision)
Expand Down Expand Up @@ -488,23 +492,30 @@ func flattenBatchEvaluateOnExits(apiObjects []*batch.EvaluateOnExit) []interface
return tfList
}

func expandJobDefinitionTimeout(item []interface{}) *batch.JobTimeout {
timeout := &batch.JobTimeout{}
data := item[0].(map[string]interface{})
func expandBatchJobTimeout(tfMap map[string]interface{}) *batch.JobTimeout {
if tfMap == nil {
return nil
}

apiObject := &batch.JobTimeout{}

if v, ok := data["attempt_duration_seconds"].(int); ok && v >= 60 {
timeout.AttemptDurationSeconds = aws.Int64(int64(v))
if v, ok := tfMap["attempt_duration_seconds"].(int); ok && v != 0 {
apiObject.AttemptDurationSeconds = aws.Int64(int64(v))
}

return timeout
return apiObject
}

func flattenBatchJobTimeout(item *batch.JobTimeout) []map[string]interface{} {
data := []map[string]interface{}{}
if item != nil && item.AttemptDurationSeconds != nil {
data = append(data, map[string]interface{}{
"attempt_duration_seconds": int(aws.Int64Value(item.AttemptDurationSeconds)),
})
func flattenBatchJobTimeout(apiObject *batch.JobTimeout) map[string]interface{} {
if apiObject == nil {
return nil
}
return data

tfMap := map[string]interface{}{}

if v := apiObject.AttemptDurationSeconds; v != nil {
tfMap["attempt_duration_seconds"] = aws.Int64Value(v)
}

return tfMap
}

0 comments on commit bb23961

Please sign in to comment.