Skip to content

Commit

Permalink
Add 'priority' property for VM scale set
Browse files Browse the repository at this point in the history
Purpose is to support low-priority VMs, which should partially
address #1249.

This is simply adding a 'priority' parameter that can be set
to Low or Regular (=default).

The evictionPolicy is not supported yet as it was introduced
in a more recent version of the compute API (2018-04-01).

Cf https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-use-low-priority
  • Loading branch information
Bertrand Roussel committed May 17, 2018
1 parent 546198c commit 4f2a5ef
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 1 deletion.
8 changes: 8 additions & 0 deletions azurerm/resource_arm_virtual_machine_scale_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ func resourceArmVirtualMachineScaleSet() *schema.Resource {
ForceNew: true,
},

"priority": {
Type: schema.TypeString,
Optional: true,
},

"os_profile": {
Type: schema.TypeList,
Required: true,
Expand Down Expand Up @@ -640,6 +645,7 @@ func resourceArmVirtualMachineScaleSetCreate(d *schema.ResourceData, meta interf
updatePolicy := d.Get("upgrade_policy_mode").(string)
overprovision := d.Get("overprovision").(bool)
singlePlacementGroup := d.Get("single_placement_group").(bool)
priority := d.Get("priority").(string)

scaleSetProps := compute.VirtualMachineScaleSetProperties{
UpgradePolicy: &compute.UpgradePolicy{
Expand All @@ -650,6 +656,7 @@ func resourceArmVirtualMachineScaleSetCreate(d *schema.ResourceData, meta interf
StorageProfile: &storageProfile,
OsProfile: osProfile,
ExtensionProfile: extensions,
Priority: compute.VirtualMachinePriorityTypes(priority),
},
Overprovision: &overprovision,
SinglePlacementGroup: &singlePlacementGroup,
Expand Down Expand Up @@ -746,6 +753,7 @@ func resourceArmVirtualMachineScaleSetRead(d *schema.ResourceData, meta interfac
d.Set("upgrade_policy_mode", properties.UpgradePolicy.Mode)
d.Set("overprovision", properties.Overprovision)
d.Set("single_placement_group", properties.SinglePlacementGroup)
d.Set("priority", properties.VirtualMachineProfile.Priority)

osProfile, err := flattenAzureRMVirtualMachineScaleSetOsProfile(d, properties.VirtualMachineProfile.OsProfile)
if err != nil {
Expand Down
120 changes: 119 additions & 1 deletion azurerm/resource_arm_virtual_machine_scale_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,25 @@ func TestAccAzureRMVirtualMachineScaleSet_overprovision(t *testing.T) {
})
}

func TestAccAzureRMVirtualMachineScaleSet_priority(t *testing.T) {
ri := acctest.RandInt()
config := testAccAzureRMVirtualMachineScaleSetPriorityTemplate(ri, testLocation())
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMVirtualMachineScaleSetDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMVirtualMachineScaleSetExists("azurerm_virtual_machine_scale_set.test"),
testCheckAzureRMVirtualMachineScaleSetPriority("azurerm_virtual_machine_scale_set.test", "Low"),
),
},
},
})
}

func TestAccAzureRMVirtualMachineScaleSet_MSI(t *testing.T) {
resourceName := "azurerm_virtual_machine_scale_set.test"
ri := acctest.RandInt()
Expand Down Expand Up @@ -812,6 +831,21 @@ func testCheckAzureRMVirtualMachineScaleSetOverprovision(name string) resource.T
}
}

func testCheckAzureRMVirtualMachineScaleSetPriority(name string, expectedPriority string) resource.TestCheckFunc {
return func(s *terraform.State) error {
resp, err := testGetAzureRMVirtualMachineScaleSet(s, name)
if err != nil {
return err
}

if resp.VirtualMachineProfile.Priority != compute.VirtualMachinePriorityTypes(expectedPriority) {
return fmt.Errorf("Bad: Priority should have been %s for scale set %v", expectedPriority, name)
}

return nil
}
}

func testCheckAzureRMVirtualMachineScaleSetSinglePlacementGroup(name string, expectedSinglePlacementGroup bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
resp, err := testGetAzureRMVirtualMachineScaleSet(s, name)
Expand All @@ -820,7 +854,7 @@ func testCheckAzureRMVirtualMachineScaleSetSinglePlacementGroup(name string, exp
}

if *resp.SinglePlacementGroup != expectedSinglePlacementGroup {
return fmt.Errorf("Bad: Overprovision should have been %t for scale set %v", expectedSinglePlacementGroup, name)
return fmt.Errorf("Bad: SinglePlacementGroup should have been %t for scale set %v", expectedSinglePlacementGroup, name)
}

return nil
Expand Down Expand Up @@ -2577,6 +2611,90 @@ resource "azurerm_virtual_machine_scale_set" "test" {
`, rInt, location, rInt, rInt, rInt, rInt, rInt)
}

func testAccAzureRMVirtualMachineScaleSetPriorityTemplate(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestrg-%d"
location = "%s"
}
resource "azurerm_virtual_network" "test" {
name = "acctvn-%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-%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%d"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"
account_tier = "Standard"
account_replication_type = "LRS"
}
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"
overprovision = false
priority = "Low"
sku {
name = "Standard_D1_v2"
tier = "Standard"
capacity = 1
}
os_profile {
computer_name_prefix = "testvm-%d"
admin_username = "myadmin"
admin_password = "Passwword1234"
}
network_profile {
name = "TestNetworkProfile"
primary = true
ip_configuration {
name = "TestIPConfiguration"
subnet_id = "${azurerm_subnet.test.id}"
}
}
storage_profile_os_disk {
name = "os-disk"
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, rInt, rInt, rInt, rInt, rInt)
}

func testAccAzureRMVirtualMachineScaleSetMSITemplate(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down

0 comments on commit 4f2a5ef

Please sign in to comment.