diff --git a/internal/services/managementgroup/migration/subscription_association_v0_to_v1.go b/internal/services/managementgroup/migration/subscription_association_v0_to_v1.go new file mode 100644 index 000000000000..38d24af59f0f --- /dev/null +++ b/internal/services/managementgroup/migration/subscription_association_v0_to_v1.go @@ -0,0 +1,46 @@ +package migration + +import ( + "context" + "log" + "strings" + + "github.com/hashicorp/go-azure-helpers/resourcemanager/commonids" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/managementgroup/validate" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" +) + +var _ pluginsdk.StateUpgrade = ManagementGroupSubscriptionAssociationV0ToV1{} + +type ManagementGroupSubscriptionAssociationV0ToV1 struct{} + +func (ManagementGroupSubscriptionAssociationV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc { + return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { + oldId := rawState["id"].(string) + newId := strings.Replace(strings.Replace(rawState["id"].(string), "/managementGroup/", "/providers/Microsoft.Management/managementGroups/", 1), "/subscription/", "/subscriptions/", 1) + + log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newId) + + rawState["id"] = newId + + return rawState, nil + } +} + +func (ManagementGroupSubscriptionAssociationV0ToV1) Schema() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{ + "management_group_id": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validate.ManagementGroupID, + }, + + "subscription_id": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: commonids.ValidateSubscriptionID, + }, + } +} diff --git a/internal/services/managementgroup/subscription_association_resource.go b/internal/services/managementgroup/subscription_association_resource.go index c6cab7a165c3..797233f715f2 100644 --- a/internal/services/managementgroup/subscription_association_resource.go +++ b/internal/services/managementgroup/subscription_association_resource.go @@ -16,6 +16,7 @@ import ( "github.com/hashicorp/go-azure-sdk/resource-manager/managementgroups/2020-05-01/managementgroups" "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/managementgroup/migration" "github.com/hashicorp/terraform-provider-azurerm/internal/services/managementgroup/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/services/managementgroup/validate" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" @@ -28,6 +29,11 @@ func resourceManagementGroupSubscriptionAssociation() *pluginsdk.Resource { Read: resourceManagementGroupSubscriptionAssociationRead, Delete: resourceManagementGroupSubscriptionAssociationDelete, + SchemaVersion: 1, + StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{ + 0: migration.ManagementGroupSubscriptionAssociationV0ToV1{}, + }), + Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(5 * time.Minute), Read: pluginsdk.DefaultTimeout(5 * time.Minute), diff --git a/website/docs/r/management_group_subscription_association.html.markdown b/website/docs/r/management_group_subscription_association.html.markdown index b3a3800feba5..c63f01180883 100644 --- a/website/docs/r/management_group_subscription_association.html.markdown +++ b/website/docs/r/management_group_subscription_association.html.markdown @@ -56,5 +56,5 @@ The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/l Managements can be imported using the `resource id`, e.g. ```shell -terraform import azurerm_management_group_subscription_association.example /managementGroup/MyManagementGroup/subscription/12345678-1234-1234-1234-123456789012 +terraform import azurerm_management_group_subscription_association.example /providers/Microsoft.Management/managementGroups/MyManagementGroup/subscriptions/12345678-1234-1234-1234-123456789012 ```