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

Add support for AWS Batch job definition timeout #4386

Merged
merged 7 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
69 changes: 61 additions & 8 deletions aws/resource_aws_batch_job_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,24 @@ 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),
},
},
},
},
"timeout": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"attempt_duration_seconds": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntAtLeast(60),
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the minimum accepted value is 60 we can add plan time validation via: ValidateFunc: validation.IntAtLeast(60), 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Thanks for pointing that out.

},
},
Expand Down Expand Up @@ -99,6 +115,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)
Expand All @@ -122,7 +142,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))

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
Expand Down Expand Up @@ -195,17 +223,42 @@ func expandJobDefinitionParameters(params map[string]interface{}) map[string]*st
}

func expandJobDefinitionRetryStrategy(item []interface{}) *batch.RetryStrategy {
retryStrategy := &batch.RetryStrategy{}
data := item[0].(map[string]interface{})

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 && item.Attempts != nil {
data = append(data, map[string]interface{}{
"attempts": int(aws.Int64Value(item.Attempts)),
})
}
return data
}

func expandJobDefinitionTimeout(item []interface{}) *batch.JobTimeout {
timeout := &batch.JobTimeout{}
data := item[0].(map[string]interface{})
return &batch.RetryStrategy{
Attempts: aws.Int64(int64(data["attempts"].(int))),

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

return timeout
}

func flattenRetryStrategy(item *batch.RetryStrategy) []map[string]interface{} {
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{}{
"attempts": item.Attempts,
"attempt_duration_seconds": int(aws.Int64Value(item.AttemptDurationSeconds)),
})
}
return data
Expand Down
6 changes: 6 additions & 0 deletions aws/resource_aws_batch_job_definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -185,6 +188,9 @@ resource "aws_batch_job_definition" "test" {
retry_strategy = {
attempts = 1
}
timeout = {
attempt_duration_seconds = 60
}
container_properties = <<CONTAINER_PROPERTIES
{
"command": ["ls", "-la"],
Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/batch_job_definition.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ The following arguments are supported:
* `parameters` - (Optional) Specifies the parameter substitution placeholders to set in the job definition.
* `retry_strategy` - (Optional) Specifies the retry strategy to use for failed jobs that are submitted with this job definition.
Maximum number of `retry_strategy` is `1`. Defined below.
* `timeout` - (Optional) Specifies the timeout for jobs so that if a job runs longer, AWS Batch terminates the job. Maximum number of `timeout` is `1`. Defined below.
* `type` - (Required) The type of job definition. Must be `container`

## retry_strategy
Expand All @@ -70,6 +71,12 @@ The following arguments are supported:

* `attempts` - (Required) 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.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still says Required (and likewise for attempts above). Both are now Optional


## Attribute Reference

The following attributes are exported:
Expand Down