From fc2174c4a57c0d7d9b665fd8131dbde6fc0fb7d8 Mon Sep 17 00:00:00 2001 From: Matthew Date: Fri, 18 Aug 2023 13:16:34 -0700 Subject: [PATCH 1/5] Fix azurerm_api_management_policy migration --- internal/services/apimanagement/migration/policy.go | 5 +++-- internal/services/apimanagement/migration/policy_v1_to_v2.go | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/services/apimanagement/migration/policy.go b/internal/services/apimanagement/migration/policy.go index ccd6a7430797..d7c97e91929b 100644 --- a/internal/services/apimanagement/migration/policy.go +++ b/internal/services/apimanagement/migration/policy.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/go-azure-helpers/lang/pointer" "github.com/hashicorp/go-azure-sdk/resource-manager/apimanagement/2021-08-01/policy" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" ) @@ -20,11 +21,11 @@ type ApiManagementPolicyV0ToV1 struct{} func (ApiManagementPolicyV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc { return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { - apiMgmtId, err := policy.ParseServiceID(rawState["id"].(string)) + apiMgmtId, err := parse.ApiManagementID(rawState["id"].(string)) if err != nil { return rawState, nil } - id := policy.NewServiceID(apiMgmtId.SubscriptionId, apiMgmtId.ResourceGroupName, apiMgmtId.ServiceName) + id := policy.NewServiceID(apiMgmtId.SubscriptionId, apiMgmtId.ResourceGroup, apiMgmtId.ServiceName) rawState["id"] = id.ID() client := meta.(*clients.Client).ApiManagement.PolicyClient diff --git a/internal/services/apimanagement/migration/policy_v1_to_v2.go b/internal/services/apimanagement/migration/policy_v1_to_v2.go index 0e02e04157d9..359a8fea7592 100644 --- a/internal/services/apimanagement/migration/policy_v1_to_v2.go +++ b/internal/services/apimanagement/migration/policy_v1_to_v2.go @@ -21,7 +21,7 @@ func (ApiManagementPolicyV1ToV2) UpgradeFunc() pluginsdk.StateUpgraderFunc { // old id : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.ApiManagement/service/instance1/policies/policy // new id : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.ApiManagement/service/instance1 oldId := rawState["id"].(string) - newId := strings.TrimSuffix(oldId, "/policies/xml") + newId := strings.TrimSuffix(oldId, "/policies/policy") log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newId) rawState["id"] = newId From 86e78f24b7a8d66439c903523ab3798964185475 Mon Sep 17 00:00:00 2001 From: Matthew Date: Fri, 18 Aug 2023 13:35:46 -0700 Subject: [PATCH 2/5] Fix migration for azurerm_api_management_gateway_api --- .../api_management_gateway_api_resource.go | 2 +- .../migration/gateway_api_v0_to_v1.go | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 internal/services/apimanagement/migration/gateway_api_v0_to_v1.go diff --git a/internal/services/apimanagement/api_management_gateway_api_resource.go b/internal/services/apimanagement/api_management_gateway_api_resource.go index 155af9e95386..629e8ca94f65 100644 --- a/internal/services/apimanagement/api_management_gateway_api_resource.go +++ b/internal/services/apimanagement/api_management_gateway_api_resource.go @@ -41,7 +41,7 @@ func resourceApiManagementGatewayApi() *pluginsdk.Resource { SchemaVersion: 1, StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{ - 0: migration.ApiManagementApiPolicyV0ToV1{}, + 0: migration.ApiManagementGatewayApiV0ToV1{}, }), Schema: map[string]*pluginsdk.Schema{ diff --git a/internal/services/apimanagement/migration/gateway_api_v0_to_v1.go b/internal/services/apimanagement/migration/gateway_api_v0_to_v1.go new file mode 100644 index 000000000000..35da94fbfc5a --- /dev/null +++ b/internal/services/apimanagement/migration/gateway_api_v0_to_v1.go @@ -0,0 +1,33 @@ +package migration + +import ( + "context" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" +) + +var _ pluginsdk.StateUpgrade = ApiManagementGatewayApiV0ToV1{} + +type ApiManagementGatewayApiV0ToV1 struct{} + +func (ApiManagementGatewayApiV0ToV1) Schema() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{ + "api_id": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + }, + "gateway_id": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + }, + } +} + +// UpgradeFunc this is a noop migration to account for a migration that was accidentally added from github.com/hashicorp/terraform-provider-azurerm/pull/22783/ +// That migration didn't do anything for this resource so we'll just swap it for a no-op migration here +func (ApiManagementGatewayApiV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc { + return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { + return rawState, nil + } +} From 36681ff4b7b1f45809ee63ab2e432f7d484ef630 Mon Sep 17 00:00:00 2001 From: Matthew Date: Fri, 18 Aug 2023 13:45:30 -0700 Subject: [PATCH 3/5] Add new migration for azurerm_api_management_policy --- .../api_management_policy_resource.go | 3 +- .../migration/policy_v1_to_v2.go | 23 ++++++++- .../migration/policy_v2_to_v3.go | 48 +++++++++++++++++++ 3 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 internal/services/apimanagement/migration/policy_v2_to_v3.go diff --git a/internal/services/apimanagement/api_management_policy_resource.go b/internal/services/apimanagement/api_management_policy_resource.go index a063b042893b..6ded0463f09f 100644 --- a/internal/services/apimanagement/api_management_policy_resource.go +++ b/internal/services/apimanagement/api_management_policy_resource.go @@ -31,10 +31,11 @@ func resourceApiManagementPolicy() *pluginsdk.Resource { return err }), - SchemaVersion: 2, + SchemaVersion: 3, StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{ 0: migration.ApiManagementPolicyV0ToV1{}, 1: migration.ApiManagementPolicyV1ToV2{}, + 2: migration.ApiManagementPolicyV2ToV3{}, }), Timeouts: &pluginsdk.ResourceTimeout{ diff --git a/internal/services/apimanagement/migration/policy_v1_to_v2.go b/internal/services/apimanagement/migration/policy_v1_to_v2.go index 359a8fea7592..1ead5a672527 100644 --- a/internal/services/apimanagement/migration/policy_v1_to_v2.go +++ b/internal/services/apimanagement/migration/policy_v1_to_v2.go @@ -13,15 +13,34 @@ var _ pluginsdk.StateUpgrade = ApiManagementPolicyV1ToV2{} type ApiManagementPolicyV1ToV2 struct{} func (ApiManagementPolicyV1ToV2) Schema() map[string]*pluginsdk.Schema { - return policySchemaForV0AndV1() + return map[string]*pluginsdk.Schema{ + "api_management_id": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + }, + + "xml_content": { + Type: pluginsdk.TypeString, + Optional: true, + Computed: true, + }, + + "xml_link": { + Type: pluginsdk.TypeString, + Optional: true, + }, + } } +// UpgradeFunc this migration doesn't do anything as `/policies/xml` is never the suffix for this but I don't believe we can remove it as we need it go from one migration +// to the next func (ApiManagementPolicyV1ToV2) UpgradeFunc() pluginsdk.StateUpgraderFunc { return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { // old id : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.ApiManagement/service/instance1/policies/policy // new id : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.ApiManagement/service/instance1 oldId := rawState["id"].(string) - newId := strings.TrimSuffix(oldId, "/policies/policy") + newId := strings.TrimSuffix(oldId, "/policies/xml") log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newId) rawState["id"] = newId diff --git a/internal/services/apimanagement/migration/policy_v2_to_v3.go b/internal/services/apimanagement/migration/policy_v2_to_v3.go new file mode 100644 index 000000000000..8bd1abd119f8 --- /dev/null +++ b/internal/services/apimanagement/migration/policy_v2_to_v3.go @@ -0,0 +1,48 @@ +package migration + +import ( + "context" + "log" + "strings" + + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" +) + +var _ pluginsdk.StateUpgrade = ApiManagementPolicyV2ToV3{} + +type ApiManagementPolicyV2ToV3 struct{} + +func (ApiManagementPolicyV2ToV3) Schema() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{ + "api_management_id": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + }, + + "xml_content": { + Type: pluginsdk.TypeString, + Optional: true, + Computed: true, + }, + + "xml_link": { + Type: pluginsdk.TypeString, + Optional: true, + }, + } +} + +func (ApiManagementPolicyV2ToV3) UpgradeFunc() pluginsdk.StateUpgraderFunc { + return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { + // old id : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.ApiManagement/service/instance1/policies/policy + // new id : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.ApiManagement/service/instance1 + oldId := rawState["id"].(string) + newId := strings.TrimSuffix(oldId, "/policies/policy") + + log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newId) + rawState["id"] = newId + + return rawState, nil + } +} From 338530556d7b32ad6656c84300e3f6a33554d1fe Mon Sep 17 00:00:00 2001 From: Matthew Date: Mon, 21 Aug 2023 08:32:59 -0700 Subject: [PATCH 4/5] Lint --- .../services/apimanagement/migration/gateway_api_v0_to_v1.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/services/apimanagement/migration/gateway_api_v0_to_v1.go b/internal/services/apimanagement/migration/gateway_api_v0_to_v1.go index 35da94fbfc5a..6af647d44103 100644 --- a/internal/services/apimanagement/migration/gateway_api_v0_to_v1.go +++ b/internal/services/apimanagement/migration/gateway_api_v0_to_v1.go @@ -2,6 +2,7 @@ package migration import ( "context" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" ) From 4e228ba52fbb7f18f9231d7c3419bc1fc90c7bf7 Mon Sep 17 00:00:00 2001 From: Matthew Date: Mon, 21 Aug 2023 08:47:16 -0700 Subject: [PATCH 5/5] Address review --- internal/services/apimanagement/migration/policy.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/services/apimanagement/migration/policy.go b/internal/services/apimanagement/migration/policy.go index d7c97e91929b..ccd6a7430797 100644 --- a/internal/services/apimanagement/migration/policy.go +++ b/internal/services/apimanagement/migration/policy.go @@ -11,7 +11,6 @@ import ( "github.com/hashicorp/go-azure-helpers/lang/pointer" "github.com/hashicorp/go-azure-sdk/resource-manager/apimanagement/2021-08-01/policy" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" - "github.com/hashicorp/terraform-provider-azurerm/internal/services/apimanagement/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" ) @@ -21,11 +20,11 @@ type ApiManagementPolicyV0ToV1 struct{} func (ApiManagementPolicyV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc { return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) { - apiMgmtId, err := parse.ApiManagementID(rawState["id"].(string)) + apiMgmtId, err := policy.ParseServiceID(rawState["id"].(string)) if err != nil { return rawState, nil } - id := policy.NewServiceID(apiMgmtId.SubscriptionId, apiMgmtId.ResourceGroup, apiMgmtId.ServiceName) + id := policy.NewServiceID(apiMgmtId.SubscriptionId, apiMgmtId.ResourceGroupName, apiMgmtId.ServiceName) rawState["id"] = id.ID() client := meta.(*clients.Client).ApiManagement.PolicyClient