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

r\linux_virtual_machine,r\windows_virtual_machine: Add support for diff_disk_settings.placement #14847

Merged
merged 4 commits into from
May 13, 2022
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
11 changes: 9 additions & 2 deletions internal/services/compute/linux_virtual_machine_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,10 @@ func resourceLinuxVirtualMachineCreate(d *pluginsdk.ResourceData, meta interface
networkInterfaceIds := expandVirtualMachineNetworkInterfaceIDs(networkInterfaceIdsRaw)

osDiskRaw := d.Get("os_disk").([]interface{})
osDisk := expandVirtualMachineOSDisk(osDiskRaw, compute.OperatingSystemTypesLinux)
osDisk, err := expandVirtualMachineOSDisk(osDiskRaw, compute.OperatingSystemTypesLinux)
if err != nil {
return fmt.Errorf("expanding `os_disk`: %+v", err)
}

secretsRaw := d.Get("secret").([]interface{})
secrets := expandLinuxSecrets(secretsRaw)
Expand Down Expand Up @@ -948,7 +951,11 @@ func resourceLinuxVirtualMachineUpdate(d *pluginsdk.ResourceData, meta interface
shouldDeallocate = true

osDiskRaw := d.Get("os_disk").([]interface{})
osDisk := expandVirtualMachineOSDisk(osDiskRaw, compute.OperatingSystemTypesLinux)
osDisk, err := expandVirtualMachineOSDisk(osDiskRaw, compute.OperatingSystemTypesLinux)
if err != nil {
return fmt.Errorf("expanding `os_disk`: %+v", err)
}

update.VirtualMachineProperties.StorageProfile = &compute.StorageProfile{
OsDisk: osDisk,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,28 @@ func TestAccLinuxVirtualMachine_diskOSDiskEncryptionSetUpdate(t *testing.T) {
})
}

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

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

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

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.diskOSEphemeralResourceDisk(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
Expand Down Expand Up @@ -625,7 +640,7 @@ resource "azurerm_linux_virtual_machine" "test" {
`, r.diskOSDiskDiskEncryptionSetResource(data), data.RandomInteger)
}

func (r LinuxVirtualMachineResource) diskOSEphemeral(data acceptance.TestData) string {
func (r LinuxVirtualMachineResource) diskOSEphemeralDefault(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

Expand Down Expand Up @@ -663,6 +678,45 @@ resource "azurerm_linux_virtual_machine" "test" {
`, r.template(data), data.RandomInteger)
}

func (r LinuxVirtualMachineResource) diskOSEphemeralResourceDisk(data acceptance.TestData) 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_F4s_v2"
admin_username = "adminuser"
network_interface_ids = [
azurerm_network_interface.test.id,
]

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

os_disk {
caching = "ReadOnly"
storage_account_type = "Standard_LRS"

diff_disk_settings {
option = "Local"
placement = "ResourceDisk"
}
}

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

func (r LinuxVirtualMachineResource) diskOSStorageAccountType(data acceptance.TestData, accountType string) string {
return fmt.Sprintf(`
%s
Expand Down
34 changes: 29 additions & 5 deletions internal/services/compute/virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package compute

import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-07-01/compute"
"github.com/hashicorp/terraform-provider-azurerm/internal/identity"
Expand Down Expand Up @@ -200,6 +201,16 @@ func virtualMachineOSDiskSchema() *pluginsdk.Schema {
string(compute.DiffDiskOptionsLocal),
}, false),
},
"placement": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
Default: string(compute.DiffDiskPlacementCacheDisk),
ValidateFunc: validation.StringInSlice([]string{
string(compute.DiffDiskPlacementCacheDisk),
string(compute.DiffDiskPlacementResourceDisk),
}, false),
},
},
},
},
Expand Down Expand Up @@ -236,10 +247,11 @@ func virtualMachineOSDiskSchema() *pluginsdk.Schema {
}
}

func expandVirtualMachineOSDisk(input []interface{}, osType compute.OperatingSystemTypes) *compute.OSDisk {
func expandVirtualMachineOSDisk(input []interface{}, osType compute.OperatingSystemTypes) (*compute.OSDisk, error) {
raw := input[0].(map[string]interface{})
caching := raw["caching"].(string)
disk := compute.OSDisk{
Caching: compute.CachingTypes(raw["caching"].(string)),
Caching: compute.CachingTypes(caching),
ManagedDisk: &compute.ManagedDiskParameters{
StorageAccountType: compute.StorageAccountTypes(raw["storage_account_type"].(string)),
},
Expand All @@ -258,9 +270,15 @@ func expandVirtualMachineOSDisk(input []interface{}, osType compute.OperatingSys
}

if diffDiskSettingsRaw := raw["diff_disk_settings"].([]interface{}); len(diffDiskSettingsRaw) > 0 {
if caching != string(compute.CachingTypesReadOnly) {
magodo marked this conversation as resolved.
Show resolved Hide resolved
// Restriction per https://docs.microsoft.com/azure/virtual-machines/ephemeral-os-disks-deploy#vm-template-deployment
return nil, fmt.Errorf("`diff_disk_settings` can only be set when `caching` is set to `ReadOnly`")
}

diffDiskRaw := diffDiskSettingsRaw[0].(map[string]interface{})
disk.DiffDiskSettings = &compute.DiffDiskSettings{
Option: compute.DiffDiskOptions(diffDiskRaw["option"].(string)),
Option: compute.DiffDiskOptions(diffDiskRaw["option"].(string)),
Placement: compute.DiffDiskPlacement(diffDiskRaw["placement"].(string)),
}
}

Expand All @@ -274,7 +292,7 @@ func expandVirtualMachineOSDisk(input []interface{}, osType compute.OperatingSys
disk.Name = utils.String(name)
}

return &disk
return &disk, nil
}

func flattenVirtualMachineOSDisk(ctx context.Context, disksClient *compute.DisksClient, input *compute.OSDisk) ([]interface{}, error) {
Expand All @@ -284,8 +302,14 @@ func flattenVirtualMachineOSDisk(ctx context.Context, disksClient *compute.Disks

diffDiskSettings := make([]interface{}, 0)
if input.DiffDiskSettings != nil {
placement := string(compute.DiffDiskPlacementCacheDisk)
if input.DiffDiskSettings.Placement != "" {
placement = string(input.DiffDiskSettings.Placement)
}

diffDiskSettings = append(diffDiskSettings, map[string]interface{}{
"option": string(input.DiffDiskSettings.Option),
"option": string(input.DiffDiskSettings.Option),
"placement": placement,
magodo marked this conversation as resolved.
Show resolved Hide resolved
})
}

Expand Down
11 changes: 9 additions & 2 deletions internal/services/compute/windows_virtual_machine_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,10 @@ func resourceWindowsVirtualMachineCreate(d *pluginsdk.ResourceData, meta interfa
networkInterfaceIds := expandVirtualMachineNetworkInterfaceIDs(networkInterfaceIdsRaw)

osDiskRaw := d.Get("os_disk").([]interface{})
osDisk := expandVirtualMachineOSDisk(osDiskRaw, compute.OperatingSystemTypesWindows)
osDisk, err := expandVirtualMachineOSDisk(osDiskRaw, compute.OperatingSystemTypesWindows)
if err != nil {
return fmt.Errorf("expanding `os_disk`: %+v", err)
}

secretsRaw := d.Get("secret").([]interface{})
secrets := expandWindowsSecrets(secretsRaw)
Expand Down Expand Up @@ -996,7 +999,11 @@ func resourceWindowsVirtualMachineUpdate(d *pluginsdk.ResourceData, meta interfa
shouldDeallocate = true

osDiskRaw := d.Get("os_disk").([]interface{})
osDisk := expandVirtualMachineOSDisk(osDiskRaw, compute.OperatingSystemTypesWindows)
osDisk, err := expandVirtualMachineOSDisk(osDiskRaw, compute.OperatingSystemTypesWindows)
if err != nil {
return fmt.Errorf("expanding `os_disk`: %+v", err)
}

update.VirtualMachineProperties.StorageProfile = &compute.StorageProfile{
OsDisk: osDisk,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,28 @@ func TestAccWindowsVirtualMachine_diskOSDiskEncryptionSetUpdate(t *testing.T) {
})
}

func TestAccWindowsVirtualMachine_diskOSEphemeral(t *testing.T) {
func TestAccWindowsVirtualMachine_diskOSEphemeralDefault(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_windows_virtual_machine", "test")
r := WindowsVirtualMachineResource{}

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

func TestAccWindowsVirtualMachine_diskOSEphemeralResourceDisk(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_windows_virtual_machine", "test")
r := WindowsVirtualMachineResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.diskOSEphemeralResourceDisk(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
Expand Down Expand Up @@ -599,7 +614,7 @@ resource "azurerm_windows_virtual_machine" "test" {
`, r.diskOSDiskDiskEncryptionSetResource(data))
}

func (r WindowsVirtualMachineResource) diskOSEphemeral(data acceptance.TestData) string {
func (r WindowsVirtualMachineResource) diskOSEphemeralDefault(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

Expand Down Expand Up @@ -634,6 +649,42 @@ resource "azurerm_windows_virtual_machine" "test" {
`, r.template(data))
}

func (r WindowsVirtualMachineResource) diskOSEphemeralResourceDisk(data acceptance.TestData) string {
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
# Message="OS disk of Ephemeral VM with size greater than 32 GB is not allowed for VM size Standard_F2s_v2"
size = "Standard_DS4_v2"
admin_username = "adminuser"
admin_password = "P@$$w0rd1234!"
network_interface_ids = [
azurerm_network_interface.test.id,
]

os_disk {
caching = "ReadOnly"
storage_account_type = "Standard_LRS"

diff_disk_settings {
option = "Local"
placement = "ResourceDisk"
}
}

source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2022-datacenter-smalldisk"
version = "latest"
}
}
`, r.template(data))
}

func (r WindowsVirtualMachineResource) diskOSStorageAccountType(data acceptance.TestData, accountType string) string {
return fmt.Sprintf(`
%s
Expand Down
6 changes: 5 additions & 1 deletion website/docs/r/linux_virtual_machine.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ A `diff_disk_settings` block supports the following:

* `option` - (Required) Specifies the Ephemeral Disk Settings for the OS Disk. At this time the only possible value is `Local`. Changing this forces a new resource to be created.

* `placement` - (Optional) Specifies where to store the Ephemeral Disk. Possible values are `CacheDisk` and `ResourceDisk`. Defaults to `CacheDisk`. Changing this forces a new resource to be created.

---

A `identity` block supports the following:
Expand All @@ -248,7 +250,9 @@ A `os_disk` block supports the following:

* `storage_account_type` - (Required) The Type of Storage Account which should back this the Internal OS Disk. Possible values are `Standard_LRS`, `StandardSSD_LRS` and `Premium_LRS`. Changing this forces a new resource to be created.

* `diff_disk_settings` (Optional) A `diff_disk_settings` block as defined above.
* `diff_disk_settings` (Optional) A `diff_disk_settings` block as defined above. Changing this forces a new resource to be created.

-> **NOTE:** `diff_disk_settings` can only be set when `caching` is set to `ReadOnly`. More information can be found [here](https://docs.microsoft.com/azure/virtual-machines/ephemeral-os-disks-deploy#vm-template-deployment)

* `disk_encryption_set_id` - (Optional) The ID of the Disk Encryption Set which should be used to Encrypt this OS Disk.

Expand Down
6 changes: 5 additions & 1 deletion website/docs/r/windows_virtual_machine.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ A `diff_disk_settings` block supports the following:

* `option` - (Required) Specifies the Ephemeral Disk Settings for the OS Disk. At this time the only possible value is `Local`. Changing this forces a new resource to be created.

* `placement` - (Optional) Specifies where to store the Ephemeral Disk. Possible values are `CacheDisk` and `ResourceDisk`. Defaults to `CacheDisk`. Changing this forces a new resource to be created.

---

A `identity` block supports the following:
Expand All @@ -239,7 +241,9 @@ A `os_disk` block supports the following:

* `storage_account_type` - (Required) The Type of Storage Account which should back this the Internal OS Disk. Possible values are `Standard_LRS`, `StandardSSD_LRS` and `Premium_LRS`. Changing this forces a new resource to be created.

* `diff_disk_settings` (Optional) A `diff_disk_settings` block as defined above.
* `diff_disk_settings` (Optional) A `diff_disk_settings` block as defined above. Changing this forces a new resource to be created.

-> **NOTE:** `diff_disk_settings` can only be set when `caching` is set to `ReadOnly`. More information can be found [here](https://docs.microsoft.com/azure/virtual-machines/ephemeral-os-disks-deploy#vm-template-deployment)

* `disk_encryption_set_id` - (Optional) The ID of the Disk Encryption Set which should be used to Encrypt this OS Disk.

Expand Down