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

Update azurerm_windows_virtual_machine - Support patch_mode #9258

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,108 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
)

func TestAccWindowsVirtualMachine_otherPatchModeManual(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_windows_virtual_machine", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: checkWindowsVirtualMachineIsDestroyed,
Steps: []resource.TestStep{
{
Config: testWindowsVirtualMachine_otherPatchModeManual(data),
Check: resource.ComposeTestCheckFunc(
checkWindowsVirtualMachineExists(data.ResourceName),
),
},
data.ImportStep(
"admin_password",
),
},
})
}

func TestAccWindowsVirtualMachine_otherPatchModeAutomaticByOS(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_windows_virtual_machine", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: checkWindowsVirtualMachineIsDestroyed,
Steps: []resource.TestStep{
{
Config: testWindowsVirtualMachine_otherPatchModeAutomaticByOS(data),
Check: resource.ComposeTestCheckFunc(
checkWindowsVirtualMachineExists(data.ResourceName),
),
},
data.ImportStep(
"admin_password",
),
},
})
}

func TestAccWindowsVirtualMachine_otherPatchModeAutomaticByPlatform(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_windows_virtual_machine", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: checkWindowsVirtualMachineIsDestroyed,
Steps: []resource.TestStep{
{
Config: testWindowsVirtualMachine_otherPatchModeAutomaticByPlatform(data),
Check: resource.ComposeTestCheckFunc(
checkWindowsVirtualMachineExists(data.ResourceName),
),
},
data.ImportStep(
"admin_password",
),
},
})
}

func TestAccWindowsVirtualMachine_otherPatchModeUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_windows_virtual_machine", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: checkWindowsVirtualMachineIsDestroyed,
Steps: []resource.TestStep{
{
Config: testWindowsVirtualMachine_otherPatchModeAutomaticByOS(data),
Check: resource.ComposeTestCheckFunc(
checkWindowsVirtualMachineExists(data.ResourceName),
),
},
data.ImportStep(
"admin_password",
),
{
Config: testWindowsVirtualMachine_otherPatchModeAutomaticByPlatform(data),
Check: resource.ComposeTestCheckFunc(
checkWindowsVirtualMachineExists(data.ResourceName),
),
},
data.ImportStep(
"admin_password",
),
{
Config: testWindowsVirtualMachine_otherPatchModeManual(data), // this update requires force replacement actually
Check: resource.ComposeTestCheckFunc(
checkWindowsVirtualMachineExists(data.ResourceName),
),
},
data.ImportStep(
"admin_password",
),
},
})
}

func TestAccWindowsVirtualMachine_otherAdditionalUnattendContent(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_windows_virtual_machine", "test")

Expand Down Expand Up @@ -855,6 +957,109 @@ func TestAccWindowsVirtualMachine_otherEncryptionAtHostEnabledWithCMK(t *testing
})
}

func testWindowsVirtualMachine_otherPatchModeManual(data acceptance.TestData) string {
template := testWindowsVirtualMachine_template(data)
return fmt.Sprintf(`
%s

resource "azurerm_windows_virtual_machine" "test" {
name = local.vm_name
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
size = "Standard_F2"
admin_username = "adminuser"
admin_password = "P@$$w0rd1234!"

network_interface_ids = [
azurerm_network_interface.test.id,
]

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

source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter"
version = "latest"
}

enable_automatic_updates = false
patch_mode = "Manual"
}
`, template)
}

func testWindowsVirtualMachine_otherPatchModeAutomaticByOS(data acceptance.TestData) string {
template := testWindowsVirtualMachine_template(data)
return fmt.Sprintf(`
%s

resource "azurerm_windows_virtual_machine" "test" {
name = local.vm_name
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
size = "Standard_F2"
admin_username = "adminuser"
admin_password = "P@$$w0rd1234!"

network_interface_ids = [
azurerm_network_interface.test.id,
]

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

source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter"
version = "latest"
}

patch_mode = "AutomaticByOS"
}
`, template)
}

func testWindowsVirtualMachine_otherPatchModeAutomaticByPlatform(data acceptance.TestData) string {
template := testWindowsVirtualMachine_template(data)
return fmt.Sprintf(`
%s

resource "azurerm_windows_virtual_machine" "test" {
name = local.vm_name
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
size = "Standard_F2"
admin_username = "adminuser"
admin_password = "P@$$w0rd1234!"

network_interface_ids = [
azurerm_network_interface.test.id,
]

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

source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter"
version = "latest"
}

patch_mode = "AutomaticByPlatform"
}
`, template)
}

func testWindowsVirtualMachine_otherAdditionalUnattendContent(data acceptance.TestData) string {
template := testWindowsVirtualMachine_template(data)
return fmt.Sprintf(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func resourceWindowsVirtualMachine() *schema.Resource {
"enable_automatic_updates": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true, // TODO: confirm
ForceNew: true, // updating this is not allowed "Changing property 'windowsConfiguration.enableAutomaticUpdates' is not allowed." Target="windowsConfiguration.enableAutomaticUpdates"
Default: true,
},

Expand Down Expand Up @@ -196,6 +196,18 @@ func resourceWindowsVirtualMachine() *schema.Resource {
ValidateFunc: validation.FloatAtLeast(-1.0),
},

// This is a preview feature: `az feature register -n InGuestAutoPatchVMPreview --namespace Microsoft.Compute`
"patch_mode": {
Type: schema.TypeString,
Optional: true,
Default: string(compute.AutomaticByOS),
ValidateFunc: validation.StringInSlice([]string{
string(compute.AutomaticByOS),
string(compute.AutomaticByPlatform),
string(compute.Manual),
}, false),
},

"plan": planSchema(),

"priority": {
Expand Down Expand Up @@ -400,6 +412,9 @@ func resourceWindowsVirtualMachineCreate(d *schema.ResourceData, meta interface{
ProvisionVMAgent: utils.Bool(provisionVMAgent),
EnableAutomaticUpdates: utils.Bool(enableAutomaticUpdates),
WinRM: winRmListeners,
PatchSettings: &compute.PatchSettings{
PatchMode: compute.InGuestPatchMode(d.Get("patch_mode").(string)),
},
},
Secrets: secrets,
},
Expand Down Expand Up @@ -622,6 +637,11 @@ func resourceWindowsVirtualMachineRead(d *schema.ResourceData, meta interface{})
d.Set("enable_automatic_updates", config.EnableAutomaticUpdates)

d.Set("provision_vm_agent", config.ProvisionVMAgent)

if patchSettings := config.PatchSettings; patchSettings != nil {
d.Set("patch_mode", patchSettings.PatchMode)
}

d.Set("timezone", config.TimeZone)

if err := d.Set("winrm_listener", flattenWinRMListener(config.WinRM)); err != nil {
Expand Down Expand Up @@ -775,6 +795,22 @@ func resourceWindowsVirtualMachineUpdate(d *schema.ResourceData, meta interface{
update.OsProfile.AllowExtensionOperations = utils.Bool(allowExtensionOperations)
}

if d.HasChange("patch_mode") {
shouldUpdate = true

if update.OsProfile == nil {
update.OsProfile = &compute.OSProfile{}
}

if update.OsProfile.WindowsConfiguration == nil {
update.OsProfile.WindowsConfiguration = &compute.WindowsConfiguration{}
}

update.OsProfile.WindowsConfiguration.PatchSettings = &compute.PatchSettings{
PatchMode: compute.InGuestPatchMode(d.Get("patch_mode").(string)),
}
}

if d.HasChange("identity") {
shouldUpdate = true

Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/windows_virtual_machine.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ The following arguments are supported:

-> **NOTE:** This can only be configured when `priority` is set to `Spot`.

* `patch_mode` - (Optional) Specifies the mode of in-guest patching to this Windows Virtual Machine. Possible values are `Manual`, `AutomaticByOS` and `AutomaticByPlatform`. Defaults to `AutomaticByOS`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we include a note that this is a preview feature and the command to enable it?

 TestAccWindowsVirtualMachine_otherPatchModeUpdate: testing.go:684: Step 2 error: errors during apply:
        
        Error: updating Windows Virtual Machine "acctestvmlv1eo" (Resource Group "acctestRG-201114233915712563"): compute.VirtualMachinesClient#Update: Failure sending request: StatusCode=400 -- Original Error: Code="InvalidParameter" Message="The property 'windowsConfiguration.patchSettings.patchMode' is not valid because the 'Microsoft.Compute/InGuestAutoPatchVMPreview' feature is not enabled for this subscription." Target="windowsConfiguration.patchSettings.patchMode"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh...yeah I will update the doc to include the enabling process for this. Thanks


-> **NOTE:** This is a preview feature, you can opt-in with the command `az feature register -n InGuestAutoPatchVMPreview --namespace Microsoft.Compute`.

* `plan` - (Optional) A `plan` block as defined below. Changing this forces a new resource to be created.

* `priority`- (Optional) Specifies the priority of this Virtual Machine. Possible values are `Regular` and `Spot`. Defaults to `Regular`. Changing this forces a new resource to be created.
Expand Down