-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
resource_arm_automation_schedule.go
479 lines (415 loc) · 15.7 KB
/
resource_arm_automation_schedule.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
package azurerm
import (
"fmt"
"log"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/services/automation/mgmt/2015-10-31/automation"
"github.com/Azure/go-autorest/autorest/date"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/set"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func resourceArmAutomationSchedule() *schema.Resource {
return &schema.Resource{
Create: resourceArmAutomationScheduleCreateUpdate,
Read: resourceArmAutomationScheduleRead,
Update: resourceArmAutomationScheduleCreateUpdate,
Delete: resourceArmAutomationScheduleDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateAutomationScheduleName(),
},
"resource_group_name": azure.SchemaResourceGroupName(),
//this is AutomationAccountName in the SDK
"account_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Deprecated: "account_name has been renamed to automation_account_name for clarity and to match the azure API",
ConflictsWith: []string{"automation_account_name"},
ValidateFunc: azure.ValidateAutomationAccountName(),
},
"automation_account_name": {
Type: schema.TypeString,
Optional: true, //todo change to required once account_name has been removed
Computed: true,
//ForceNew: true, //todo this needs to come back once account_name has been removed
ConflictsWith: []string{"account_name"},
ValidateFunc: azure.ValidateAutomationAccountName(),
},
"frequency": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice([]string{
string(automation.Day),
string(automation.Hour),
string(automation.Month),
string(automation.OneTime),
string(automation.Week),
}, true),
},
//ignored when frequency is `OneTime`
"interval": {
Type: schema.TypeInt,
Optional: true,
Computed: true, //defaults to 1 if frequency is not OneTime
ValidateFunc: validation.IntBetween(1, 100),
},
"start_time": {
Type: schema.TypeString,
Optional: true,
Computed: true,
DiffSuppressFunc: suppress.RFC3339Time,
ValidateFunc: validate.RFC3339DateInFutureBy(time.Duration(5) * time.Minute),
//defaults to now + 7 minutes in create function if not set
},
"expiry_time": {
Type: schema.TypeString,
Optional: true,
Computed: true, //same as start time when OneTime, ridiculous value when recurring: "9999-12-31T15:59:00-08:00"
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validate.RFC3339Time,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"timezone": {
Type: schema.TypeString,
Optional: true,
Default: "UTC",
//todo figure out how to validate this properly
},
"week_days": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
string(automation.Monday),
string(automation.Tuesday),
string(automation.Wednesday),
string(automation.Thursday),
string(automation.Friday),
string(automation.Saturday),
string(automation.Sunday),
}, true),
},
Set: set.HashStringIgnoreCase,
ConflictsWith: []string{"month_days", "monthly_occurrence"},
},
"month_days": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeInt,
ValidateFunc: validate.IntBetweenAndNot(-1, 31, 0),
},
Set: set.HashInt,
ConflictsWith: []string{"week_days", "monthly_occurrence"},
},
"monthly_occurrence": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"day": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice([]string{
string(automation.Monday),
string(automation.Tuesday),
string(automation.Wednesday),
string(automation.Thursday),
string(automation.Friday),
string(automation.Saturday),
string(automation.Sunday),
}, true),
},
"occurrence": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validate.IntBetweenAndNot(-1, 5, 0),
},
},
},
ConflictsWith: []string{"week_days", "month_days"},
},
},
CustomizeDiff: func(diff *schema.ResourceDiff, v interface{}) error {
frequency := strings.ToLower(diff.Get("frequency").(string))
interval, _ := diff.GetOk("interval")
if frequency == "onetime" && interval.(int) > 0 {
return fmt.Errorf("`interval` cannot be set when frequency is `OneTime`")
}
_, hasWeekDays := diff.GetOk("week_days")
if hasWeekDays && frequency != "week" {
return fmt.Errorf("`week_days` can only be set when frequency is `Week`")
}
_, hasMonthDays := diff.GetOk("month_days")
if hasMonthDays && frequency != "month" {
return fmt.Errorf("`month_days` can only be set when frequency is `Month`")
}
_, hasMonthlyOccurrences := diff.GetOk("monthly_occurrence")
if hasMonthlyOccurrences && frequency != "month" {
return fmt.Errorf("`monthly_occurrence` can only be set when frequency is `Month`")
}
_, hasAccount := diff.GetOk("automation_account_name")
_, hasAutomationAccountWeb := diff.GetOk("account_name")
if !hasAccount && !hasAutomationAccountWeb {
return fmt.Errorf("`automation_account_name` must be set")
}
//if automation_account_name changed or account_name changed to or from nil force a new resource
//remove once we remove the deprecated property
oAan, nAan := diff.GetChange("automation_account_name")
if oAan != "" && nAan != "" {
diff.ForceNew("automation_account_name")
}
oAn, nAn := diff.GetChange("account_name")
if oAn != "" && nAn != "" {
diff.ForceNew("account_name")
}
return nil
},
}
}
func resourceArmAutomationScheduleCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Automation.ScheduleClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*ArmClient).StopContext, d)
defer cancel()
log.Printf("[INFO] preparing arguments for AzureRM Automation Schedule creation.")
name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
//CustomizeDiff should ensure one of these two is set
//todo remove this once `account_name` is removed
accountName := ""
if v, ok := d.GetOk("automation_account_name"); ok {
accountName = v.(string)
} else if v, ok := d.GetOk("account_name"); ok {
accountName = v.(string)
}
if features.ShouldResourcesBeImported() && d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, accountName, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Automation Schedule %q (Account %q / Resource Group %q): %s", name, accountName, resGroup, err)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_automation_schedule", *existing.ID)
}
}
frequency := d.Get("frequency").(string)
timeZone := d.Get("timezone").(string)
description := d.Get("description").(string)
parameters := automation.ScheduleCreateOrUpdateParameters{
Name: &name,
ScheduleCreateOrUpdateProperties: &automation.ScheduleCreateOrUpdateProperties{
Description: &description,
Frequency: automation.ScheduleFrequency(frequency),
TimeZone: &timeZone,
},
}
properties := parameters.ScheduleCreateOrUpdateProperties
//start time can default to now + 7 (5 could be invalid by the time the API is called)
if v, ok := d.GetOk("start_time"); ok {
t, _ := time.Parse(time.RFC3339, v.(string)) //should be validated by the schema
properties.StartTime = &date.Time{Time: t}
} else {
properties.StartTime = &date.Time{Time: time.Now().Add(time.Duration(7) * time.Minute)}
}
if v, ok := d.GetOk("expiry_time"); ok {
t, _ := time.Parse(time.RFC3339, v.(string)) //should be validated by the schema
properties.ExpiryTime = &date.Time{Time: t}
}
//only pay attention to interval if frequency is not OneTime, and default it to 1 if not set
if properties.Frequency != automation.OneTime {
if v, ok := d.GetOk("interval"); ok {
properties.Interval = utils.Int32(int32(v.(int)))
} else {
properties.Interval = 1
}
}
//only pay attention to the advanced schedule fields if frequency is either Week or Month
if properties.Frequency == automation.Week || properties.Frequency == automation.Month {
advancedRef, err := expandArmAutomationScheduleAdvanced(d, d.Id() != "")
if err != nil {
return err
}
properties.AdvancedSchedule = advancedRef
}
if _, err := client.CreateOrUpdate(ctx, resGroup, accountName, name, parameters); err != nil {
return err
}
read, err := client.Get(ctx, resGroup, accountName, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read Automation Schedule '%s' (resource group %s) ID", name, resGroup)
}
d.SetId(*read.ID)
return resourceArmAutomationScheduleRead(d, meta)
}
func resourceArmAutomationScheduleRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Automation.ScheduleClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()
id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}
name := id.Path["schedules"]
resGroup := id.ResourceGroup
accountName := id.Path["automationAccounts"]
resp, err := client.Get(ctx, resGroup, accountName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on AzureRM Automation Schedule '%s': %+v", name, err)
}
d.Set("name", resp.Name)
d.Set("resource_group_name", resGroup)
d.Set("automation_account_name", accountName)
d.Set("account_name", accountName) //todo remove once `account_name` is removed
d.Set("frequency", string(resp.Frequency))
if v := resp.StartTime; v != nil {
d.Set("start_time", v.Format(time.RFC3339))
}
if v := resp.ExpiryTime; v != nil {
d.Set("expiry_time", v.Format(time.RFC3339))
}
if v := resp.Interval; v != nil {
//seems to me missing its type in swagger, leading to it being a interface{} float64
d.Set("interval", int(v.(float64)))
}
if v := resp.Description; v != nil {
d.Set("description", v)
}
if v := resp.TimeZone; v != nil {
d.Set("timezone", v)
}
if v := resp.AdvancedSchedule; v != nil {
if err := d.Set("week_days", flattenArmAutomationScheduleAdvancedWeekDays(v)); err != nil {
return fmt.Errorf("Error setting `week_days`: %+v", err)
}
if err := d.Set("month_days", flattenArmAutomationScheduleAdvancedMonthDays(v)); err != nil {
return fmt.Errorf("Error setting `month_days`: %+v", err)
}
if err := d.Set("monthly_occurrence", flattenArmAutomationScheduleAdvancedMonthlyOccurrences(v)); err != nil {
return fmt.Errorf("Error setting `monthly_occurrence`: %+v", err)
}
}
return nil
}
func resourceArmAutomationScheduleDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Automation.ScheduleClient
ctx, cancel := timeouts.ForDelete(meta.(*ArmClient).StopContext, d)
defer cancel()
id, err := azure.ParseAzureResourceID(d.Id())
if err != nil {
return err
}
name := id.Path["schedules"]
resGroup := id.ResourceGroup
accountName := id.Path["automationAccounts"]
resp, err := client.Delete(ctx, resGroup, accountName, name)
if err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("Error issuing AzureRM delete request for Automation Schedule '%s': %+v", name, err)
}
}
return nil
}
func expandArmAutomationScheduleAdvanced(d *schema.ResourceData, isUpdate bool) (*automation.AdvancedSchedule, error) {
expandedAdvancedSchedule := automation.AdvancedSchedule{}
// If frequency is set to `Month` the `week_days` array cannot be set (even empty), otherwise the API returns an error.
// During update it can be set and it will not return an error. Workaround for the APIs behavior
if v, ok := d.GetOk("week_days"); ok {
weekDays := v.(*schema.Set).List()
expandedWeekDays := make([]string, len(weekDays))
for i := range weekDays {
expandedWeekDays[i] = weekDays[i].(string)
}
expandedAdvancedSchedule.WeekDays = &expandedWeekDays
} else if isUpdate {
expandedAdvancedSchedule.WeekDays = &[]string{}
}
// Same as above with `week_days`
if v, ok := d.GetOk("month_days"); ok {
monthDays := v.(*schema.Set).List()
expandedMonthDays := make([]int32, len(monthDays))
for i := range monthDays {
expandedMonthDays[i] = int32(monthDays[i].(int))
}
expandedAdvancedSchedule.MonthDays = &expandedMonthDays
} else if isUpdate {
expandedAdvancedSchedule.MonthDays = &[]int32{}
}
monthlyOccurrences := d.Get("monthly_occurrence").([]interface{})
expandedMonthlyOccurrences := make([]automation.AdvancedScheduleMonthlyOccurrence, len(monthlyOccurrences))
for i := range monthlyOccurrences {
m := monthlyOccurrences[i].(map[string]interface{})
occurrence := int32(m["occurrence"].(int))
expandedMonthlyOccurrences[i] = automation.AdvancedScheduleMonthlyOccurrence{
Occurrence: &occurrence,
Day: automation.ScheduleDay(m["day"].(string)),
}
}
expandedAdvancedSchedule.MonthlyOccurrences = &expandedMonthlyOccurrences
return &expandedAdvancedSchedule, nil
}
func flattenArmAutomationScheduleAdvancedWeekDays(s *automation.AdvancedSchedule) *schema.Set {
flattenedWeekDays := schema.NewSet(set.HashStringIgnoreCase, []interface{}{})
if weekDays := s.WeekDays; weekDays != nil {
for _, v := range *weekDays {
flattenedWeekDays.Add(v)
}
}
return flattenedWeekDays
}
func flattenArmAutomationScheduleAdvancedMonthDays(s *automation.AdvancedSchedule) *schema.Set {
flattenedMonthDays := schema.NewSet(set.HashInt, []interface{}{})
if monthDays := s.MonthDays; monthDays != nil {
for _, v := range *monthDays {
flattenedMonthDays.Add(int(v))
}
}
return flattenedMonthDays
}
func flattenArmAutomationScheduleAdvancedMonthlyOccurrences(s *automation.AdvancedSchedule) []map[string]interface{} {
flattenedMonthlyOccurrences := make([]map[string]interface{}, 0)
if monthlyOccurrences := s.MonthlyOccurrences; monthlyOccurrences != nil {
for _, v := range *monthlyOccurrences {
f := make(map[string]interface{})
f["day"] = v.Day
f["occurrence"] = int(*v.Occurrence)
flattenedMonthlyOccurrences = append(flattenedMonthlyOccurrences, f)
}
}
return flattenedMonthlyOccurrences
}