From 2492ee10fd36be6dff9a2d0505b6722c636f7630 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Sat, 3 Nov 2018 15:17:49 +0000 Subject: [PATCH] VMSS: support for eviction policy --- .../resource_arm_virtual_machine_scale_set.go | 18 ++- ...urce_arm_virtual_machine_scale_set_test.go | 115 ++++++++++++++++++ .../r/virtual_machine_scale_set.html.markdown | 57 +++++++-- 3 files changed, 176 insertions(+), 14 deletions(-) diff --git a/azurerm/resource_arm_virtual_machine_scale_set.go b/azurerm/resource_arm_virtual_machine_scale_set.go index 1da2ae2e3ed0b..e285c561b33fd 100644 --- a/azurerm/resource_arm_virtual_machine_scale_set.go +++ b/azurerm/resource_arm_virtual_machine_scale_set.go @@ -196,6 +196,16 @@ func resourceArmVirtualMachineScaleSet() *schema.Resource { }, true), }, + "eviction_policy": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + string(compute.Deallocate), + string(compute.Delete), + }, false), + }, + "os_profile": { Type: schema.TypeList, Required: true, @@ -782,6 +792,7 @@ func resourceArmVirtualMachineScaleSetCreate(d *schema.ResourceData, meta interf overprovision := d.Get("overprovision").(bool) singlePlacementGroup := d.Get("single_placement_group").(bool) priority := d.Get("priority").(string) + evictionPolicy := d.Get("eviction_policy").(string) scaleSetProps := compute.VirtualMachineScaleSetProperties{ UpgradePolicy: &compute.UpgradePolicy{ @@ -800,6 +811,10 @@ func resourceArmVirtualMachineScaleSetCreate(d *schema.ResourceData, meta interf SinglePlacementGroup: &singlePlacementGroup, } + if strings.EqualFold(priority, string(compute.Low)) { + scaleSetProps.VirtualMachineProfile.EvictionPolicy = compute.VirtualMachineEvictionPolicyTypes(evictionPolicy) + } + if _, ok := d.GetOk("boot_diagnostics"); ok { diagnosticProfile := expandAzureRMVirtualMachineScaleSetsDiagnosticProfile(d) scaleSetProps.VirtualMachineProfile.DiagnosticsProfile = &diagnosticProfile @@ -912,7 +927,8 @@ func resourceArmVirtualMachineScaleSetRead(d *schema.ResourceData, meta interfac if profile := properties.VirtualMachineProfile; profile != nil { d.Set("license_type", profile.LicenseType) - d.Set("priority", profile.Priority) + d.Set("priority", string(profile.Priority)) + d.Set("eviction_policy", string(profile.EvictionPolicy)) osProfile := flattenAzureRMVirtualMachineScaleSetOsProfile(d, profile.OsProfile) if err := d.Set("os_profile", osProfile); err != nil { diff --git a/azurerm/resource_arm_virtual_machine_scale_set_test.go b/azurerm/resource_arm_virtual_machine_scale_set_test.go index c2f7947744aed..cdd72dc6a997d 100644 --- a/azurerm/resource_arm_virtual_machine_scale_set_test.go +++ b/azurerm/resource_arm_virtual_machine_scale_set_test.go @@ -28,6 +28,33 @@ func TestAccAzureRMVirtualMachineScaleSet_basic(t *testing.T) { testCheckAzureRMVirtualMachineScaleSetExists(resourceName), // testing default scaleset values testCheckAzureRMVirtualMachineScaleSetSinglePlacementGroup(resourceName, true), + resource.TestCheckResourceAttr(resourceName, "eviction_policy", "deallocate"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"os_profile.0.admin_password"}, + }, + }, + }) +} + +func TestAccAzureRMVirtualMachineScaleSet_evictionPolicyDelete(t *testing.T) { + resourceName := "azurerm_virtual_machine_scale_set.test" + ri := acctest.RandInt() + config := testAccAzureRMVirtualMachineScaleSet_evictionPolicyDelete(ri, testLocation()) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMVirtualMachineScaleSetDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMVirtualMachineScaleSetExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "eviction_policy", "delete"), ), }, { @@ -1295,6 +1322,94 @@ resource "azurerm_virtual_machine_scale_set" "test" { `, rInt, location) } +func testAccAzureRMVirtualMachineScaleSet_evictionPolicyDelete(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%[1]d" + location = "%[2]s" +} + +resource "azurerm_virtual_network" "test" { + name = "acctvn-%[1]d" + address_space = ["10.0.0.0/16"] + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" +} + +resource "azurerm_subnet" "test" { + name = "acctsub-%[1]d" + resource_group_name = "${azurerm_resource_group.test.name}" + virtual_network_name = "${azurerm_virtual_network.test.name}" + address_prefix = "10.0.2.0/24" +} + +resource "azurerm_storage_account" "test" { + name = "accsa%[1]d" + 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-%[1]d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + upgrade_policy_mode = "Manual" + priority = "Low" + eviction_policy = "Delete" + + sku { + name = "Standard_D1_v2" + tier = "Standard" + capacity = 2 + } + + os_profile { + computer_name_prefix = "testvm-%[1]d" + admin_username = "myadmin" + admin_password = "Passwword1234" + } + + network_profile { + name = "TestNetworkProfile-%[1]d" + primary = true + + ip_configuration { + name = "TestIPConfiguration" + primary = true + subnet_id = "${azurerm_subnet.test.id}" + } + } + + 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}"] + } + + storage_profile_image_reference { + publisher = "Canonical" + offer = "UbuntuServer" + sku = "16.04-LTS" + version = "latest" + } +} +`, rInt, location) +} + func testAccAzureRMVirtualMachineScaleSet_standardSSD(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { diff --git a/website/docs/r/virtual_machine_scale_set.html.markdown b/website/docs/r/virtual_machine_scale_set.html.markdown index b231661bf74c3..7c060433c26f0 100644 --- a/website/docs/r/virtual_machine_scale_set.html.markdown +++ b/website/docs/r/virtual_machine_scale_set.html.markdown @@ -264,34 +264,65 @@ resource "azurerm_virtual_machine_scale_set" "test" { The following arguments are supported: * `name` - (Required) Specifies the name of the virtual machine scale set resource. Changing this forces a new resource to be created. + * `resource_group_name` - (Required) The name of the resource group in which to create the virtual machine scale set. Changing this forces a new resource to be created. + * `location` - (Required) Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created. + +* `network_profile` - (Required) A collection of network profile block as documented below. + +* `os_profile` - (Required) A Virtual Machine OS Profile block as documented below. + +* `os_profile_windows_config` - (Required, when a windows machine) A Windows config block as documented below. + +* `os_profile_linux_config` - (Required, when a linux machine) A Linux config block as documented below. + * `sku` - (Required) A sku block as documented below. + +* `storage_profile_os_disk` - (Required) A storage profile os disk block as documented below + * `upgrade_policy_mode` - (Required) Specifies the mode of an upgrade to virtual machines in the scale set. Possible values, `Rolling`, `Manual`, or `Automatic`. When choosing `Rolling`, you will need to set a health probe. + +--- + * `automatic_os_upgrade` - (Optional) Automatic OS patches can be applied by Azure to your scaleset. This is particularly useful when `upgrade_policy_mode` is set to `Rolling`. Defaults to `false`. -* `rolling_upgrade_policy` - (Optional) A `rolling_upgrade_policy` block as defined below. This is only applicable when the `upgrade_policy_mode` is `Rolling`. + +* `boot_diagnostics` - (Optional) A boot diagnostics profile block as referenced below. + +* `extension` - (Optional) Can be specified multiple times to add extension profiles to the scale set. Each `extension` block supports the fields documented below. + +* `eviction_policy` - (Optional) Specifies the eviction policy for Virtual Machines in this Scale Set. Possible values are `Deallocate` and `Delete`. + +-> **NOTE:** `eviction_policy` can only be set when `priority` is set to `Low`. + * `health_probe_id` - (Optional) Specifies the identifier for the load balancer health probe. Required when using `Rolling` as your `upgrade_policy_mode`. -* `overprovision` - (Optional) Specifies whether the virtual machine scale set should be overprovisioned. -* `single_placement_group` - (Optional) Specifies whether the scale set is limited to a single placement group with a maximum size of 100 virtual machines. If set to false, managed disks must be used. Default is true. Changing this forces a - new resource to be created. See [documentation](http://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-placement-groups) for more information. + * `license_type` - (Optional, when a Windows machine) Specifies the Windows OS license type. If supplied, the only allowed values are `Windows_Client` and `Windows_Server`. -* `os_profile` - (Required) A Virtual Machine OS Profile block as documented below. + * `os_profile_secrets` - (Optional) A collection of Secret blocks as documented below. -* `os_profile_windows_config` - (Required, when a windows machine) A Windows config block as documented below. -* `os_profile_linux_config` - (Required, when a linux machine) A Linux config block as documented below. -* `network_profile` - (Required) A collection of network profile block as documented below. -* `storage_profile_os_disk` - (Required) A storage profile os disk block as documented below + +* `overprovision` - (Optional) Specifies whether the virtual machine scale set should be overprovisioned. + +* `plan` - (Optional) A plan block as documented below. + +* `priority` - (Optional) Specifies the priority for the Virtual Machines in the Scale Set. Defaults to `Regular`. Possible values are `Low` and `Regular`. + +* `rolling_upgrade_policy` - (Optional) A `rolling_upgrade_policy` block as defined below. This is only applicable when the `upgrade_policy_mode` is `Rolling`. + +* `single_placement_group` - (Optional) Specifies whether the scale set is limited to a single placement group with a maximum size of 100 virtual machines. If set to false, managed disks must be used. Default is true. Changing this forces a new resource to be created. See [documentation](http://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-placement-groups) for more information. + * `storage_profile_data_disk` - (Optional) A storage profile data disk block as documented below + * `storage_profile_image_reference` - (Optional) A storage profile image reference block as documented below. -* `extension` - (Optional) Can be specified multiple times to add extension profiles to the scale set. Each `extension` block supports the fields documented below. -* `boot_diagnostics` - (Optional) A boot diagnostics profile block as referenced below. -* `plan` - (Optional) A plan block as documented below. -* `priority` - (Optional) Specifies the priority for the virtual machines in the scale set, defaults to `Regular`. Possible values are `Low` and `Regular`. + * `tags` - (Optional) A mapping of tags to assign to the resource. + * `zones` - (Optional) A collection of availability zones to spread the Virtual Machines over. -> **Please Note**: Availability Zones are [only supported in several regions at this time](https://docs.microsoft.com/en-us/azure/availability-zones/az-overview). +--- + `sku` supports the following: * `name` - (Required) Specifies the size of virtual machines in a scale set.