Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_mssql_database - make short_term_retention_policy. backup_interval_in_hours computed #27656

Merged
merged 6 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading