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 batch_job_definition and batch_job_queue #1710

Merged
merged 5 commits into from
Sep 28, 2017
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
2 changes: 2 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ func Provider() terraform.ResourceProvider {
"aws_wafregional_byte_match_set": resourceAwsWafRegionalByteMatchSet(),
"aws_wafregional_ipset": resourceAwsWafRegionalIPSet(),
"aws_batch_compute_environment": resourceAwsBatchComputeEnvironment(),
"aws_batch_job_definition": resourceAwsBatchJobDefinition(),
"aws_batch_job_queue": resourceAwsBatchJobQueue(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
2 changes: 1 addition & 1 deletion aws/resource_aws_batch_compute_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func resourceAwsBatchComputeEnvironment() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateBatchComputeEnvironmentName,
ValidateFunc: validateBatchName,
},
"compute_resources": {
Type: schema.TypeList,
Expand Down
10 changes: 10 additions & 0 deletions aws/resource_aws_batch_compute_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ resource "aws_batch_compute_environment" "ec2" {
}
service_role = "${aws_iam_role.aws_batch_service_role.arn}"
type = "MANAGED"
depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"]
}
`, rInt)
}
Expand Down Expand Up @@ -423,6 +424,7 @@ resource "aws_batch_compute_environment" "ec2" {
}
service_role = "${aws_iam_role.aws_batch_service_role.arn}"
type = "MANAGED"
depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"]
}
`, rInt)
}
Expand Down Expand Up @@ -450,6 +452,7 @@ resource "aws_batch_compute_environment" "spot" {
}
service_role = "${aws_iam_role.aws_batch_service_role.arn}"
type = "MANAGED"
depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"]
}
`, rInt)
}
Expand All @@ -460,6 +463,7 @@ resource "aws_batch_compute_environment" "unmanaged" {
compute_environment_name = "tf_acc_test_%d"
service_role = "${aws_iam_role.aws_batch_service_role.arn}"
type = "UNMANAGED"
depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"]
}
`, rInt)
}
Expand All @@ -485,6 +489,7 @@ resource "aws_batch_compute_environment" "ec2" {
}
service_role = "${aws_iam_role.aws_batch_service_role.arn}"
type = "MANAGED"
depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"]
}
`, rInt)
}
Expand All @@ -511,6 +516,7 @@ resource "aws_batch_compute_environment" "ec2" {
}
service_role = "${aws_iam_role.aws_batch_service_role.arn}"
type = "MANAGED"
depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"]
}
`, rInt)
}
Expand All @@ -536,6 +542,7 @@ resource "aws_batch_compute_environment" "ec2" {
}
service_role = "${aws_iam_role.aws_batch_service_role.arn}"
type = "MANAGED"
depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"]
}
`, rInt)
}
Expand All @@ -546,6 +553,7 @@ resource "aws_batch_compute_environment" "ec2" {
compute_environment_name = "tf_acc_test_%d"
service_role = "${aws_iam_role.aws_batch_service_role.arn}"
type = "MANAGED"
depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"]
}
`, rInt)
}
Expand All @@ -571,6 +579,7 @@ resource "aws_batch_compute_environment" "unmanaged" {
}
service_role = "${aws_iam_role.aws_batch_service_role.arn}"
type = "UNMANAGED"
depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"]
}
`, rInt)
}
Expand All @@ -596,6 +605,7 @@ resource "aws_batch_compute_environment" "ec2" {
}
service_role = "${aws_iam_role.aws_batch_service_role.arn}"
type = "MANAGED"
depends_on = ["aws_iam_role_policy_attachment.aws_batch_service_role"]
}
`, rInt)
}
211 changes: 211 additions & 0 deletions aws/resource_aws_batch_job_definition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package aws

import (
"fmt"

"encoding/json"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/batch"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceAwsBatchJobDefinition() *schema.Resource {
return &schema.Resource{
Create: resourceAwsBatchJobDefinitionCreate,
Read: resourceAwsBatchJobDefinitionRead,
Delete: resourceAwsBatchJobDefinitionDelete,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateBatchName,
},
"container_properties": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
StateFunc: func(v interface{}) string {
Copy link
Member

Choose a reason for hiding this comment

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

Did you add this StateFunc just to suppress the JSON formatting differences in plan? If so, then we have a little bit more elegant solution which doesn't hide the JSON from the plan:
https://github.com/terraform-providers/terraform-provider-aws/blob/90d698c66db80d676e2db197be97c1a9977e5393/aws/resource_aws_cloudwatch_dashboard.go#L36-L40

Whether the user wants to see full JSON in the plan is questionable, but it seems some folks find it useful and it would be nice to avoid breaking change later on, like here.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, that was the intent. Thanks for pointing me to that function.

json, _ := normalizeJsonString(v)
return json
},
DiffSuppressFunc: suppressEquivalentJsonDiffs,
ValidateFunc: validateAwsBatchJobContainerProperties,
},
"parameters": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: schema.TypeString,
},
"retry_strategy": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"attempts": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{batch.JobDefinitionTypeContainer}, true),
},
"revision": {
Type: schema.TypeInt,
Computed: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceAwsBatchJobDefinitionCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).batchconn
name := d.Get("name").(string)

input := &batch.RegisterJobDefinitionInput{
JobDefinitionName: aws.String(name),
Type: aws.String(d.Get("type").(string)),
}

if v, ok := d.GetOk("container_properties"); ok {
props, err := expandBatchJobContainerProperties(v.(string))
if err != nil {
return fmt.Errorf("%s %q", err, name)
}
input.ContainerProperties = props
}

if v, ok := d.GetOk("parameters"); ok {
input.Parameters = expandJobDefinitionParameters(v.(map[string]interface{}))
}

if v, ok := d.GetOk("retry_strategy"); ok {
input.RetryStrategy = expandJobDefinitionRetryStrategy(v.([]interface{}))
}

out, err := conn.RegisterJobDefinition(input)
if err != nil {
return fmt.Errorf("%s %q", err, name)
}
d.SetId(*out.JobDefinitionArn)
d.Set("arn", out.JobDefinitionArn)
return resourceAwsBatchJobDefinitionRead(d, meta)
}

func resourceAwsBatchJobDefinitionRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).batchconn
arn := d.Get("arn").(string)
job, err := getJobDefinition(conn, arn)
if err != nil {
return fmt.Errorf("%s %q", err, arn)
}
if job == nil {
d.SetId("")
return nil
}
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("revision", job.Revision)
d.Set("type", job.Type)
return nil
}

func resourceAwsBatchJobDefinitionDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).batchconn
arn := d.Get("arn").(string)
_, err := conn.DeregisterJobDefinition(&batch.DeregisterJobDefinitionInput{
JobDefinition: aws.String(arn),
})
if err != nil {
return fmt.Errorf("%s %q", err, arn)
}
d.SetId("")
return nil
}

func getJobDefinition(conn *batch.Batch, arn string) (*batch.JobDefinition, error) {
describeOpts := &batch.DescribeJobDefinitionsInput{
JobDefinitions: []*string{aws.String(arn)},
}
resp, err := conn.DescribeJobDefinitions(describeOpts)
if err != nil {
return nil, err
}

numJobDefinitions := len(resp.JobDefinitions)
switch {
case numJobDefinitions == 0:
return nil, nil
case numJobDefinitions == 1:
if *resp.JobDefinitions[0].Status == "ACTIVE" {
return resp.JobDefinitions[0], nil
}
return nil, nil
case numJobDefinitions > 1:
return nil, fmt.Errorf("Multiple Job Definitions with name %s", arn)
}
return nil, nil
}

func validateAwsBatchJobContainerProperties(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
_, err := expandBatchJobContainerProperties(value)
if err != nil {
errors = append(errors, fmt.Errorf("AWS Batch Job container_properties is invalid: %s", err))
}
return
}

func expandBatchJobContainerProperties(rawProps string) (*batch.ContainerProperties, error) {
var props *batch.ContainerProperties

err := json.Unmarshal([]byte(rawProps), &props)
if err != nil {
return nil, fmt.Errorf("Error decoding JSON: %s", err)
}

return props, nil
}

func expandJobDefinitionParameters(params map[string]interface{}) map[string]*string {
var jobParams = make(map[string]*string)
for k, v := range params {
jobParams[k] = aws.String(v.(string))
}

return jobParams
}

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

func flattenRetryStrategy(item *batch.RetryStrategy) []map[string]interface{} {
data := []map[string]interface{}{}
if item != nil {
data = append(data, map[string]interface{}{
"attempts": item.Attempts,
})
}
return data
}
Loading