From bbc09caa7665a8e9c0e1b394c92591af98a5916b Mon Sep 17 00:00:00 2001 From: Junyi Yi Date: Thu, 23 Aug 2018 01:35:12 -0700 Subject: [PATCH] Bug Fix of azurerm_virtual_machine.storage_os_disk.image_uri (#1799) * Add repro test for bug #838 * Set image_uri back to schema * Add image_uri check in test case --- azurerm/resource_arm_virtual_machine.go | 3 + ...rm_virtual_machine_unmanaged_disks_test.go | 68 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/azurerm/resource_arm_virtual_machine.go b/azurerm/resource_arm_virtual_machine.go index 2f0f70df2a12..72237a6a615a 100644 --- a/azurerm/resource_arm_virtual_machine.go +++ b/azurerm/resource_arm_virtual_machine.go @@ -1181,6 +1181,9 @@ func flattenAzureRmVirtualMachineOsDisk(disk *compute.OSDisk, diskInfo *compute. if disk.Vhd != nil { result["vhd_uri"] = *disk.Vhd.URI } + if disk.Image != nil && disk.Image.URI != nil { + result["image_uri"] = *disk.Image.URI + } if disk.ManagedDisk != nil { result["managed_disk_type"] = string(disk.ManagedDisk.StorageAccountType) if disk.ManagedDisk.ID != nil { diff --git a/azurerm/resource_arm_virtual_machine_unmanaged_disks_test.go b/azurerm/resource_arm_virtual_machine_unmanaged_disks_test.go index c711ba8c7d5f..b27559639fd2 100644 --- a/azurerm/resource_arm_virtual_machine_unmanaged_disks_test.go +++ b/azurerm/resource_arm_virtual_machine_unmanaged_disks_test.go @@ -2,6 +2,7 @@ package azurerm import ( "fmt" + "regexp" "strings" "testing" @@ -102,6 +103,29 @@ func TestAccAzureRMVirtualMachine_basicLinuxMachine_disappears(t *testing.T) { }) } +func TestAccAzureRMVirtualMachine_basicLinuxMachineUseExistingOsDiskImage(t *testing.T) { + var vm, mirrorVm compute.VirtualMachine + ri := acctest.RandInt() + config := testAccAzureRMVirtualMachine_basicLinuxMachineUseExistingOsDiskImage(ri, testLocation()) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMVirtualMachineDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm), + testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.mirror", &mirrorVm), + testCheckAzureRMVirtualMachineVHDExistence("myosdisk1.vhd", true), + testCheckAzureRMVirtualMachineVHDExistence("mirrorosdisk.vhd", true), + resource.TestMatchResourceAttr("azurerm_virtual_machine.mirror", "storage_os_disk.0.image_uri", regexp.MustCompile("myosdisk1.vhd$")), + ), + }, + }, + }) +} + func TestAccAzureRMVirtualMachine_withDataDisk(t *testing.T) { var vm compute.VirtualMachine @@ -919,6 +943,50 @@ resource "azurerm_virtual_machine" "test" { `, rInt, location, rInt, rInt, rInt, rInt, rInt, rInt) } +func testAccAzureRMVirtualMachine_basicLinuxMachineUseExistingOsDiskImage(rInt int, location string) string { + baseConfig := testAccAzureRMVirtualMachine_basicLinuxMachine(rInt, location) + return fmt.Sprintf(`%s +resource "azurerm_network_interface" "mirror" { + name = "acctmirrorni-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + ip_configuration { + name = "testconfiguration1" + subnet_id = "${azurerm_subnet.test.id}" + private_ip_address_allocation = "dynamic" + } +} + +resource "azurerm_virtual_machine" "mirror" { + name = "acctmirrorvm-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + network_interface_ids = ["${azurerm_network_interface.mirror.id}"] + vm_size = "Standard_F2" + delete_os_disk_on_termination = false + + os_profile { + computer_name = "hnmirror%d" + admin_username = "testadmin" + admin_password = "Password1234!" + } + + os_profile_linux_config { + disable_password_authentication = false + } + + storage_os_disk { + name = "mirror" + image_uri = "${azurerm_virtual_machine.test.storage_os_disk.0.vhd_uri}" + vhd_uri = "${azurerm_storage_account.test.primary_blob_endpoint}${azurerm_storage_container.test.name}/mirrorosdisk.vhd" + create_option = "FromImage" + os_type = "Linux" + } +} +`, baseConfig, rInt, rInt, rInt) +} + func testAccAzureRMVirtualMachine_machineNameBeforeUpdate(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" {