Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_monitor_autoscale_setting - support for predictive block #22038

Merged
merged 6 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 71 additions & 4 deletions internal/services/monitor/monitor_autoscale_setting_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/Azure/go-autorest/autorest/date"
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2022-10-01/autoscalesettings"
Expand Down Expand Up @@ -72,6 +73,32 @@ func resourceMonitorAutoScaleSetting() *pluginsdk.Resource {
Default: true,
},

"predictive": {
Type: pluginsdk.TypeList,
MaxItems: 1,
MinItems: 1,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"scale_mode": {
Type: pluginsdk.TypeString,
Required: true,
// Disabled is not exposed, omission of this block to mean disabled
ValidateFunc: validation.StringInSlice([]string{
string(autoscalesettings.PredictiveAutoscalePolicyScaleModeEnabled),
string(autoscalesettings.PredictiveAutoscalePolicyScaleModeForecastOnly),
}, false),
},

"look_ahead_time": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validate.ISO8601DurationBetween("PT1M", "PT1H"),
},
},
},
},

"profile": {
Type: pluginsdk.TypeList,
Required: true,
Expand Down Expand Up @@ -444,10 +471,11 @@ func resourceMonitorAutoScaleSettingCreateUpdate(d *pluginsdk.ResourceData, meta
parameters := autoscalesettings.AutoscaleSettingResource{
Location: location,
Properties: autoscalesettings.AutoscaleSetting{
Enabled: &enabled,
Profiles: profiles,
Notifications: notifications,
TargetResourceUri: &targetResourceId,
Enabled: &enabled,
Profiles: profiles,
PredictiveAutoscalePolicy: expandAzureRmMonitorAutoScaleSettingPredictive(d.Get("predictive").([]interface{})),
Notifications: notifications,
TargetResourceUri: &targetResourceId,
},
Tags: utils.ExpandPtrMapStringString(t),
}
Expand Down Expand Up @@ -500,6 +528,10 @@ func resourceMonitorAutoScaleSettingRead(d *pluginsdk.ResourceData, meta interfa
return fmt.Errorf("setting `profile` of %s: %+v", *id, err)
}

if err = d.Set("predictive", flattenAzureRmMonitorAutoScaleSettingPredictive(props.PredictiveAutoscalePolicy)); err != nil {
return fmt.Errorf("setting `predictive_scale_mode` of %s: %+v", *id, err)
}

notifications := flattenAzureRmMonitorAutoScaleSettingNotification(props.Notifications)
if err = d.Set("notification", notifications); err != nil {
return fmt.Errorf("setting `notification` of %s: %+v", *id, err)
Expand Down Expand Up @@ -578,6 +610,23 @@ func expandAzureRmMonitorAutoScaleSettingProfile(input []interface{}) ([]autosca
return results, nil
}

func expandAzureRmMonitorAutoScaleSettingPredictive(input []interface{}) *autoscalesettings.PredictiveAutoscalePolicy {
if len(input) == 0 || input[0] == nil {
return nil
}

raw := input[0].(map[string]interface{})
predictive := autoscalesettings.PredictiveAutoscalePolicy{
ScaleMode: autoscalesettings.PredictiveAutoscalePolicyScaleMode(raw["scale_mode"].(string)),
}

if lookAheadTime := raw["look_ahead_time"].(string); lookAheadTime != "" {
predictive.ScaleLookAheadTime = pointer.To(lookAheadTime)
}

return &predictive
}

func expandAzureRmMonitorAutoScaleSettingRule(input []interface{}) []autoscalesettings.ScaleRule {
rules := make([]autoscalesettings.ScaleRule, 0)

Expand Down Expand Up @@ -806,6 +855,24 @@ func flattenAzureRmMonitorAutoScaleSettingProfile(profiles []autoscalesettings.A
return results, nil
}

func flattenAzureRmMonitorAutoScaleSettingPredictive(input *autoscalesettings.PredictiveAutoscalePolicy) []interface{} {
if input == nil {
return []interface{}{}
}

// omit the block if disabled
if input.ScaleMode == autoscalesettings.PredictiveAutoscalePolicyScaleModeDisabled {
return []interface{}{}
}

result := map[string]interface{}{
"look_ahead_time": pointer.From(input.ScaleLookAheadTime),
"scale_mode": string(input.ScaleMode),
}

return []interface{}{result}
}

func flattenAzureRmMonitorAutoScaleSettingCapacity(input autoscalesettings.ScaleCapacity) ([]interface{}, error) {

result := make(map[string]interface{})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,50 @@ func TestAccMonitorAutoScaleSetting_multipleProfiles(t *testing.T) {
})
}

func TestAccMonitorAutoScaleSetting_predictive(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_monitor_autoscale_setting", "test")
r := MonitorAutoScaleSettingResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.predictive(data, "Enabled", "PT1M"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccMonitorAutoScaleSetting_predictiveUpdated(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_monitor_autoscale_setting", "test")
r := MonitorAutoScaleSettingResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.predictive(data, "Enabled", "PT1H"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.predictive(data, "ForecastOnly", "PT1M"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccMonitorAutoScaleSetting_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_monitor_autoscale_setting", "test")
r := MonitorAutoScaleSettingResource{}
Expand Down Expand Up @@ -390,6 +434,56 @@ resource "azurerm_monitor_autoscale_setting" "import" {
`, template)
}

func (MonitorAutoScaleSettingResource) predictive(data acceptance.TestData, scaleMode, scaleLookAheadTime string) string {
template := MonitorAutoScaleSettingResource{}.template(data)
return fmt.Sprintf(`
%s

resource "azurerm_monitor_autoscale_setting" "test" {
name = "acctestautoscale-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
target_resource_id = azurerm_linux_virtual_machine_scale_set.test.id

profile {
name = "metricRules"

capacity {
default = 1
minimum = 1
maximum = 10
}

rule {
metric_trigger {
metric_name = "Percentage CPU"
metric_resource_id = azurerm_linux_virtual_machine_scale_set.test.id
time_grain = "PT1M"
statistic = "Average"
time_window = "PT5M"
time_aggregation = "Last"
operator = "GreaterThan"
threshold = 75
divide_by_instance_count = true
}

scale_action {
direction = "Increase"
type = "ChangeCount"
value = 1
cooldown = "PT1M"
}
}
}

predictive {
scale_mode = %q
look_ahead_time = %q
}
}
`, template, data.RandomInteger, scaleMode, scaleLookAheadTime)
}

func (MonitorAutoScaleSettingResource) multipleProfiles(data acceptance.TestData) string {
template := MonitorAutoScaleSettingResource{}.template(data)
return fmt.Sprintf(`
Expand Down
15 changes: 15 additions & 0 deletions website/docs/r/monitor_autoscale_setting.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ resource "azurerm_monitor_autoscale_setting" "example" {
}
}

predictive {
scale_mode = "Enabled"
look_ahead_time = "PT5M"
}

notification {
email {
send_to_subscription_administrator = true
Expand Down Expand Up @@ -438,6 +443,8 @@ The following arguments are supported:

* `notification` - (Optional) Specifies a `notification` block as defined below.

* `predictive` - (Optional) A `predictive` block as defined below.

* `tags` - (Optional) A mapping of tags to assign to the resource.

---
Expand Down Expand Up @@ -572,6 +579,14 @@ A `dimensions` block supports the following:

* `values` - (Required) A list of dimension values.

---

A `predictive` block supports the following:

* `scale_mode` - (Required) Specifies the predictive scale mode. Possible values are `Enabled` or `ForecastOnly`.

* `look_ahead_time` - (Optional) Specifies the amount of time by which instances are launched in advance. It must be between `PT1M` and `PT1H` in ISO 8601 format.

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:
Expand Down