From 0d2689094ebf238a201e6ac21953a9f594929e34 Mon Sep 17 00:00:00 2001 From: Elena Xin Date: Wed, 30 Aug 2023 16:27:05 +0800 Subject: [PATCH 1/2] fix parsing id error --- ...anagement_api_operation_policy_resource.go | 3 +- .../api_management_api_policy_resource.go | 3 +- .../api_management_product_policy_resource.go | 3 +- .../api_operation_policy_v1_to_v2.go | 71 +++++++++++++++++++ .../migration/api_policy_v1_to_v2.go | 65 +++++++++++++++++ .../migration/api_product_policy_v1_to_v2.go | 64 +++++++++++++++++ 6 files changed, 206 insertions(+), 3 deletions(-) create mode 100644 internal/services/apimanagement/migration/api_operation_policy_v1_to_v2.go create mode 100644 internal/services/apimanagement/migration/api_policy_v1_to_v2.go create mode 100644 internal/services/apimanagement/migration/api_product_policy_v1_to_v2.go diff --git a/internal/services/apimanagement/api_management_api_operation_policy_resource.go b/internal/services/apimanagement/api_management_api_operation_policy_resource.go index b8ee32d49614..52a3326ac016 100644 --- a/internal/services/apimanagement/api_management_api_operation_policy_resource.go +++ b/internal/services/apimanagement/api_management_api_operation_policy_resource.go @@ -39,9 +39,10 @@ func resourceApiManagementApiOperationPolicy() *pluginsdk.Resource { Delete: pluginsdk.DefaultTimeout(30 * time.Minute), }, - SchemaVersion: 1, + SchemaVersion: 2, StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{ 0: migration.ApiManagementApiOperationPolicyV0ToV1{}, + 1: migration.ApiManagementApiOperationPolicyV1ToV2{}, }), Schema: map[string]*pluginsdk.Schema{ diff --git a/internal/services/apimanagement/api_management_api_policy_resource.go b/internal/services/apimanagement/api_management_api_policy_resource.go index a47542f96b0c..967847189398 100644 --- a/internal/services/apimanagement/api_management_api_policy_resource.go +++ b/internal/services/apimanagement/api_management_api_policy_resource.go @@ -39,9 +39,10 @@ func resourceApiManagementApiPolicy() *pluginsdk.Resource { Delete: pluginsdk.DefaultTimeout(30 * time.Minute), }, - SchemaVersion: 1, + SchemaVersion: 2, StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{ 0: migration.ApiManagementApiPolicyV0ToV1{}, + 1: migration.ApiManagementApiPolicyV1ToV2{}, }), Schema: map[string]*pluginsdk.Schema{ diff --git a/internal/services/apimanagement/api_management_product_policy_resource.go b/internal/services/apimanagement/api_management_product_policy_resource.go index c3188a2ab9e0..fd56130c7c35 100644 --- a/internal/services/apimanagement/api_management_product_policy_resource.go +++ b/internal/services/apimanagement/api_management_product_policy_resource.go @@ -39,9 +39,10 @@ func resourceApiManagementProductPolicy() *pluginsdk.Resource { Delete: pluginsdk.DefaultTimeout(30 * time.Minute), }, - SchemaVersion: 1, + SchemaVersion: 2, StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{ 0: migration.ApiManagementProductPolicyV0ToV1{}, + 1: migration.ApiManagementProductPolicyV1ToV2{}, }), Schema: map[string]*pluginsdk.Schema{ diff --git a/internal/services/apimanagement/migration/api_operation_policy_v1_to_v2.go b/internal/services/apimanagement/migration/api_operation_policy_v1_to_v2.go new file mode 100644 index 000000000000..4340a28c9110 --- /dev/null +++ b/internal/services/apimanagement/migration/api_operation_policy_v1_to_v2.go @@ -0,0 +1,71 @@ +package migration + +import ( + "context" + "log" + "strings" + + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" +) + +var _ pluginsdk.StateUpgrade = ApiManagementApiOperationPolicyV1ToV2{} + +type ApiManagementApiOperationPolicyV1ToV2 struct{} + +func (ApiManagementApiOperationPolicyV1ToV2) Schema() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{ + + "resource_group_name": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + }, + + "api_management_name": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + }, + + "api_name": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + }, + + "operation_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 (ApiManagementApiOperationPolicyV1ToV2) 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/apis/api1/operations/operation1/policies/policy + // new id : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.ApiManagement/service/instance1/apis/api1/operations/operation1 + oldId := rawState["id"].(string) + + // Prior to v3.70.0 of Terraform Provider, after importing resource, the id in state file ends with "/policies/policy", the id in state file ends with "/policies/xml" for creating resource by Terraform. + // So after migrating pandora SDK (starting from v3.70.0), these two cases need to be migrated. + // In ApiManagementApiPolicyV0ToV1, only the case where the ID ends with "/policies/xml" is processed, so the case where the ID ends with "/policies/policy" is processed here to solve the parse id error. + newId := strings.TrimSuffix(oldId, "/policies/policy") + + log.Printf("[DEBUG] Updating ID from %q tmakeo %q", oldId, newId) + rawState["id"] = newId + + return rawState, nil + } +} diff --git a/internal/services/apimanagement/migration/api_policy_v1_to_v2.go b/internal/services/apimanagement/migration/api_policy_v1_to_v2.go new file mode 100644 index 000000000000..a96ec284b89b --- /dev/null +++ b/internal/services/apimanagement/migration/api_policy_v1_to_v2.go @@ -0,0 +1,65 @@ +package migration + +import ( + "context" + "log" + "strings" + + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" +) + +var _ pluginsdk.StateUpgrade = ApiManagementApiPolicyV1ToV2{} + +type ApiManagementApiPolicyV1ToV2 struct{} + +func (ApiManagementApiPolicyV1ToV2) Schema() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{ + + "resource_group_name": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + }, + + "api_management_name": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + }, + + "api_name": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + }, + + "xml_content": { + Type: pluginsdk.TypeString, + Optional: true, + Computed: true, + }, + + "xml_link": { + Type: pluginsdk.TypeString, + Optional: true, + }, + } +} + +func (ApiManagementApiPolicyV1ToV2) 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/service1/apis/exampleId/policies/policy + // new id : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.ApiManagement/service/service1/apis/exampleId + oldId := rawState["id"].(string) + + // Prior to v3.70.0 of Terraform Provider, after importing resource, the id in state file ends with "/policies/policy", the id in state file ends with "/policies/xml" for creating resource by Terraform. + // So after migrating pandora SDK (starting from v3.70.0), these two cases need to be migrated. + // In ApiManagementApiPolicyV0ToV1, only the case where the ID ends with "/policies/xml" is processed, so the case where the ID ends with "/policies/policy" is processed here to solve the parse id error. + newId := strings.TrimSuffix(oldId, "/policies/policy") + + log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newId) + rawState["id"] = newId + + return rawState, nil + } +} diff --git a/internal/services/apimanagement/migration/api_product_policy_v1_to_v2.go b/internal/services/apimanagement/migration/api_product_policy_v1_to_v2.go new file mode 100644 index 000000000000..69f2f69bbfa0 --- /dev/null +++ b/internal/services/apimanagement/migration/api_product_policy_v1_to_v2.go @@ -0,0 +1,64 @@ +package migration + +import ( + "context" + "log" + "strings" + + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" +) + +var _ pluginsdk.StateUpgrade = ApiManagementProductPolicyV1ToV2{} + +type ApiManagementProductPolicyV1ToV2 struct{} + +func (ApiManagementProductPolicyV1ToV2) Schema() map[string]*pluginsdk.Schema { + return map[string]*pluginsdk.Schema{ + "resource_group_name": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + }, + + "api_management_name": { + Type: pluginsdk.TypeString, + Required: true, + ForceNew: true, + }, + + "product_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 (ApiManagementProductPolicyV1ToV2) 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/service1/products/exampleId/policies/policy + // new id : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.ApiManagement/service/service1/products/exampleId + oldId := rawState["id"].(string) + + // Prior to v3.70.0 of Terraform Provider, after importing resource, the id in state file ends with "/policies/policy", the id in state file ends with "/policies/xml" for creating resource by Terraform. + // So after migrating pandora SDK (starting from v3.70.0), these two cases need to be migrated. + // In ApiManagementApiPolicyV0ToV1, only the case where the ID ends with "/policies/xml" is processed, so the case where the ID ends with "/policies/policy" is processed here to solve the parse id error. + newId := strings.TrimSuffix(oldId, "/policies/policy") + + log.Printf("[DEBUG] Updating ID from %q tmakeo %q", oldId, newId) + rawState["id"] = newId + + return rawState, nil + } +} From d9b66dd1d44f2b0a701e3cf65f841813d65d7760 Mon Sep 17 00:00:00 2001 From: Elena Xin Date: Fri, 1 Sep 2023 11:51:23 +0800 Subject: [PATCH 2/2] update code --- .../migration/api_operation_policy_v1_to_v2.go | 9 +++++++-- .../apimanagement/migration/api_policy_v1_to_v2.go | 7 ++++++- .../migration/api_product_policy_v1_to_v2.go | 9 +++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/internal/services/apimanagement/migration/api_operation_policy_v1_to_v2.go b/internal/services/apimanagement/migration/api_operation_policy_v1_to_v2.go index 4340a28c9110..20dbe8473a8f 100644 --- a/internal/services/apimanagement/migration/api_operation_policy_v1_to_v2.go +++ b/internal/services/apimanagement/migration/api_operation_policy_v1_to_v2.go @@ -5,6 +5,7 @@ import ( "log" "strings" + "github.com/hashicorp/go-azure-sdk/resource-manager/apimanagement/2021-08-01/apioperationpolicy" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" ) @@ -62,8 +63,12 @@ func (ApiManagementApiOperationPolicyV1ToV2) UpgradeFunc() pluginsdk.StateUpgrad // So after migrating pandora SDK (starting from v3.70.0), these two cases need to be migrated. // In ApiManagementApiPolicyV0ToV1, only the case where the ID ends with "/policies/xml" is processed, so the case where the ID ends with "/policies/policy" is processed here to solve the parse id error. newId := strings.TrimSuffix(oldId, "/policies/policy") - - log.Printf("[DEBUG] Updating ID from %q tmakeo %q", oldId, newId) + parsed, err := apioperationpolicy.ParseOperationID(newId) + if err != nil { + return rawState, err + } + newId = parsed.ID() + log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newId) rawState["id"] = newId return rawState, nil diff --git a/internal/services/apimanagement/migration/api_policy_v1_to_v2.go b/internal/services/apimanagement/migration/api_policy_v1_to_v2.go index a96ec284b89b..18e73afb07ad 100644 --- a/internal/services/apimanagement/migration/api_policy_v1_to_v2.go +++ b/internal/services/apimanagement/migration/api_policy_v1_to_v2.go @@ -5,6 +5,7 @@ import ( "log" "strings" + "github.com/hashicorp/go-azure-sdk/resource-manager/apimanagement/2021-08-01/apipolicy" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" ) @@ -56,7 +57,11 @@ func (ApiManagementApiPolicyV1ToV2) UpgradeFunc() pluginsdk.StateUpgraderFunc { // So after migrating pandora SDK (starting from v3.70.0), these two cases need to be migrated. // In ApiManagementApiPolicyV0ToV1, only the case where the ID ends with "/policies/xml" is processed, so the case where the ID ends with "/policies/policy" is processed here to solve the parse id error. newId := strings.TrimSuffix(oldId, "/policies/policy") - + parsed, err := apipolicy.ParseApiID(newId) + if err != nil { + return rawState, err + } + newId = parsed.ID() log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newId) rawState["id"] = newId diff --git a/internal/services/apimanagement/migration/api_product_policy_v1_to_v2.go b/internal/services/apimanagement/migration/api_product_policy_v1_to_v2.go index 69f2f69bbfa0..a74c1f10cc1d 100644 --- a/internal/services/apimanagement/migration/api_product_policy_v1_to_v2.go +++ b/internal/services/apimanagement/migration/api_product_policy_v1_to_v2.go @@ -5,6 +5,7 @@ import ( "log" "strings" + "github.com/hashicorp/go-azure-sdk/resource-manager/apimanagement/2021-08-01/productpolicy" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" ) @@ -55,8 +56,12 @@ func (ApiManagementProductPolicyV1ToV2) UpgradeFunc() pluginsdk.StateUpgraderFun // So after migrating pandora SDK (starting from v3.70.0), these two cases need to be migrated. // In ApiManagementApiPolicyV0ToV1, only the case where the ID ends with "/policies/xml" is processed, so the case where the ID ends with "/policies/policy" is processed here to solve the parse id error. newId := strings.TrimSuffix(oldId, "/policies/policy") - - log.Printf("[DEBUG] Updating ID from %q tmakeo %q", oldId, newId) + parsed, err := productpolicy.ParseProductID(newId) + if err != nil { + return rawState, err + } + newId = parsed.ID() + log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newId) rawState["id"] = newId return rawState, nil