-
Notifications
You must be signed in to change notification settings - Fork 9.2k
/
resource_aws_batch_job_queue.go
251 lines (223 loc) · 6.98 KB
/
resource_aws_batch_job_queue.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
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/batch"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceAwsBatchJobQueue() *schema.Resource {
return &schema.Resource{
Create: resourceAwsBatchJobQueueCreate,
Read: resourceAwsBatchJobQueueRead,
Update: resourceAwsBatchJobQueueUpdate,
Delete: resourceAwsBatchJobQueueDelete,
Schema: map[string]*schema.Schema{
"compute_environments": {
Type: schema.TypeList,
Required: true,
MaxItems: 3,
Elem: &schema.Schema{Type: schema.TypeString},
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateBatchName,
},
"priority": {
Type: schema.TypeInt,
Required: true,
},
"state": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{batch.JQStateDisabled, batch.JQStateEnabled}, true),
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsBatchJobQueueCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).batchconn
input := batch.CreateJobQueueInput{
ComputeEnvironmentOrder: createComputeEnvironmentOrder(d.Get("compute_environments").([]interface{})),
JobQueueName: aws.String(d.Get("name").(string)),
Priority: aws.Int64(int64(d.Get("priority").(int))),
State: aws.String(d.Get("state").(string)),
}
name := d.Get("name").(string)
out, err := conn.CreateJobQueue(&input)
if err != nil {
return fmt.Errorf("%s %q", err, name)
}
stateConf := &resource.StateChangeConf{
Pending: []string{batch.JQStatusCreating, batch.JQStatusUpdating},
Target: []string{batch.JQStatusValid},
Refresh: batchJobQueueRefreshStatusFunc(conn, name),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("[WARN] Error waiting for JobQueue state to be \"VALID\": %s", err)
}
arn := *out.JobQueueArn
log.Printf("[DEBUG] JobQueue created: %s", arn)
d.SetId(arn)
return resourceAwsBatchJobQueueRead(d, meta)
}
func resourceAwsBatchJobQueueRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).batchconn
jq, err := getJobQueue(conn, d.Get("name").(string))
if err != nil {
return err
}
if jq == nil {
return fmt.Errorf("[WARN] Error reading JobQueue: \"%s\"", err)
}
d.Set("arn", jq.JobQueueArn)
d.Set("compute_environments", jq.ComputeEnvironmentOrder)
d.Set("name", jq.JobQueueName)
d.Set("priority", jq.Priority)
d.Set("state", jq.State)
return nil
}
func resourceAwsBatchJobQueueUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).batchconn
name := d.Get("name").(string)
updateInput := &batch.UpdateJobQueueInput{
ComputeEnvironmentOrder: createComputeEnvironmentOrder(d.Get("compute_environments").([]interface{})),
JobQueue: aws.String(name),
Priority: aws.Int64(int64(d.Get("priority").(int))),
State: aws.String(d.Get("state").(string)),
}
_, err := conn.UpdateJobQueue(updateInput)
if err != nil {
return err
}
stateConf := &resource.StateChangeConf{
Pending: []string{batch.JQStatusUpdating},
Target: []string{batch.JQStatusValid},
Refresh: batchJobQueueRefreshStatusFunc(conn, name),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return err
}
return resourceAwsBatchJobQueueRead(d, meta)
}
func resourceAwsBatchJobQueueDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).batchconn
name := d.Get("name").(string)
log.Printf("[DEBUG] Disabling Batch Job Queue %s", name)
err := disableBatchJobQueue(name, 10*time.Minute, conn)
if err != nil {
return fmt.Errorf("error disabling Batch Job Queue (%s): %s", name, err)
}
log.Printf("[DEBUG] Deleting Batch Job Queue %s", name)
err = deleteBatchJobQueue(name, 10*time.Minute, conn)
if err != nil {
return fmt.Errorf("error deleting Batch Job Queue (%s): %s", name, err)
}
return nil
}
func createComputeEnvironmentOrder(order []interface{}) (envs []*batch.ComputeEnvironmentOrder) {
for i, env := range order {
envs = append(envs, &batch.ComputeEnvironmentOrder{
Order: aws.Int64(int64(i)),
ComputeEnvironment: aws.String(env.(string)),
})
}
return
}
func deleteBatchJobQueue(jobQueue string, timeout time.Duration, conn *batch.Batch) error {
_, err := conn.DeleteJobQueue(&batch.DeleteJobQueueInput{
JobQueue: aws.String(jobQueue),
})
if err != nil {
return err
}
stateChangeConf := &resource.StateChangeConf{
Pending: []string{batch.JQStateDisabled, batch.JQStatusDeleting},
Target: []string{batch.JQStatusDeleted},
Refresh: batchJobQueueRefreshStatusFunc(conn, jobQueue),
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateChangeConf.WaitForState()
return err
}
func disableBatchJobQueue(jobQueue string, timeout time.Duration, conn *batch.Batch) error {
_, err := conn.UpdateJobQueue(&batch.UpdateJobQueueInput{
JobQueue: aws.String(jobQueue),
State: aws.String(batch.JQStateDisabled),
})
if err != nil {
return err
}
stateChangeConf := &resource.StateChangeConf{
Pending: []string{batch.JQStatusUpdating},
Target: []string{batch.JQStatusValid},
Refresh: batchJobQueueRefreshStatusFunc(conn, jobQueue),
Timeout: timeout,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = stateChangeConf.WaitForState()
return err
}
func getJobQueue(conn *batch.Batch, sn string) (*batch.JobQueueDetail, error) {
describeOpts := &batch.DescribeJobQueuesInput{
JobQueues: []*string{aws.String(sn)},
}
resp, err := conn.DescribeJobQueues(describeOpts)
if err != nil {
return nil, err
}
numJobQueues := len(resp.JobQueues)
switch {
case numJobQueues == 0:
log.Printf("[DEBUG] Job Queue %q is already gone", sn)
return nil, nil
case numJobQueues == 1:
return resp.JobQueues[0], nil
case numJobQueues > 1:
return nil, fmt.Errorf("Multiple Job Queues with name %s", sn)
}
return nil, nil
}
func batchJobQueueRefreshStatusFunc(conn *batch.Batch, sn string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
ce, err := getJobQueue(conn, sn)
if err != nil {
return nil, "failed", err
}
if ce == nil {
return 42, batch.JQStatusDeleted, nil
}
return ce, *ce.Status, nil
}
}
func batchJobQueueRefreshStateFunc(conn *batch.Batch, sn string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
ce, err := getJobQueue(conn, sn)
if err != nil {
return nil, "failed", err
}
if ce == nil {
return 42, batch.JQStateDisabled, nil
}
return ce, *ce.State, nil
}
}