forked from hashicorp/terraform-provider-aws
/
resource_aws_batch_job_definition.go
267 lines (233 loc) · 7.03 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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/structure"
"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, _ := structure.NormalizeJsonString(v)
return json
},
DiffSuppressFunc: suppressEquivalentJsonDiffs,
ValidateFunc: validateAwsBatchJobContainerProperties,
},
"parameters": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: 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,
Optional: true,
ForceNew: 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,
ForceNew: true,
ValidateFunc: validation.IntAtLeast(60),
},
},
},
},
"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{}))
}
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)
}
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))
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
}
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)
}
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 {
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{})
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 && item.AttemptDurationSeconds != nil {
data = append(data, map[string]interface{}{
"attempt_duration_seconds": int(aws.Int64Value(item.AttemptDurationSeconds)),
})
}
return data
}