-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
azurerm_mssql_database
- add support for `long_term_retention_polic…
…y` and `short_term_retention_policy` (#8765) Co-authored-by: Ramunas Leknickas <[email protected]> Co-authored-by: Ramunas <[email protected]> Co-authored-by: kt <[email protected]>
- Loading branch information
1 parent
a4b36ce
commit edacf0a
Showing
7 changed files
with
489 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
azurerm/internal/services/mssql/helper/sql_retention_policies.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package helper | ||
|
||
import ( | ||
"github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/v3.0/sql" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func LongTermRetentionPolicySchema() *schema.Schema { | ||
return &schema.Schema{ | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Computed: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
// WeeklyRetention - The weekly retention policy for an LTR backup in an ISO 8601 format. | ||
"weekly_retention": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ValidateFunc: azure.ValidateLongTermRetentionPoliciesIsoFormat, | ||
}, | ||
// MonthlyRetention - The monthly retention policy for an LTR backup in an ISO 8601 format. | ||
"monthly_retention": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ValidateFunc: azure.ValidateLongTermRetentionPoliciesIsoFormat, | ||
}, | ||
// YearlyRetention - The yearly retention policy for an LTR backup in an ISO 8601 format. | ||
"yearly_retention": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ValidateFunc: azure.ValidateLongTermRetentionPoliciesIsoFormat, | ||
}, | ||
// WeekOfYear - The week of year to take the yearly backup in an ISO 8601 format. | ||
"week_of_year": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Computed: true, | ||
ValidateFunc: validation.IntBetween(1, 52), | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func ShortTermRetentionPolicySchema() *schema.Schema { | ||
return &schema.Schema{ | ||
Type: schema.TypeList, | ||
Optional: true, | ||
Computed: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"retention_days": { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
ValidateFunc: validation.IntBetween(7, 35), | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func ExpandLongTermRetentionPolicy(input []interface{}) *sql.LongTermRetentionPolicyProperties { | ||
if len(input) == 0 || input[0] == nil { | ||
return nil | ||
} | ||
|
||
longTermRetentionPolicy := input[0].(map[string]interface{}) | ||
|
||
longTermPolicyProperties := sql.LongTermRetentionPolicyProperties{ | ||
WeeklyRetention: utils.String("PT0S"), | ||
MonthlyRetention: utils.String("PT0S"), | ||
YearlyRetention: utils.String("PT0S"), | ||
WeekOfYear: utils.Int32(0), | ||
} | ||
|
||
if v, ok := longTermRetentionPolicy["weekly_retention"]; ok { | ||
longTermPolicyProperties.WeeklyRetention = utils.String(v.(string)) | ||
} | ||
|
||
if v, ok := longTermRetentionPolicy["monthly_retention"]; ok { | ||
longTermPolicyProperties.MonthlyRetention = utils.String(v.(string)) | ||
} | ||
|
||
if v, ok := longTermRetentionPolicy["yearly_retention"]; ok { | ||
longTermPolicyProperties.YearlyRetention = utils.String(v.(string)) | ||
} | ||
|
||
if v, ok := longTermRetentionPolicy["week_of_year"]; ok { | ||
longTermPolicyProperties.WeekOfYear = utils.Int32(int32(v.(int))) | ||
} | ||
|
||
return &longTermPolicyProperties | ||
} | ||
|
||
func FlattenLongTermRetentionPolicy(longTermRetentionPolicy *sql.BackupLongTermRetentionPolicy, d *schema.ResourceData) []interface{} { | ||
if longTermRetentionPolicy == nil { | ||
return []interface{}{} | ||
} | ||
|
||
monthlyRetention := "PT0S" | ||
if longTermRetentionPolicy.MonthlyRetention != nil { | ||
monthlyRetention = *longTermRetentionPolicy.MonthlyRetention | ||
} | ||
|
||
weeklyRetention := "PT0S" | ||
if longTermRetentionPolicy.WeeklyRetention != nil { | ||
weeklyRetention = *longTermRetentionPolicy.WeeklyRetention | ||
} | ||
|
||
weekOfYear := int32(0) | ||
if longTermRetentionPolicy.WeekOfYear != nil { | ||
weekOfYear = *longTermRetentionPolicy.WeekOfYear | ||
} | ||
|
||
yearlyRetention := "PT0S" | ||
if longTermRetentionPolicy.YearlyRetention != nil { | ||
yearlyRetention = *longTermRetentionPolicy.YearlyRetention | ||
} | ||
|
||
return []interface{}{ | ||
map[string]interface{}{ | ||
"monthly_retention": monthlyRetention, | ||
"weekly_retention": weeklyRetention, | ||
"week_of_year": weekOfYear, | ||
"yearly_retention": yearlyRetention, | ||
}, | ||
} | ||
} | ||
|
||
func ExpandShortTermRetentionPolicy(input []interface{}) *sql.BackupShortTermRetentionPolicyProperties { | ||
if len(input) == 0 || input[0] == nil { | ||
return nil | ||
} | ||
|
||
shortTermRetentionPolicy := input[0].(map[string]interface{}) | ||
|
||
shortTermPolicyProperties := sql.BackupShortTermRetentionPolicyProperties{ | ||
RetentionDays: utils.Int32(7), | ||
} | ||
|
||
if v, ok := shortTermRetentionPolicy["retention_days"]; ok { | ||
shortTermPolicyProperties.RetentionDays = utils.Int32(int32(v.(int))) | ||
} | ||
|
||
return &shortTermPolicyProperties | ||
} | ||
|
||
func FlattenShortTermRetentionPolicy(shortTermRetentionPolicy *sql.BackupShortTermRetentionPolicy, d *schema.ResourceData) []interface{} { | ||
if shortTermRetentionPolicy == nil { | ||
return []interface{}{} | ||
} | ||
|
||
retentionDays := int32(7) | ||
if shortTermRetentionPolicy.RetentionDays != nil { | ||
retentionDays = *shortTermRetentionPolicy.RetentionDays | ||
} | ||
|
||
return []interface{}{ | ||
map[string]interface{}{ | ||
"retention_days": retentionDays, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.