Skip to content

Commit

Permalink
Support patch_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcturusZhang committed Nov 11, 2020
1 parent 9b9ee99 commit f479d4d
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,69 @@ 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_otherAdditionalUnattendContent(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_windows_virtual_machine", "test")

Expand Down Expand Up @@ -855,6 +918,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 @@ -196,6 +196,17 @@ func resourceWindowsVirtualMachine() *schema.Resource {
ValidateFunc: validation.FloatAtLeast(-1.0),
},

"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 @@ -477,6 +488,12 @@ func resourceWindowsVirtualMachineCreate(d *schema.ResourceData, meta interface{
}
}

if v, ok := d.GetOk("patch_mode"); ok {
params.VirtualMachineProperties.OsProfile.WindowsConfiguration.PatchSettings = &compute.PatchSettings{
PatchMode: compute.InGuestPatchMode(v.(string)),
}
}

if v, ok := d.GetOk("proximity_placement_group_id"); ok {
params.ProximityPlacementGroup = &compute.SubResource{
ID: utils.String(v.(string)),
Expand Down Expand Up @@ -622,6 +639,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
2 changes: 2 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,8 @@ 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`.

* `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

0 comments on commit f479d4d

Please sign in to comment.