Skip to content

Commit 20003b2

Browse files
TStraub-rmsstack72
authored andcommitted
provider/azurerm support license_type virtual_machine property for Windows machines. (hashicorp#10539)
* Added license_type to Azure VirtualMachineProperties call. * Updated websit documentation. * Added validation for license_type * Added acceptance test * Clarified documentation.
1 parent 617fb5a commit 20003b2

3 files changed

Lines changed: 120 additions & 3 deletions

File tree

builtin/providers/azurerm/resource_arm_virtual_machine.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ func resourceArmVirtualMachine() *schema.Resource {
7575
},
7676

7777
"license_type": {
78-
Type: schema.TypeString,
79-
Optional: true,
80-
Computed: true,
78+
Type: schema.TypeString,
79+
Optional: true,
80+
Computed: true,
81+
ValidateFunc: validateLicenseType,
8182
},
8283

8384
"vm_size": {
@@ -431,6 +432,15 @@ func resourceArmVirtualMachine() *schema.Resource {
431432
}
432433
}
433434

435+
func validateLicenseType(v interface{}, k string) (ws []string, errors []error) {
436+
value := v.(string)
437+
if value != "" && value != "Windows_Server" {
438+
errors = append(errors, fmt.Errorf(
439+
"[ERROR] license_type must be 'Windows_Server' or empty"))
440+
}
441+
return
442+
}
443+
434444
func validateDiskSizeGB(v interface{}, k string) (ws []string, errors []error) {
435445
value := v.(int)
436446
if value < 1 || value > 1023 {
@@ -478,12 +488,14 @@ func resourceArmVirtualMachineCreate(d *schema.ResourceData, meta interface{}) e
478488

479489
networkProfile := expandAzureRmVirtualMachineNetworkProfile(d)
480490
vmSize := d.Get("vm_size").(string)
491+
licenseType := d.Get("license_type").(string)
481492
properties := compute.VirtualMachineProperties{
482493
NetworkProfile: &networkProfile,
483494
HardwareProfile: &compute.HardwareProfile{
484495
VMSize: compute.VirtualMachineSizeTypes(vmSize),
485496
},
486497
StorageProfile: &storageProfile,
498+
LicenseType: &licenseType,
487499
}
488500

489501
if _, ok := d.GetOk("boot_diagnostics"); ok {

builtin/providers/azurerm/resource_arm_virtual_machine_test.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,25 @@ func testCheckAzureRMVirtualMachineDisappears(name string, vm *compute.VirtualMa
515515
}
516516
}
517517

518+
func TestAccAzureRMVirtualMachine_windowsLicenseType(t *testing.T) {
519+
var vm compute.VirtualMachine
520+
ri := acctest.RandInt()
521+
config := fmt.Sprintf(testAccAzureRMVirtualMachine_windowsLicenseType, ri, ri, ri, ri, ri, ri)
522+
resource.Test(t, resource.TestCase{
523+
PreCheck: func() { testAccPreCheck(t) },
524+
Providers: testAccProviders,
525+
CheckDestroy: testCheckAzureRMVirtualMachineDestroy,
526+
Steps: []resource.TestStep{
527+
{
528+
Config: config,
529+
Check: resource.ComposeTestCheckFunc(
530+
testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm),
531+
),
532+
},
533+
},
534+
})
535+
}
536+
518537
var testAccAzureRMVirtualMachine_basicLinuxMachine = `
519538
resource "azurerm_resource_group" "test" {
520539
name = "acctestRG-%d"
@@ -2055,3 +2074,88 @@ resource "azurerm_virtual_machine" "test" {
20552074
}
20562075
}
20572076
`
2077+
2078+
var testAccAzureRMVirtualMachine_windowsLicenseType = `
2079+
resource "azurerm_resource_group" "test" {
2080+
name = "acctestRG-%d"
2081+
location = "West US"
2082+
}
2083+
2084+
resource "azurerm_virtual_network" "test" {
2085+
name = "acctvn-%d"
2086+
address_space = ["10.0.0.0/16"]
2087+
location = "West US"
2088+
resource_group_name = "${azurerm_resource_group.test.name}"
2089+
}
2090+
2091+
resource "azurerm_subnet" "test" {
2092+
name = "acctsub-%d"
2093+
resource_group_name = "${azurerm_resource_group.test.name}"
2094+
virtual_network_name = "${azurerm_virtual_network.test.name}"
2095+
address_prefix = "10.0.2.0/24"
2096+
}
2097+
2098+
resource "azurerm_network_interface" "test" {
2099+
name = "acctni-%d"
2100+
location = "West US"
2101+
resource_group_name = "${azurerm_resource_group.test.name}"
2102+
2103+
ip_configuration {
2104+
name = "testconfiguration1"
2105+
subnet_id = "${azurerm_subnet.test.id}"
2106+
private_ip_address_allocation = "dynamic"
2107+
}
2108+
}
2109+
2110+
resource "azurerm_storage_account" "test" {
2111+
name = "accsa%d"
2112+
resource_group_name = "${azurerm_resource_group.test.name}"
2113+
location = "westus"
2114+
account_type = "Standard_LRS"
2115+
2116+
tags {
2117+
environment = "staging"
2118+
}
2119+
}
2120+
2121+
resource "azurerm_storage_container" "test" {
2122+
name = "vhds"
2123+
resource_group_name = "${azurerm_resource_group.test.name}"
2124+
storage_account_name = "${azurerm_storage_account.test.name}"
2125+
container_access_type = "private"
2126+
}
2127+
2128+
resource "azurerm_virtual_machine" "test" {
2129+
name = "acctvm-%d"
2130+
location = "West US"
2131+
resource_group_name = "${azurerm_resource_group.test.name}"
2132+
network_interface_ids = ["${azurerm_network_interface.test.id}"]
2133+
vm_size = "Standard_A0"
2134+
license_type = "Windows_Server"
2135+
2136+
storage_image_reference {
2137+
publisher = "MicrosoftWindowsServer"
2138+
offer = "WindowsServer-HUB"
2139+
sku = "2008-R2-SP1-HUB"
2140+
version = "latest"
2141+
}
2142+
2143+
storage_os_disk {
2144+
name = "myosdisk1"
2145+
vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/myosdisk1.vhd"
2146+
caching = "ReadWrite"
2147+
create_option = "FromImage"
2148+
}
2149+
2150+
os_profile {
2151+
computer_name = "winhost01"
2152+
admin_username = "testadmin"
2153+
admin_password = "Password1234!"
2154+
}
2155+
2156+
os_profile_windows_config {
2157+
enable_automatic_upgrades = false
2158+
provision_vm_agent = true
2159+
}
2160+
}
2161+
`

website/source/docs/providers/azurerm/r/virtual_machine.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ The following arguments are supported:
215215
* `storage_data_disk` - (Optional) A list of Storage Data disk blocks as referenced below.
216216
* `delete_data_disks_on_termination` - (Optional) Flag to enable deletion of Storage Disk VHD blobs when the VM is deleted, defaults to `false`
217217
* `os_profile` - (Required) An OS Profile block as documented below.
218+
* `license_type` - (Optional, when a windows machine) Specifies the Windows OS license type. The only allowable value, if supplied, is `Windows_Server`.
218219
* `os_profile_windows_config` - (Required, when a windows machine) A Windows config block as documented below.
219220
* `os_profile_linux_config` - (Required, when a linux machine) A Linux config block as documented below.
220221
* `os_profile_secrets` - (Optional) A collection of Secret blocks as documented below.

0 commit comments

Comments
 (0)