Skip to content

Commit

Permalink
frontdoor rules engine - a state migration to work around the previou…
Browse files Browse the repository at this point in the history
…sly incorrect id casing
  • Loading branch information
mbfrahry committed Dec 1, 2022
1 parent e104dea commit 096cf3f
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 17 deletions.
6 changes: 5 additions & 1 deletion internal/services/frontdoor/frontdoor_rules_engine_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"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 @@ -24,7 +25,10 @@ func resourceFrontDoorRulesEngine() *pluginsdk.Resource {
Update: resourceFrontDoorRulesEngineCreateUpdate,
Delete: resourceFrontDoorRulesEngineDelete,

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

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,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 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": 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 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
}
}
14 changes: 7 additions & 7 deletions internal/services/frontdoor/parse/rules_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (id RulesEngineId) String() string {
}

func (id RulesEngineId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/frontdoors/%s/rulesengines/%s"
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/frontdoors/%s/rulesEngines/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.FrontdoorName, id.Name)
}

Expand All @@ -63,7 +63,7 @@ func RulesEngineID(input string) (*RulesEngineId, error) {
if resourceId.FrontdoorName, err = id.PopSegment("frontdoors"); err != nil {
return nil, err
}
if resourceId.Name, err = id.PopSegment("rulesengines"); err != nil {
if resourceId.Name, err = id.PopSegment("rulesEngines"); err != nil {
return nil, err
}

Expand Down Expand Up @@ -111,15 +111,15 @@ func RulesEngineIDInsensitively(input string) (*RulesEngineId, error) {
return nil, err
}

// find the correct casing for the 'rulesengines' segment
rulesenginesKey := "rulesengines"
// find the correct casing for the 'rulesEngines' segment
rulesEnginesKey := "rulesEngines"
for key := range id.Path {
if strings.EqualFold(key, rulesenginesKey) {
rulesenginesKey = key
if strings.EqualFold(key, rulesEnginesKey) {
rulesEnginesKey = key
break
}
}
if resourceId.Name, err = id.PopSegment(rulesenginesKey); err != nil {
if resourceId.Name, err = id.PopSegment(rulesEnginesKey); err != nil {
return nil, err
}

Expand Down
10 changes: 5 additions & 5 deletions internal/services/frontdoor/parse/rules_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var _ resourceids.Id = RulesEngineId{}

func TestRulesEngineIDFormatter(t *testing.T) {
actual := NewRulesEngineID("12345678-1234-9876-4563-123456789012", "resGroup1", "frontdoor1", "rule1").ID()
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesengines/rule1"
expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesEngines/rule1"
if actual != expected {
t.Fatalf("Expected %q but got %q", expected, actual)
}
Expand Down Expand Up @@ -75,13 +75,13 @@ func TestRulesEngineID(t *testing.T) {

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesengines/",
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesEngines/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesengines/rule1",
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesEngines/rule1",
Expected: &RulesEngineId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
Expand Down Expand Up @@ -184,13 +184,13 @@ func TestRulesEngineIDInsensitively(t *testing.T) {

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesengines/",
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesEngines/",
Error: true,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesengines/rule1",
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesEngines/rule1",
Expected: &RulesEngineId{
SubscriptionId: "12345678-1234-9876-4563-123456789012",
ResourceGroup: "resGroup1",
Expand Down
2 changes: 1 addition & 1 deletion internal/services/frontdoor/resourceids.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ package frontdoor
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=WebApplicationFirewallPolicy -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontDoorWebApplicationFirewallPolicies/policy1 -rewrite=true
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=CustomHttpsConfiguration -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontDoors/frontdoor1/customHttpsConfiguration/endpoint1 -rewrite=true
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=LoadBalancingRule -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/loadBalancers/loadBalancer1/loadBalancingRules/rule1
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=RulesEngine -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesengines/rule1 -rewrite=true
//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=RulesEngine -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesEngines/rule1 -rewrite=true
4 changes: 2 additions & 2 deletions internal/services/frontdoor/validate/rules_engine_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ func TestRulesEngineID(t *testing.T) {

{
// missing value for Name
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesengines/",
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesEngines/",
Valid: false,
},

{
// valid
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesengines/rule1",
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesEngines/rule1",
Valid: true,
},

Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/frontdoor_rules_engine.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,5 @@ The `match_condition` block supports the following:
Azure Front Door Rules Engine's can be imported using the `resource id`, e.g.

```shell
terraform import azurerm_frontdoor_rules_engine.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesengines/rule1
terraform import azurerm_frontdoor_rules_engine.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Network/frontdoors/frontdoor1/rulesEngines/rule1
```

0 comments on commit 096cf3f

Please sign in to comment.