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 2 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
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 @@ -157,15 +161,15 @@ func (r MsSqlManagedDatabaseResource) Create() sdk.ResourceFunc {
parameters := manageddatabases.ManagedDatabase{
Location: managedInstance.Model.Location,
Properties: &manageddatabases.ManagedDatabaseProperties{},
Tags: &model.Tags,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Tags: &model.Tags,
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 Down Expand Up @@ -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

Expand All @@ -233,6 +238,17 @@ func (r MsSqlManagedDatabaseResource) Update() sdk.ResourceFunc {

d := metadata.ResourceData

if d.HasChange("tags") {
parameters := manageddatabases.ManagedDatabase{
Tags: &model.Tags,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Tags: &model.Tags,
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") {
longTermRetentionProps := expandLongTermRetentionPolicy(model.LongTermRetentionPolicy)

Expand Down Expand Up @@ -319,6 +335,10 @@ func (r MsSqlManagedDatabaseResource) Read() sdk.ResourceFunc {
model.PointInTimeRestore = flattenManagedDatabasePointInTimeRestore(v)
}

if model.Tags != nil {
state.Tags = model.Tags
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could get rid of this block and just do state.Tags = pointer.From(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
Loading