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 - allow RestorableDatabaseID to be used for point in time restore #25568

Merged
merged 1 commit into from
Apr 29, 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"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/services/sql/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -102,7 +103,7 @@ func (r MsSqlManagedDatabaseResource) Arguments() map[string]*pluginsdk.Schema {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ManagedDatabaseID,
ValidateFunc: validation.Any(validate.ManagedDatabaseID, validate.RestorableDatabaseID),
},
},
},
Expand Down Expand Up @@ -163,7 +164,13 @@ func (r MsSqlManagedDatabaseResource) Create() sdk.ResourceFunc {
parameters.RestorePointInTime = &date.Time{
Time: t,
}
parameters.SourceDatabaseID = pointer.To(restorePointInTime.SourceDatabaseId)

_, err := miParse.RestorableDroppedDatabaseID(restorePointInTime.SourceDatabaseId)
if err == nil {
parameters.RestorableDroppedDatabaseID = pointer.To(restorePointInTime.SourceDatabaseId)
} else {
parameters.SourceDatabaseID = pointer.To(restorePointInTime.SourceDatabaseId)
}
}

metadata.Logger.Infof("Creating %s", id)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package parse

import (
"fmt"
"strings"

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
)

// note: this can't be automated today due to the comma in the Resource ID

type RestorableDroppedDatabaseId struct {
Name string
MsSqlManagedInstance string
ResourceGroup string
RestoreName string
}

func RestorableDroppedDatabaseID(input string) (*RestorableDroppedDatabaseId, error) {
inputList := strings.Split(input, ",")

if len(inputList) != 2 {
return nil, fmt.Errorf("[ERROR] Unable to parse Microsoft Sql Managed Instance Restorable DB ID %q, please refer to '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/managedInstance1/restorableDroppedDatabases/miDB1,000000000000000000'", input)
}

restorableDBId := RestorableDroppedDatabaseId{
RestoreName: inputList[1],
}

id, err := azure.ParseAzureResourceID(inputList[0])
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse Microsoft Sql Managed Instance Restorable DB ID %q: %+v", input, err)
}

restorableDBId.ResourceGroup = id.ResourceGroup

if restorableDBId.MsSqlManagedInstance, err = id.PopSegment("managedInstances"); err != nil {
return nil, err
}

if restorableDBId.Name, err = id.PopSegment("restorableDroppedDatabases"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(inputList[0]); err != nil {
return nil, err
}

return &restorableDBId, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package parse

import "testing"

func TestMsSqlRestoreDBID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *RestorableDroppedDatabaseId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Restore Name",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Expected: nil,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000,000000000000000000",
Expected: nil,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/,000000000000000000",
Expected: nil,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/,000000000000000000",
Expected: nil,
},
{
Name: "Missing Sql Managed Instance Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/,000000000000000000",
Expected: nil,
},
{
Name: "Missing Sql Managed Instance Restorable Database",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/managedInstance1,000000000000000000",
Expected: nil,
},
{
Name: "Missing Sql Managed Instance Restorable Database Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/managedInstance1/restorableDroppedDatabases,000000000000000000",
Expected: nil,
},
{
Name: "Sql Managed Instance Restorable Database ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/managedInstance1/restorableDroppedDatabases/miDB1,000000000000000000",
Expected: &RestorableDroppedDatabaseId{
Name: "miDB1",
MsSqlManagedInstance: "managedInstance1",
ResourceGroup: "resGroup1",
RestoreName: "000000000000000000",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Sql/managedInstances/managedInstance1/RestorableDroppedDatabases/miDB1,000000000000000000",
Expected: nil,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := RestorableDroppedDatabaseID(v.Input)
if err != nil {
if v.Expected == nil {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.RestoreName != v.Expected.RestoreName {
t.Fatalf("Expected %q but got %q for Restore Name", v.Expected.Name, actual.Name)
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}

if actual.MsSqlManagedInstance != v.Expected.MsSqlManagedInstance {
t.Fatalf("Expected %q but got %q for Sql Server", v.Expected.Name, actual.Name)
}

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package validate

import (
"fmt"

"github.com/hashicorp/terraform-provider-azurerm/internal/services/mssqlmanagedinstance/parse"
)

func RestorableDatabaseID(i interface{}, k string) (warnings []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return warnings, errors
}

if _, err := parse.RestorableDroppedDatabaseID(v); err != nil {
errors = append(errors, fmt.Errorf("cannot parse %q as a MsSql Managed Instance Restorable Database resource id: %v", k, err))
}

return warnings, errors
}
Loading