Skip to content

Commit

Permalink
azurerm_frontdoor_rules_engine - a state migration to work around t…
Browse files Browse the repository at this point in the history
…he previously incorrect id casing (#19512)
  • Loading branch information
mbfrahry authored Dec 2, 2022
1 parent 41a0e31 commit d56cd44
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 17 deletions.
7 changes: 6 additions & 1 deletion internal/services/frontdoor/frontdoor_rules_engine_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/frontdoor/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/frontdoor/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/frontdoor/sdk/2020-05-01/frontdoors"
azValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/frontdoor/validate"
Expand All @@ -24,7 +25,11 @@ func resourceFrontDoorRulesEngine() *pluginsdk.Resource {
Update: resourceFrontDoorRulesEngineCreateUpdate,
Delete: resourceFrontDoorRulesEngineDelete,

SchemaVersion: 1,
SchemaVersion: 2,
StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{
0: migration.RulesEngineV0ToV1{},
1: migration.RulesEngineV1ToV2{},
}),

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.RulesEngineID(id)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package migration

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

// RulesEngineV0ToV1 doesn't do anything functionally, it lets us upgrade to SchemaVersion 2 without any errors from Terraform.
// This resource started at SchemaVersion: 1 and we must have a state migration from 0 to 1 if we want to bump to SchemaVersion: 2.
type RulesEngineV0ToV1 struct{}

func (s RulesEngineV0ToV1) Schema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},
"frontdoor_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},
"location": {
Type: schema.TypeString,
Computed: true,
},

"resource_group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"rule": {
Type: pluginsdk.TypeList,
MaxItems: 100,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
},

"priority": {
Type: pluginsdk.TypeInt,
Required: true,
},

"match_condition": {
Type: pluginsdk.TypeList,
MaxItems: 100,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"variable": {
Type: pluginsdk.TypeString,
Optional: true,
},

"selector": {
Type: pluginsdk.TypeString,
Optional: true,
},

"operator": {
Type: pluginsdk.TypeString,
Required: true,
},

"transform": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 6,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"negate_condition": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"value": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 25,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},

"action": {
Type: pluginsdk.TypeList,
MaxItems: 1,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"request_header": {
Type: pluginsdk.TypeList,
MaxItems: 100,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"header_action_type": {
Type: pluginsdk.TypeString,
Optional: true,
},

"header_name": {
Type: pluginsdk.TypeString,
Optional: true,
},

"value": {
Type: pluginsdk.TypeString,
Optional: true,
},
},
},
},

"response_header": {
Type: pluginsdk.TypeList,
MaxItems: 100,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"header_action_type": {
Type: pluginsdk.TypeString,
Optional: true,
},

"header_name": {
Type: pluginsdk.TypeString,
Optional: true,
},

"value": {
Type: pluginsdk.TypeString,
Optional: true,
},
},
},
},
},
},
},
},
},
},
}
}

func (s RulesEngineV0ToV1) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
return rawState, nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package migration

import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/frontdoor/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type RulesEngineV1ToV2 struct{}

func (s RulesEngineV1ToV2) Schema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},
"frontdoor_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
},
"location": {
Type: schema.TypeString,
Computed: true,
},

"resource_group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"rule": {
Type: pluginsdk.TypeList,
MaxItems: 100,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
},

"priority": {
Type: pluginsdk.TypeInt,
Required: true,
},

"match_condition": {
Type: pluginsdk.TypeList,
MaxItems: 100,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"variable": {
Type: pluginsdk.TypeString,
Optional: true,
},

"selector": {
Type: pluginsdk.TypeString,
Optional: true,
},

"operator": {
Type: pluginsdk.TypeString,
Required: true,
},

"transform": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 6,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"negate_condition": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"value": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 25,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},

"action": {
Type: pluginsdk.TypeList,
MaxItems: 1,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"request_header": {
Type: pluginsdk.TypeList,
MaxItems: 100,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"header_action_type": {
Type: pluginsdk.TypeString,
Optional: true,
},

"header_name": {
Type: pluginsdk.TypeString,
Optional: true,
},

"value": {
Type: pluginsdk.TypeString,
Optional: true,
},
},
},
},

"response_header": {
Type: pluginsdk.TypeList,
MaxItems: 100,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"header_action_type": {
Type: pluginsdk.TypeString,
Optional: true,
},

"header_name": {
Type: pluginsdk.TypeString,
Optional: true,
},

"value": {
Type: pluginsdk.TypeString,
Optional: true,
},
},
},
},
},
},
},
},
},
},
}
}

func (s RulesEngineV1ToV2) UpgradeFunc() pluginsdk.StateUpgraderFunc {
return func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
oldId := rawState["id"].(string)
newId, err := parse.RulesEngineIDInsensitively(oldId)
if err != nil {
return nil, err
}

log.Printf("[DEBUG] Updating ID from %q to %q", oldId, newId)

rawState["id"] = newId.ID()
return rawState, nil
}
}
Loading

0 comments on commit d56cd44

Please sign in to comment.