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

Support for Edge Zones #15890

Merged
merged 14 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
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
26 changes: 26 additions & 0 deletions internal/services/compute/edge_zone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package compute

import (
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/hashicorp/go-azure-helpers/resourcemanager/edgezones"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func expandEdgeZone(input string) *compute.ExtendedLocation {
normalized := edgezones.Normalize(input)
if normalized == "" {
return nil
}

return &compute.ExtendedLocation{
Name: utils.String(normalized),
Type: compute.ExtendedLocationTypesEdgeZone,
}
}

func flattenEdgeZone(input *compute.ExtendedLocation) string {
if input == nil || input.Type != compute.ExtendedLocationTypesEdgeZone || input.Name == nil {
return ""
}
return edgezones.NormalizeNilable(input.Name)
}
38 changes: 21 additions & 17 deletions internal/services/compute/linux_virtual_machine_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/resourcemanager/location"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
Expand Down Expand Up @@ -166,6 +168,8 @@ func resourceLinuxVirtualMachine() *pluginsdk.Resource {
Default: true,
},

"edge_zone": commonschema.EdgeZoneOptionalForceNew(),

"encryption_at_host_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Expand Down Expand Up @@ -424,10 +428,11 @@ func resourceLinuxVirtualMachineCreate(d *pluginsdk.ResourceData, meta interface
sshKeys := ExpandSSHKeys(sshKeysRaw)

params := compute.VirtualMachine{
Name: utils.String(id.Name),
Location: utils.String(location),
Identity: identity,
Plan: plan,
Name: utils.String(id.Name),
ExtendedLocation: expandEdgeZone(d.Get("edge_zone").(string)),
Location: utils.String(location),
Identity: identity,
Plan: plan,
VirtualMachineProperties: &compute.VirtualMachineProperties{
HardwareProfile: &compute.HardwareProfile{
VMSize: compute.VirtualMachineSizeTypes(size),
Expand Down Expand Up @@ -466,23 +471,23 @@ func resourceLinuxVirtualMachineCreate(d *pluginsdk.ResourceData, meta interface
Tags: tags.Expand(t),
}

if v, ok := d.GetOk("patch_mode"); ok {
if v == string(compute.LinuxVMGuestPatchModeAutomaticByPlatform) && !provisionVMAgent {
return fmt.Errorf("%q cannot be set to %q when %q is set to %q", "patch_mode", "AutomaticByPlatform", "provision_vm_agent", "false")
}

params.VirtualMachineProperties.OsProfile.LinuxConfiguration.PatchSettings = &compute.LinuxPatchSettings{
PatchMode: compute.LinuxVMGuestPatchMode(v.(string)),
if encryptionAtHostEnabled, ok := d.GetOk("encryption_at_host_enabled"); ok {
params.VirtualMachineProperties.SecurityProfile = &compute.SecurityProfile{
EncryptionAtHost: utils.Bool(encryptionAtHostEnabled.(bool)),
}
}

if v, ok := d.GetOk("license_type"); ok {
params.VirtualMachineProperties.LicenseType = utils.String(v.(string))
}

if encryptionAtHostEnabled, ok := d.GetOk("encryption_at_host_enabled"); ok {
params.VirtualMachineProperties.SecurityProfile = &compute.SecurityProfile{
EncryptionAtHost: utils.Bool(encryptionAtHostEnabled.(bool)),
if v, ok := d.GetOk("patch_mode"); ok {
if v == string(compute.LinuxVMGuestPatchModeAutomaticByPlatform) && !provisionVMAgent {
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("%q cannot be set to %q when %q is set to %q", "patch_mode", "AutomaticByPlatform", "provision_vm_agent", "false")
}

params.VirtualMachineProperties.OsProfile.LinuxConfiguration.PatchSettings = &compute.LinuxPatchSettings{
PatchMode: compute.LinuxVMGuestPatchMode(v.(string)),
}
}

Expand Down Expand Up @@ -632,9 +637,8 @@ func resourceLinuxVirtualMachineRead(d *pluginsdk.ResourceData, meta interface{}

d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
d.Set("location", location.NormalizeNilable(resp.Location))
d.Set("edge_zone", flattenEdgeZone(resp.ExtendedLocation))

identity, err := flattenVirtualMachineIdentity(resp.Identity)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,21 @@ func TestAccLinuxVirtualMachine_otherCustomData(t *testing.T) {
})
}

func TestAccLinuxVirtualMachine_otherEdgeZone(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_linux_virtual_machine", "test")
r := LinuxVirtualMachineResource{}

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

func TestAccLinuxVirtualMachine_otherUserData(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_linux_virtual_machine", "test")
r := LinuxVirtualMachineResource{}
Expand Down Expand Up @@ -1142,6 +1157,49 @@ resource "azurerm_linux_virtual_machine" "test" {
`, r.template(data), data.RandomInteger)
}

func (r LinuxVirtualMachineResource) otherEdgeZone(data acceptance.TestData) string {
// @tombuildsstuff: WestUS has an edge zone available - so hard-code to that for now
data.Locations.Primary = "westus"

return fmt.Sprintf(`
%[1]s

data "azurerm_extended_locations" "test" {
location = azurerm_resource_group.test.location
}

resource "azurerm_linux_virtual_machine" "test" {
name = "acctestVM-%[2]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
size = "Standard_D2s_v3" # intentional for premium/edgezones
admin_username = "adminuser"
network_interface_ids = [
azurerm_network_interface.test.id,
]

edge_zone = data.azurerm_extended_locations.test.extended_locations[0]

admin_ssh_key {
username = "adminuser"
public_key = local.first_public_key
}

os_disk {
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}

source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
}
`, r.otherBootDiagnosticsTemplate(data), data.RandomInteger)
}

func (r LinuxVirtualMachineResource) otherUserData(data acceptance.TestData, userData string) string {
return fmt.Sprintf(`
%s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ func TestAccLinuxVirtualMachineScaleSet_otherCustomData(t *testing.T) {
})
}

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

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

func TestAccLinuxVirtualMachineScaleSet_otherUserData(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_linux_virtual_machine_scale_set", "test")
r := LinuxVirtualMachineScaleSetResource{}
Expand Down Expand Up @@ -910,9 +925,16 @@ resource "azurerm_linux_virtual_machine_scale_set" "test" {
`, r.template(data), data.RandomInteger, customData)
}

func (r LinuxVirtualMachineScaleSetResource) otherUserData(data acceptance.TestData, userData string) string {
func (r LinuxVirtualMachineScaleSetResource) otherEdgeZone(data acceptance.TestData) string {
// @tombuildsstuff: WestUS has an edge zone available - so hard-code to that for now
data.Locations.Primary = "westus"

return fmt.Sprintf(`
%s
%[1]s

data "azurerm_extended_locations" "test" {
location = azurerm_resource_group.test.location
}

resource "azurerm_linux_virtual_machine_scale_set" "test" {
name = "acctestvmss-%d"
Expand All @@ -922,19 +944,19 @@ resource "azurerm_linux_virtual_machine_scale_set" "test" {
instances = 1
admin_username = "adminuser"
admin_password = "P@ssword1234!"
user_data = base64encode(%q)
edge_zone = data.azurerm_extended_locations.test.extended_locations[0]

disable_password_authentication = false

source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
sku = "18.04-LTS"
version = "latest"
}

os_disk {
storage_account_type = "Standard_LRS"
storage_account_type = "Premium_LRS"
caching = "ReadWrite"
}

Expand All @@ -949,7 +971,7 @@ resource "azurerm_linux_virtual_machine_scale_set" "test" {
}
}
}
`, r.template(data), data.RandomInteger, userData)
`, r.template(data), data.RandomInteger)
}

func (r LinuxVirtualMachineScaleSetResource) otherForceDelete(data acceptance.TestData) string {
Expand Down Expand Up @@ -1542,6 +1564,48 @@ resource "azurerm_linux_virtual_machine_scale_set" "test" {
`, r.template(data), data.RandomInteger)
}

func (r LinuxVirtualMachineScaleSetResource) otherUserData(data acceptance.TestData, userData string) string {
return fmt.Sprintf(`
%s

resource "azurerm_linux_virtual_machine_scale_set" "test" {
name = "acctestvmss-%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
sku = "Standard_F2"
instances = 1
admin_username = "adminuser"
admin_password = "P@ssword1234!"
user_data = base64encode(%q)

disable_password_authentication = false

source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}

os_disk {
storage_account_type = "Standard_LRS"
caching = "ReadWrite"
}

network_interface {
name = "example"
primary = true

ip_configuration {
name = "internal"
primary = true
subnet_id = azurerm_subnet.test.id
}
}
}
`, r.template(data), data.RandomInteger, userData)
}

func (r LinuxVirtualMachineScaleSetResource) otherVMAgent(data acceptance.TestData, enabled bool) string {
return fmt.Sprintf(`
%s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ func resourceLinuxVirtualMachineScaleSet() *pluginsdk.Resource {
Default: false,
},

"edge_zone": commonschema.EdgeZoneOptionalForceNew(),

"encryption_at_host_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Expand Down Expand Up @@ -534,7 +536,8 @@ func resourceLinuxVirtualMachineScaleSetCreate(d *pluginsdk.ResourceData, meta i
automaticRepairsPolicy := ExpandVirtualMachineScaleSetAutomaticRepairsPolicy(automaticRepairsPolicyRaw)

props := compute.VirtualMachineScaleSet{
Location: utils.String(location),
ExtendedLocation: expandEdgeZone(d.Get("edge_zone").(string)),
Location: utils.String(location),
Sku: &compute.Sku{
Name: utils.String(d.Get("sku").(string)),
Capacity: utils.Int64(int64(d.Get("instances").(int))),
Expand Down Expand Up @@ -944,6 +947,7 @@ func resourceLinuxVirtualMachineScaleSetRead(d *pluginsdk.ResourceData, meta int
d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("location", location.NormalizeNilable(resp.Location))
d.Set("edge_zone", flattenEdgeZone(resp.ExtendedLocation))
d.Set("zones", zones.Flatten(resp.Zones))

var skuName *string
Expand Down
12 changes: 8 additions & 4 deletions internal/services/compute/managed_disk_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ func resourceManagedDisk() *pluginsdk.Resource {
}, false),
},

"edge_zone": commonschema.EdgeZoneOptionalForceNew(),

"logical_sector_size": {
Type: pluginsdk.TypeInt,
Optional: true,
Expand Down Expand Up @@ -464,9 +466,10 @@ func resourceManagedDiskCreate(d *pluginsdk.ResourceData, meta interface{}) erro
}

createDisk := compute.Disk{
Name: &name,
Location: &location,
DiskProperties: props,
Name: &name,
ExtendedLocation: expandEdgeZone(d.Get("edge_zone").(string)),
Location: &location,
DiskProperties: props,
Sku: &compute.DiskSku{
Name: skuName,
},
Expand Down Expand Up @@ -823,8 +826,9 @@ func resourceManagedDiskRead(d *pluginsdk.ResourceData, meta interface{}) error

d.Set("name", resp.Name)
d.Set("resource_group_name", id.ResourceGroup)

d.Set("location", location.NormalizeNilable(resp.Location))
d.Set("edge_zone", flattenEdgeZone(resp.ExtendedLocation))

if features.ThreePointOhBeta() {
zone := ""
if resp.Zones != nil && len(*resp.Zones) > 0 {
Expand Down
Loading