forked from hashicorp/terraform-provider-aws
/
resource_aws_batch_job_definition.go
211 lines (188 loc) · 5.44 KB
/
resource_aws_batch_job_definition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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 {
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
}