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

user_assigned_identity: add state migration for ID casing #10196

Merged
merged 3 commits into from
Jan 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
@@ -0,0 +1,62 @@
package migration

import (
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/msi/parse"
)

func UserAssignedIdentityV0ToV1() schema.StateUpgrader {
return schema.StateUpgrader{
Version: 0,
Type: userAssignedIdentityV0Schema().CoreConfigSchema().ImpliedType(),
Upgrade: userAssignedIdentityUpgradeV0ToV1,
}
}

func userAssignedIdentityV0Schema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(3, 128),
},

"resource_group_name": azure.SchemaResourceGroupName(),

"location": azure.SchemaLocation(),

"tags": tags.Schema(),

"principal_id": {
Type: schema.TypeString,
Computed: true,
},

"client_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func userAssignedIdentityUpgradeV0ToV1(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
oldId := rawState["id"].(string)
id, err := parse.UserAssignedIdentityID(oldId)
if err != nil {
return rawState, err
}

newId := id.ID()
log.Printf("Updating `id` from %q to %q", oldId, newId)
rawState["id"] = newId
return rawState, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/location"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/msi/migration"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/msi/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
Expand All @@ -30,6 +31,11 @@ func resourceArmUserAssignedIdentity() *schema.Resource {
return err
}),

SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
migration.UserAssignedIdentityV0ToV1(),
},

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Expand Down