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 1 commit
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
83 changes: 52 additions & 31 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,17 +73,26 @@ func resourceMonitorAutoScaleSetting() *pluginsdk.Resource {
Default: true,
},

"predictive_scale_look_ahead_time": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validate.ISO8601DurationBetween("PT1M", "PT1H"),
},
"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,
ValidateFunc: validation.StringInSlice(autoscalesettings.PossibleValuesForPredictiveAutoscalePolicyScaleMode(), false),
},

"predictive_scale_mode": {
Type: pluginsdk.TypeString,
Optional: true,
Default: string(autoscalesettings.PredictiveAutoscalePolicyScaleModeDisabled),
ValidateFunc: validation.StringInSlice(autoscalesettings.PossibleValuesForPredictiveAutoscalePolicyScaleMode(), false),
"look_ahead_time": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validate.ISO8601DurationBetween("PT1M", "PT1H"),
},
},
},
},

"profile": {
Expand Down Expand Up @@ -454,20 +464,12 @@ func resourceMonitorAutoScaleSettingCreateUpdate(d *pluginsdk.ResourceData, meta

t := d.Get("tags").(map[string]interface{})

predictiveAutoscalePolicy := autoscalesettings.PredictiveAutoscalePolicy{
ScaleMode: autoscalesettings.PredictiveAutoscalePolicyScaleMode(d.Get("predictive_scale_mode").(string)),
}

if scaleLookAheadTime := d.Get("predictive_scale_look_ahead_time").(string); scaleLookAheadTime != "" {
predictiveAutoscalePolicy.ScaleLookAheadTime = &scaleLookAheadTime
}

parameters := autoscalesettings.AutoscaleSettingResource{
Location: location,
Properties: autoscalesettings.AutoscaleSetting{
Enabled: &enabled,
Profiles: profiles,
PredictiveAutoscalePolicy: &predictiveAutoscalePolicy,
PredictiveAutoscalePolicy: expandAzureRmMonitorAutoScaleSettingPredictive(d.Get("predictive").([]interface{})),
Notifications: notifications,
TargetResourceUri: &targetResourceId,
},
Expand Down Expand Up @@ -522,20 +524,9 @@ func resourceMonitorAutoScaleSettingRead(d *pluginsdk.ResourceData, meta interfa
return fmt.Errorf("setting `profile` of %s: %+v", *id, err)
}

var scaleMode, scaleLookAheadTime string
if props.PredictiveAutoscalePolicy != nil {
scaleMode = string(props.PredictiveAutoscalePolicy.ScaleMode)
if props.PredictiveAutoscalePolicy.ScaleLookAheadTime != nil {
scaleLookAheadTime = *props.PredictiveAutoscalePolicy.ScaleLookAheadTime
}
}

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

notifications := flattenAzureRmMonitorAutoScaleSettingNotification(props.Notifications)
if err = d.Set("notification", notifications); err != nil {
Expand Down Expand Up @@ -615,6 +606,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 @@ -843,6 +851,19 @@ func flattenAzureRmMonitorAutoScaleSettingProfile(profiles []autoscalesettings.A
return results, nil
}

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

result := make(map[string]interface{})

result["look_ahead_time"] = pointer.From(input.ScaleLookAheadTime)
result["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,13 +74,13 @@ func TestAccMonitorAutoScaleSetting_multipleProfiles(t *testing.T) {
})
}

func TestAccMonitorAutoScaleSetting_predictiveAutoscalePolicy(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.predictiveAutoscalePolicy(data, "Enabled", "PT1M"),
Config: r.predictive(data, "Enabled", "PT1M"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
Expand All @@ -89,7 +89,7 @@ func TestAccMonitorAutoScaleSetting_predictiveAutoscalePolicy(t *testing.T) {
})
}

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

Expand All @@ -102,14 +102,14 @@ func TestAccMonitorAutoScaleSetting_predictiveAutoscalePolicyUpdated(t *testing.
},
data.ImportStep(),
{
Config: r.predictiveAutoscalePolicy(data, "Enabled", "PT1H"),
Config: r.predictive(data, "Enabled", "PT1H"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.predictiveAutoscalePolicy(data, "ForecastOnly", "PT1M"),
Config: r.predictive(data, "ForecastOnly", "PT1M"),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
Expand Down Expand Up @@ -386,6 +386,9 @@ resource "azurerm_monitor_autoscale_setting" "test" {
}
}
}
predictive {
scale_mode = "Disabled"
}
}
`, template, data.RandomInteger)
}
Expand Down Expand Up @@ -430,11 +433,14 @@ resource "azurerm_monitor_autoscale_setting" "import" {
}
}
}
predictive {
scale_mode = "Disabled"
}
}
`, template)
}

func (MonitorAutoScaleSettingResource) predictiveAutoscalePolicy(data acceptance.TestData, scaleMode, scaleLookAheadTime string) string {
func (MonitorAutoScaleSettingResource) predictive(data acceptance.TestData, scaleMode, scaleLookAheadTime string) string {
template := MonitorAutoScaleSettingResource{}.template(data)
return fmt.Sprintf(`
%s
Expand Down Expand Up @@ -476,12 +482,11 @@ resource "azurerm_monitor_autoscale_setting" "test" {
}
}

predictive_scale_mode = %q
predictive_scale_look_ahead_time = %q

predictive {
scale_mode = %q
look_ahead_time = %q
}
}


`, template, data.RandomInteger, scaleMode, scaleLookAheadTime)
}

Expand Down Expand Up @@ -568,6 +573,10 @@ resource "azurerm_monitor_autoscale_setting" "test" {
minutes = [0]
}
}

predictive {
scale_mode = "Disabled"
}
}
`, template, data.RandomInteger)
}
Expand Down Expand Up @@ -613,6 +622,10 @@ resource "azurerm_monitor_autoscale_setting" "test" {
}
}
}

predictive {
scale_mode = "Disabled"
}
}
`, template, data.RandomInteger, defaultVal, min, max)
}
Expand Down Expand Up @@ -680,6 +693,10 @@ resource "azurerm_monitor_autoscale_setting" "test" {
}
}
}

predictive {
scale_mode = "Disabled"
}
}
`, template, data.RandomInteger)
}
Expand Down Expand Up @@ -732,6 +749,10 @@ resource "azurerm_monitor_autoscale_setting" "test" {
custom_emails = ["acctest1-%d@example.com"]
}
}

predictive {
scale_mode = "Disabled"
}
}
`, template, data.RandomInteger, data.RandomInteger)
}
Expand Down Expand Up @@ -784,6 +805,10 @@ resource "azurerm_monitor_autoscale_setting" "test" {
custom_emails = ["acctest1-%d@example.com", "acctest2-%d@example.com"]
}
}

predictive {
scale_mode = "Disabled"
}
}
`, template, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}
Expand Down Expand Up @@ -828,6 +853,10 @@ resource "azurerm_monitor_autoscale_setting" "test" {
send_to_subscription_co_administrator = false
}
}

predictive {
scale_mode = "Disabled"
}
}
`, template, data.RandomInteger)
}
Expand Down Expand Up @@ -872,6 +901,10 @@ resource "azurerm_monitor_autoscale_setting" "test" {
send_to_subscription_co_administrator = false
}
}

predictive {
scale_mode = "Disabled"
}
}
`, template, data.RandomInteger)
}
Expand Down Expand Up @@ -902,6 +935,10 @@ resource "azurerm_monitor_autoscale_setting" "test" {
end = "2020-06-18T23:59:59Z"
}
}

predictive {
scale_mode = "Disabled"
}
}
`, template, data.RandomInteger)
}
Expand Down Expand Up @@ -975,6 +1012,10 @@ resource "azurerm_monitor_autoscale_setting" "test" {
}
}
}

predictive {
scale_mode = "Disabled"
}
}
`, template, data.RandomInteger)
}
Expand Down Expand Up @@ -1053,6 +1094,10 @@ resource "azurerm_monitor_autoscale_setting" "test" {
}
}
}

predictive {
scale_mode = "Disabled"
}
}
`, template, data.RandomInteger)
}
Expand Down
18 changes: 13 additions & 5 deletions website/docs/r/monitor_autoscale_setting.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ resource "azurerm_monitor_autoscale_setting" "example" {
}
}

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

notification {
email {
Expand Down Expand Up @@ -441,9 +443,7 @@ The following arguments are supported:

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

* `predictive_scale_mode` - (Optional) Specifies the predictive scale mode. Possible values are `Disabled`, `Enabled` and `ForecastOnly`. Default to `Disabled`.

* `predictive_scale_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.
* `predictive` - (Optional) A `predictive` block as defined below.

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

Expand Down Expand Up @@ -579,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 `Disabled`, `Enabled` and `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