From 6553a20e9cf614b5a1a363c4d65afd7f2260c108 Mon Sep 17 00:00:00 2001 From: kt Date: Fri, 1 Nov 2024 12:36:58 -0700 Subject: [PATCH 1/6] azurerm_mssql_managed_database - support for tags --- .../mssql_managed_database_resource.go | 24 +++++++- .../mssql_managed_database_resource_test.go | 56 +++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/internal/services/mssqlmanagedinstance/mssql_managed_database_resource.go b/internal/services/mssqlmanagedinstance/mssql_managed_database_resource.go index 6803b6fdc4b8..1e7d7d0a52b0 100644 --- a/internal/services/mssqlmanagedinstance/mssql_managed_database_resource.go +++ b/internal/services/mssqlmanagedinstance/mssql_managed_database_resource.go @@ -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" @@ -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 { @@ -110,6 +112,8 @@ func (r MsSqlManagedDatabaseResource) Arguments() map[string]*pluginsdk.Schema { }, }, }, + + "tags": tags.Schema(), } } @@ -157,6 +161,7 @@ func (r MsSqlManagedDatabaseResource) Create() sdk.ResourceFunc { parameters := manageddatabases.ManagedDatabase{ Location: managedInstance.Model.Location, Properties: &manageddatabases.ManagedDatabaseProperties{}, + Tags: &model.Tags, } if len(model.PointInTimeRestore) > 0 { @@ -164,8 +169,7 @@ func (r MsSqlManagedDatabaseResource) Create() sdk.ResourceFunc { 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) @@ -215,6 +219,7 @@ 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 longTermRetentionClient := metadata.Client.MSSQLManagedInstance.ManagedInstancesLongTermRetentionPoliciesClient shortTermRetentionClient := metadata.Client.MSSQLManagedInstance.ManagedInstancesShortTermRetentionPoliciesClient @@ -233,6 +238,17 @@ func (r MsSqlManagedDatabaseResource) Update() sdk.ResourceFunc { d := metadata.ResourceData + if d.HasChange("tags") { + parameters := manageddatabases.ManagedDatabase{ + Tags: &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") { longTermRetentionProps := expandLongTermRetentionPolicy(model.LongTermRetentionPolicy) @@ -319,6 +335,10 @@ func (r MsSqlManagedDatabaseResource) Read() sdk.ResourceFunc { model.PointInTimeRestore = flattenManagedDatabasePointInTimeRestore(v) } + if model.Tags != nil { + state.Tags = model.Tags + } + return metadata.Encode(&model) }, } diff --git a/internal/services/mssqlmanagedinstance/mssql_managed_database_resource_test.go b/internal/services/mssqlmanagedinstance/mssql_managed_database_resource_test.go index f2b0739ac601..c28496d833aa 100644 --- a/internal/services/mssqlmanagedinstance/mssql_managed_database_resource_test.go +++ b/internal/services/mssqlmanagedinstance/mssql_managed_database_resource_test.go @@ -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{} @@ -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 From f08262209b9ecfade34a5beba5c896d90b0903a5 Mon Sep 17 00:00:00 2001 From: kt Date: Fri, 1 Nov 2024 12:52:31 -0700 Subject: [PATCH 2/6] make terrafmt --- .../mssql_managed_database_resource_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/mssqlmanagedinstance/mssql_managed_database_resource_test.go b/internal/services/mssqlmanagedinstance/mssql_managed_database_resource_test.go index c28496d833aa..41cecfbdac5f 100644 --- a/internal/services/mssqlmanagedinstance/mssql_managed_database_resource_test.go +++ b/internal/services/mssqlmanagedinstance/mssql_managed_database_resource_test.go @@ -154,7 +154,7 @@ func (r MsSqlManagedDatabase) complete(data acceptance.TestData) string { resource "azurerm_mssql_managed_database" "test" { managed_instance_id = azurerm_mssql_managed_instance.test.id name = "acctest-%[2]d" - tags = { Environment = "Testing" } + tags = { Environment = "Testing" } } `, MsSqlManagedInstanceResource{}.basic(data), data.RandomInteger) } From e08478528c09156a2e399d440f31c4d7db65bb62 Mon Sep 17 00:00:00 2001 From: kt Date: Fri, 1 Nov 2024 13:33:24 -0700 Subject: [PATCH 3/6] point.to --- .../mssql_managed_database_resource.go | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/internal/services/mssqlmanagedinstance/mssql_managed_database_resource.go b/internal/services/mssqlmanagedinstance/mssql_managed_database_resource.go index 1e7d7d0a52b0..8c9b686ae487 100644 --- a/internal/services/mssqlmanagedinstance/mssql_managed_database_resource.go +++ b/internal/services/mssqlmanagedinstance/mssql_managed_database_resource.go @@ -160,8 +160,8 @@ func (r MsSqlManagedDatabaseResource) Create() sdk.ResourceFunc { parameters := manageddatabases.ManagedDatabase{ Location: managedInstance.Model.Location, - Properties: &manageddatabases.ManagedDatabaseProperties{}, - Tags: &model.Tags, + Properties: pointer.To(manageddatabases.ManagedDatabaseProperties{}), + Tags: pointer.To(model.Tags), } if len(model.PointInTimeRestore) > 0 { @@ -184,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) @@ -197,7 +195,6 @@ func (r MsSqlManagedDatabaseResource) Create() sdk.ResourceFunc { } if model.ShortTermRetentionDays > 0 { - shortTermRetentionPolicy := managedbackupshorttermretentionpolicies.ManagedBackupShortTermRetentionPolicy{ Properties: &managedbackupshorttermretentionpolicies.ManagedBackupShortTermRetentionPolicyProperties{ RetentionDays: pointer.To(model.ShortTermRetentionDays), @@ -240,7 +237,7 @@ func (r MsSqlManagedDatabaseResource) Update() sdk.ResourceFunc { if d.HasChange("tags") { parameters := manageddatabases.ManagedDatabase{ - Tags: &model.Tags, + Tags: pointer.To(model.Tags), } err = client.CreateOrUpdateThenPoll(ctx, id, parameters) @@ -250,10 +247,8 @@ func (r MsSqlManagedDatabaseResource) Update() sdk.ResourceFunc { } if d.HasChange("long_term_retention_policy") { - longTermRetentionProps := expandLongTermRetentionPolicy(model.LongTermRetentionPolicy) - longTermRetentionPolicy := managedinstancelongtermretentionpolicies.ManagedInstanceLongTermRetentionPolicy{ - Properties: &longTermRetentionProps, + Properties: pointer.To(expandLongTermRetentionPolicy(model.LongTermRetentionPolicy)), } err := longTermRetentionClient.CreateOrUpdateThenPoll(ctx, id, longTermRetentionPolicy) @@ -263,7 +258,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), @@ -335,9 +329,7 @@ func (r MsSqlManagedDatabaseResource) Read() sdk.ResourceFunc { model.PointInTimeRestore = flattenManagedDatabasePointInTimeRestore(v) } - if model.Tags != nil { - state.Tags = model.Tags - } + state.Tags = model.Tags return metadata.Encode(&model) }, From 025616686080d9421595e68a1e189049b9d78291 Mon Sep 17 00:00:00 2001 From: kt Date: Fri, 1 Nov 2024 14:28:35 -0700 Subject: [PATCH 4/6] fix bug --- .../mssqlmanagedinstance/mssql_managed_database_resource.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/mssqlmanagedinstance/mssql_managed_database_resource.go b/internal/services/mssqlmanagedinstance/mssql_managed_database_resource.go index 8c9b686ae487..5c128ff279f5 100644 --- a/internal/services/mssqlmanagedinstance/mssql_managed_database_resource.go +++ b/internal/services/mssqlmanagedinstance/mssql_managed_database_resource.go @@ -329,7 +329,7 @@ func (r MsSqlManagedDatabaseResource) Read() sdk.ResourceFunc { model.PointInTimeRestore = flattenManagedDatabasePointInTimeRestore(v) } - state.Tags = model.Tags + model.Tags = pointer.From(result.Model.Tags) return metadata.Encode(&model) }, From 2a8b21009b23bd01c4a4a5983107bb9d288c3a27 Mon Sep 17 00:00:00 2001 From: kt Date: Fri, 1 Nov 2024 14:44:11 -0700 Subject: [PATCH 5/6] docs --- website/docs/r/mssql_managed_database.html.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/docs/r/mssql_managed_database.html.markdown b/website/docs/r/mssql_managed_database.html.markdown index 4b95e8185d65..d921ec59d4e0 100644 --- a/website/docs/r/mssql_managed_database.html.markdown +++ b/website/docs/r/mssql_managed_database.html.markdown @@ -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: From 1bae680c4647cbe15e4ddfe16d47da63e955f469 Mon Sep 17 00:00:00 2001 From: kt Date: Mon, 4 Nov 2024 10:26:38 -0800 Subject: [PATCH 6/6] update with location --- internal/services/mssql/helper/elasticpool.go | 15 +++++++++++++++ .../mssql_managed_database_resource.go | 9 ++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/internal/services/mssql/helper/elasticpool.go b/internal/services/mssql/helper/elasticpool.go index a00c89cfe039..2432babdd9d7 100644 --- a/internal/services/mssql/helper/elasticpool.go +++ b/internal/services/mssql/helper/elasticpool.go @@ -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, + }, }, } diff --git a/internal/services/mssqlmanagedinstance/mssql_managed_database_resource.go b/internal/services/mssqlmanagedinstance/mssql_managed_database_resource.go index 5c128ff279f5..eb0536952721 100644 --- a/internal/services/mssqlmanagedinstance/mssql_managed_database_resource.go +++ b/internal/services/mssqlmanagedinstance/mssql_managed_database_resource.go @@ -217,6 +217,7 @@ func (r MsSqlManagedDatabaseResource) Update() 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 @@ -236,8 +237,14 @@ func (r MsSqlManagedDatabaseResource) Update() sdk.ResourceFunc { d := metadata.ResourceData 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{ - Tags: pointer.To(model.Tags), + Location: managedInstance.Model.Location, + Tags: pointer.To(model.Tags), } err = client.CreateOrUpdateThenPoll(ctx, id, parameters)