Skip to content

Commit

Permalink
r/(linux|windows)_virtual_machine(_scale_set): support for Force Dele…
Browse files Browse the repository at this point in the history
…te (hashicorp#11216)

Co-authored-by: kt <[email protected]>
Fixes hashicorp#11089
  • Loading branch information
tombuildsstuff authored and yupwei68 committed Jul 26, 2021
1 parent 2f6d2e4 commit 9e74452
Show file tree
Hide file tree
Showing 15 changed files with 455 additions and 86 deletions.
6 changes: 4 additions & 2 deletions azurerm/internal/features/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ func Default() UserFeatures {
DeleteNestedItemsDuringDeletion: true,
},
VirtualMachine: VirtualMachineFeatures{
DeleteOSDiskOnDeletion: true,
GracefulShutdown: false,
DeleteOSDiskOnDeletion: true,
GracefulShutdown: false,
SkipShutdownAndForceDelete: false,
},
VirtualMachineScaleSet: VirtualMachineScaleSetFeatures{
ForceDelete: false,
RollInstancesWhenRequired: true,
},
}
Expand Down
6 changes: 4 additions & 2 deletions azurerm/internal/features/user_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ type UserFeatures struct {
}

type VirtualMachineFeatures struct {
DeleteOSDiskOnDeletion bool
GracefulShutdown bool
DeleteOSDiskOnDeletion bool
GracefulShutdown bool
SkipShutdownAndForceDelete bool
}

type VirtualMachineScaleSetFeatures struct {
ForceDelete bool
RollInstancesWhenRequired bool
}

Expand Down
15 changes: 15 additions & 0 deletions azurerm/internal/provider/features.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package provider

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk"
)
Expand Down Expand Up @@ -84,6 +85,10 @@ func schemaFeatures(supportLegacyTestSuite bool) *pluginsdk.Schema {
Type: pluginsdk.TypeBool,
Optional: true,
},
"skip_shutdown_and_force_delete": {
Type: schema.TypeBool,
Optional: true,
},
},
},
},
Expand All @@ -94,6 +99,10 @@ func schemaFeatures(supportLegacyTestSuite bool) *pluginsdk.Schema {
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"force_delete": {
Type: pluginsdk.TypeBool,
Optional: true,
},
"roll_instances_when_required": {
Type: pluginsdk.TypeBool,
Required: true,
Expand Down Expand Up @@ -189,6 +198,9 @@ func expandFeatures(input []interface{}) features.UserFeatures {
if v, ok := virtualMachinesRaw["graceful_shutdown"]; ok {
features.VirtualMachine.GracefulShutdown = v.(bool)
}
if v, ok := virtualMachinesRaw["skip_shutdown_and_force_delete"]; ok {
features.VirtualMachine.SkipShutdownAndForceDelete = v.(bool)
}
}
}

Expand All @@ -199,6 +211,9 @@ func expandFeatures(input []interface{}) features.UserFeatures {
if v, ok := scaleSetRaw["roll_instances_when_required"]; ok {
features.VirtualMachineScaleSet.RollInstancesWhenRequired = v.(bool)
}
if v, ok := scaleSetRaw["force_delete"]; ok {
features.VirtualMachineScaleSet.ForceDelete = v.(bool)
}
}
}

Expand Down
150 changes: 116 additions & 34 deletions azurerm/internal/provider/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,24 @@ func TestExpandFeatures(t *testing.T) {
PurgeSoftDeleteOnDestroy: true,
RecoverSoftDeletedKeyVaults: true,
},
LogAnalyticsWorkspace: features.LogAnalyticsWorkspaceFeatures{
PermanentlyDeleteOnDestroy: false,
},
Network: features.NetworkFeatures{
RelaxedLocking: false,
},
TemplateDeployment: features.TemplateDeploymentFeatures{
DeleteNestedItemsDuringDeletion: true,
},
VirtualMachine: features.VirtualMachineFeatures{
DeleteOSDiskOnDeletion: true,
DeleteOSDiskOnDeletion: true,
GracefulShutdown: false,
SkipShutdownAndForceDelete: false,
},
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: false,
RollInstancesWhenRequired: true,
},
LogAnalyticsWorkspace: features.LogAnalyticsWorkspaceFeatures{
PermanentlyDeleteOnDestroy: false,
},
},
},
{
Expand Down Expand Up @@ -66,13 +69,15 @@ func TestExpandFeatures(t *testing.T) {
},
"virtual_machine": []interface{}{
map[string]interface{}{
"delete_os_disk_on_deletion": true,
"graceful_shutdown": true,
"delete_os_disk_on_deletion": true,
"graceful_shutdown": true,
"skip_shutdown_and_force_delete": true,
},
},
"virtual_machine_scale_set": []interface{}{
map[string]interface{}{
"roll_instances_when_required": true,
"force_delete": true,
},
},
},
Expand All @@ -92,22 +97,29 @@ func TestExpandFeatures(t *testing.T) {
DeleteNestedItemsDuringDeletion: true,
},
VirtualMachine: features.VirtualMachineFeatures{
DeleteOSDiskOnDeletion: true,
GracefulShutdown: true,
DeleteOSDiskOnDeletion: true,
GracefulShutdown: true,
SkipShutdownAndForceDelete: true,
},
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
RollInstancesWhenRequired: true,
ForceDelete: true,
},
},
},
{
Name: "Complete Disabled",
Input: []interface{}{
map[string]interface{}{
"virtual_machine": []interface{}{
"key_vault": []interface{}{
map[string]interface{}{
"delete_os_disk_on_deletion": false,
"graceful_shutdown": false,
"purge_soft_delete_on_destroy": false,
"recover_soft_deleted_key_vaults": false,
},
},
"log_analytics_workspace": []interface{}{
map[string]interface{}{
"permanently_delete_on_destroy": false,
},
},
"network_locking": []interface{}{
Expand All @@ -120,20 +132,17 @@ func TestExpandFeatures(t *testing.T) {
"delete_nested_items_during_deletion": false,
},
},
"virtual_machine_scale_set": []interface{}{
map[string]interface{}{
"roll_instances_when_required": false,
},
},
"key_vault": []interface{}{
"virtual_machine": []interface{}{
map[string]interface{}{
"purge_soft_delete_on_destroy": false,
"recover_soft_deleted_key_vaults": false,
"delete_os_disk_on_deletion": false,
"graceful_shutdown": false,
"skip_shutdown_and_force_delete": false,
},
},
"log_analytics_workspace": []interface{}{
"virtual_machine_scale_set": []interface{}{
map[string]interface{}{
"permanently_delete_on_destroy": false,
"force_delete": false,
"roll_instances_when_required": false,
},
},
},
Expand All @@ -153,10 +162,12 @@ func TestExpandFeatures(t *testing.T) {
DeleteNestedItemsDuringDeletion: false,
},
VirtualMachine: features.VirtualMachineFeatures{
DeleteOSDiskOnDeletion: false,
GracefulShutdown: false,
DeleteOSDiskOnDeletion: false,
GracefulShutdown: false,
SkipShutdownAndForceDelete: false,
},
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: false,
RollInstancesWhenRequired: false,
},
},
Expand Down Expand Up @@ -388,46 +399,94 @@ func TestExpandFeaturesVirtualMachine(t *testing.T) {
},
Expected: features.UserFeatures{
VirtualMachine: features.VirtualMachineFeatures{
DeleteOSDiskOnDeletion: true,
GracefulShutdown: false,
DeleteOSDiskOnDeletion: true,
GracefulShutdown: false,
SkipShutdownAndForceDelete: false,
},
},
},
{
Name: "Delete OS Disk and Graceful Shutdown Enabled",
Name: "Delete OS Disk Enabled",
Input: []interface{}{
map[string]interface{}{
"virtual_machine": []interface{}{
map[string]interface{}{
"delete_os_disk_on_deletion": true,
"graceful_shutdown": true,
"graceful_shutdown": false,
"force_delete": false,
"shutdown_before_deletion": false,
},
},
},
},
Expected: features.UserFeatures{
VirtualMachine: features.VirtualMachineFeatures{
DeleteOSDiskOnDeletion: true,
GracefulShutdown: true,
DeleteOSDiskOnDeletion: true,
GracefulShutdown: false,
SkipShutdownAndForceDelete: false,
},
},
},
{
Name: "Delete OS Disk and Graceful Shutdown Disabled",
Name: "Graceful Shutdown Enabled",
Input: []interface{}{
map[string]interface{}{
"virtual_machine": []interface{}{
map[string]interface{}{
"delete_os_disk_on_deletion": false,
"graceful_shutdown": false,
"graceful_shutdown": true,
"force_delete": false,
},
},
},
},
Expected: features.UserFeatures{
VirtualMachine: features.VirtualMachineFeatures{
DeleteOSDiskOnDeletion: false,
GracefulShutdown: true,
SkipShutdownAndForceDelete: false,
},
},
},
{
Name: "Skip Shutdown and Force Delete Enabled",
Input: []interface{}{
map[string]interface{}{
"virtual_machine": []interface{}{
map[string]interface{}{
"delete_os_disk_on_deletion": false,
"graceful_shutdown": false,
"skip_shutdown_and_force_delete": true,
},
},
},
},
Expected: features.UserFeatures{
VirtualMachine: features.VirtualMachineFeatures{
DeleteOSDiskOnDeletion: false,
GracefulShutdown: false,
SkipShutdownAndForceDelete: true,
},
},
},
{
Name: "All Disabled",
Input: []interface{}{
map[string]interface{}{
"virtual_machine": []interface{}{
map[string]interface{}{
"delete_os_disk_on_deletion": false,
"graceful_shutdown": false,
"skip_shutdown_and_force_delete": false,
},
},
},
},
Expected: features.UserFeatures{
VirtualMachine: features.VirtualMachineFeatures{
DeleteOSDiskOnDeletion: false,
GracefulShutdown: false,
DeleteOSDiskOnDeletion: false,
GracefulShutdown: false,
SkipShutdownAndForceDelete: false,
},
},
},
Expand Down Expand Up @@ -462,36 +521,59 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) {
},
},
},
{
Name: "Force Delete Enabled",
Input: []interface{}{
map[string]interface{}{
"virtual_machine_scale_set": []interface{}{
map[string]interface{}{
"force_delete": true,
"roll_instances_when_required": false,
},
},
},
},
Expected: features.UserFeatures{
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: true,
RollInstancesWhenRequired: false,
},
},
},
{
Name: "Roll Instances Enabled",
Input: []interface{}{
map[string]interface{}{
"virtual_machine_scale_set": []interface{}{
map[string]interface{}{
"force_delete": false,
"roll_instances_when_required": true,
},
},
},
},
Expected: features.UserFeatures{
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: false,
RollInstancesWhenRequired: true,
},
},
},
{
Name: "Roll Instances Disabled",
Name: "All Fields Disabled",
Input: []interface{}{
map[string]interface{}{
"virtual_machine_scale_set": []interface{}{
map[string]interface{}{
"force_delete": false,
"roll_instances_when_required": false,
},
},
},
},
Expected: features.UserFeatures{
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
ForceDelete: false,
RollInstancesWhenRequired: false,
},
},
Expand Down
Loading

0 comments on commit 9e74452

Please sign in to comment.