Skip to content

Commit

Permalink
allow permanent delete of log_analytics_workspace_resource (#10235)
Browse files Browse the repository at this point in the history
Co-authored-by: kt <[email protected]>

Allow permanent delete of log_analytics_workspace_resource without go to recycle bin.
  • Loading branch information
lde authored Mar 5, 2021
1 parent ddbb37a commit af6a75c
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 3 deletions.
3 changes: 3 additions & 0 deletions azurerm/internal/features/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ func Default() UserFeatures {
PurgeSoftDeleteOnDestroy: true,
RecoverSoftDeletedKeyVaults: true,
},
LogAnalyticsWorkspace: LogAnalyticsWorkspaceFeatures{
PermanentlyDeleteOnDestroy: false,
},
Network: NetworkFeatures{
RelaxedLocking: false,
},
Expand Down
5 changes: 5 additions & 0 deletions azurerm/internal/features/user_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type UserFeatures struct {
KeyVault KeyVaultFeatures
Network NetworkFeatures
TemplateDeployment TemplateDeploymentFeatures
LogAnalyticsWorkspace LogAnalyticsWorkspaceFeatures
}

type VirtualMachineFeatures struct {
Expand All @@ -29,3 +30,7 @@ type NetworkFeatures struct {
type TemplateDeploymentFeatures struct {
DeleteNestedItemsDuringDeletion bool
}

type LogAnalyticsWorkspaceFeatures struct {
PermanentlyDeleteOnDestroy bool
}
23 changes: 23 additions & 0 deletions azurerm/internal/provider/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ func schemaFeatures(supportLegacyTestSuite bool) *schema.Schema {
},
},
},
"log_analytics_workspace": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"permanently_delete_on_destroy": {
Type: schema.TypeBool,
Required: true,
},
},
},
},

"network": {
Type: schema.TypeList,
Expand Down Expand Up @@ -134,6 +147,16 @@ func expandFeatures(input []interface{}) features.UserFeatures {
}
}

if raw, ok := val["log_analytics_workspace"]; ok {
items := raw.([]interface{})
if len(items) > 0 {
logAnalyticsWorkspaceRaw := items[0].(map[string]interface{})
if v, ok := logAnalyticsWorkspaceRaw["permanently_delete_on_destroy"]; ok {
features.LogAnalyticsWorkspace.PermanentlyDeleteOnDestroy = v.(bool)
}
}
}

if raw, ok := val["network"]; ok {
items := raw.([]interface{})
if len(items) > 0 {
Expand Down
83 changes: 83 additions & 0 deletions azurerm/internal/provider/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func TestExpandFeatures(t *testing.T) {
VirtualMachineScaleSet: features.VirtualMachineScaleSetFeatures{
RollInstancesWhenRequired: true,
},
LogAnalyticsWorkspace: features.LogAnalyticsWorkspaceFeatures{
PermanentlyDeleteOnDestroy: false,
},
},
},
{
Expand All @@ -46,6 +49,11 @@ func TestExpandFeatures(t *testing.T) {
"recover_soft_deleted_key_vaults": true,
},
},
"log_analytics_workspace": []interface{}{
map[string]interface{}{
"permanently_delete_on_destroy": true,
},
},
"network": []interface{}{
map[string]interface{}{
"relaxed_locking": true,
Expand Down Expand Up @@ -74,6 +82,9 @@ func TestExpandFeatures(t *testing.T) {
PurgeSoftDeleteOnDestroy: true,
RecoverSoftDeletedKeyVaults: true,
},
LogAnalyticsWorkspace: features.LogAnalyticsWorkspaceFeatures{
PermanentlyDeleteOnDestroy: true,
},
Network: features.NetworkFeatures{
RelaxedLocking: true,
},
Expand Down Expand Up @@ -120,13 +131,21 @@ func TestExpandFeatures(t *testing.T) {
"recover_soft_deleted_key_vaults": false,
},
},
"log_analytics_workspace": []interface{}{
map[string]interface{}{
"permanently_delete_on_destroy": false,
},
},
},
},
Expected: features.UserFeatures{
KeyVault: features.KeyVaultFeatures{
PurgeSoftDeleteOnDestroy: false,
RecoverSoftDeletedKeyVaults: false,
},
LogAnalyticsWorkspace: features.LogAnalyticsWorkspaceFeatures{
PermanentlyDeleteOnDestroy: false,
},
Network: features.NetworkFeatures{
RelaxedLocking: false,
},
Expand Down Expand Up @@ -487,3 +506,67 @@ func TestExpandFeaturesVirtualMachineScaleSet(t *testing.T) {
}
}
}

func TestExpandFeaturesLogAnalyticsWorkspace(t *testing.T) {
testData := []struct {
Name string
Input []interface{}
EnvVars map[string]interface{}
Expected features.UserFeatures
}{
{
Name: "Empty Block",
Input: []interface{}{
map[string]interface{}{
"log_analytics_workspace": []interface{}{},
},
},
Expected: features.UserFeatures{
LogAnalyticsWorkspace: features.LogAnalyticsWorkspaceFeatures{
PermanentlyDeleteOnDestroy: false,
},
},
},
{
Name: "Permanent Delete Enabled",
Input: []interface{}{
map[string]interface{}{
"log_analytics_workspace": []interface{}{
map[string]interface{}{
"permanently_delete_on_destroy": true,
},
},
},
},
Expected: features.UserFeatures{
LogAnalyticsWorkspace: features.LogAnalyticsWorkspaceFeatures{
PermanentlyDeleteOnDestroy: true,
},
},
},
{
Name: "Permanent Delete Disabled",
Input: []interface{}{
map[string]interface{}{
"log_analytics_workspace": []interface{}{
map[string]interface{}{
"permanently_delete_on_destroy": false,
},
},
},
},
Expected: features.UserFeatures{
LogAnalyticsWorkspace: features.LogAnalyticsWorkspaceFeatures{
PermanentlyDeleteOnDestroy: false,
},
},
},
}
for _, testCase := range testData {
t.Logf("[DEBUG] Test Case: %q", testCase.Name)
result := expandFeatures(testCase.Input)
if !reflect.DeepEqual(result.LogAnalyticsWorkspace, testCase.Expected.LogAnalyticsWorkspace) {
t.Fatalf("Expected %+v but got %+v", result.LogAnalyticsWorkspace, testCase.Expected.LogAnalyticsWorkspace)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,8 @@ func resourceLogAnalyticsWorkspaceDelete(d *schema.ResourceData, meta interface{
if err != nil {
return err
}

force := false
future, err := client.Delete(ctx, id.ResourceGroup, id.WorkspaceName, utils.Bool(force))
PermanentlyDeleteOnDestroy := meta.(*clients.Client).Features.LogAnalyticsWorkspace.PermanentlyDeleteOnDestroy
future, err := client.Delete(ctx, id.ResourceGroup, id.WorkspaceName, utils.Bool(PermanentlyDeleteOnDestroy))
if err != nil {
return fmt.Errorf("issuing AzureRM delete request for Log Analytics Workspaces '%s': %+v", id.WorkspaceName, err)
}
Expand Down
8 changes: 8 additions & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ The `features` block supports the following:

* `key_vault` - (Optional) A `key_vault` block as defined below.

* `log_analytics_workspace` - (Optional) A `log_analytics_workspace` block as defined below.

* `template_deployment` - (Optional) A `template_deployment` block as defined below.

* `virtual_machine` - (Optional) A `virtual_machine` block as defined below.
Expand All @@ -172,6 +174,12 @@ The `features` block supports the following:

---

The `log_analytics_workspace` block supports the following:

* `permanently_delete_on_destroy` - (Optional) Should the `azurerm_log_analytics_workspace` be permanently deleted (e.g. purged) when destroyed? Defaults to `false`.

---

The `key_vault` block supports the following:

* `recover_soft_deleted_key_vaults` - (Optional) Should the `azurerm_key_vault`, `azurerm_key_vault_certificate`, `azurerm_key_vault_key` and `azurerm_key_vault_secret` resources recover a Soft-Deleted Key Vault/Item? Defaults to `true`.
Expand Down

0 comments on commit af6a75c

Please sign in to comment.