forked from hashicorp/terraform-provider-azurerm
/
resource_arm_scheduler_job_collection.go
290 lines (235 loc) · 9.16 KB
/
resource_arm_scheduler_job_collection.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// nolint: megacheck
// entire automation SDK has been depreciated in v21.3 in favor of logic apps, an entirely different service.
package azurerm
import (
"fmt"
"log"
"regexp"
"github.com/Azure/azure-sdk-for-go/services/scheduler/mgmt/2016-03-01/scheduler"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func resourceArmSchedulerJobCollection() *schema.Resource {
return &schema.Resource{
Create: resourceArmSchedulerJobCollectionCreateUpdate,
Read: resourceArmSchedulerJobCollectionRead,
Update: resourceArmSchedulerJobCollectionCreateUpdate,
Delete: resourceArmSchedulerJobCollectionDelete,
DeprecationMessage: "Scheduler Job Collection has been deprecated in favour of Logic Apps - more information can be found at https://docs.microsoft.com/en-us/azure/scheduler/migrate-from-scheduler-to-logic-apps",
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile("^[a-zA-Z][-_a-zA-Z0-9]{0,99}$"),
"Job Collection Name name must be 1 - 100 characters long, start with a letter and contain only letters, numbers, hyphens and underscores.",
),
},
"location": locationSchema(),
"resource_group_name": resourceGroupNameSchema(),
"tags": tagsSchema(),
"sku": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
ValidateFunc: validation.StringInSlice([]string{
string(scheduler.Free),
string(scheduler.Standard),
string(scheduler.P10Premium),
string(scheduler.P20Premium),
}, true),
},
//optional
"state": {
Type: schema.TypeString,
Optional: true,
Default: string(scheduler.Enabled),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
ValidateFunc: validation.StringInSlice([]string{
string(scheduler.Enabled),
string(scheduler.Suspended),
string(scheduler.Disabled),
}, true),
},
"quota": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
//max_job_occurrence doesn't seem to do anything and always remains empty
"max_job_count": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntAtLeast(0),
},
"max_recurrence_frequency": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
ValidateFunc: validation.StringInSlice([]string{
string(scheduler.Minute),
string(scheduler.Hour),
string(scheduler.Day),
string(scheduler.Week),
string(scheduler.Month),
}, true),
},
// API documentation states the MaxRecurrence.Interval "Gets or sets the interval between retries."
// however it does appear it is the max interval allowed for recurrences
"max_retry_interval": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Deprecated: "Renamed to `max_recurrence_interval` to match azure",
ValidateFunc: validation.IntAtLeast(1), //changes depending on the frequency, unknown maximums
},
"max_recurrence_interval": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntAtLeast(1), //changes depending on the frequency, unknown maximums
},
},
},
},
},
}
}
func resourceArmSchedulerJobCollectionCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).schedulerJobCollectionsClient
ctx := meta.(*ArmClient).StopContext
name := d.Get("name").(string)
location := azureRMNormalizeLocation(d.Get("location").(string))
resourceGroup := d.Get("resource_group_name").(string)
tags := d.Get("tags").(map[string]interface{})
log.Printf("[DEBUG] Creating/updating Scheduler Job Collection %q (resource group %q)", name, resourceGroup)
collection := scheduler.JobCollectionDefinition{
Location: utils.String(location),
Tags: expandTags(tags),
Properties: &scheduler.JobCollectionProperties{
Sku: &scheduler.Sku{
Name: scheduler.SkuDefinition(d.Get("sku").(string)),
},
},
}
if state, ok := d.Get("state").(string); ok {
collection.Properties.State = scheduler.JobCollectionState(state)
}
collection.Properties.Quota = expandAzureArmSchedulerJobCollectionQuota(d)
//create job collection
collection, err := client.CreateOrUpdate(ctx, resourceGroup, name, collection)
if err != nil {
return fmt.Errorf("Error creating/updating Scheduler Job Collection %q (Resource Group %q): %+v", name, resourceGroup, err)
}
//ensure collection actually exists and we have the correct ID
collection, err = client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error reading Scheduler Job Collection %q after create/update (Resource Group %q): %+v", name, resourceGroup, err)
}
d.SetId(*collection.ID)
return resourceArmSchedulerJobCollectionRead(d, meta)
}
func resourceArmSchedulerJobCollectionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).schedulerJobCollectionsClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
name := id.Path["jobCollections"]
resourceGroup := id.ResourceGroup
log.Printf("[DEBUG] Reading Scheduler Job Collection %q (resource group %q)", name, resourceGroup)
collection, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(collection.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Scheduler Job Collection %q (Resource Group %q): %+v", name, resourceGroup, err)
}
//standard properties
d.Set("name", collection.Name)
d.Set("resource_group_name", resourceGroup)
if location := collection.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}
flattenAndSetTags(d, collection.Tags)
//resource specific
if properties := collection.Properties; properties != nil {
if sku := properties.Sku; sku != nil {
d.Set("sku", sku.Name)
}
d.Set("state", string(properties.State))
if err := d.Set("quota", flattenAzureArmSchedulerJobCollectionQuota(properties.Quota)); err != nil {
return fmt.Errorf("Error setting quota for Job Collection %q (Resource Group %q): %+v", *collection.Name, resourceGroup, err)
}
}
return nil
}
func resourceArmSchedulerJobCollectionDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).schedulerJobCollectionsClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
name := id.Path["jobCollections"]
resourceGroup := id.ResourceGroup
log.Printf("[DEBUG] Deleting Scheduler Job Collection %q (resource group %q)", name, resourceGroup)
future, err := client.Delete(ctx, resourceGroup, name)
if err != nil {
if !response.WasNotFound(future.Response()) {
return fmt.Errorf("Error issuing delete request for Scheduler Job Collection %q (Resource Group %q): %+v", name, resourceGroup, err)
}
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
if !response.WasNotFound(future.Response()) {
return fmt.Errorf("Error waiting for deletion of Scheduler Job Collection %q (Resource Group %q): %+v", name, resourceGroup, err)
}
}
return nil
}
func expandAzureArmSchedulerJobCollectionQuota(d *schema.ResourceData) *scheduler.JobCollectionQuota {
if qb, ok := d.Get("quota").([]interface{}); ok && len(qb) > 0 {
quota := scheduler.JobCollectionQuota{
MaxRecurrence: &scheduler.JobMaxRecurrence{},
}
quotaBlock := qb[0].(map[string]interface{})
if v, ok := quotaBlock["max_job_count"].(int); ok {
quota.MaxJobCount = utils.Int32(int32(v))
}
if v, ok := quotaBlock["max_recurrence_frequency"].(string); ok {
quota.MaxRecurrence.Frequency = scheduler.RecurrenceFrequency(v)
}
if v, ok := quotaBlock["max_recurrence_interval"].(int); ok && v > 0 {
quota.MaxRecurrence.Interval = utils.Int32(int32(v))
} else if v, ok := quotaBlock["max_retry_interval"].(int); ok && v > 0 { //todo remove once max_retry_interval is removed
quota.MaxRecurrence.Interval = utils.Int32(int32(v))
}
return "a
}
return nil
}
func flattenAzureArmSchedulerJobCollectionQuota(quota *scheduler.JobCollectionQuota) []interface{} {
if quota == nil {
return nil
}
quotaBlock := make(map[string]interface{})
if v := quota.MaxJobCount; v != nil {
quotaBlock["max_job_count"] = *v
}
if recurrence := quota.MaxRecurrence; recurrence != nil {
if v := recurrence.Interval; v != nil {
quotaBlock["max_recurrence_interval"] = *v
quotaBlock["max_retry_interval"] = *v //todo remove once max_retry_interval is retired
}
quotaBlock["max_recurrence_frequency"] = string(recurrence.Frequency)
}
return []interface{}{quotaBlock}
}