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_data_factory" - migrate to the correct ID format when resource name is in upper case #12128

Merged
merged 2 commits into from
Jun 18, 2021
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 @@ -29,9 +29,10 @@ func resourceDataFactory() *pluginsdk.Resource {
Update: resourceDataFactoryCreateUpdate,
Delete: resourceDataFactoryDelete,

SchemaVersion: 1,
SchemaVersion: 2,
StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{
0: migration.DataFactoryV0ToV1{},
1: migration.DataFactoryV1ToV2{},
}),

// TODO: replace this with an importer which validates the ID during import
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ resource "azurerm_resource_group" "test" {
}

resource "azurerm_data_factory" "test" {
name = "acctestdf%d"
name = "acctestDF%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}
Expand All @@ -256,7 +256,7 @@ resource "azurerm_resource_group" "test" {
}

resource "azurerm_data_factory" "test" {
name = "acctestdf%d"
name = "acctestDF%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name

Expand All @@ -279,7 +279,7 @@ resource "azurerm_resource_group" "test" {
}

resource "azurerm_data_factory" "test" {
name = "acctestdf%d"
name = "acctestDF%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name

Expand All @@ -303,7 +303,7 @@ resource "azurerm_resource_group" "test" {
}

resource "azurerm_data_factory" "test" {
name = "acctestdf%d"
name = "acctestDF%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name

Expand All @@ -326,7 +326,7 @@ resource "azurerm_resource_group" "test" {
}

resource "azurerm_data_factory" "test" {
name = "acctestdf%d"
name = "acctestDF%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name

Expand All @@ -353,7 +353,7 @@ resource "azurerm_resource_group" "test" {
}

resource "azurerm_data_factory" "test" {
name = "acctestdf%d"
name = "acctestDF%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name

Expand All @@ -380,7 +380,7 @@ resource "azurerm_resource_group" "test" {
}

resource "azurerm_data_factory" "test" {
name = "acctestdf%d"
name = "acctestDF%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name

Expand Down
158 changes: 158 additions & 0 deletions azurerm/internal/services/datafactory/migration/data_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"log"

"github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk"
)

var _ pluginsdk.StateUpgrade = DataFactoryV0ToV1{}
var _ pluginsdk.StateUpgrade = DataFactoryV1ToV2{}

type DataFactoryV0ToV1 struct{}

Expand Down Expand Up @@ -139,3 +141,159 @@ func (DataFactoryV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return rawState, nil
}
}

type DataFactoryV1ToV2 struct{}

func (DataFactoryV1ToV2) Schema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"location": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"resource_group_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},

"identity": {
Type: pluginsdk.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"type": {
Type: pluginsdk.TypeString,
Required: true,
},
"identity_ids": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"principal_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
"tenant_id": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
},
},

"github_configuration": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"vsts_configuration"},
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"account_name": {
Type: pluginsdk.TypeString,
Required: true,
},
"branch_name": {
Type: pluginsdk.TypeString,
Required: true,
},
"git_url": {
Type: pluginsdk.TypeString,
Required: true,
},
"repository_name": {
Type: pluginsdk.TypeString,
Required: true,
},
"root_folder": {
Type: pluginsdk.TypeString,
Required: true,
},
},
},
},

"vsts_configuration": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
ConflictsWith: []string{"github_configuration"},
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"account_name": {
Type: pluginsdk.TypeString,
Required: true,
},
"branch_name": {
Type: pluginsdk.TypeString,
Required: true,
},
"project_name": {
Type: pluginsdk.TypeString,
Required: true,
},
"repository_name": {
Type: pluginsdk.TypeString,
Required: true,
},
"root_folder": {
Type: pluginsdk.TypeString,
Required: true,
},
"tenant_id": {
Type: pluginsdk.TypeString,
Required: true,
},
},
},
},
"public_network_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"customer_managed_key_id": {
Type: pluginsdk.TypeString,
Optional: true,
RequiredWith: []string{"identity.0.identity_ids"},
},

"tags": {
Type: pluginsdk.TypeMap,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
}
}

func (DataFactoryV1ToV2) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
log.Printf("[DEBUG] Updating `id` if resourceName is in upper case")

oldId := rawState["id"].(string)
id, err := parse.DataFactoryID(oldId)
if err != nil {
return nil, err
}

id.FactoryName = rawState["name"].(string)
rawState["id"] = id.ID()

return rawState, nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package migration

import (
"context"
"testing"
)

func TestDataFactoryMigrateState(t *testing.T) {
cases := map[string]struct {
StateVersion int
InputAttributes map[string]interface{}
ExpectedNewID string
}{
"name_upper_case": {
StateVersion: 1,
InputAttributes: map[string]interface{}{
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.DataFactory/factories/acctest",
"name": "ACCTEST",
"location": "westeurope",
"resource_group_name": "resGroup1",
},
ExpectedNewID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.DataFactory/factories/ACCTEST",
},
}

for _, tc := range cases {
newID, _ := DataFactoryV1ToV2{}.UpgradeFunc()(context.TODO(), tc.InputAttributes, nil)

if newID["id"].(string) != tc.ExpectedNewID {
t.Fatalf("ID migration failed, expected %q, got: %q", tc.ExpectedNewID, newID["id"].(string))
}
}
}