Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws_batch_job_definition: Don't crash when setting timeout.attempt_duration_seconds to null #19505

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}