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_api_management_policy - fixes an error caused by a migration #23018

Merged
merged 5 commits into from
Aug 21, 2023
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 @@ -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{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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
}
}
21 changes: 20 additions & 1 deletion internal/services/apimanagement/migration/policy_v1_to_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,28 @@ 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
Expand Down
48 changes: 48 additions & 0 deletions internal/services/apimanagement/migration/policy_v2_to_v3.go
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading