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_managed_database - support for tags #27857

Merged
merged 6 commits into from
Nov 7, 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
15 changes: 15 additions & 0 deletions internal/services/mssql/helper/elasticpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,21 @@ var getvCoreMaxGB = map[string]map[string]map[int]float64{
40: 4096,
80: 4096,
},
"PRMS": {
4: 1024,
6: 1536,
8: 2048,
10: 2048,
12: 3072,
14: 3072,
16: 3072,
18: 3072,
20: 3072,
24: 4096,
32: 4096,
40: 4096,
80: 4096,
},
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/services/mssql/helper"
miParse "github.com/hashicorp/terraform-provider-azurerm/internal/services/mssqlmanagedinstance/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/mssqlmanagedinstance/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/suppress"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
Expand All @@ -31,6 +32,7 @@ type MsSqlManagedDatabaseModel struct {
LongTermRetentionPolicy []LongTermRetentionPolicy `tfschema:"long_term_retention_policy"`
ShortTermRetentionDays int64 `tfschema:"short_term_retention_days"`
PointInTimeRestore []PointInTimeRestore `tfschema:"point_in_time_restore"`
Tags map[string]string `tfschema:"tags"`
}

type LongTermRetentionPolicy struct {
Expand Down Expand Up @@ -110,6 +112,8 @@ func (r MsSqlManagedDatabaseResource) Arguments() map[string]*pluginsdk.Schema {
},
},
},

"tags": tags.Schema(),
}
}

Expand Down Expand Up @@ -156,16 +160,16 @@ func (r MsSqlManagedDatabaseResource) Create() sdk.ResourceFunc {

parameters := manageddatabases.ManagedDatabase{
Location: managedInstance.Model.Location,
Properties: &manageddatabases.ManagedDatabaseProperties{},
Properties: pointer.To(manageddatabases.ManagedDatabaseProperties{}),
Tags: pointer.To(model.Tags),
}

if len(model.PointInTimeRestore) > 0 {
restorePointInTime := model.PointInTimeRestore[0]
parameters.Properties.CreateMode = pointer.To(manageddatabases.ManagedDatabaseCreateModePointInTimeRestore)
parameters.Properties.RestorePointInTime = &restorePointInTime.RestorePointInTime

_, err := miParse.RestorableDroppedDatabaseID(restorePointInTime.SourceDatabaseId)
if err == nil {
if _, err := miParse.RestorableDroppedDatabaseID(restorePointInTime.SourceDatabaseId); err == nil {
parameters.Properties.RestorableDroppedDatabaseId = pointer.To(restorePointInTime.SourceDatabaseId)
} else {
parameters.Properties.SourceDatabaseId = pointer.To(restorePointInTime.SourceDatabaseId)
Expand All @@ -180,10 +184,8 @@ func (r MsSqlManagedDatabaseResource) Create() sdk.ResourceFunc {
}

if len(model.LongTermRetentionPolicy) > 0 {
longTermRetentionProps := expandLongTermRetentionPolicy(model.LongTermRetentionPolicy)

longTermRetentionPolicy := managedinstancelongtermretentionpolicies.ManagedInstanceLongTermRetentionPolicy{
Properties: &longTermRetentionProps,
Properties: pointer.To(expandLongTermRetentionPolicy(model.LongTermRetentionPolicy)),
}

err := longTermRetentionClient.CreateOrUpdateThenPoll(ctx, id, longTermRetentionPolicy)
Expand All @@ -193,7 +195,6 @@ func (r MsSqlManagedDatabaseResource) Create() sdk.ResourceFunc {
}

if model.ShortTermRetentionDays > 0 {

shortTermRetentionPolicy := managedbackupshorttermretentionpolicies.ManagedBackupShortTermRetentionPolicy{
Properties: &managedbackupshorttermretentionpolicies.ManagedBackupShortTermRetentionPolicyProperties{
RetentionDays: pointer.To(model.ShortTermRetentionDays),
Expand All @@ -215,6 +216,8 @@ func (r MsSqlManagedDatabaseResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.MSSQLManagedInstance.ManagedDatabasesClient
instancesClient := metadata.Client.MSSQLManagedInstance.ManagedInstancesClient
longTermRetentionClient := metadata.Client.MSSQLManagedInstance.ManagedInstancesLongTermRetentionPoliciesClient
shortTermRetentionClient := metadata.Client.MSSQLManagedInstance.ManagedInstancesShortTermRetentionPoliciesClient

Expand All @@ -233,11 +236,26 @@ func (r MsSqlManagedDatabaseResource) Update() sdk.ResourceFunc {

d := metadata.ResourceData

if d.HasChange("long_term_retention_policy") {
longTermRetentionProps := expandLongTermRetentionPolicy(model.LongTermRetentionPolicy)
if d.HasChange("tags") {
managedInstance, err := instancesClient.Get(ctx, *managedInstanceId, managedinstances.GetOperationOptions{})
if err != nil || managedInstance.Model == nil || managedInstance.Model.Location == "" {
return fmt.Errorf("checking for existence and region of Managed Instance for %s: %+v", id, err)
}

parameters := manageddatabases.ManagedDatabase{
Location: managedInstance.Model.Location,
Tags: pointer.To(model.Tags),
}

err = client.CreateOrUpdateThenPoll(ctx, id, parameters)
if err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}
}

if d.HasChange("long_term_retention_policy") {
longTermRetentionPolicy := managedinstancelongtermretentionpolicies.ManagedInstanceLongTermRetentionPolicy{
Properties: &longTermRetentionProps,
Properties: pointer.To(expandLongTermRetentionPolicy(model.LongTermRetentionPolicy)),
}

err := longTermRetentionClient.CreateOrUpdateThenPoll(ctx, id, longTermRetentionPolicy)
Expand All @@ -247,7 +265,6 @@ func (r MsSqlManagedDatabaseResource) Update() sdk.ResourceFunc {
}

if d.HasChange("short_term_retention_days") {

shortTermRetentionPolicy := managedbackupshorttermretentionpolicies.ManagedBackupShortTermRetentionPolicy{
Properties: &managedbackupshorttermretentionpolicies.ManagedBackupShortTermRetentionPolicyProperties{
RetentionDays: pointer.To(model.ShortTermRetentionDays),
Expand Down Expand Up @@ -319,6 +336,8 @@ func (r MsSqlManagedDatabaseResource) Read() sdk.ResourceFunc {
model.PointInTimeRestore = flattenManagedDatabasePointInTimeRestore(v)
}

model.Tags = pointer.From(result.Model.Tags)

return metadata.Encode(&model)
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,50 @@ func TestAccMsSqlManagedDatabase_basic(t *testing.T) {
})
}

func TestAccMsSqlManagedDatabase_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_managed_database", "test")
r := MsSqlManagedDatabase{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(""),
})
}

func TestAccMsSqlManagedDatabase_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_managed_database", "test")
r := MsSqlManagedDatabase{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(""),
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(""),
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(""),
})
}

func TestAccMsSqlManagedDatabase_withRetentionPolicies(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_mssql_managed_database", "test")
r := MsSqlManagedDatabase{}
Expand Down Expand Up @@ -103,6 +147,18 @@ resource "azurerm_mssql_managed_database" "test" {
`, MsSqlManagedInstanceResource{}.basic(data), data.RandomInteger)
}

func (r MsSqlManagedDatabase) complete(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s

resource "azurerm_mssql_managed_database" "test" {
managed_instance_id = azurerm_mssql_managed_instance.test.id
name = "acctest-%[2]d"
tags = { Environment = "Testing" }
}
`, MsSqlManagedInstanceResource{}.basic(data), data.RandomInteger)
}

func (r MsSqlManagedDatabase) withRetentionPolicies(data acceptance.TestData) string {
return fmt.Sprintf(`
%[1]s
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/mssql_managed_database.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ The following arguments are supported:

* `point_in_time_restore` - (Optional) A `point_in_time_restore` block as defined below. Changing this forces a new resource to be created.

* `tags` - (Optional) A mapping of tags to assign to the resource.

---

A `long_term_retention_policy` block supports the following:
Expand Down
Loading