-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
resource_arm_windows_virtual_machine_scale_set.go
969 lines (788 loc) · 35 KB
/
resource_arm_windows_virtual_machine_scale_set.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
package compute
import (
"fmt"
"log"
"time"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
"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/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/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/base64"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func resourceArmWindowsVirtualMachineScaleSet() *schema.Resource {
return &schema.Resource{
Create: resourceArmWindowsVirtualMachineScaleSetCreate,
Read: resourceArmWindowsVirtualMachineScaleSetRead,
Update: resourceArmWindowsVirtualMachineScaleSetUpdate,
Delete: resourceArmWindowsVirtualMachineScaleSetDelete,
Importer: azSchema.ValidateResourceIDPriorToImportThen(func(id string) error {
_, err := ParseVirtualMachineScaleSetID(id)
return err
}, importVirtualMachineScaleSet(compute.Windows, "azurerm_windows_virtual_machine_scale_set")),
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(60 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(60 * time.Minute),
Delete: schema.DefaultTimeout(60 * time.Minute),
},
// TODO: exposing requireGuestProvisionSignal once it's available
// https://github.com/Azure/azure-rest-api-specs/pull/7246
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: ValidateWindowsName,
},
"resource_group_name": azure.SchemaResourceGroupName(),
"location": azure.SchemaLocation(),
// Required
"admin_username": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"admin_password": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Sensitive: true,
DiffSuppressFunc: adminPasswordDiffSuppressFunc,
ValidateFunc: validation.StringIsNotEmpty,
},
"network_interface": VirtualMachineScaleSetNetworkInterfaceSchema(),
"os_disk": VirtualMachineScaleSetOSDiskSchema(),
"instances": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntAtLeast(0),
},
"sku": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
// Optional
"additional_capabilities": VirtualMachineScaleSetAdditionalCapabilitiesSchema(),
"additional_unattend_content": additionalUnattendContentSchema(),
"automatic_os_upgrade_policy": VirtualMachineScaleSetAutomatedOSUpgradePolicySchema(),
"boot_diagnostics": bootDiagnosticsSchema(),
"computer_name_prefix": {
Type: schema.TypeString,
Optional: true,
// Computed since we reuse the VM name if one's not specified
Computed: true,
ForceNew: true,
// note: whilst the portal says 1-15 characters it seems to mirror the rules for the vm name
// (e.g. 1-15 for Windows, 1-63 for Windows)
ValidateFunc: ValidateWindowsName,
},
"custom_data": base64.OptionalSchema(false),
"data_disk": VirtualMachineScaleSetDataDiskSchema(),
"do_not_run_extensions_on_overprovisioned_machines": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"enable_automatic_updates": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"eviction_policy": {
// only applicable when `priority` is set to `Spot`
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(compute.Deallocate),
string(compute.Delete),
}, false),
},
"health_probe_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},
"identity": VirtualMachineScaleSetIdentitySchema(),
"license_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"None",
"Windows_Client",
"Windows_Server",
}, false),
},
"max_bid_price": {
Type: schema.TypeFloat,
Optional: true,
Default: -1,
},
"overprovision": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"plan": planSchema(),
"priority": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: string(compute.Regular),
ValidateFunc: validation.StringInSlice([]string{
string(compute.Regular),
string(compute.Spot),
}, false),
},
"provision_vm_agent": {
Type: schema.TypeBool,
Optional: true,
Default: true,
ForceNew: true,
},
"proximity_placement_group_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
// the Compute API is broken and returns the Resource Group name in UPPERCASE :shrug:
DiffSuppressFunc: suppress.CaseDifference,
},
"rolling_upgrade_policy": VirtualMachineScaleSetRollingUpgradePolicySchema(),
"secret": windowsSecretSchema(),
"single_placement_group": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"source_image_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: azure.ValidateResourceID,
},
"source_image_reference": sourceImageReferenceSchema(false),
"tags": tags.Schema(),
"timezone": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.VirtualMachineTimeZone(),
},
"upgrade_mode": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: string(compute.Manual),
ValidateFunc: validation.StringInSlice([]string{
string(compute.Automatic),
string(compute.Manual),
string(compute.Rolling),
}, false),
},
"winrm_listener": winRmListenerSchema(),
"zone_balance": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: false,
},
"zones": azure.SchemaZones(),
// Computed
"unique_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceArmWindowsVirtualMachineScaleSetCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.VMScaleSetClient
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()
resourceGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)
if features.ShouldResourcesBeImported() {
resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error checking for existing Windows Virtual Machine Scale Set %q (Resource Group %q): %+v", name, resourceGroup, err)
}
}
if !utils.ResponseWasNotFound(resp.Response) {
return tf.ImportAsExistsError("azurerm_windows_virtual_machine_scale_set", *resp.ID)
}
}
location := azure.NormalizeLocation(d.Get("location").(string))
t := d.Get("tags").(map[string]interface{})
additionalCapabilitiesRaw := d.Get("additional_capabilities").([]interface{})
additionalCapabilities := ExpandVirtualMachineScaleSetAdditionalCapabilities(additionalCapabilitiesRaw)
additionalUnattendContentRaw := d.Get("additional_unattend_content").([]interface{})
additionalUnattendContent := expandAdditionalUnattendContent(additionalUnattendContentRaw)
bootDiagnosticsRaw := d.Get("boot_diagnostics").([]interface{})
bootDiagnostics := expandBootDiagnostics(bootDiagnosticsRaw)
dataDisksRaw := d.Get("data_disk").([]interface{})
dataDisks := ExpandVirtualMachineScaleSetDataDisk(dataDisksRaw)
identityRaw := d.Get("identity").([]interface{})
identity, err := ExpandVirtualMachineScaleSetIdentity(identityRaw)
if err != nil {
return fmt.Errorf("Error expanding `identity`: %+v", err)
}
networkInterfacesRaw := d.Get("network_interface").([]interface{})
networkInterfaces, err := ExpandVirtualMachineScaleSetNetworkInterface(networkInterfacesRaw)
if err != nil {
return fmt.Errorf("Error expanding `network_interface`: %+v", err)
}
osDiskRaw := d.Get("os_disk").([]interface{})
osDisk := ExpandVirtualMachineScaleSetOSDisk(osDiskRaw, compute.Windows)
planRaw := d.Get("plan").([]interface{})
plan := expandPlan(planRaw)
sourceImageReferenceRaw := d.Get("source_image_reference").([]interface{})
sourceImageId := d.Get("source_image_id").(string)
sourceImageReference, err := expandSourceImageReference(sourceImageReferenceRaw, sourceImageId)
if err != nil {
return err
}
healthProbeId := d.Get("health_probe_id").(string)
upgradeMode := compute.UpgradeMode(d.Get("upgrade_mode").(string))
automaticOSUpgradePolicyRaw := d.Get("automatic_os_upgrade_policy").([]interface{})
automaticOSUpgradePolicy := ExpandVirtualMachineScaleSetAutomaticUpgradePolicy(automaticOSUpgradePolicyRaw)
rollingUpgradePolicyRaw := d.Get("rolling_upgrade_policy").([]interface{})
rollingUpgradePolicy := ExpandVirtualMachineScaleSetRollingUpgradePolicy(rollingUpgradePolicyRaw)
if upgradeMode != compute.Manual && healthProbeId == "" {
return fmt.Errorf("`healthProbeId` must be set when `upgrade_mode` is set to %q", string(upgradeMode))
}
if upgradeMode != compute.Automatic && len(automaticOSUpgradePolicyRaw) > 0 {
return fmt.Errorf("An `automatic_os_upgrade_policy` block cannot be specified when `upgrade_mode` is not set to `Automatic`")
}
if upgradeMode == compute.Automatic && len(automaticOSUpgradePolicyRaw) == 0 {
return fmt.Errorf("An `automatic_os_upgrade_policy` block must be specified when `upgrade_mode` is set to `Automatic`")
}
shouldHaveRollingUpgradePolicy := upgradeMode == compute.Automatic || upgradeMode == compute.Rolling
if !shouldHaveRollingUpgradePolicy && len(rollingUpgradePolicyRaw) > 0 {
return fmt.Errorf("A `rolling_upgrade_policy` block cannot be specified when `upgrade_mode` is set to %q", string(upgradeMode))
}
if shouldHaveRollingUpgradePolicy && len(rollingUpgradePolicyRaw) == 0 {
return fmt.Errorf("A `rolling_upgrade_policy` block must be specified when `upgrade_mode` is set to %q", string(upgradeMode))
}
winRmListenersRaw := d.Get("winrm_listener").(*schema.Set).List()
winRmListeners := expandWinRMListener(winRmListenersRaw)
secretsRaw := d.Get("secret").([]interface{})
secrets := expandWindowsSecrets(secretsRaw)
zonesRaw := d.Get("zones").([]interface{})
zones := azure.ExpandZones(zonesRaw)
var computerNamePrefix string
if v, ok := d.GetOk("computer_name_prefix"); ok && len(v.(string)) > 0 {
computerNamePrefix = v.(string)
} else {
computerNamePrefix = name
}
networkProfile := &compute.VirtualMachineScaleSetNetworkProfile{
NetworkInterfaceConfigurations: networkInterfaces,
}
if healthProbeId != "" {
networkProfile.HealthProbe = &compute.APIEntityReference{
ID: utils.String(healthProbeId),
}
}
priority := compute.VirtualMachinePriorityTypes(d.Get("priority").(string))
upgradePolicy := compute.UpgradePolicy{
Mode: upgradeMode,
AutomaticOSUpgradePolicy: automaticOSUpgradePolicy,
RollingUpgradePolicy: rollingUpgradePolicy,
}
virtualMachineProfile := compute.VirtualMachineScaleSetVMProfile{
Priority: priority,
OsProfile: &compute.VirtualMachineScaleSetOSProfile{
AdminPassword: utils.String(d.Get("admin_password").(string)),
AdminUsername: utils.String(d.Get("admin_username").(string)),
ComputerNamePrefix: utils.String(computerNamePrefix),
WindowsConfiguration: &compute.WindowsConfiguration{
ProvisionVMAgent: utils.Bool(d.Get("provision_vm_agent").(bool)),
WinRM: winRmListeners,
},
Secrets: secrets,
},
DiagnosticsProfile: bootDiagnostics,
NetworkProfile: networkProfile,
StorageProfile: &compute.VirtualMachineScaleSetStorageProfile{
ImageReference: sourceImageReference,
OsDisk: osDisk,
DataDisks: dataDisks,
},
}
enableAutomaticUpdates := d.Get("enable_automatic_updates").(bool)
if upgradeMode != compute.Automatic {
virtualMachineProfile.OsProfile.WindowsConfiguration.EnableAutomaticUpdates = utils.Bool(enableAutomaticUpdates)
} else if !enableAutomaticUpdates {
return fmt.Errorf("`enable_automatic_updates` must be set to `true` when `upgrade_mode` is set to `Automatic`")
}
if v, ok := d.Get("max_bid_price").(float64); ok && v > 0 {
if priority != compute.Spot {
return fmt.Errorf("`max_bid_price` can only be configured when `priority` is set to `Spot`")
}
virtualMachineProfile.BillingProfile = &compute.BillingProfile{
MaxPrice: utils.Float(v),
}
}
if v, ok := d.GetOk("custom_data"); ok {
virtualMachineProfile.OsProfile.CustomData = utils.String(v.(string))
}
if evictionPolicyRaw, ok := d.GetOk("eviction_policy"); ok {
if virtualMachineProfile.Priority != compute.Spot {
return fmt.Errorf("An `eviction_policy` can only be specified when `priority` is set to `Spot`")
}
virtualMachineProfile.EvictionPolicy = compute.VirtualMachineEvictionPolicyTypes(evictionPolicyRaw.(string))
} else if priority == compute.Spot {
return fmt.Errorf("An `eviction_policy` must be specified when `priority` is set to `Spot`")
}
if len(additionalUnattendContentRaw) > 0 {
virtualMachineProfile.OsProfile.WindowsConfiguration.AdditionalUnattendContent = additionalUnattendContent
}
if v, ok := d.GetOk("license_type"); ok {
virtualMachineProfile.LicenseType = utils.String(v.(string))
}
if v, ok := d.GetOk("timezone"); ok {
virtualMachineProfile.OsProfile.WindowsConfiguration.TimeZone = utils.String(v.(string))
}
props := compute.VirtualMachineScaleSet{
Location: utils.String(location),
Sku: &compute.Sku{
Name: utils.String(d.Get("sku").(string)),
Capacity: utils.Int64(int64(d.Get("instances").(int))),
// doesn't appear this can be set to anything else, even Promo machines are Standard
Tier: utils.String("Standard"),
},
Identity: identity,
Plan: plan,
Tags: tags.Expand(t),
VirtualMachineScaleSetProperties: &compute.VirtualMachineScaleSetProperties{
AdditionalCapabilities: additionalCapabilities,
DoNotRunExtensionsOnOverprovisionedVMs: utils.Bool(d.Get("do_not_run_extensions_on_overprovisioned_machines").(bool)),
Overprovision: utils.Bool(d.Get("overprovision").(bool)),
SinglePlacementGroup: utils.Bool(d.Get("single_placement_group").(bool)),
VirtualMachineProfile: &virtualMachineProfile,
UpgradePolicy: &upgradePolicy,
},
Zones: zones,
}
if v, ok := d.GetOk("proximity_placement_group_id"); ok {
props.VirtualMachineScaleSetProperties.ProximityPlacementGroup = &compute.SubResource{
ID: utils.String(v.(string)),
}
}
if v, ok := d.GetOk("zone_balance"); ok && v.(bool) {
if len(zonesRaw) == 0 {
return fmt.Errorf("`zone_balance` can only be set to `true` when zones are specified")
}
props.VirtualMachineScaleSetProperties.ZoneBalance = utils.Bool(v.(bool))
}
log.Printf("[DEBUG] Creating Windows Virtual Machine Scale Set %q (Resource Group %q)..", name, resourceGroup)
future, err := client.CreateOrUpdate(ctx, resourceGroup, name, props)
if err != nil {
return fmt.Errorf("Error creating Windows Virtual Machine Scale Set %q (Resource Group %q): %+v", name, resourceGroup, err)
}
log.Printf("[DEBUG] Waiting for Windows Virtual Machine Scale Set %q (Resource Group %q) to be created..", name, resourceGroup)
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for creation of Windows Virtual Machine Scale Set %q (Resource Group %q): %+v", name, resourceGroup, err)
}
log.Printf("[DEBUG] Virtual Machine Scale Set %q (Resource Group %q) was created", name, resourceGroup)
log.Printf("[DEBUG] Retrieving Virtual Machine Scale Set %q (Resource Group %q)..", name, resourceGroup)
resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error retrieving Windows Virtual Machine Scale Set %q (Resource Group %q): %+v", name, resourceGroup, err)
}
if resp.ID == nil {
return fmt.Errorf("Error retrieving Windows Virtual Machine Scale Set %q (Resource Group %q): ID was nil", name, resourceGroup)
}
d.SetId(*resp.ID)
return resourceArmWindowsVirtualMachineScaleSetRead(d, meta)
}
func resourceArmWindowsVirtualMachineScaleSetUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.VMScaleSetClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := ParseVirtualMachineScaleSetID(d.Id())
if err != nil {
return err
}
updateInstances := false
// retrieve
existing, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("Error retrieving Windows Virtual Machine Scale Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
if existing.VirtualMachineScaleSetProperties == nil {
return fmt.Errorf("Error retrieving Windows Virtual Machine Scale Set %q (Resource Group %q): `properties` was nil", id.Name, id.ResourceGroup)
}
updateProps := compute.VirtualMachineScaleSetUpdateProperties{
VirtualMachineProfile: &compute.VirtualMachineScaleSetUpdateVMProfile{},
// if an upgrade policy's been configured previously (which it will have) it must be threaded through
// this doesn't matter for Manual - but breaks when updating anything on a Automatic and Rolling Mode Scale Set
UpgradePolicy: existing.VirtualMachineScaleSetProperties.UpgradePolicy,
}
update := compute.VirtualMachineScaleSetUpdate{}
upgradeMode := compute.UpgradeMode(d.Get("upgrade_mode").(string))
// first try and pull this from existing vm, which covers no changes being made to this block
automaticOSUpgradeIsEnabled := false
if policy := existing.VirtualMachineScaleSetProperties.UpgradePolicy; policy != nil {
if policy.AutomaticOSUpgradePolicy != nil && policy.AutomaticOSUpgradePolicy.EnableAutomaticOSUpgrade != nil {
automaticOSUpgradeIsEnabled = *policy.AutomaticOSUpgradePolicy.EnableAutomaticOSUpgrade
}
}
if d.HasChange("automatic_os_upgrade_policy") || d.HasChange("rolling_upgrade_policy") {
upgradePolicy := compute.UpgradePolicy{
Mode: upgradeMode,
}
if d.HasChange("automatic_os_upgrade_policy") {
automaticRaw := d.Get("automatic_os_upgrade_policy").([]interface{})
upgradePolicy.AutomaticOSUpgradePolicy = ExpandVirtualMachineScaleSetAutomaticUpgradePolicy(automaticRaw)
// however if this block has been changed then we need to pull it
// we can guarantee this always has a value since it'll have been expanded and thus is safe to de-ref
automaticOSUpgradeIsEnabled = *upgradePolicy.AutomaticOSUpgradePolicy.EnableAutomaticOSUpgrade
}
if d.HasChange("rolling_upgrade_policy") {
rollingRaw := d.Get("rolling_upgrade_policy").([]interface{})
upgradePolicy.RollingUpgradePolicy = ExpandVirtualMachineScaleSetRollingUpgradePolicy(rollingRaw)
}
updateProps.UpgradePolicy = &upgradePolicy
}
priority := compute.VirtualMachinePriorityTypes(d.Get("priority").(string))
if d.HasChange("max_bid_price") {
if priority != compute.Spot {
return fmt.Errorf("`max_bid_price` can only be configured when `priority` is set to `Spot`")
}
updateProps.VirtualMachineProfile.BillingProfile = &compute.BillingProfile{
MaxPrice: utils.Float(d.Get("max_bid_price").(float64)),
}
}
if d.HasChange("single_placement_group") {
updateProps.SinglePlacementGroup = utils.Bool(d.Get("single_placement_group").(bool))
}
if d.HasChange("enable_automatic_updates") ||
d.HasChange("custom_data") ||
d.HasChange("provision_vm_agent") ||
d.HasChange("secret") ||
d.HasChange("timezone") {
osProfile := compute.VirtualMachineScaleSetUpdateOSProfile{}
if d.HasChange("enable_automatic_updates") || d.HasChange("provision_vm_agent") || d.HasChange("timezone") {
windowsConfig := compute.WindowsConfiguration{}
if d.HasChange("enable_automatic_updates") {
if upgradeMode == compute.Automatic {
return fmt.Errorf("`enable_automatic_updates` cannot be changed for when `upgrade_mode` is `Automatic`")
}
windowsConfig.EnableAutomaticUpdates = utils.Bool(d.Get("enable_automatic_updates").(bool))
}
if d.HasChange("provision_vm_agent") {
windowsConfig.ProvisionVMAgent = utils.Bool(d.Get("provision_vm_agent").(bool))
}
if d.HasChange("timezone") {
windowsConfig.TimeZone = utils.String(d.Get("timezone").(string))
}
osProfile.WindowsConfiguration = &windowsConfig
}
if d.HasChange("custom_data") {
updateInstances = true
// customData can only be sent if it's a base64 encoded string,
// so it's not possible to remove this without tainting the resource
if v, ok := d.GetOk("custom_data"); ok {
osProfile.CustomData = utils.String(v.(string))
}
}
if d.HasChange("secret") {
secretsRaw := d.Get("secret").([]interface{})
osProfile.Secrets = expandWindowsSecrets(secretsRaw)
}
updateProps.VirtualMachineProfile.OsProfile = &osProfile
}
if d.HasChange("data_disk") || d.HasChange("os_disk") || d.HasChange("source_image_id") || d.HasChange("source_image_reference") {
updateInstances = true
storageProfile := &compute.VirtualMachineScaleSetUpdateStorageProfile{}
if d.HasChange("data_disk") {
dataDisksRaw := d.Get("data_disk").([]interface{})
storageProfile.DataDisks = ExpandVirtualMachineScaleSetDataDisk(dataDisksRaw)
}
if d.HasChange("os_disk") {
osDiskRaw := d.Get("os_disk").([]interface{})
storageProfile.OsDisk = ExpandVirtualMachineScaleSetOSDiskUpdate(osDiskRaw)
}
if d.HasChange("source_image_id") || d.HasChange("source_image_reference") {
sourceImageReferenceRaw := d.Get("source_image_reference").([]interface{})
sourceImageId := d.Get("source_image_id").(string)
sourceImageReference, err := expandSourceImageReference(sourceImageReferenceRaw, sourceImageId)
if err != nil {
return err
}
storageProfile.ImageReference = sourceImageReference
}
updateProps.VirtualMachineProfile.StorageProfile = storageProfile
}
if d.HasChange("network_interface") {
networkInterfacesRaw := d.Get("network_interface").([]interface{})
networkInterfaces, err := ExpandVirtualMachineScaleSetNetworkInterfaceUpdate(networkInterfacesRaw)
if err != nil {
return fmt.Errorf("Error expanding `network_interface`: %+v", err)
}
updateProps.VirtualMachineProfile.NetworkProfile = &compute.VirtualMachineScaleSetUpdateNetworkProfile{
NetworkInterfaceConfigurations: networkInterfaces,
}
healthProbeId := d.Get("health_probe_id").(string)
if healthProbeId != "" {
updateProps.VirtualMachineProfile.NetworkProfile.HealthProbe = &compute.APIEntityReference{
ID: utils.String(healthProbeId),
}
}
}
if d.HasChange("boot_diagnostics") {
updateInstances = true
bootDiagnosticsRaw := d.Get("boot_diagnostics").([]interface{})
updateProps.VirtualMachineProfile.DiagnosticsProfile = expandBootDiagnostics(bootDiagnosticsRaw)
}
if d.HasChange("identity") {
identityRaw := d.Get("identity").([]interface{})
identity, err := ExpandVirtualMachineScaleSetIdentity(identityRaw)
if err != nil {
return fmt.Errorf("Error expanding `identity`: %+v", err)
}
update.Identity = identity
}
if d.HasChange("plan") {
planRaw := d.Get("plan").([]interface{})
update.Plan = expandPlan(planRaw)
}
if d.HasChange("sku") || d.HasChange("instances") {
// in-case ignore_changes is being used, since both fields are required
// look up the current values and override them as needed
sku := existing.Sku
if d.HasChange("sku") {
updateInstances = true
sku.Name = utils.String(d.Get("sku").(string))
}
if d.HasChange("instances") {
sku.Capacity = utils.Int64(int64(d.Get("instances").(int)))
}
update.Sku = sku
}
if d.HasChange("tags") {
update.Tags = tags.Expand(d.Get("tags").(map[string]interface{}))
}
update.VirtualMachineScaleSetUpdateProperties = &updateProps
metaData := virtualMachineScaleSetUpdateMetaData{
AutomaticOSUpgradeIsEnabled: automaticOSUpgradeIsEnabled,
CanRollInstancesWhenRequired: meta.(*clients.Client).Features.VirtualMachineScaleSet.RollInstancesWhenRequired,
UpdateInstances: updateInstances,
Client: meta.(*clients.Client).Compute,
Existing: existing,
ID: id,
OSType: compute.Windows,
}
if err := metaData.performUpdate(ctx, update); err != nil {
return err
}
return resourceArmWindowsVirtualMachineScaleSetRead(d, meta)
}
func resourceArmWindowsVirtualMachineScaleSetRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.VMScaleSetClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := ParseVirtualMachineScaleSetID(d.Id())
if err != nil {
return err
}
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Windows Virtual Machine Scale Set %q was not found in Resource Group %q - removing from state!", id.Name, id.ResourceGroup)
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving Windows Virtual Machine Scale Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
var skuName *string
var instances int
if resp.Sku != nil {
skuName = resp.Sku.Name
if resp.Sku.Capacity != nil {
instances = int(*resp.Sku.Capacity)
}
}
d.Set("instances", instances)
d.Set("sku", skuName)
if err := d.Set("identity", FlattenVirtualMachineScaleSetIdentity(resp.Identity)); err != nil {
return fmt.Errorf("Error setting `identity`: %+v", err)
}
if err := d.Set("plan", flattenPlan(resp.Plan)); err != nil {
return fmt.Errorf("Error setting `plan`: %+v", err)
}
if resp.VirtualMachineScaleSetProperties == nil {
return fmt.Errorf("Error retrieving Windows Virtual Machine Scale Set %q (Resource Group %q): `properties` was nil", id.Name, id.ResourceGroup)
}
props := *resp.VirtualMachineScaleSetProperties
if err := d.Set("additional_capabilities", FlattenVirtualMachineScaleSetAdditionalCapabilities(props.AdditionalCapabilities)); err != nil {
return fmt.Errorf("Error setting `additional_capabilities`: %+v", props.AdditionalCapabilities)
}
d.Set("do_not_run_extensions_on_overprovisioned_machines", props.DoNotRunExtensionsOnOverprovisionedVMs)
d.Set("overprovision", props.Overprovision)
proximityPlacementGroupId := ""
if props.ProximityPlacementGroup != nil && props.ProximityPlacementGroup.ID != nil {
proximityPlacementGroupId = *props.ProximityPlacementGroup.ID
}
d.Set("proximity_placement_group_id", proximityPlacementGroupId)
d.Set("single_placement_group", props.SinglePlacementGroup)
d.Set("unique_id", props.UniqueID)
d.Set("zone_balance", props.ZoneBalance)
var upgradeMode compute.UpgradeMode
if policy := props.UpgradePolicy; policy != nil {
upgradeMode = policy.Mode
d.Set("upgrade_mode", string(policy.Mode))
flattenedAutomatic := FlattenVirtualMachineScaleSetAutomaticOSUpgradePolicy(policy.AutomaticOSUpgradePolicy)
if err := d.Set("automatic_os_upgrade_policy", flattenedAutomatic); err != nil {
return fmt.Errorf("Error setting `automatic_os_upgrade_policy`: %+v", err)
}
flattenedRolling := FlattenVirtualMachineScaleSetRollingUpgradePolicy(policy.RollingUpgradePolicy)
if err := d.Set("rolling_upgrade_policy", flattenedRolling); err != nil {
return fmt.Errorf("Error setting `rolling_upgrade_policy`: %+v", err)
}
}
if profile := props.VirtualMachineProfile; profile != nil {
if err := d.Set("boot_diagnostics", flattenBootDiagnostics(profile.DiagnosticsProfile)); err != nil {
return fmt.Errorf("Error setting `boot_diagnostics`: %+v", err)
}
// defaulted since BillingProfile isn't returned if it's unset
maxBidPrice := float64(-1.0)
if profile.BillingProfile != nil && profile.BillingProfile.MaxPrice != nil {
maxBidPrice = *profile.BillingProfile.MaxPrice
}
d.Set("max_bid_price", maxBidPrice)
d.Set("eviction_policy", string(profile.EvictionPolicy))
d.Set("license_type", profile.LicenseType)
d.Set("priority", string(profile.Priority))
if storageProfile := profile.StorageProfile; storageProfile != nil {
if err := d.Set("os_disk", FlattenVirtualMachineScaleSetOSDisk(storageProfile.OsDisk)); err != nil {
return fmt.Errorf("Error setting `os_disk`: %+v", err)
}
if err := d.Set("data_disk", FlattenVirtualMachineScaleSetDataDisk(storageProfile.DataDisks)); err != nil {
return fmt.Errorf("Error setting `data_disk`: %+v", err)
}
if err := d.Set("source_image_reference", flattenSourceImageReference(storageProfile.ImageReference)); err != nil {
return fmt.Errorf("Error setting `source_image_reference`: %+v", err)
}
var storageImageId string
if storageProfile.ImageReference != nil && storageProfile.ImageReference.ID != nil {
storageImageId = *storageProfile.ImageReference.ID
}
d.Set("source_image_id", storageImageId)
}
if osProfile := profile.OsProfile; osProfile != nil {
// admin_password isn't returned, but it's a top level field so we can ignore it without consequence
d.Set("admin_username", osProfile.AdminUsername)
d.Set("computer_name_prefix", osProfile.ComputerNamePrefix)
if err := d.Set("secret", flattenWindowsSecrets(osProfile.Secrets)); err != nil {
return fmt.Errorf("Error setting `secret`: %+v", err)
}
if windows := osProfile.WindowsConfiguration; windows != nil {
if err := d.Set("additional_unattend_content", flattenAdditionalUnattendContent(windows.AdditionalUnattendContent, d)); err != nil {
return fmt.Errorf("Error setting `additional_unattend_content`: %+v", err)
}
enableAutomaticUpdates := false
if windows.EnableAutomaticUpdates != nil {
enableAutomaticUpdates = *windows.EnableAutomaticUpdates
}
// the API requires this is set to 'true' on submission (since it's now required for Windows VMSS's with
// an Automatic Upgrade Mode configured) however it actually returns false from the API..
// after a bunch of testing the least bad option appears to be not to set this if it's an Automatic Upgrade Mode
if upgradeMode != compute.Automatic {
d.Set("enable_automatic_updates", enableAutomaticUpdates)
}
d.Set("provision_vm_agent", windows.ProvisionVMAgent)
d.Set("timezone", windows.TimeZone)
if err := d.Set("winrm_listener", flattenWinRMListener(windows.WinRM)); err != nil {
return fmt.Errorf("Error setting `winrm_listener`: %+v", err)
}
}
}
if nwProfile := profile.NetworkProfile; nwProfile != nil {
flattenedNics := FlattenVirtualMachineScaleSetNetworkInterface(nwProfile.NetworkInterfaceConfigurations)
if err := d.Set("network_interface", flattenedNics); err != nil {
return fmt.Errorf("Error setting `network_interface`: %+v", err)
}
healthProbeId := ""
if nwProfile.HealthProbe != nil && nwProfile.HealthProbe.ID != nil {
healthProbeId = *nwProfile.HealthProbe.ID
}
d.Set("health_probe_id", healthProbeId)
}
}
if err := d.Set("zones", resp.Zones); err != nil {
return fmt.Errorf("Error setting `zones`: %+v", err)
}
return tags.FlattenAndSet(d, resp.Tags)
}
func resourceArmWindowsVirtualMachineScaleSetDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Compute.VMScaleSetClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := ParseVirtualMachineScaleSetID(d.Id())
if err != nil {
return err
}
resp, err := client.Get(ctx, id.ResourceGroup, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return nil
}
return fmt.Errorf("Error retrieving Windows Virtual Machine Scale Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
// Sometimes VMSS's aren't fully deleted when the `Delete` call returns - as such we'll try to scale the cluster
// to 0 nodes first, then delete the cluster - which should ensure there's no Network Interfaces kicking around
// and work around this Azure API bug:
// Original Error: Code="InUseSubnetCannotBeDeleted" Message="Subnet internal is in use by
// /{nicResourceID}/|providers|Microsoft.Compute|virtualMachineScaleSets|acctestvmss-190923101253410278|virtualMachines|0|networkInterfaces|example/ipConfigurations/internal and cannot be deleted.
// In order to delete the subnet, delete all the resources within the subnet. See aka.ms/deletesubnet.
if resp.Sku != nil {
resp.Sku.Capacity = utils.Int64(int64(0))
log.Printf("[DEBUG] Scaling instances to 0 prior to deletion - this helps avoids networking issues within Azure")
update := compute.VirtualMachineScaleSetUpdate{
Sku: resp.Sku,
}
future, err := client.Update(ctx, id.ResourceGroup, id.Name, update)
if err != nil {
return fmt.Errorf("Error updating number of instances in Windows Virtual Machine Scale Set %q (Resource Group %q) to scale to 0: %+v", id.Name, id.ResourceGroup, err)
}
log.Printf("[DEBUG] Waiting for scaling of instances to 0 prior to deletion - this helps avoids networking issues within Azure")
err = future.WaitForCompletionRef(ctx, client.Client)
if err != nil {
return fmt.Errorf("Error waiting for number of instances in Windows Virtual Machine Scale Set %q (Resource Group %q) to scale to 0: %+v", id.Name, id.ResourceGroup, err)
}
log.Printf("[DEBUG] Scaled instances to 0 prior to deletion - this helps avoids networking issues within Azure")
} else {
log.Printf("[DEBUG] Unable to scale instances to `0` since the `sku` block is nil - trying to delete anyway")
}
log.Printf("[DEBUG] Deleting Windows Virtual Machine Scale Set %q (Resource Group %q)..", id.Name, id.ResourceGroup)
future, err := client.Delete(ctx, id.ResourceGroup, id.Name)
if err != nil {
return fmt.Errorf("Error deleting Windows Virtual Machine Scale Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
log.Printf("[DEBUG] Waiting for deletion of Windows Virtual Machine Scale Set %q (Resource Group %q)..", id.Name, id.ResourceGroup)
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for deletion of Windows Virtual Machine Scale Set %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
log.Printf("[DEBUG] Deleted Windows Virtual Machine Scale Set %q (Resource Group %q).", id.Name, id.ResourceGroup)
return nil
}