Skip to content

Commit

Permalink
azurerm_monitor_autoscale_setting - a state migration to work aroun…
Browse files Browse the repository at this point in the history
…d the previously incorrect id casing (#19492)
  • Loading branch information
mbfrahry authored Nov 30, 2022
1 parent 07772c5 commit fd36a66
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 22 deletions.
10 changes: 5 additions & 5 deletions internal/services/monitor/migration/autoscale_setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ func TestAutoscaleSettingV0ToV1(t *testing.T) {
{
name: "old id",
input: map[string]interface{}{
"id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourcegroups/group1/providers/microsoft.insights/autoscalesettings/setting1",
"id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourcegroups/group1/providers/microsoft.insights/autoScaleSettings/setting1",
},
expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoscaleSettings/setting1"),
expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoScaleSettings/setting1"),
},
{
name: "old id - mixed case",
input: map[string]interface{}{
"id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourcegroups/group1/providers/microsoft.insights/AutoscaleSettings/setting1",
},
expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoscaleSettings/setting1"),
expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoScaleSettings/setting1"),
},
{
name: "new id",
input: map[string]interface{}{
"id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoscaleSettings/setting1",
"id": "/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoScaleSettings/setting1",
},
expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoscaleSettings/setting1"),
expected: utils.String("/subscriptions/12345678-1234-5678-1234-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoScaleSettings/setting1"),
},
}
for _, test := range testData {
Expand Down
309 changes: 309 additions & 0 deletions internal/services/monitor/migration/autoscale_setting_v1_to_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,309 @@
package migration

import (
"context"
"log"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/monitor/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

type AutoscaleSettingUpgradeV1ToV2 struct{}

func (s AutoscaleSettingUpgradeV1ToV2) Schema() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
},

"resource_group_name": commonschema.ResourceGroupName(),

"location": commonschema.Location(),

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

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

"profile": {
Type: pluginsdk.TypeList,
Required: true,
MaxItems: 20,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
},
"capacity": {
Type: pluginsdk.TypeList,
Required: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"minimum": {
Type: pluginsdk.TypeInt,
Required: true,
},
"maximum": {
Type: pluginsdk.TypeInt,
Required: true,
},
"default": {
Type: pluginsdk.TypeInt,
Required: true,
},
},
},
},
"rule": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 10,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"metric_trigger": {
Type: pluginsdk.TypeList,
Required: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"metric_name": {
Type: pluginsdk.TypeString,
Required: true,
},
"metric_resource_id": {
Type: pluginsdk.TypeString,
Required: true,
},
"time_grain": {
Type: pluginsdk.TypeString,
Required: true,
},
"statistic": {
Type: pluginsdk.TypeString,
Required: true,
},
"time_window": {
Type: pluginsdk.TypeString,
Required: true,
},
"time_aggregation": {
Type: pluginsdk.TypeString,
Required: true,
},
"operator": {
Type: pluginsdk.TypeString,
Required: true,
},
"threshold": {
Type: pluginsdk.TypeFloat,
Required: true,
},

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

"divide_by_instance_count": {
Type: pluginsdk.TypeBool,
Optional: true,
},

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

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

"values": {
Type: pluginsdk.TypeList,
Required: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},
},
},
},
"scale_action": {
Type: pluginsdk.TypeList,
Required: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"direction": {
Type: pluginsdk.TypeString,
Required: true,
},
"type": {
Type: pluginsdk.TypeString,
Required: true,
},
"value": {
Type: pluginsdk.TypeInt,
Required: true,
},
"cooldown": {
Type: pluginsdk.TypeString,
Required: true,
},
},
},
},
},
},
},
"fixed_date": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"timezone": {
Type: pluginsdk.TypeString,
Optional: true,
},
"start": {
Type: pluginsdk.TypeString,
Required: true,
},
"end": {
Type: pluginsdk.TypeString,
Required: true,
},
},
},
},
"recurrence": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"timezone": {
Type: pluginsdk.TypeString,
Optional: true,
},
"days": {
Type: pluginsdk.TypeList,
Required: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"hours": {
Type: pluginsdk.TypeList,
Required: true,
MaxItems: 1,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeInt,
},
},
"minutes": {
Type: pluginsdk.TypeList,
Required: true,
MaxItems: 1,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeInt,
},
},
},
},
},
},
},
},

"notification": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"email": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"send_to_subscription_administrator": {
Type: pluginsdk.TypeBool,
Optional: true,
},
"send_to_subscription_co_administrator": {
Type: pluginsdk.TypeBool,
Optional: true,
},
"custom_emails": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},
"webhook": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"service_uri": {
Type: pluginsdk.TypeString,
Required: true,
},
"properties": {
Type: pluginsdk.TypeMap,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
},
},
},
},
},

"tags": tags.Schema(),
}
}

func (s AutoscaleSettingUpgradeV1ToV2) 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.AutoscaleSettingIDInsensitively(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
Expand Up @@ -35,9 +35,10 @@ func resourceMonitorAutoScaleSetting() *pluginsdk.Resource {
return err
}),

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

Timeouts: &pluginsdk.ResourceTimeout{
Expand Down
14 changes: 7 additions & 7 deletions internal/services/monitor/parse/autoscale_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (id AutoscaleSettingId) String() string {
}

func (id AutoscaleSettingId) ID() string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Insights/autoscaleSettings/%s"
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Insights/autoScaleSettings/%s"
return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.Name)
}

Expand All @@ -57,7 +57,7 @@ func AutoscaleSettingID(input string) (*AutoscaleSettingId, error) {
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}

if resourceId.Name, err = id.PopSegment("autoscaleSettings"); err != nil {
if resourceId.Name, err = id.PopSegment("autoScaleSettings"); err != nil {
return nil, err
}

Expand Down Expand Up @@ -93,15 +93,15 @@ func AutoscaleSettingIDInsensitively(input string) (*AutoscaleSettingId, error)
return nil, fmt.Errorf("ID was missing the 'resourceGroups' element")
}

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

Expand Down
Loading

0 comments on commit fd36a66

Please sign in to comment.