From 6c713bef05a35687722c6dac7fc1ea335f2e6763 Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Wed, 6 Oct 2021 07:49:01 +0100 Subject: [PATCH 1/2] feature for scale_in_on_delete --- internal/features/defaults.go | 1 + internal/features/user_flags.go | 1 + internal/provider/features.go | 7 +++++ internal/provider/features_test.go | 31 ++++++++++++++++++- ...inux_virtual_machine_scale_set_resource.go | 3 +- ...dows_virtual_machine_scale_set_resource.go | 3 +- website/docs/index.html.markdown | 4 ++- 7 files changed, 46 insertions(+), 4 deletions(-) diff --git a/internal/features/defaults.go b/internal/features/defaults.go index 5f187e3b256b..1d02f3b42a53 100644 --- a/internal/features/defaults.go +++ b/internal/features/defaults.go @@ -33,6 +33,7 @@ func Default() UserFeatures { VirtualMachineScaleSet: VirtualMachineScaleSetFeatures{ ForceDelete: false, RollInstancesWhenRequired: true, + ScaleInOnDelete: true, }, } } diff --git a/internal/features/user_flags.go b/internal/features/user_flags.go index 435854c80a55..d6b63e84dd40 100644 --- a/internal/features/user_flags.go +++ b/internal/features/user_flags.go @@ -25,6 +25,7 @@ type VirtualMachineFeatures struct { type VirtualMachineScaleSetFeatures struct { ForceDelete bool RollInstancesWhenRequired bool + ScaleInOnDelete bool } type KeyVaultFeatures struct { diff --git a/internal/provider/features.go b/internal/provider/features.go index 03ebcb759ab5..c91f40d7c7ff 100644 --- a/internal/provider/features.go +++ b/internal/provider/features.go @@ -136,6 +136,10 @@ func schemaFeatures(supportLegacyTestSuite bool) *pluginsdk.Schema { Type: pluginsdk.TypeBool, Required: true, }, + "scale_in_on_delete": { + Type: pluginsdk.TypeBool, + Optional: true, + }, }, }, }, @@ -277,6 +281,9 @@ func expandFeatures(input []interface{}) features.UserFeatures { if v, ok := scaleSetRaw["force_delete"]; ok { features.VirtualMachineScaleSet.ForceDelete = v.(bool) } + if v, ok := scaleSetRaw["scale_in_on_delete"]; ok { + features.VirtualMachineScaleSet.ScaleInOnDelete = v.(bool) + } } } diff --git a/internal/provider/features_test.go b/internal/provider/features_test.go index 7605fd1fb890..8151f89d11ef 100644 --- a/internal/provider/features_test.go +++ b/internal/provider/features_test.go @@ -45,6 +45,7 @@ func TestExpandFeatures(t *testing.T) { VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{ ForceDelete: false, RollInstancesWhenRequired: true, + ScaleInOnDelete: true, }, ResourceGroup: features.ResourceGroupFeatures{ PreventDeletionIfContainsResources: false, @@ -102,6 +103,7 @@ func TestExpandFeatures(t *testing.T) { map[string]interface{}{ "roll_instances_when_required": true, "force_delete": true, + "scale_in_on_delete": true, }, }, }, @@ -137,6 +139,7 @@ func TestExpandFeatures(t *testing.T) { VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{ RollInstancesWhenRequired: true, ForceDelete: true, + ScaleInOnDelete: true, }, }, }, @@ -191,6 +194,7 @@ func TestExpandFeatures(t *testing.T) { map[string]interface{}{ "force_delete": false, "roll_instances_when_required": false, + "scale_in_on_delete": false, }, }, }, @@ -226,6 +230,7 @@ func TestExpandFeatures(t *testing.T) { VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{ ForceDelete: false, RollInstancesWhenRequired: false, + ScaleInOnDelete: false, }, }, }, @@ -705,6 +710,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) { Expected: features.UserFeatures{ VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{ RollInstancesWhenRequired: true, + ScaleInOnDelete: true, }, }, }, @@ -724,6 +730,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) { VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{ ForceDelete: true, RollInstancesWhenRequired: false, + ScaleInOnDelete: true, }, }, }, @@ -743,6 +750,26 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) { VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{ ForceDelete: false, RollInstancesWhenRequired: true, + ScaleInOnDelete: true, + }, + }, + }, + { + Name: "Scale In On Delete Disabled", + Input: []interface{}{ + map[string]interface{}{ + "virtual_machine_scale_set": []interface{}{ + map[string]interface{}{ + "force_delete": false, + "scale_in_on_delete": false, + }, + }, + }, + }, + Expected: features.UserFeatures{ + VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{ + ScaleInOnDelete: false, + RollInstancesWhenRequired: true, }, }, }, @@ -754,6 +781,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) { map[string]interface{}{ "force_delete": false, "roll_instances_when_required": false, + "scale_in_on_delete": false, }, }, }, @@ -762,6 +790,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) { VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{ ForceDelete: false, RollInstancesWhenRequired: false, + ScaleInOnDelete: false, }, }, }, @@ -771,7 +800,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) { t.Logf("[DEBUG] Test Case: %q", testCase.Name) result := expandFeatures(testCase.Input) if !reflect.DeepEqual(result.VirtualMachineScaleSet, testCase.Expected.VirtualMachineScaleSet) { - t.Fatalf("Expected %+v but got %+v", result.VirtualMachineScaleSet, testCase.Expected.VirtualMachineScaleSet) + t.Fatalf("Expected %+v but got %+v", testCase.Expected.VirtualMachineScaleSet, result.VirtualMachineScaleSet) } } } diff --git a/internal/services/compute/linux_virtual_machine_scale_set_resource.go b/internal/services/compute/linux_virtual_machine_scale_set_resource.go index 06f99534a2b4..05ea0f69b7b9 100644 --- a/internal/services/compute/linux_virtual_machine_scale_set_resource.go +++ b/internal/services/compute/linux_virtual_machine_scale_set_resource.go @@ -1084,7 +1084,8 @@ func resourceLinuxVirtualMachineScaleSetDelete(d *pluginsdk.ResourceData, meta i // 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 { + scaleInOnDelete := meta.(*clients.Client).Features.VirtualMachineScaleSet.ScaleInOnDelete + if scaleInOnDelete && 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") diff --git a/internal/services/compute/windows_virtual_machine_scale_set_resource.go b/internal/services/compute/windows_virtual_machine_scale_set_resource.go index a55900cea61a..193c65afc2b7 100644 --- a/internal/services/compute/windows_virtual_machine_scale_set_resource.go +++ b/internal/services/compute/windows_virtual_machine_scale_set_resource.go @@ -1143,7 +1143,8 @@ func resourceWindowsVirtualMachineScaleSetDelete(d *pluginsdk.ResourceData, meta // 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 { + scaleInOnDelete := meta.(*clients.Client).Features.VirtualMachineScaleSet.ScaleInOnDelete + if scaleInOnDelete && 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") diff --git a/website/docs/index.html.markdown b/website/docs/index.html.markdown index 953316c507d1..be29d291d476 100644 --- a/website/docs/index.html.markdown +++ b/website/docs/index.html.markdown @@ -241,7 +241,7 @@ The `virtual_machine` block supports the following: ~> **Note:** When using a graceful shutdown, Azure gives the Virtual Machine a 5 minutes window in which to complete the shutdown process, at which point the machine will be force powered off - [more information can be found in this blog post](https://azure.microsoft.com/en-us/blog/linux-and-graceful-shutdowns-2/). -* `skip_shutdown_and_force_delete` - Should the `azurerm_linux_virtual_machine` and `azurerm_windows_virtual_machine` skip the shutdown command and `Force Delete`, this provides the ability to forcefully and immediately delete the VM and detach all sub-resources associated with the virtual machine. This allows those freed resources to be reattached to another VM instance or deleted. Defaults to `false`. +* `skip_shutdown_and_force_delete` - Should the `azurerm_linux_virtual_machine` and `azurerm_windows_virtual_machine` skip the shutdown command and `Force Delete`, this provides the ability to forcefully and immediately delete the VM and detach all sub-resources associated with the virtual machine. This allows those freed resources to be reattached to another VM instance or deleted. Defaults to `false`. ~> **Note:** Support for Force Delete is in an opt-in Preview. @@ -254,3 +254,5 @@ The `virtual_machine_scale_set` block supports the following: ~> **Note:** Support for Force Delete is in an opt-in Preview. * `roll_instances_when_required` - (Optional) Should the `azurerm_linux_virtual_machine_scale_set` and `azurerm_windows_virtual_machine_scale_set` resources automatically roll the instances in the Scale Set when Required (for example when updating the Sku/Image). Defaults to `true`. + +* `scale_in_on_delete` - (Optional) Should the `azurerm_linux_virtual_machine_scale_set` and `azurerm_windows_virtual_machine_scale_set` resources scale to 0 instances before deleting the resource. Defaults to `true`. From 71031d26eb1f376ef8b65fdc7cbd8c1afba91c5a Mon Sep 17 00:00:00 2001 From: Henry Buckle Date: Sat, 9 Oct 2021 11:16:14 +0100 Subject: [PATCH 2/2] rename --- internal/features/defaults.go | 2 +- internal/features/user_flags.go | 2 +- internal/provider/features.go | 6 +-- internal/provider/features_test.go | 40 ++++++++++--------- ...inux_virtual_machine_scale_set_resource.go | 4 +- ...dows_virtual_machine_scale_set_resource.go | 4 +- website/docs/index.html.markdown | 2 +- 7 files changed, 31 insertions(+), 29 deletions(-) diff --git a/internal/features/defaults.go b/internal/features/defaults.go index 1d02f3b42a53..8f16083fe539 100644 --- a/internal/features/defaults.go +++ b/internal/features/defaults.go @@ -33,7 +33,7 @@ func Default() UserFeatures { VirtualMachineScaleSet: VirtualMachineScaleSetFeatures{ ForceDelete: false, RollInstancesWhenRequired: true, - ScaleInOnDelete: true, + ScaleToZeroOnDelete: true, }, } } diff --git a/internal/features/user_flags.go b/internal/features/user_flags.go index d6b63e84dd40..e9fc0c0f1353 100644 --- a/internal/features/user_flags.go +++ b/internal/features/user_flags.go @@ -25,7 +25,7 @@ type VirtualMachineFeatures struct { type VirtualMachineScaleSetFeatures struct { ForceDelete bool RollInstancesWhenRequired bool - ScaleInOnDelete bool + ScaleToZeroOnDelete bool } type KeyVaultFeatures struct { diff --git a/internal/provider/features.go b/internal/provider/features.go index c91f40d7c7ff..1b5338679eb2 100644 --- a/internal/provider/features.go +++ b/internal/provider/features.go @@ -136,7 +136,7 @@ func schemaFeatures(supportLegacyTestSuite bool) *pluginsdk.Schema { Type: pluginsdk.TypeBool, Required: true, }, - "scale_in_on_delete": { + "scale_to_zero_before_deletion": { Type: pluginsdk.TypeBool, Optional: true, }, @@ -281,8 +281,8 @@ func expandFeatures(input []interface{}) features.UserFeatures { if v, ok := scaleSetRaw["force_delete"]; ok { features.VirtualMachineScaleSet.ForceDelete = v.(bool) } - if v, ok := scaleSetRaw["scale_in_on_delete"]; ok { - features.VirtualMachineScaleSet.ScaleInOnDelete = v.(bool) + if v, ok := scaleSetRaw["scale_to_zero_before_deletion"]; ok { + features.VirtualMachineScaleSet.ScaleToZeroOnDelete = v.(bool) } } } diff --git a/internal/provider/features_test.go b/internal/provider/features_test.go index 8151f89d11ef..a3c0fde7f64e 100644 --- a/internal/provider/features_test.go +++ b/internal/provider/features_test.go @@ -45,7 +45,7 @@ func TestExpandFeatures(t *testing.T) { VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{ ForceDelete: false, RollInstancesWhenRequired: true, - ScaleInOnDelete: true, + ScaleToZeroOnDelete: true, }, ResourceGroup: features.ResourceGroupFeatures{ PreventDeletionIfContainsResources: false, @@ -101,9 +101,9 @@ func TestExpandFeatures(t *testing.T) { }, "virtual_machine_scale_set": []interface{}{ map[string]interface{}{ - "roll_instances_when_required": true, - "force_delete": true, - "scale_in_on_delete": true, + "roll_instances_when_required": true, + "force_delete": true, + "scale_to_zero_before_deletion": true, }, }, }, @@ -139,7 +139,7 @@ func TestExpandFeatures(t *testing.T) { VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{ RollInstancesWhenRequired: true, ForceDelete: true, - ScaleInOnDelete: true, + ScaleToZeroOnDelete: true, }, }, }, @@ -192,9 +192,9 @@ func TestExpandFeatures(t *testing.T) { }, "virtual_machine_scale_set": []interface{}{ map[string]interface{}{ - "force_delete": false, - "roll_instances_when_required": false, - "scale_in_on_delete": false, + "force_delete": false, + "roll_instances_when_required": false, + "scale_to_zero_before_deletion": false, }, }, }, @@ -230,7 +230,7 @@ func TestExpandFeatures(t *testing.T) { VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{ ForceDelete: false, RollInstancesWhenRequired: false, - ScaleInOnDelete: false, + ScaleToZeroOnDelete: false, }, }, }, @@ -710,7 +710,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) { Expected: features.UserFeatures{ VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{ RollInstancesWhenRequired: true, - ScaleInOnDelete: true, + ScaleToZeroOnDelete: true, }, }, }, @@ -730,7 +730,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) { VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{ ForceDelete: true, RollInstancesWhenRequired: false, - ScaleInOnDelete: true, + ScaleToZeroOnDelete: true, }, }, }, @@ -750,7 +750,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) { VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{ ForceDelete: false, RollInstancesWhenRequired: true, - ScaleInOnDelete: true, + ScaleToZeroOnDelete: true, }, }, }, @@ -760,16 +760,18 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) { map[string]interface{}{ "virtual_machine_scale_set": []interface{}{ map[string]interface{}{ - "force_delete": false, - "scale_in_on_delete": false, + "force_delete": false, + "roll_instances_when_required": true, + "scale_to_zero_before_deletion": false, }, }, }, }, Expected: features.UserFeatures{ VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{ - ScaleInOnDelete: false, + ForceDelete: false, RollInstancesWhenRequired: true, + ScaleToZeroOnDelete: false, }, }, }, @@ -779,9 +781,9 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) { map[string]interface{}{ "virtual_machine_scale_set": []interface{}{ map[string]interface{}{ - "force_delete": false, - "roll_instances_when_required": false, - "scale_in_on_delete": false, + "force_delete": false, + "roll_instances_when_required": false, + "scale_to_zero_before_deletion": false, }, }, }, @@ -790,7 +792,7 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) { VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{ ForceDelete: false, RollInstancesWhenRequired: false, - ScaleInOnDelete: false, + ScaleToZeroOnDelete: false, }, }, }, diff --git a/internal/services/compute/linux_virtual_machine_scale_set_resource.go b/internal/services/compute/linux_virtual_machine_scale_set_resource.go index 05ea0f69b7b9..abea74ff5ce8 100644 --- a/internal/services/compute/linux_virtual_machine_scale_set_resource.go +++ b/internal/services/compute/linux_virtual_machine_scale_set_resource.go @@ -1084,8 +1084,8 @@ func resourceLinuxVirtualMachineScaleSetDelete(d *pluginsdk.ResourceData, meta i // 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. - scaleInOnDelete := meta.(*clients.Client).Features.VirtualMachineScaleSet.ScaleInOnDelete - if scaleInOnDelete && resp.Sku != nil { + scaleToZeroOnDelete := meta.(*clients.Client).Features.VirtualMachineScaleSet.ScaleToZeroOnDelete + if scaleToZeroOnDelete && 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") diff --git a/internal/services/compute/windows_virtual_machine_scale_set_resource.go b/internal/services/compute/windows_virtual_machine_scale_set_resource.go index 193c65afc2b7..0b06b4f265f6 100644 --- a/internal/services/compute/windows_virtual_machine_scale_set_resource.go +++ b/internal/services/compute/windows_virtual_machine_scale_set_resource.go @@ -1143,8 +1143,8 @@ func resourceWindowsVirtualMachineScaleSetDelete(d *pluginsdk.ResourceData, meta // 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. - scaleInOnDelete := meta.(*clients.Client).Features.VirtualMachineScaleSet.ScaleInOnDelete - if scaleInOnDelete && resp.Sku != nil { + scaleToZeroOnDelete := meta.(*clients.Client).Features.VirtualMachineScaleSet.ScaleToZeroOnDelete + if scaleToZeroOnDelete && 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") diff --git a/website/docs/index.html.markdown b/website/docs/index.html.markdown index be29d291d476..ace17cf0d0c3 100644 --- a/website/docs/index.html.markdown +++ b/website/docs/index.html.markdown @@ -255,4 +255,4 @@ The `virtual_machine_scale_set` block supports the following: * `roll_instances_when_required` - (Optional) Should the `azurerm_linux_virtual_machine_scale_set` and `azurerm_windows_virtual_machine_scale_set` resources automatically roll the instances in the Scale Set when Required (for example when updating the Sku/Image). Defaults to `true`. -* `scale_in_on_delete` - (Optional) Should the `azurerm_linux_virtual_machine_scale_set` and `azurerm_windows_virtual_machine_scale_set` resources scale to 0 instances before deleting the resource. Defaults to `true`. +* `scale_to_zero_before_deletion` - (Optional) Should the `azurerm_linux_virtual_machine_scale_set` and `azurerm_windows_virtual_machine_scale_set` resources scale to 0 instances before deleting the resource. Defaults to `true`.