From 1857746eb6c25780e97f627918a7461fef527a08 Mon Sep 17 00:00:00 2001 From: Hector Castro Date: Sat, 28 Apr 2018 13:42:07 -0400 Subject: [PATCH 1/7] Add support for AWS Batch job definition timeout Update the existing `aws_batch_job_definition` resource so that it supports the newly added timeout feature. The timeout configuration is supplied through a `timeout` block inside `aws_batch_job_definition` with a single `attempt_duration_seconds` attribute. --- aws/resource_aws_batch_job_definition.go | 36 +++++++++++++++++++ aws/resource_aws_batch_job_definition_test.go | 6 ++++ .../docs/r/batch_job_definition.html.markdown | 7 ++++ 3 files changed, 49 insertions(+) diff --git a/aws/resource_aws_batch_job_definition.go b/aws/resource_aws_batch_job_definition.go index 1bef71db7998..ff99004b1f56 100644 --- a/aws/resource_aws_batch_job_definition.go +++ b/aws/resource_aws_batch_job_definition.go @@ -56,6 +56,20 @@ func resourceAwsBatchJobDefinition() *schema.Resource { }, }, }, + "timeout": { + Type: schema.TypeList, + Optional: true, + ForceNew: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "attempt_duration_seconds": { + Type: schema.TypeInt, + Required: true, + }, + }, + }, + }, "type": { Type: schema.TypeString, Required: true, @@ -99,6 +113,10 @@ func resourceAwsBatchJobDefinitionCreate(d *schema.ResourceData, meta interface{ input.RetryStrategy = expandJobDefinitionRetryStrategy(v.([]interface{})) } + if v, ok := d.GetOk("timeout"); ok { + input.Timeout = expandJobDefinitionTimeout(v.([]interface{})) + } + out, err := conn.RegisterJobDefinition(input) if err != nil { return fmt.Errorf("%s %q", err, name) @@ -123,6 +141,7 @@ func resourceAwsBatchJobDefinitionRead(d *schema.ResourceData, meta interface{}) d.Set("container_properties", job.ContainerProperties) d.Set("parameters", aws.StringValueMap(job.Parameters)) d.Set("retry_strategy", flattenRetryStrategy(job.RetryStrategy)) + d.Set("timeout", flattenTimeout(job.Timeout)) d.Set("revision", job.Revision) d.Set("type", job.Type) return nil @@ -210,3 +229,20 @@ func flattenRetryStrategy(item *batch.RetryStrategy) []map[string]interface{} { } return data } + +func expandJobDefinitionTimeout(item []interface{}) *batch.JobTimeout { + data := item[0].(map[string]interface{}) + return &batch.JobTimeout{ + AttemptDurationSeconds: aws.Int64(int64(data["attempt_duration_seconds"].(int))), + } +} + +func flattenTimeout(item *batch.JobTimeout) []map[string]interface{} { + data := []map[string]interface{}{} + if item != nil { + data = append(data, map[string]interface{}{ + "attempt_duration_seconds": item.AttemptDurationSeconds, + }) + } + return data +} diff --git a/aws/resource_aws_batch_job_definition_test.go b/aws/resource_aws_batch_job_definition_test.go index eba4d9ca3de5..293bc6fb813e 100644 --- a/aws/resource_aws_batch_job_definition_test.go +++ b/aws/resource_aws_batch_job_definition_test.go @@ -24,6 +24,9 @@ func TestAccAWSBatchJobDefinition_basic(t *testing.T) { RetryStrategy: &batch.RetryStrategy{ Attempts: aws.Int64(int64(1)), }, + Timeout: &batch.JobTimeout{ + AttemptDurationSeconds: aws.Int64(int64(60)), + }, ContainerProperties: &batch.ContainerProperties{ Command: []*string{aws.String("ls"), aws.String("-la")}, Environment: []*batch.KeyValuePair{ @@ -185,6 +188,9 @@ resource "aws_batch_job_definition" "test" { retry_strategy = { attempts = 1 } + timeout = { + attempt_duration_seconds = 60 + } container_properties = < Date: Mon, 30 Apr 2018 20:00:41 -0400 Subject: [PATCH 2/7] Add validation for minimum attempt_duration_seconds --- aws/resource_aws_batch_job_definition.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_batch_job_definition.go b/aws/resource_aws_batch_job_definition.go index ff99004b1f56..f5ddd12e2c60 100644 --- a/aws/resource_aws_batch_job_definition.go +++ b/aws/resource_aws_batch_job_definition.go @@ -64,8 +64,9 @@ func resourceAwsBatchJobDefinition() *schema.Resource { Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "attempt_duration_seconds": { - Type: schema.TypeInt, - Required: true, + Type: schema.TypeInt, + Required: true, + ValidateFunc: validation.IntAtLeast(60), }, }, }, From e2391079c68bac8c93a2eac9a396ad50bada8462 Mon Sep 17 00:00:00 2001 From: Hector Castro Date: Mon, 30 Apr 2018 20:01:35 -0400 Subject: [PATCH 3/7] Add error checking to retry_strategy and timeout --- aws/resource_aws_batch_job_definition.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/aws/resource_aws_batch_job_definition.go b/aws/resource_aws_batch_job_definition.go index f5ddd12e2c60..d5bdd530dd6a 100644 --- a/aws/resource_aws_batch_job_definition.go +++ b/aws/resource_aws_batch_job_definition.go @@ -141,8 +141,15 @@ func resourceAwsBatchJobDefinitionRead(d *schema.ResourceData, meta interface{}) d.Set("arn", job.JobDefinitionArn) d.Set("container_properties", job.ContainerProperties) d.Set("parameters", aws.StringValueMap(job.Parameters)) - d.Set("retry_strategy", flattenRetryStrategy(job.RetryStrategy)) - d.Set("timeout", flattenTimeout(job.Timeout)) + + if err := d.Set("retry_strategy", flattenBatchRetryStrategy(job.RetryStrategy)); err != nil { + return fmt.Errorf("error setting retry_strategy: %s", err) + } + + if err := d.Set("timeout", flattenBatchJobTimeout(job.Timeout)); err != nil { + return fmt.Errorf("error setting timeout: %s", err) + } + d.Set("revision", job.Revision) d.Set("type", job.Type) return nil @@ -221,7 +228,7 @@ func expandJobDefinitionRetryStrategy(item []interface{}) *batch.RetryStrategy { } } -func flattenRetryStrategy(item *batch.RetryStrategy) []map[string]interface{} { +func flattenBatchRetryStrategy(item *batch.RetryStrategy) []map[string]interface{} { data := []map[string]interface{}{} if item != nil { data = append(data, map[string]interface{}{ @@ -238,7 +245,7 @@ func expandJobDefinitionTimeout(item []interface{}) *batch.JobTimeout { } } -func flattenTimeout(item *batch.JobTimeout) []map[string]interface{} { +func flattenBatchJobTimeout(item *batch.JobTimeout) []map[string]interface{} { data := []map[string]interface{}{} if item != nil { data = append(data, map[string]interface{}{ From 61300baa4eb3af217eaa941a849ac496ceb2ca8c Mon Sep 17 00:00:00 2001 From: Hector Castro Date: Mon, 30 Apr 2018 20:03:03 -0400 Subject: [PATCH 4/7] Wrap attempts and attempt_duration_seconds in aws.Int64Value --- aws/resource_aws_batch_job_definition.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_batch_job_definition.go b/aws/resource_aws_batch_job_definition.go index d5bdd530dd6a..12d011d38905 100644 --- a/aws/resource_aws_batch_job_definition.go +++ b/aws/resource_aws_batch_job_definition.go @@ -232,7 +232,7 @@ func flattenBatchRetryStrategy(item *batch.RetryStrategy) []map[string]interface data := []map[string]interface{}{} if item != nil { data = append(data, map[string]interface{}{ - "attempts": item.Attempts, + "attempts": int(aws.Int64Value(item.Attempts)), }) } return data @@ -249,7 +249,7 @@ func flattenBatchJobTimeout(item *batch.JobTimeout) []map[string]interface{} { data := []map[string]interface{}{} if item != nil { data = append(data, map[string]interface{}{ - "attempt_duration_seconds": item.AttemptDurationSeconds, + "attempt_duration_seconds": int(aws.Int64Value(item.AttemptDurationSeconds)), }) } return data From 773f023c2d1a1455cf4e848e09b4dda1f9420e04 Mon Sep 17 00:00:00 2001 From: Hector Castro Date: Tue, 1 May 2018 06:59:59 -0400 Subject: [PATCH 5/7] Make attempts and attempt_duration_seconds optional Also, add validation to `attempts`. --- aws/resource_aws_batch_job_definition.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_batch_job_definition.go b/aws/resource_aws_batch_job_definition.go index 12d011d38905..5e4056759db4 100644 --- a/aws/resource_aws_batch_job_definition.go +++ b/aws/resource_aws_batch_job_definition.go @@ -50,8 +50,9 @@ func resourceAwsBatchJobDefinition() *schema.Resource { Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "attempts": { - Type: schema.TypeInt, - Required: true, + Type: schema.TypeInt, + Optional: true, + ValidateFunc: validation.IntBetween(1, 10), }, }, }, @@ -65,7 +66,7 @@ func resourceAwsBatchJobDefinition() *schema.Resource { Schema: map[string]*schema.Schema{ "attempt_duration_seconds": { Type: schema.TypeInt, - Required: true, + Optional: true, ValidateFunc: validation.IntAtLeast(60), }, }, From 23841c826ec18ae1c5e34d46a2a07e307aeb2994 Mon Sep 17 00:00:00 2001 From: Hector Castro Date: Tue, 1 May 2018 07:22:35 -0400 Subject: [PATCH 6/7] Add additional error checking to flatten and expand For timeout and retry strategy. --- aws/resource_aws_batch_job_definition.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/aws/resource_aws_batch_job_definition.go b/aws/resource_aws_batch_job_definition.go index 5e4056759db4..68b1e9f5d05c 100644 --- a/aws/resource_aws_batch_job_definition.go +++ b/aws/resource_aws_batch_job_definition.go @@ -223,15 +223,19 @@ func expandJobDefinitionParameters(params map[string]interface{}) map[string]*st } func expandJobDefinitionRetryStrategy(item []interface{}) *batch.RetryStrategy { + retryStrategy := &batch.RetryStrategy{} data := item[0].(map[string]interface{}) - return &batch.RetryStrategy{ - Attempts: aws.Int64(int64(data["attempts"].(int))), + + if v, ok := data["attempts"].(int); ok && v > 0 && v <= 10 { + retryStrategy.Attempts = aws.Int64(int64(v)) } + + return retryStrategy } func flattenBatchRetryStrategy(item *batch.RetryStrategy) []map[string]interface{} { data := []map[string]interface{}{} - if item != nil { + if item != nil && item.Attempts != nil { data = append(data, map[string]interface{}{ "attempts": int(aws.Int64Value(item.Attempts)), }) @@ -240,15 +244,19 @@ func flattenBatchRetryStrategy(item *batch.RetryStrategy) []map[string]interface } func expandJobDefinitionTimeout(item []interface{}) *batch.JobTimeout { + timeout := &batch.JobTimeout{} data := item[0].(map[string]interface{}) - return &batch.JobTimeout{ - AttemptDurationSeconds: aws.Int64(int64(data["attempt_duration_seconds"].(int))), + + if v, ok := data["attempt_duration_seconds"].(int); ok && v >= 60 { + timeout.AttemptDurationSeconds = aws.Int64(int64(v)) } + + return timeout } func flattenBatchJobTimeout(item *batch.JobTimeout) []map[string]interface{} { data := []map[string]interface{}{} - if item != nil { + if item != nil && item.AttemptDurationSeconds != nil { data = append(data, map[string]interface{}{ "attempt_duration_seconds": int(aws.Int64Value(item.AttemptDurationSeconds)), }) From 9fe5e66667c38b455d34aaf1b4f7a0996e35f35e Mon Sep 17 00:00:00 2001 From: Hector Castro Date: Tue, 1 May 2018 07:53:39 -0400 Subject: [PATCH 7/7] Update docs to reflect optional status --- website/docs/r/batch_job_definition.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/docs/r/batch_job_definition.html.markdown b/website/docs/r/batch_job_definition.html.markdown index 233ef9ff0b19..d284ef98bb46 100644 --- a/website/docs/r/batch_job_definition.html.markdown +++ b/website/docs/r/batch_job_definition.html.markdown @@ -69,13 +69,13 @@ The following arguments are supported: `retry_strategy` supports the following: -* `attempts` - (Required) The number of times to move a job to the `RUNNABLE` status. You may specify between `1` and `10` attempts. +* `attempts` - (Optional) The number of times to move a job to the `RUNNABLE` status. You may specify between `1` and `10` attempts. ## timeout `timeout` supports the following: -* `attempt_duration_seconds` - (Required) The time duration in seconds after which AWS Batch terminates your jobs if they have not finished. The minimum value for the timeout is `60` seconds. +* `attempt_duration_seconds` - (Optional) The time duration in seconds after which AWS Batch terminates your jobs if they have not finished. The minimum value for the timeout is `60` seconds. ## Attribute Reference