diff --git a/internal/services/monitor/migration/autoscale_setting_test.go b/internal/services/monitor/migration/autoscale_setting_test.go index 1ab148511f6e..458f30fafd08 100644 --- a/internal/services/monitor/migration/autoscale_setting_test.go +++ b/internal/services/monitor/migration/autoscale_setting_test.go @@ -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 { diff --git a/internal/services/monitor/migration/autoscale_setting_v1_to_v2.go b/internal/services/monitor/migration/autoscale_setting_v1_to_v2.go new file mode 100644 index 000000000000..671e1e623f64 --- /dev/null +++ b/internal/services/monitor/migration/autoscale_setting_v1_to_v2.go @@ -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 + } +} diff --git a/internal/services/monitor/monitor_autoscale_setting_resource.go b/internal/services/monitor/monitor_autoscale_setting_resource.go index dfb649564828..c9da34378e1f 100644 --- a/internal/services/monitor/monitor_autoscale_setting_resource.go +++ b/internal/services/monitor/monitor_autoscale_setting_resource.go @@ -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{ diff --git a/internal/services/monitor/parse/autoscale_setting.go b/internal/services/monitor/parse/autoscale_setting.go index 47f58f4781f2..07ee7a5e548b 100644 --- a/internal/services/monitor/parse/autoscale_setting.go +++ b/internal/services/monitor/parse/autoscale_setting.go @@ -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) } @@ -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 } @@ -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 } diff --git a/internal/services/monitor/parse/autoscale_setting_test.go b/internal/services/monitor/parse/autoscale_setting_test.go index 26775d981793..401359db6af0 100644 --- a/internal/services/monitor/parse/autoscale_setting_test.go +++ b/internal/services/monitor/parse/autoscale_setting_test.go @@ -12,7 +12,7 @@ var _ resourceids.Id = AutoscaleSettingId{} func TestAutoscaleSettingIDFormatter(t *testing.T) { actual := NewAutoscaleSettingID("12345678-1234-9876-4563-123456789012", "group1", "setting1").ID() - expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoscaleSettings/setting1" + expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoScaleSettings/setting1" if actual != expected { t.Fatalf("Expected %q but got %q", expected, actual) } @@ -63,13 +63,13 @@ func TestAutoscaleSettingID(t *testing.T) { { // missing value for Name - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoscaleSettings/", + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoScaleSettings/", Error: true, }, { // valid - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoscaleSettings/setting1", + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoScaleSettings/setting1", Expected: &AutoscaleSettingId{ SubscriptionId: "12345678-1234-9876-4563-123456789012", ResourceGroup: "group1", @@ -156,13 +156,13 @@ func TestAutoscaleSettingIDInsensitively(t *testing.T) { { // missing value for Name - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoscaleSettings/", + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoScaleSettings/", Error: true, }, { // valid - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoscaleSettings/setting1", + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoScaleSettings/setting1", Expected: &AutoscaleSettingId{ SubscriptionId: "12345678-1234-9876-4563-123456789012", ResourceGroup: "group1", diff --git a/internal/services/monitor/resourceids.go b/internal/services/monitor/resourceids.go index 23f55831bc10..1e5c86e27dff 100644 --- a/internal/services/monitor/resourceids.go +++ b/internal/services/monitor/resourceids.go @@ -4,7 +4,7 @@ package monitor //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ActionRule -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.AlertsManagement/actionRules/actionRule1 //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=SmartDetectorAlertRule -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.AlertsManagement/smartdetectoralertrules/rule1 //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=ActivityLogAlert -rewrite=true -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/activityLogAlerts/alert1 -//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=AutoscaleSetting -rewrite=true -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoscaleSettings/setting1 +//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=AutoscaleSetting -rewrite=true -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoScaleSettings/setting1 -rewrite=true //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=LogProfile -id=/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Insights/logProfiles/profile1 //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=MetricAlert -rewrite=true -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/metricAlerts/alert1 //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=PrivateLinkScope -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/privateLinkScopes/pls1 diff --git a/internal/services/monitor/validate/autoscale_setting_id_test.go b/internal/services/monitor/validate/autoscale_setting_id_test.go index f3a2d2f50d00..290949c7ba99 100644 --- a/internal/services/monitor/validate/autoscale_setting_id_test.go +++ b/internal/services/monitor/validate/autoscale_setting_id_test.go @@ -48,13 +48,13 @@ func TestAutoscaleSettingID(t *testing.T) { { // missing value for Name - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoscaleSettings/", + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoScaleSettings/", Valid: false, }, { // valid - Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoscaleSettings/setting1", + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/group1/providers/Microsoft.Insights/autoScaleSettings/setting1", Valid: true, }, diff --git a/website/docs/r/monitor_autoscale_setting.html.markdown b/website/docs/r/monitor_autoscale_setting.html.markdown index 9c10848631ee..44429d52a349 100644 --- a/website/docs/r/monitor_autoscale_setting.html.markdown +++ b/website/docs/r/monitor_autoscale_setting.html.markdown @@ -592,5 +592,5 @@ The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/l AutoScale Setting can be imported using the `resource id`, e.g. ```shell -terraform import azurerm_monitor_autoscale_setting.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Insights/autoscaleSettings/setting1 +terraform import azurerm_monitor_autoscale_setting.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Insights/autoScaleSettings/setting1 ```