From 29e10c1342f3cc786e3d7acc9649f8083425a749 Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Fri, 22 Oct 2021 22:01:39 +0200 Subject: [PATCH 1/4] Added support for patch_mode in resource_linux_virtual_machine. Fixes #13257 --- .../compute/linux_virtual_machine_resource.go | 15 ++++++ ...inux_virtual_machine_resource_auth_test.go | 54 +++++++++++++++++++ .../r/linux_virtual_machine.html.markdown | 2 + 3 files changed, 71 insertions(+) diff --git a/internal/services/compute/linux_virtual_machine_resource.go b/internal/services/compute/linux_virtual_machine_resource.go index 41a74dd6c7e4..646ccb101ddf 100644 --- a/internal/services/compute/linux_virtual_machine_resource.go +++ b/internal/services/compute/linux_virtual_machine_resource.go @@ -211,6 +211,15 @@ func resourceLinuxVirtualMachine() *pluginsdk.Resource { ForceNew: true, }, + "patch_mode": { + Type: pluginsdk.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + string(compute.LinuxVMGuestPatchModeAutomaticByPlatform), + string(compute.LinuxVMGuestPatchModeImageDefault), + }, false), + }, + "proximity_placement_group_id": { Type: pluginsdk.TypeString, Optional: true, @@ -416,6 +425,12 @@ func resourceLinuxVirtualMachineCreate(d *pluginsdk.ResourceData, meta interface Tags: tags.Expand(t), } + if v, ok := d.GetOk("patch_mode"); ok { + params.VirtualMachineProperties.OsProfile.LinuxConfiguration.PatchSettings = &compute.LinuxPatchSettings{ + PatchMode: compute.LinuxVMGuestPatchMode(compute.LinuxVMGuestPatchMode(v)), + } + } + if v, ok := d.GetOk("license_type"); ok { params.VirtualMachineProperties.LicenseType = utils.String(v.(string)) } diff --git a/internal/services/compute/linux_virtual_machine_resource_auth_test.go b/internal/services/compute/linux_virtual_machine_resource_auth_test.go index 4254fd248f63..9ac8674a89a5 100644 --- a/internal/services/compute/linux_virtual_machine_resource_auth_test.go +++ b/internal/services/compute/linux_virtual_machine_resource_auth_test.go @@ -72,6 +72,29 @@ func TestAccLinuxVirtualMachine_authSSHMultipleKeys(t *testing.T) { }) } +func TestAccLinuxVirtualMachine_linuxPatchModeSetting(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_linux_virtual_machine", "test") + r := LinuxVirtualMachineResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.linuxPatchModeSetting(data, "AutomaticByPlatform"), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("patch_mode").HasValue("AutomaticByPlatform"), + ), + }, + { + Config: r.linuxPatchModeSetting(data, "ImageDefault"), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("patch_mode").HasValue("ImageDefault"), + ), + }, + data.ImportStep(), + }) +} + func (r LinuxVirtualMachineResource) authPassword(data acceptance.TestData) string { return fmt.Sprintf(` %s @@ -210,3 +233,34 @@ resource "azurerm_linux_virtual_machine" "test" { } `, r.template(data), data.RandomInteger) } + +func (r LinuxVirtualMachineResource) linuxPatchModeSetting(data acceptance.TestData, patchMode string) string { + return fmt.Sprintf(` +%s + +resource "azurerm_linux_virtual_machine" "test" { + name = "acctestVM-%d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + size = "Standard_F2" + admin_username = "adminuser" + admin_password = "password1234!" + network_interface_ids = [ + azurerm_network_interface.test.id, + ] + patch_mode = "%s" + + os_disk { + caching = "ReadWrite" + storage_account_type = "Standard_LRS" + } + + source_image_reference { + publisher = "Canonical" + offer = "UbuntuServer" + sku = "20.04-LTS" + version = "latest" + } +} +`, r.template(data), data.RandomInteger, patchMode) +} diff --git a/website/docs/r/linux_virtual_machine.html.markdown b/website/docs/r/linux_virtual_machine.html.markdown index f9afeba995c8..bcdbef913978 100644 --- a/website/docs/r/linux_virtual_machine.html.markdown +++ b/website/docs/r/linux_virtual_machine.html.markdown @@ -152,6 +152,8 @@ The following arguments are supported: * `identity` - (Optional) An `identity` block as defined below. +* `patch_mode` - (Optional) Specifies the mode of in-guest patching to this Linux Virtual Machine. Possible values are `AutomaticByPlatform` and `ImageDefault`. Defaults to `ImageDefault`. + * `max_bid_price` - (Optional) The maximum price you're willing to pay for this Virtual Machine, in US Dollars; which must be greater than the current spot price. If this bid price falls below the current spot price the Virtual Machine will be evicted using the `eviction_policy`. Defaults to `-1`, which means that the Virtual Machine should not be evicted for price reasons. -> **NOTE:** This can only be configured when `priority` is set to `Spot`. From 829100911816cbe360c68c25522f24526af6f182 Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Fri, 29 Oct 2021 21:03:02 +0200 Subject: [PATCH 2/4] Update functionality --- internal/services/compute/linux_virtual_machine_resource.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/services/compute/linux_virtual_machine_resource.go b/internal/services/compute/linux_virtual_machine_resource.go index 646ccb101ddf..350563232c34 100644 --- a/internal/services/compute/linux_virtual_machine_resource.go +++ b/internal/services/compute/linux_virtual_machine_resource.go @@ -945,6 +945,12 @@ func resourceLinuxVirtualMachineUpdate(d *pluginsdk.ResourceData, meta interface } } + if d.HasChange("patch_mode") { + update.VirtualMachineProperties.OsProfile.LinuxConfiguration.PatchSettings = &compute.LinuxPatchSettings{ + PatchMode: compute.LinuxVMGuestPatchMode(compute.LinuxVMGuestPatchMode(d.Get("patch_mode").(string))), + } + } + if d.HasChange("allow_extension_operations") { allowExtensionOperations := d.Get("allow_extension_operations").(bool) From f053f070dc3e5e25fbffa3d1bdf27a5d4cb890a7 Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Fri, 29 Oct 2021 22:31:07 +0200 Subject: [PATCH 3/4] Fix linting --- internal/services/compute/linux_virtual_machine_resource.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/services/compute/linux_virtual_machine_resource.go b/internal/services/compute/linux_virtual_machine_resource.go index 350563232c34..bf602d20dbf4 100644 --- a/internal/services/compute/linux_virtual_machine_resource.go +++ b/internal/services/compute/linux_virtual_machine_resource.go @@ -427,7 +427,7 @@ func resourceLinuxVirtualMachineCreate(d *pluginsdk.ResourceData, meta interface if v, ok := d.GetOk("patch_mode"); ok { params.VirtualMachineProperties.OsProfile.LinuxConfiguration.PatchSettings = &compute.LinuxPatchSettings{ - PatchMode: compute.LinuxVMGuestPatchMode(compute.LinuxVMGuestPatchMode(v)), + PatchMode: compute.LinuxVMGuestPatchMode(v.(string)), } } @@ -947,7 +947,7 @@ func resourceLinuxVirtualMachineUpdate(d *pluginsdk.ResourceData, meta interface if d.HasChange("patch_mode") { update.VirtualMachineProperties.OsProfile.LinuxConfiguration.PatchSettings = &compute.LinuxPatchSettings{ - PatchMode: compute.LinuxVMGuestPatchMode(compute.LinuxVMGuestPatchMode(d.Get("patch_mode").(string))), + PatchMode: compute.LinuxVMGuestPatchMode(d.Get("patch_mode").(string)), } } From 2456f604ac617d99254876f4981de29c2d8a98f1 Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Mon, 1 Nov 2021 21:09:12 +0100 Subject: [PATCH 4/4] Add SSH key to test --- .../compute/linux_virtual_machine_resource_auth_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/internal/services/compute/linux_virtual_machine_resource_auth_test.go b/internal/services/compute/linux_virtual_machine_resource_auth_test.go index 9ac8674a89a5..2f11325bf5ea 100644 --- a/internal/services/compute/linux_virtual_machine_resource_auth_test.go +++ b/internal/services/compute/linux_virtual_machine_resource_auth_test.go @@ -250,6 +250,11 @@ resource "azurerm_linux_virtual_machine" "test" { ] patch_mode = "%s" + admin_ssh_key { + username = "adminuser" + public_key = local.first_public_key + } + os_disk { caching = "ReadWrite" storage_account_type = "Standard_LRS"