From 66de6084ad1035d2e5ba9106f8eb86f32397b839 Mon Sep 17 00:00:00 2001 From: Tom Harvey Date: Thu, 7 Feb 2019 09:42:32 +0100 Subject: [PATCH] re-opening #2806 (adjust autoscale limit validation) (#2815) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Including the changes from @georgespatton in #2806 - pushing to that fork broken the PR 🤷‍♂️ Original PR: --- This should fix #2136 as well as #2624 to follow the Azure documentation here: https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/overview Fixes #2136 Fixes #2624 --- azurerm/resource_arm_autoscale_setting.go | 6 +- .../resource_arm_autoscale_setting_test.go | 205 ++++++++++++------ .../resource_arm_monitor_autoscale_setting.go | 6 +- ...urce_arm_monitor_autoscale_setting_test.go | 192 ++++++++++------ examples/vmss-ubuntu/main.tf | 56 +++++ .../docs/r/autoscale_setting.html.markdown | 6 +- .../r/monitor_autoscale_setting.html.markdown | 6 +- 7 files changed, 331 insertions(+), 146 deletions(-) diff --git a/azurerm/resource_arm_autoscale_setting.go b/azurerm/resource_arm_autoscale_setting.go index f8fff2d97e99..8de1667ab976 100644 --- a/azurerm/resource_arm_autoscale_setting.go +++ b/azurerm/resource_arm_autoscale_setting.go @@ -79,17 +79,17 @@ As such the existing 'azurerm_autoscale_setting' resource is deprecated and will "minimum": { Type: schema.TypeInt, Required: true, - ValidateFunc: validation.IntBetween(1, 40), + ValidateFunc: validation.IntBetween(0, 1000), }, "maximum": { Type: schema.TypeInt, Required: true, - ValidateFunc: validation.IntBetween(1, 40), + ValidateFunc: validation.IntBetween(0, 1000), }, "default": { Type: schema.TypeInt, Required: true, - ValidateFunc: validation.IntBetween(1, 40), + ValidateFunc: validation.IntBetween(0, 1000), }, }, }, diff --git a/azurerm/resource_arm_autoscale_setting_test.go b/azurerm/resource_arm_autoscale_setting_test.go index 83ec76e398ce..31bf65e7f226 100644 --- a/azurerm/resource_arm_autoscale_setting_test.go +++ b/azurerm/resource_arm_autoscale_setting_test.go @@ -5,7 +5,6 @@ import ( "net/http" "testing" - "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" @@ -14,9 +13,8 @@ import ( func TestAccAzureRMAutoScaleSetting_basic(t *testing.T) { resourceName := "azurerm_autoscale_setting.test" ri := tf.AccRandTimeInt() - rs := acctest.RandString(6) location := testLocation() - config := testAccAzureRMAutoScaleSetting_basic(ri, rs, location) + config := testAccAzureRMAutoScaleSetting_basic(ri, location) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -52,7 +50,6 @@ func TestAccAzureRMAutoScaleSetting_requiresImport(t *testing.T) { resourceName := "azurerm_autoscale_setting.test" ri := tf.AccRandTimeInt() - rs := acctest.RandString(6) location := testLocation() resource.ParallelTest(t, resource.TestCase{ @@ -61,13 +58,13 @@ func TestAccAzureRMAutoScaleSetting_requiresImport(t *testing.T) { CheckDestroy: testCheckAzureRMAutoScaleSettingDestroy, Steps: []resource.TestStep{ { - Config: testAccAzureRMAutoScaleSetting_basic(ri, rs, location), + Config: testAccAzureRMAutoScaleSetting_basic(ri, location), Check: resource.ComposeTestCheckFunc( testCheckAzureRMAutoScaleSettingExists(resourceName), ), }, { - Config: testAccAzureRMAutoScaleSetting_requiresImport(ri, rs, location), + Config: testAccAzureRMAutoScaleSetting_requiresImport(ri, location), ExpectError: testRequiresImportError("azurerm_autoscale_setting"), }, }, @@ -77,9 +74,8 @@ func TestAccAzureRMAutoScaleSetting_requiresImport(t *testing.T) { func TestAccAzureRMAutoScaleSetting_multipleProfiles(t *testing.T) { resourceName := "azurerm_autoscale_setting.test" ri := tf.AccRandTimeInt() - rs := acctest.RandString(6) location := testLocation() - config := testAccAzureRMAutoScaleSetting_multipleProfiles(ri, rs, location) + config := testAccAzureRMAutoScaleSetting_multipleProfiles(ri, location) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -100,10 +96,56 @@ func TestAccAzureRMAutoScaleSetting_multipleProfiles(t *testing.T) { }) } +func TestAccAzureRMAutoScaleSetting_update(t *testing.T) { + resourceName := "azurerm_autoscale_setting.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMAutoScaleSettingDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMAutoScaleSetting_capacity(ri, location, 1, 3, 2), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutoScaleSettingExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "false"), + resource.TestCheckResourceAttr(resourceName, "profile.#", "1"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.minimum", "1"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.maximum", "3"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.default", "2"), + ), + }, + { + Config: testAccAzureRMAutoScaleSetting_capacity(ri, location, 0, 400, 0), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutoScaleSettingExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "false"), + resource.TestCheckResourceAttr(resourceName, "profile.#", "1"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.minimum", "0"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.maximum", "400"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.default", "0"), + ), + }, + { + Config: testAccAzureRMAutoScaleSetting_capacity(ri, location, 2, 45, 3), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMAutoScaleSettingExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "false"), + resource.TestCheckResourceAttr(resourceName, "profile.#", "1"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.minimum", "2"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.maximum", "45"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.default", "3"), + ), + }, + }, + }) +} + func TestAccAzureRMAutoScaleSetting_multipleRules(t *testing.T) { resourceName := "azurerm_autoscale_setting.test" ri := tf.AccRandTimeInt() - rs := acctest.RandString(6) location := testLocation() resource.ParallelTest(t, resource.TestCase{ @@ -112,7 +154,7 @@ func TestAccAzureRMAutoScaleSetting_multipleRules(t *testing.T) { CheckDestroy: testCheckAzureRMAutoScaleSettingDestroy, Steps: []resource.TestStep{ { - Config: testAccAzureRMAutoScaleSetting_basic(ri, rs, location), + Config: testAccAzureRMAutoScaleSetting_basic(ri, location), Check: resource.ComposeTestCheckFunc( testCheckAzureRMAutoScaleSettingExists(resourceName), resource.TestCheckResourceAttr(resourceName, "enabled", "true"), @@ -124,7 +166,7 @@ func TestAccAzureRMAutoScaleSetting_multipleRules(t *testing.T) { ), }, { - Config: testAccAzureRMAutoScaleSetting_multipleRules(ri, rs, location), + Config: testAccAzureRMAutoScaleSetting_multipleRules(ri, location), Check: resource.ComposeTestCheckFunc( testCheckAzureRMAutoScaleSettingExists(resourceName), resource.TestCheckResourceAttr(resourceName, "enabled", "true"), @@ -143,7 +185,6 @@ func TestAccAzureRMAutoScaleSetting_multipleRules(t *testing.T) { func TestAccAzureRMAutoScaleSetting_customEmails(t *testing.T) { resourceName := "azurerm_autoscale_setting.test" ri := tf.AccRandTimeInt() - rs := acctest.RandString(6) location := testLocation() resource.ParallelTest(t, resource.TestCase{ @@ -152,7 +193,7 @@ func TestAccAzureRMAutoScaleSetting_customEmails(t *testing.T) { CheckDestroy: testCheckAzureRMAutoScaleSettingDestroy, Steps: []resource.TestStep{ { - Config: testAccAzureRMAutoScaleSetting_email(ri, rs, location), + Config: testAccAzureRMAutoScaleSetting_email(ri, location), Check: resource.ComposeTestCheckFunc( testCheckAzureRMAutoScaleSettingExists(resourceName), resource.TestCheckResourceAttr(resourceName, "notification.#", "1"), @@ -162,7 +203,7 @@ func TestAccAzureRMAutoScaleSetting_customEmails(t *testing.T) { ), }, { - Config: testAccAzureRMAutoScaleSetting_emailUpdated(ri, rs, location), + Config: testAccAzureRMAutoScaleSetting_emailUpdated(ri, location), Check: resource.ComposeTestCheckFunc( testCheckAzureRMAutoScaleSettingExists(resourceName), resource.TestCheckResourceAttr(resourceName, "notification.#", "1"), @@ -179,9 +220,8 @@ func TestAccAzureRMAutoScaleSetting_customEmails(t *testing.T) { func TestAccAzureRMAutoScaleSetting_recurrence(t *testing.T) { resourceName := "azurerm_autoscale_setting.test" ri := tf.AccRandTimeInt() - rs := acctest.RandString(6) location := testLocation() - config := testAccAzureRMAutoScaleSetting_recurrence(ri, rs, location) + config := testAccAzureRMAutoScaleSetting_recurrence(ri, location) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -211,7 +251,6 @@ func TestAccAzureRMAutoScaleSetting_recurrence(t *testing.T) { func TestAccAzureRMAutoScaleSetting_recurrenceUpdate(t *testing.T) { resourceName := "azurerm_autoscale_setting.test" ri := tf.AccRandTimeInt() - rs := acctest.RandString(6) location := testLocation() resource.ParallelTest(t, resource.TestCase{ @@ -220,7 +259,7 @@ func TestAccAzureRMAutoScaleSetting_recurrenceUpdate(t *testing.T) { CheckDestroy: testCheckAzureRMAutoScaleSettingDestroy, Steps: []resource.TestStep{ { - Config: testAccAzureRMAutoScaleSetting_recurrence(ri, rs, location), + Config: testAccAzureRMAutoScaleSetting_recurrence(ri, location), Check: resource.ComposeTestCheckFunc( testCheckAzureRMAutoScaleSettingExists(resourceName), resource.TestCheckResourceAttr(resourceName, "notification.#", "1"), @@ -233,7 +272,7 @@ func TestAccAzureRMAutoScaleSetting_recurrenceUpdate(t *testing.T) { ), }, { - Config: testAccAzureRMAutoScaleSetting_recurrenceUpdated(ri, rs, location), + Config: testAccAzureRMAutoScaleSetting_recurrenceUpdated(ri, location), Check: resource.ComposeTestCheckFunc( testCheckAzureRMAutoScaleSettingExists(resourceName), resource.TestCheckResourceAttr(resourceName, "profile.0.recurrence.#", "1"), @@ -252,9 +291,8 @@ func TestAccAzureRMAutoScaleSetting_recurrenceUpdate(t *testing.T) { func TestAccAzureRMAutoScaleSetting_fixedDate(t *testing.T) { resourceName := "azurerm_autoscale_setting.test" ri := tf.AccRandTimeInt() - rs := acctest.RandString(6) location := testLocation() - config := testAccAzureRMAutoScaleSetting_fixedDate(ri, rs, location) + config := testAccAzureRMAutoScaleSetting_fixedDate(ri, location) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -336,8 +374,8 @@ func testCheckAzureRMAutoScaleSettingDestroy(s *terraform.State) error { return nil } -func testAccAzureRMAutoScaleSetting_basic(rInt int, rString string, location string) string { - template := testAccAzureRMAutoScaleSetting_template(rInt, rString, location) +func testAccAzureRMAutoScaleSetting_basic(rInt int, location string) string { + template := testAccAzureRMAutoScaleSetting_template(rInt, location) return fmt.Sprintf(` %s @@ -380,8 +418,8 @@ resource "azurerm_autoscale_setting" "test" { `, template, rInt) } -func testAccAzureRMAutoScaleSetting_requiresImport(rInt int, rString string, location string) string { - template := testAccAzureRMAutoScaleSetting_basic(rInt, rString, location) +func testAccAzureRMAutoScaleSetting_requiresImport(rInt int, location string) string { + template := testAccAzureRMAutoScaleSetting_basic(rInt, location) return fmt.Sprintf(` %s @@ -424,8 +462,8 @@ resource "azurerm_autoscale_setting" "import" { `, template) } -func testAccAzureRMAutoScaleSetting_multipleProfiles(rInt int, rString string, location string) string { - template := testAccAzureRMAutoScaleSetting_template(rInt, rString, location) +func testAccAzureRMAutoScaleSetting_multipleProfiles(rInt int, location string) string { + template := testAccAzureRMAutoScaleSetting_template(rInt, location) return fmt.Sprintf(` %s @@ -511,8 +549,8 @@ resource "azurerm_autoscale_setting" "test" { `, template, rInt) } -func testAccAzureRMAutoScaleSetting_multipleRules(rInt int, rString string, location string) string { - template := testAccAzureRMAutoScaleSetting_template(rInt, rString, location) +func testAccAzureRMAutoScaleSetting_multipleRules(rInt int, location string) string { + template := testAccAzureRMAutoScaleSetting_template(rInt, location) return fmt.Sprintf(` %s @@ -576,8 +614,53 @@ resource "azurerm_autoscale_setting" "test" { `, template, rInt) } -func testAccAzureRMAutoScaleSetting_email(rInt int, rString string, location string) string { - template := testAccAzureRMAutoScaleSetting_template(rInt, rString, location) +func testAccAzureRMAutoScaleSetting_capacity(rInt int, location string, min int, max int, defaultVal int) string { + template := testAccAzureRMAutoScaleSetting_template(rInt, location) + return fmt.Sprintf(` +%s + +resource "azurerm_autoscale_setting" "test" { + name = "acctestautoscale-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + location = "${azurerm_resource_group.test.location}" + target_resource_id = "${azurerm_virtual_machine_scale_set.test.id}" + enabled = false + + profile { + name = "metricRules" + + capacity { + default = %d + minimum = %d + maximum = %d + } + + rule { + metric_trigger { + metric_name = "Percentage CPU" + metric_resource_id = "${azurerm_virtual_machine_scale_set.test.id}" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT5M" + time_aggregation = "Average" + operator = "GreaterThan" + threshold = 75 + } + + scale_action { + direction = "Increase" + type = "ChangeCount" + value = 1 + cooldown = "PT1M" + } + } + } +} +`, template, rInt, defaultVal, min, max) +} + +func testAccAzureRMAutoScaleSetting_email(rInt int, location string) string { + template := testAccAzureRMAutoScaleSetting_template(rInt, location) return fmt.Sprintf(` %s @@ -628,8 +711,8 @@ resource "azurerm_autoscale_setting" "test" { `, template, rInt, rInt) } -func testAccAzureRMAutoScaleSetting_emailUpdated(rInt int, rString string, location string) string { - template := testAccAzureRMAutoScaleSetting_template(rInt, rString, location) +func testAccAzureRMAutoScaleSetting_emailUpdated(rInt int, location string) string { + template := testAccAzureRMAutoScaleSetting_template(rInt, location) return fmt.Sprintf(` %s @@ -680,8 +763,8 @@ resource "azurerm_autoscale_setting" "test" { `, template, rInt, rInt, rInt) } -func testAccAzureRMAutoScaleSetting_recurrence(rInt int, rString string, location string) string { - template := testAccAzureRMAutoScaleSetting_template(rInt, rString, location) +func testAccAzureRMAutoScaleSetting_recurrence(rInt int, location string) string { + template := testAccAzureRMAutoScaleSetting_template(rInt, location) return fmt.Sprintf(` %s @@ -724,8 +807,8 @@ resource "azurerm_autoscale_setting" "test" { `, template, rInt) } -func testAccAzureRMAutoScaleSetting_recurrenceUpdated(rInt int, rString string, location string) string { - template := testAccAzureRMAutoScaleSetting_template(rInt, rString, location) +func testAccAzureRMAutoScaleSetting_recurrenceUpdated(rInt int, location string) string { + template := testAccAzureRMAutoScaleSetting_template(rInt, location) return fmt.Sprintf(` %s @@ -768,8 +851,8 @@ resource "azurerm_autoscale_setting" "test" { `, template, rInt) } -func testAccAzureRMAutoScaleSetting_fixedDate(rInt int, rString string, location string) string { - template := testAccAzureRMAutoScaleSetting_template(rInt, rString, location) +func testAccAzureRMAutoScaleSetting_fixedDate(rInt int, location string) string { + template := testAccAzureRMAutoScaleSetting_template(rInt, location) return fmt.Sprintf(` %s @@ -798,7 +881,7 @@ resource "azurerm_autoscale_setting" "test" { `, template, rInt) } -func testAccAzureRMAutoScaleSetting_template(rInt int, rString string, location string) string { +func testAccAzureRMAutoScaleSetting_template(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { name = "acctestRG-%d" @@ -819,35 +902,17 @@ resource "azurerm_subnet" "test" { address_prefix = "10.0.2.0/24" } -resource "azurerm_storage_account" "test" { - name = "accsa%s" - resource_group_name = "${azurerm_resource_group.test.name}" - location = "${azurerm_resource_group.test.location}" - account_tier = "Standard" - account_replication_type = "LRS" - - tags { - environment = "staging" - } -} - -resource "azurerm_storage_container" "test" { - name = "vhds" - resource_group_name = "${azurerm_resource_group.test.name}" - storage_account_name = "${azurerm_storage_account.test.name}" - container_access_type = "private" -} - resource "azurerm_virtual_machine_scale_set" "test" { - name = "acctvmss-%d" - location = "${azurerm_resource_group.test.location}" - resource_group_name = "${azurerm_resource_group.test.name}" - upgrade_policy_mode = "Manual" + name = "acctvmss-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + upgrade_policy_mode = "Automatic" + single_placement_group = "false" sku { - name = "Standard_F2" + name = "Standard_DS1_v2" tier = "Standard" - capacity = 2 + capacity = 30 } os_profile { @@ -868,10 +933,10 @@ resource "azurerm_virtual_machine_scale_set" "test" { } storage_profile_os_disk { - name = "osDiskProfile" - caching = "ReadWrite" - create_option = "FromImage" - vhd_containers = ["${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}"] + name = "" + caching = "ReadWrite" + create_option = "FromImage" + managed_disk_type = "StandardSSD_LRS" } storage_profile_image_reference { @@ -881,5 +946,5 @@ resource "azurerm_virtual_machine_scale_set" "test" { version = "latest" } } -`, rInt, location, rInt, rString, rInt, rInt, rInt) +`, rInt, location, rInt, rInt, rInt, rInt) } diff --git a/azurerm/resource_arm_monitor_autoscale_setting.go b/azurerm/resource_arm_monitor_autoscale_setting.go index 31d0bdd275c2..7f760b4e6a56 100644 --- a/azurerm/resource_arm_monitor_autoscale_setting.go +++ b/azurerm/resource_arm_monitor_autoscale_setting.go @@ -72,17 +72,17 @@ func resourceArmMonitorAutoScaleSetting() *schema.Resource { "minimum": { Type: schema.TypeInt, Required: true, - ValidateFunc: validation.IntBetween(1, 40), + ValidateFunc: validation.IntBetween(0, 1000), }, "maximum": { Type: schema.TypeInt, Required: true, - ValidateFunc: validation.IntBetween(1, 40), + ValidateFunc: validation.IntBetween(0, 1000), }, "default": { Type: schema.TypeInt, Required: true, - ValidateFunc: validation.IntBetween(1, 40), + ValidateFunc: validation.IntBetween(0, 1000), }, }, }, diff --git a/azurerm/resource_arm_monitor_autoscale_setting_test.go b/azurerm/resource_arm_monitor_autoscale_setting_test.go index b88abba22fb0..a827e088e604 100644 --- a/azurerm/resource_arm_monitor_autoscale_setting_test.go +++ b/azurerm/resource_arm_monitor_autoscale_setting_test.go @@ -5,7 +5,6 @@ import ( "net/http" "testing" - "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" @@ -14,9 +13,8 @@ import ( func TestAccAzureRMMonitorAutoScaleSetting_basic(t *testing.T) { resourceName := "azurerm_monitor_autoscale_setting.test" ri := tf.AccRandTimeInt() - rs := acctest.RandString(6) location := testLocation() - config := testAccAzureRMMonitorAutoScaleSetting_basic(ri, rs, location) + config := testAccAzureRMMonitorAutoScaleSetting_basic(ri, location) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -52,7 +50,6 @@ func TestAccAzureRMMonitorAutoScaleSetting_requiresImport(t *testing.T) { resourceName := "azurerm_monitor_autoscale_setting.test" ri := tf.AccRandTimeInt() - rs := acctest.RandString(6) location := testLocation() resource.ParallelTest(t, resource.TestCase{ @@ -61,13 +58,13 @@ func TestAccAzureRMMonitorAutoScaleSetting_requiresImport(t *testing.T) { CheckDestroy: testCheckAzureRMMonitorAutoScaleSettingDestroy, Steps: []resource.TestStep{ { - Config: testAccAzureRMMonitorAutoScaleSetting_basic(ri, rs, location), + Config: testAccAzureRMMonitorAutoScaleSetting_basic(ri, location), Check: resource.ComposeTestCheckFunc( testCheckAzureRMMonitorAutoScaleSettingExists(resourceName), ), }, { - Config: testAccAzureRMMonitorAutoScaleSetting_requiresImport(ri, rs, location), + Config: testAccAzureRMMonitorAutoScaleSetting_requiresImport(ri, location), ExpectError: testRequiresImportError("azurerm_monitor_autoscale_setting"), }, }, @@ -77,9 +74,8 @@ func TestAccAzureRMMonitorAutoScaleSetting_requiresImport(t *testing.T) { func TestAccAzureRMMonitorAutoScaleSetting_multipleProfiles(t *testing.T) { resourceName := "azurerm_monitor_autoscale_setting.test" ri := tf.AccRandTimeInt() - rs := acctest.RandString(6) location := testLocation() - config := testAccAzureRMMonitorAutoScaleSetting_multipleProfiles(ri, rs, location) + config := testAccAzureRMMonitorAutoScaleSetting_multipleProfiles(ri, location) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -100,10 +96,56 @@ func TestAccAzureRMMonitorAutoScaleSetting_multipleProfiles(t *testing.T) { }) } +func TestAccAzureRMMonitorAutoScaleSetting_update(t *testing.T) { + resourceName := "azurerm_monitor_autoscale_setting.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMMonitorAutoScaleSettingDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMMonitorAutoScaleSetting_capacity(ri, location, 1, 3, 2), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorAutoScaleSettingExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "false"), + resource.TestCheckResourceAttr(resourceName, "profile.#", "1"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.minimum", "1"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.maximum", "3"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.default", "2"), + ), + }, + { + Config: testAccAzureRMMonitorAutoScaleSetting_capacity(ri, location, 2, 4, 3), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorAutoScaleSettingExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "false"), + resource.TestCheckResourceAttr(resourceName, "profile.#", "1"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.minimum", "2"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.maximum", "4"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.default", "3"), + ), + }, + { + Config: testAccAzureRMMonitorAutoScaleSetting_capacity(ri, location, 2, 45, 3), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMonitorAutoScaleSettingExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "enabled", "false"), + resource.TestCheckResourceAttr(resourceName, "profile.#", "1"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.minimum", "2"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.maximum", "45"), + resource.TestCheckResourceAttr(resourceName, "profile.0.capacity.0.default", "3"), + ), + }, + }, + }) +} + func TestAccAzureRMMonitorAutoScaleSetting_multipleRules(t *testing.T) { resourceName := "azurerm_monitor_autoscale_setting.test" ri := tf.AccRandTimeInt() - rs := acctest.RandString(6) location := testLocation() resource.ParallelTest(t, resource.TestCase{ @@ -112,7 +154,7 @@ func TestAccAzureRMMonitorAutoScaleSetting_multipleRules(t *testing.T) { CheckDestroy: testCheckAzureRMMonitorAutoScaleSettingDestroy, Steps: []resource.TestStep{ { - Config: testAccAzureRMMonitorAutoScaleSetting_basic(ri, rs, location), + Config: testAccAzureRMMonitorAutoScaleSetting_basic(ri, location), Check: resource.ComposeTestCheckFunc( testCheckAzureRMMonitorAutoScaleSettingExists(resourceName), resource.TestCheckResourceAttr(resourceName, "enabled", "true"), @@ -124,7 +166,7 @@ func TestAccAzureRMMonitorAutoScaleSetting_multipleRules(t *testing.T) { ), }, { - Config: testAccAzureRMMonitorAutoScaleSetting_multipleRules(ri, rs, location), + Config: testAccAzureRMMonitorAutoScaleSetting_multipleRules(ri, location), Check: resource.ComposeTestCheckFunc( testCheckAzureRMMonitorAutoScaleSettingExists(resourceName), resource.TestCheckResourceAttr(resourceName, "enabled", "true"), @@ -143,7 +185,6 @@ func TestAccAzureRMMonitorAutoScaleSetting_multipleRules(t *testing.T) { func TestAccAzureRMMonitorAutoScaleSetting_customEmails(t *testing.T) { resourceName := "azurerm_monitor_autoscale_setting.test" ri := tf.AccRandTimeInt() - rs := acctest.RandString(6) location := testLocation() resource.ParallelTest(t, resource.TestCase{ @@ -152,7 +193,7 @@ func TestAccAzureRMMonitorAutoScaleSetting_customEmails(t *testing.T) { CheckDestroy: testCheckAzureRMMonitorAutoScaleSettingDestroy, Steps: []resource.TestStep{ { - Config: testAccAzureRMMonitorAutoScaleSetting_email(ri, rs, location), + Config: testAccAzureRMMonitorAutoScaleSetting_email(ri, location), Check: resource.ComposeTestCheckFunc( testCheckAzureRMMonitorAutoScaleSettingExists(resourceName), resource.TestCheckResourceAttr(resourceName, "notification.#", "1"), @@ -162,7 +203,7 @@ func TestAccAzureRMMonitorAutoScaleSetting_customEmails(t *testing.T) { ), }, { - Config: testAccAzureRMMonitorAutoScaleSetting_emailUpdated(ri, rs, location), + Config: testAccAzureRMMonitorAutoScaleSetting_emailUpdated(ri, location), Check: resource.ComposeTestCheckFunc( testCheckAzureRMMonitorAutoScaleSettingExists(resourceName), resource.TestCheckResourceAttr(resourceName, "notification.#", "1"), @@ -179,9 +220,8 @@ func TestAccAzureRMMonitorAutoScaleSetting_customEmails(t *testing.T) { func TestAccAzureRMMonitorAutoScaleSetting_recurrence(t *testing.T) { resourceName := "azurerm_monitor_autoscale_setting.test" ri := tf.AccRandTimeInt() - rs := acctest.RandString(6) location := testLocation() - config := testAccAzureRMMonitorAutoScaleSetting_recurrence(ri, rs, location) + config := testAccAzureRMMonitorAutoScaleSetting_recurrence(ri, location) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -211,7 +251,6 @@ func TestAccAzureRMMonitorAutoScaleSetting_recurrence(t *testing.T) { func TestAccAzureRMMonitorAutoScaleSetting_recurrenceUpdate(t *testing.T) { resourceName := "azurerm_monitor_autoscale_setting.test" ri := tf.AccRandTimeInt() - rs := acctest.RandString(6) location := testLocation() resource.ParallelTest(t, resource.TestCase{ @@ -220,7 +259,7 @@ func TestAccAzureRMMonitorAutoScaleSetting_recurrenceUpdate(t *testing.T) { CheckDestroy: testCheckAzureRMMonitorAutoScaleSettingDestroy, Steps: []resource.TestStep{ { - Config: testAccAzureRMMonitorAutoScaleSetting_recurrence(ri, rs, location), + Config: testAccAzureRMMonitorAutoScaleSetting_recurrence(ri, location), Check: resource.ComposeTestCheckFunc( testCheckAzureRMMonitorAutoScaleSettingExists(resourceName), resource.TestCheckResourceAttr(resourceName, "notification.#", "1"), @@ -233,7 +272,7 @@ func TestAccAzureRMMonitorAutoScaleSetting_recurrenceUpdate(t *testing.T) { ), }, { - Config: testAccAzureRMMonitorAutoScaleSetting_recurrenceUpdated(ri, rs, location), + Config: testAccAzureRMMonitorAutoScaleSetting_recurrenceUpdated(ri, location), Check: resource.ComposeTestCheckFunc( testCheckAzureRMMonitorAutoScaleSettingExists(resourceName), resource.TestCheckResourceAttr(resourceName, "profile.0.recurrence.#", "1"), @@ -252,9 +291,8 @@ func TestAccAzureRMMonitorAutoScaleSetting_recurrenceUpdate(t *testing.T) { func TestAccAzureRMMonitorAutoScaleSetting_fixedDate(t *testing.T) { resourceName := "azurerm_monitor_autoscale_setting.test" ri := tf.AccRandTimeInt() - rs := acctest.RandString(6) location := testLocation() - config := testAccAzureRMMonitorAutoScaleSetting_fixedDate(ri, rs, location) + config := testAccAzureRMMonitorAutoScaleSetting_fixedDate(ri, location) resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -336,8 +374,8 @@ func testCheckAzureRMMonitorAutoScaleSettingDestroy(s *terraform.State) error { return nil } -func testAccAzureRMMonitorAutoScaleSetting_basic(rInt int, rString string, location string) string { - template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, rString, location) +func testAccAzureRMMonitorAutoScaleSetting_basic(rInt int, location string) string { + template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, location) return fmt.Sprintf(` %s @@ -380,8 +418,8 @@ resource "azurerm_monitor_autoscale_setting" "test" { `, template, rInt) } -func testAccAzureRMMonitorAutoScaleSetting_requiresImport(rInt int, rString string, location string) string { - template := testAccAzureRMMonitorAutoScaleSetting_basic(rInt, rString, location) +func testAccAzureRMMonitorAutoScaleSetting_requiresImport(rInt int, location string) string { + template := testAccAzureRMMonitorAutoScaleSetting_basic(rInt, location) return fmt.Sprintf(` %s @@ -424,8 +462,8 @@ resource "azurerm_monitor_autoscale_setting" "import" { `, template) } -func testAccAzureRMMonitorAutoScaleSetting_multipleProfiles(rInt int, rString string, location string) string { - template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, rString, location) +func testAccAzureRMMonitorAutoScaleSetting_multipleProfiles(rInt int, location string) string { + template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, location) return fmt.Sprintf(` %s @@ -511,8 +549,53 @@ resource "azurerm_monitor_autoscale_setting" "test" { `, template, rInt) } -func testAccAzureRMMonitorAutoScaleSetting_multipleRules(rInt int, rString string, location string) string { - template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, rString, location) +func testAccAzureRMMonitorAutoScaleSetting_capacity(rInt int, location string, min int, max int, defaultVal int) string { + template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, location) + 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_virtual_machine_scale_set.test.id}" + enabled = false + + profile { + name = "metricRules" + + capacity { + default = %d + minimum = %d + maximum = %d + } + + rule { + metric_trigger { + metric_name = "Percentage CPU" + metric_resource_id = "${azurerm_virtual_machine_scale_set.test.id}" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT5M" + time_aggregation = "Average" + operator = "GreaterThan" + threshold = 75 + } + + scale_action { + direction = "Increase" + type = "ChangeCount" + value = 1 + cooldown = "PT1M" + } + } + } +} +`, template, rInt, defaultVal, min, max) +} + +func testAccAzureRMMonitorAutoScaleSetting_multipleRules(rInt int, location string) string { + template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, location) return fmt.Sprintf(` %s @@ -576,8 +659,8 @@ resource "azurerm_monitor_autoscale_setting" "test" { `, template, rInt) } -func testAccAzureRMMonitorAutoScaleSetting_email(rInt int, rString string, location string) string { - template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, rString, location) +func testAccAzureRMMonitorAutoScaleSetting_email(rInt int, location string) string { + template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, location) return fmt.Sprintf(` %s @@ -628,8 +711,8 @@ resource "azurerm_monitor_autoscale_setting" "test" { `, template, rInt, rInt) } -func testAccAzureRMMonitorAutoScaleSetting_emailUpdated(rInt int, rString string, location string) string { - template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, rString, location) +func testAccAzureRMMonitorAutoScaleSetting_emailUpdated(rInt int, location string) string { + template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, location) return fmt.Sprintf(` %s @@ -680,8 +763,8 @@ resource "azurerm_monitor_autoscale_setting" "test" { `, template, rInt, rInt, rInt) } -func testAccAzureRMMonitorAutoScaleSetting_recurrence(rInt int, rString string, location string) string { - template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, rString, location) +func testAccAzureRMMonitorAutoScaleSetting_recurrence(rInt int, location string) string { + template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, location) return fmt.Sprintf(` %s @@ -724,8 +807,8 @@ resource "azurerm_monitor_autoscale_setting" "test" { `, template, rInt) } -func testAccAzureRMMonitorAutoScaleSetting_recurrenceUpdated(rInt int, rString string, location string) string { - template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, rString, location) +func testAccAzureRMMonitorAutoScaleSetting_recurrenceUpdated(rInt int, location string) string { + template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, location) return fmt.Sprintf(` %s @@ -768,8 +851,8 @@ resource "azurerm_monitor_autoscale_setting" "test" { `, template, rInt) } -func testAccAzureRMMonitorAutoScaleSetting_fixedDate(rInt int, rString string, location string) string { - template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, rString, location) +func testAccAzureRMMonitorAutoScaleSetting_fixedDate(rInt int, location string) string { + template := testAccAzureRMMonitorAutoScaleSetting_template(rInt, location) return fmt.Sprintf(` %s @@ -798,7 +881,7 @@ resource "azurerm_monitor_autoscale_setting" "test" { `, template, rInt) } -func testAccAzureRMMonitorAutoScaleSetting_template(rInt int, rString string, location string) string { +func testAccAzureRMMonitorAutoScaleSetting_template(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { name = "acctestRG-%d" @@ -819,25 +902,6 @@ resource "azurerm_subnet" "test" { address_prefix = "10.0.2.0/24" } -resource "azurerm_storage_account" "test" { - name = "accsa%s" - resource_group_name = "${azurerm_resource_group.test.name}" - location = "${azurerm_resource_group.test.location}" - account_tier = "Standard" - account_replication_type = "LRS" - - tags { - environment = "staging" - } -} - -resource "azurerm_storage_container" "test" { - name = "vhds" - resource_group_name = "${azurerm_resource_group.test.name}" - storage_account_name = "${azurerm_storage_account.test.name}" - container_access_type = "private" -} - resource "azurerm_virtual_machine_scale_set" "test" { name = "acctvmss-%d" location = "${azurerm_resource_group.test.location}" @@ -868,10 +932,10 @@ resource "azurerm_virtual_machine_scale_set" "test" { } storage_profile_os_disk { - name = "osDiskProfile" - caching = "ReadWrite" - create_option = "FromImage" - vhd_containers = ["${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}"] + name = "" + caching = "ReadWrite" + create_option = "FromImage" + managed_disk_type = "StandardSSD_LRS" } storage_profile_image_reference { @@ -881,5 +945,5 @@ resource "azurerm_virtual_machine_scale_set" "test" { version = "latest" } } -`, rInt, location, rInt, rString, rInt, rInt, rInt) +`, rInt, location, rInt, rInt, rInt, rInt) } diff --git a/examples/vmss-ubuntu/main.tf b/examples/vmss-ubuntu/main.tf index 4701a0bc3551..82fb2a28b991 100644 --- a/examples/vmss-ubuntu/main.tf +++ b/examples/vmss-ubuntu/main.tf @@ -127,3 +127,59 @@ resource "azurerm_virtual_machine_scale_set" "scaleset" { version = "latest" } } + +resource "azurerm_autoscale_setting" "autoscale-cpu" { + name = "autoscale-cpu" + target_resource_id = "${azurerm_virtual_machine_scale_set.scaleset.id}" + location = "${azurerm_resource_group.rg.location}" + resource_group_name = "${azurerm_resource_group.rg.name}" + + profile { + name = "autoscale-cpu" + + capacity { + default = "${var.instance_count}" + minimum = 0 + maximum = 1000 + } + + rule { + metric_trigger { + metric_name = "Percentage CPU" + metric_resource_id = "${azurerm_virtual_machine_scale_set.scaleset.id}" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT5M" + time_aggregation = "Average" + operator = "GreaterThan" + threshold = 75 + } + + scale_action { + direction = "Increase" + type = "ChangeCount" + value = "1" + cooldown = "PT1M" + } + } + + rule { + metric_trigger { + metric_name = "Percentage CPU" + metric_resource_id = "${azurerm_virtual_machine_scale_set.scaleset.id}" + time_grain = "PT1M" + statistic = "Average" + time_window = "PT5M" + time_aggregation = "Average" + operator = "LessThan" + threshold = 15 + } + scale_action { + direction = "Decrease" + type = "ChangeCount" + value = "1" + cooldown = "PT1M" + } + } + } +} diff --git a/website/docs/r/autoscale_setting.html.markdown b/website/docs/r/autoscale_setting.html.markdown index 35f532de6366..1ee02d975cd0 100644 --- a/website/docs/r/autoscale_setting.html.markdown +++ b/website/docs/r/autoscale_setting.html.markdown @@ -299,13 +299,13 @@ A `profile` block supports the following: A `capacity` block supports the following: -* `default` - (Required) The number of instances that are available for scaling if metrics are not available for evaluation. The default is only used if the current instance count is lower than the default. +* `default` - (Required) The number of instances that are available for scaling if metrics are not available for evaluation. The default is only used if the current instance count is lower than the default. Valid values are between `0` and `1000`. -* `maximum` - (Required) The maximum number of instances for this resource. Valid values are between `1` and `40`. +* `maximum` - (Required) The maximum number of instances for this resource. Valid values are between `0` and `1000`. -> **NOTE:** The maximum number of instances is also limited by the amount of Cores available in the subscription. -* `minimum` - (Required) The minimum number of instances for this resource. Valid values are between `1` and `40`. +* `minimum` - (Required) The minimum number of instances for this resource. Valid values are between `0` and `1000`. --- diff --git a/website/docs/r/monitor_autoscale_setting.html.markdown b/website/docs/r/monitor_autoscale_setting.html.markdown index 5990446ad4d9..bc86e18a7e15 100644 --- a/website/docs/r/monitor_autoscale_setting.html.markdown +++ b/website/docs/r/monitor_autoscale_setting.html.markdown @@ -297,13 +297,13 @@ A `profile` block supports the following: A `capacity` block supports the following: -* `default` - (Required) The number of instances that are available for scaling if metrics are not available for evaluation. The default is only used if the current instance count is lower than the default. +* `default` - (Required) The number of instances that are available for scaling if metrics are not available for evaluation. The default is only used if the current instance count is lower than the default. Valid values are between `0` and `1000`. -* `maximum` - (Required) The maximum number of instances for this resource. Valid values are between `1` and `40`. +* `maximum` - (Required) The maximum number of instances for this resource. Valid values are between `0` and `1000`. -> **NOTE:** The maximum number of instances is also limited by the amount of Cores available in the subscription. -* `minimum` - (Required) The minimum number of instances for this resource. Valid values are between `1` and `40`. +* `minimum` - (Required) The minimum number of instances for this resource. Valid values are between `0` and `1000`. ---