Skip to content

Commit

Permalink
azurerm_mssql_database - make `short_term_retention_policy. backup_in…
Browse files Browse the repository at this point in the history
…terval_in_hours` computed (#27656)

* fix backup_interval_in_hours schema

* lint

* fix backup_interval_in_hours schema

* make backup_interval_in_hours computed

* remove DiffFunc and fix test

* tidy up
  • Loading branch information
catriona-m authored Oct 17, 2024
1 parent b15bf85 commit 1421af4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
16 changes: 1 addition & 15 deletions internal/services/mssql/helper/sql_retention_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
package helper

import (
"strconv"
"strings"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/backupshorttermretentionpolicies"
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/longtermretentionpolicies"
Expand Down Expand Up @@ -83,19 +80,8 @@ func ShortTermRetentionPolicySchema() *pluginsdk.Schema {
"backup_interval_in_hours": {
Type: pluginsdk.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validation.IntInSlice([]int{12, 24}),
Default: 12,
// HyperScale SKus can't set `backup_interval_in_hours so we'll ignore that value when it is 0 in the state file so we don't break the Default Value for existing users
DiffSuppressFunc: func(_, old, _ string, d *pluginsdk.ResourceData) bool {
skuName, ok := d.GetOk("sku_name")
if ok {
if strings.HasPrefix(skuName.(string), "HS") {
oldInt, _ := strconv.Atoi(old)
return oldInt == 0
}
}
return false
},
},
},
},
Expand Down
24 changes: 13 additions & 11 deletions internal/services/mssql/mssql_database_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,33 +598,35 @@ func resourceMsSqlDatabaseCreate(d *pluginsdk.ResourceData, meta interface{}) er
return nil
}

securityAlertPolicyProps := helper.ExpandLongTermRetentionPolicy(d.Get("long_term_retention_policy").([]interface{}))
if securityAlertPolicyProps != nil {
securityAlertPolicyPayload := longtermretentionpolicies.LongTermRetentionPolicy{}
longTermRetentionPolicyProps := helper.ExpandLongTermRetentionPolicy(d.Get("long_term_retention_policy").([]interface{}))
if longTermRetentionPolicyProps != nil {
longTermRetentionPolicyPayload := longtermretentionpolicies.LongTermRetentionPolicy{}

// DataWarehouse SKUs do not support LRP currently
if !isDwSku {
securityAlertPolicyPayload.Properties = securityAlertPolicyProps
longTermRetentionPolicyPayload.Properties = longTermRetentionPolicyProps
}

if err := longTermRetentionClient.CreateOrUpdateThenPoll(ctx, id, securityAlertPolicyPayload); err != nil {
if err := longTermRetentionClient.CreateOrUpdateThenPoll(ctx, id, longTermRetentionPolicyPayload); err != nil {
return fmt.Errorf("setting Long Term Retention Policies for %s: %+v", id, err)
}
}

shortTermSecurityAlertPolicyProps := helper.ExpandShortTermRetentionPolicy(d.Get("short_term_retention_policy").([]interface{}))
if shortTermSecurityAlertPolicyProps != nil {
securityAlertPolicyPayload := backupshorttermretentionpolicies.BackupShortTermRetentionPolicy{}
shortTermRetentionPolicyProps := helper.ExpandShortTermRetentionPolicy(d.Get("short_term_retention_policy").([]interface{}))
if shortTermRetentionPolicyProps != nil {
shortTermRetentionPolicyPayload := backupshorttermretentionpolicies.BackupShortTermRetentionPolicy{}

if !isDwSku {
securityAlertPolicyPayload.Properties = shortTermSecurityAlertPolicyProps
shortTermRetentionPolicyPayload.Properties = shortTermRetentionPolicyProps
}

if strings.HasPrefix(skuName, "HS") || strings.HasPrefix(elasticPoolSku, "HS") {
securityAlertPolicyPayload.Properties.DiffBackupIntervalInHours = nil
shortTermRetentionPolicyPayload.Properties.DiffBackupIntervalInHours = nil
} else if shortTermRetentionPolicyProps.DiffBackupIntervalInHours == nil || pointer.From(shortTermRetentionPolicyProps.DiffBackupIntervalInHours) == 0 {
shortTermRetentionPolicyPayload.Properties.DiffBackupIntervalInHours = pointer.To(backupshorttermretentionpolicies.DiffBackupIntervalInHoursOneTwo)
}

if err := shortTermRetentionClient.CreateOrUpdateThenPoll(ctx, id, securityAlertPolicyPayload); err != nil {
if err := shortTermRetentionClient.CreateOrUpdateThenPoll(ctx, id, shortTermRetentionPolicyPayload); err != nil {
return fmt.Errorf("setting Short Term Retention Policies for %s: %+v", id, err)
}
}
Expand Down

0 comments on commit 1421af4

Please sign in to comment.