Skip to content

Commit

Permalink
VMSS - Support updating zones (hashicorp#27288)
Browse files Browse the repository at this point in the history
* support vmss zone update

* resolve comments
  • Loading branch information
ms-zhenhua authored Sep 19, 2024
1 parent 1a09e09 commit 9c2cc02
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package compute

import (
"context"
"fmt"
"log"
"time"
Expand Down Expand Up @@ -58,6 +59,30 @@ func resourceLinuxVirtualMachineScaleSet() *pluginsdk.Resource {
// https://github.com/Azure/azure-rest-api-specs/pull/7246

Schema: resourceLinuxVirtualMachineScaleSetSchema(),

CustomizeDiff: pluginsdk.CustomDiffWithAll(
// Removing existing zones is currently not supported for Virtual Machine Scale Sets
pluginsdk.ForceNewIfChange("zones", func(ctx context.Context, old, new, meta interface{}) bool {
oldZones := zones.ExpandUntyped(old.(*schema.Set).List())
newZones := zones.ExpandUntyped(new.(*schema.Set).List())

for _, ov := range oldZones {
found := false
for _, nv := range newZones {
if ov == nv {
found = true
break
}
}

if !found {
return true
}
}

return false
}),
),
}
}

Expand Down Expand Up @@ -518,6 +543,10 @@ func resourceLinuxVirtualMachineScaleSetUpdate(d *pluginsdk.ResourceData, meta i
}
}

if d.HasChange("zones") {
update.Zones = pointer.To(zones.ExpandUntyped(d.Get("zones").(*schema.Set).List()))
}

if d.HasChange("automatic_os_upgrade_policy") || d.HasChange("rolling_upgrade_policy") {
upgradePolicy := virtualmachinescalesets.UpgradePolicy{}
if existing.Model.Properties.UpgradePolicy == nil {
Expand Down Expand Up @@ -1441,7 +1470,7 @@ func resourceLinuxVirtualMachineScaleSetSchema() map[string]*pluginsdk.Schema {

"termination_notification": VirtualMachineScaleSetTerminationNotificationSchema(),

"zones": commonschema.ZonesMultipleOptionalForceNew(),
"zones": commonschema.ZonesMultipleOptional(),

// Computed
"unique_id": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,28 @@ func TestAccLinuxVirtualMachineScaleSet_scalingZonesMultiple(t *testing.T) {
})
}

func TestAccLinuxVirtualMachineScaleSet_scalingZonesUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_linux_virtual_machine_scale_set", "test")
r := LinuxVirtualMachineScaleSetResource{}

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

func TestAccLinuxVirtualMachineScaleSet_scalingZonesBalance(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_linux_virtual_machine_scale_set", "test")
r := LinuxVirtualMachineScaleSetResource{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package compute

import (
"context"
"fmt"
"log"
"strconv"
Expand Down Expand Up @@ -250,7 +251,7 @@ func resourceOrchestratedVirtualMachineScaleSet() *pluginsdk.Resource {

"termination_notification": OrchestratedVirtualMachineScaleSetTerminationNotificationSchema(),

"zones": commonschema.ZonesMultipleOptionalForceNew(),
"zones": commonschema.ZonesMultipleOptional(),

"tags": commonschema.Tags(),

Expand All @@ -269,6 +270,30 @@ func resourceOrchestratedVirtualMachineScaleSet() *pluginsdk.Resource {

"priority_mix": OrchestratedVirtualMachineScaleSetPriorityMixPolicySchema(),
},

CustomizeDiff: pluginsdk.CustomDiffWithAll(
// Removing existing zones is currently not supported for Virtual Machine Scale Sets
pluginsdk.ForceNewIfChange("zones", func(ctx context.Context, old, new, meta interface{}) bool {
oldZones := zones.ExpandUntyped(old.(*schema.Set).List())
newZones := zones.ExpandUntyped(new.(*schema.Set).List())

for _, ov := range oldZones {
found := false
for _, nv := range newZones {
if ov == nv {
found = true
break
}
}

if !found {
return true
}
}

return false
}),
),
}
}

Expand Down Expand Up @@ -1072,6 +1097,10 @@ func resourceOrchestratedVirtualMachineScaleSetUpdate(d *pluginsdk.ResourceData,
}
}

if d.HasChange("zones") {
update.Zones = pointer.To(zones.ExpandUntyped(d.Get("zones").(*schema.Set).List()))
}

// Only two fields that can change in legacy mode
if d.HasChange("proximity_placement_group_id") {
if v, ok := d.GetOk("proximity_placement_group_id"); ok {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ func TestAccOrchestratedVirtualMachineScaleSet_priority(t *testing.T) {
})
}

func TestAccOrchestratedVirtualMachineScaleSet_zoneUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_orchestrated_virtual_machine_scale_set", "test")
r := OrchestratedVirtualMachineScaleSetResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.singleZone(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
{
Config: r.multipleZone(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
})
}

func TestAccOrchestratedVirtualMachineScaleSet_NonStandardCasing(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_orchestrated_virtual_machine_scale_set", "test")
r := OrchestratedVirtualMachineScaleSetResource{}
Expand Down Expand Up @@ -441,6 +461,60 @@ resource "azurerm_orchestrated_virtual_machine_scale_set" "test" {
`, data.RandomInteger, data.Locations.Primary, r.natgateway_template(data))
}

func (OrchestratedVirtualMachineScaleSetResource) singleZone(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-OVMSS-%[1]d"
location = "%[2]s"
}
resource "azurerm_orchestrated_virtual_machine_scale_set" "test" {
name = "acctestOVMSS-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
platform_fault_domain_count = 1
zones = ["1"]
tags = {
ENV = "Test"
}
}
`, data.RandomInteger, data.Locations.Primary)
}

func (OrchestratedVirtualMachineScaleSetResource) multipleZone(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-OVMSS-%[1]d"
location = "%[2]s"
}
resource "azurerm_orchestrated_virtual_machine_scale_set" "test" {
name = "acctestOVMSS-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
platform_fault_domain_count = 1
zones = ["1", "2"]
tags = {
ENV = "Test"
}
}
`, data.RandomInteger, data.Locations.Primary)
}

func (OrchestratedVirtualMachineScaleSetResource) windowsHotpatchingEnabled(data acceptance.TestData) string {
r := OrchestratedVirtualMachineScaleSetResource{}
return fmt.Sprintf(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package compute

import (
"context"
"fmt"
"log"
"time"
Expand Down Expand Up @@ -57,6 +58,30 @@ func resourceWindowsVirtualMachineScaleSet() *pluginsdk.Resource {
// https://github.com/Azure/azure-rest-api-specs/pull/7246

Schema: resourceWindowsVirtualMachineScaleSetSchema(),

CustomizeDiff: pluginsdk.CustomDiffWithAll(
// Removing existing zones is currently not supported for Virtual Machine Scale Sets
pluginsdk.ForceNewIfChange("zones", func(ctx context.Context, old, new, meta interface{}) bool {
oldZones := zones.ExpandUntyped(old.(*schema.Set).List())
newZones := zones.ExpandUntyped(new.(*schema.Set).List())

for _, ov := range oldZones {
found := false
for _, nv := range newZones {
if ov == nv {
found = true
break
}
}

if !found {
return true
}
}

return false
}),
),
}
}

Expand Down Expand Up @@ -524,6 +549,11 @@ func resourceWindowsVirtualMachineScaleSetUpdate(d *pluginsdk.ResourceData, meta
automaticOSUpgradeIsEnabled = *policy.AutomaticOSUpgradePolicy.EnableAutomaticOSUpgrade
}
}

if d.HasChange("zones") {
update.Zones = pointer.To(zones.ExpandUntyped(d.Get("zones").(*schema.Set).List()))
}

if d.HasChange("automatic_os_upgrade_policy") || d.HasChange("rolling_upgrade_policy") {
upgradePolicy := virtualmachinescalesets.UpgradePolicy{}
if existing.Model.Properties.UpgradePolicy == nil {
Expand Down Expand Up @@ -1498,7 +1528,7 @@ func resourceWindowsVirtualMachineScaleSetSchema() map[string]*pluginsdk.Schema

"termination_notification": VirtualMachineScaleSetTerminationNotificationSchema(),

"zones": commonschema.ZonesMultipleOptionalForceNew(),
"zones": commonschema.ZonesMultipleOptional(),

"unique_id": {
Type: pluginsdk.TypeString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,28 @@ func TestAccWindowsVirtualMachineScaleSet_scalingZonesMultiple(t *testing.T) {
})
}

func TestAccWindowsVirtualMachineScaleSet_scalingZonesUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_windows_virtual_machine_scale_set", "test")
r := WindowsVirtualMachineScaleSetResource{}

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

func TestAccWindowsVirtualMachineScaleSet_scalingZonesBalance(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_windows_virtual_machine_scale_set", "test")
r := WindowsVirtualMachineScaleSetResource{}
Expand Down
4 changes: 3 additions & 1 deletion website/docs/r/linux_virtual_machine_scale_set.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ resource "azurerm_linux_virtual_machine_scale_set" "example" {

-> **Note:** This can only be set to `true` when one or more `zones` are configured.

* `zones` - (Optional) Specifies a list of Availability Zones in which this Linux Virtual Machine Scale Set should be located. Changing this forces a new Linux Virtual Machine Scale Set to be created.
* `zones` - (Optional) Specifies a list of Availability Zones in which this Linux Virtual Machine Scale Set should be located.

-> **Note:** Updating `zones` to remove an existing zone forces a new Virtual Machine Scale Set to be created.

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ resource "azurerm_orchestrated_virtual_machine_scale_set" "example" {

-> **Note:** This can only be set to `true` when one or more `zones` are configured.

* `zones` - (Optional) Specifies a list of Availability Zones across which the Virtual Machine Scale Set will create instances. Changing this forces a new Virtual Machine Scale Set to be created.
* `zones` - (Optional) Specifies a list of Availability Zones across which the Virtual Machine Scale Set will create instances.

-> **Note:** Updating `zones` to remove an existing zone forces a new Virtual Machine Scale Set to be created.

-> **Note:** Availability Zones are [only supported in several regions at this time](https://docs.microsoft.com/azure/availability-zones/az-overview).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,9 @@ resource "azurerm_windows_virtual_machine_scale_set" "example" {

-> **Note:** This can only be set to `true` when one or more `zones` are configured.

* `zones` - (Optional) Specifies a list of Availability Zones in which this Windows Virtual Machine Scale Set should be located. Changing this forces a new Windows Virtual Machine Scale Set to be created.
* `zones` - (Optional) Specifies a list of Availability Zones in which this Windows Virtual Machine Scale Set should be located.

-> **Note:** Updating `zones` to remove an existing zone forces a new Virtual Machine Scale Set to be created.

---

Expand Down

0 comments on commit 9c2cc02

Please sign in to comment.