Skip to content

Commit

Permalink
Add migration from SchemaVersion 0 to 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mbfrahry committed Dec 1, 2022
1 parent 096cf3f commit fab7b47
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 14 deletions.
6 changes: 4 additions & 2 deletions internal/services/frontdoor/frontdoor_rules_engine_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ 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"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/synapse/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand All @@ -27,7 +27,9 @@ func resourceFrontDoorRulesEngine() *pluginsdk.Resource {

SchemaVersion: 2,
StateUpgraders: pluginsdk.StateUpgrades(map[int]pluginsdk.StateUpgrade{
1: migration.SynapseLinkedServiceV0ToV1{},
//This resource was merged starting at SchemaVersion:1
0: migration.RulesEngineV0ToV1{},
1: migration.RulesEngineV1ToV2{},
}),

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package migration

import (
"context"
"log"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/frontdoor/parse"
"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 {
Expand Down Expand Up @@ -161,15 +160,6 @@ func (s RulesEngineV0ToV1) Schema() map[string]*pluginsdk.Schema {

func (s RulesEngineV0ToV1) 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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package migration

import (
"context"
"log"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"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": commonschema.LocationComputed(),

"resource_group_name": commonschema.ResourceGroupName(),

"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
}
}

0 comments on commit fab7b47

Please sign in to comment.