From 5e904f7ea67d7e2d3e0beee13080d58901abc0ca Mon Sep 17 00:00:00 2001 From: Thiago Caiubi Date: Mon, 7 Aug 2017 16:34:16 -0300 Subject: [PATCH 1/4] Force recreation if storage_data_disk.creation_option changes Azure Resource Manager API doesn't support changes to this field. It should recreate the virtual machine and attach the disks again. module.xpto.azurerm_virtual_machine.prometheus_virtual_machine: Modifying... (ID: /subscriptions/xxxxx...ute/virtualMachines/vm) delete_data_disks_on_termination: "" => "false" delete_os_disk_on_termination: "" => "true" storage_data_disk.0.create_option: "Empty" => "Attach" storage_data_disk.1.create_option: "Empty" => "Attach" tags.%: "0" => "1" tags.role: "" => "xpto" Error applying plan: 1 error(s) occurred: * module.xpto.azurerm_virtual_machine.vm: 1 error(s) occurred: * azurerm_virtual_machine.prometheus_virtual_machine: compute.VirtualMachinesClient#CreateOrUpdate: Failure responding to request: StatusCode=409 -- Original Error: autorest/azure: Service returned an error. Status=409 Code="PropertyChangeNotAllowed" Message="Changing property 'dataDisk.createOption' is not allowed." --- azurerm/resource_arm_virtual_machine.go | 1 + ..._arm_virtual_machine_managed_disks_test.go | 111 ++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/azurerm/resource_arm_virtual_machine.go b/azurerm/resource_arm_virtual_machine.go index 4cabbf3e99b2..c3b9455a40f3 100644 --- a/azurerm/resource_arm_virtual_machine.go +++ b/azurerm/resource_arm_virtual_machine.go @@ -245,6 +245,7 @@ func resourceArmVirtualMachine() *schema.Resource { "create_option": { Type: schema.TypeString, Required: true, + ForceNew: true, DiffSuppressFunc: ignoreCaseDiffSuppressFunc, }, diff --git a/azurerm/resource_arm_virtual_machine_managed_disks_test.go b/azurerm/resource_arm_virtual_machine_managed_disks_test.go index 28915a897fb4..68fe38934cbc 100644 --- a/azurerm/resource_arm_virtual_machine_managed_disks_test.go +++ b/azurerm/resource_arm_virtual_machine_managed_disks_test.go @@ -225,6 +225,33 @@ func TestAccAzureRMVirtualMachine_bugAzureRM33(t *testing.T) { }) } +func TestAccAzureRMVirtualMachine_changeStorageDataDiskCreationOption(t *testing.T) { + var afterCreate, afterUpdate compute.VirtualMachine + ri := acctest.RandInt() + preConfig := testAccAzureRMVirtualMachine_basicLinuxMachine_managedDisk_empty(ri, testLocation()) + postConfig := testAccAzureRMVirtualMachine_basicLinuxMachine_managedDisk_attach(ri, testLocation()) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMVirtualMachineDestroy, + Steps: []resource.TestStep{ + { + Config: preConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &afterCreate), + ), + }, + { + Config: postConfig, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &afterUpdate), + testAccCheckVirtualMachineRecreated(t, &afterCreate, &afterUpdate), + ), + }, + }, + }) +} + func testAccAzureRMVirtualMachine_basicLinuxMachine_managedDisk_explicit(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { @@ -460,6 +487,90 @@ resource "azurerm_virtual_machine" "test" { `, rInt, location, rInt, rInt, rInt, rInt, rInt, rInt, rInt) } +func testAccAzureRMVirtualMachine_basicLinuxMachine_managedDisk_empty(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_virtual_network" "test" { + name = "acctvn-%d" + address_space = ["10.0.0.0/16"] + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" +} + +resource "azurerm_subnet" "test" { + name = "acctsub-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + virtual_network_name = "${azurerm_virtual_network.test.name}" + address_prefix = "10.0.2.0/24" +} + +resource "azurerm_network_interface" "test" { + name = "acctni-%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" "test" { + name = "acctvm-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + network_interface_ids = ["${azurerm_network_interface.test.id}"] + vm_size = "Standard_D1_v2" + + delete_os_disk_on_termination = true + delete_data_disks_on_termination = true + + storage_image_reference { + publisher = "Canonical" + offer = "UbuntuServer" + sku = "14.04.2-LTS" + version = "latest" + } + + storage_os_disk { + name = "osd-%d" + caching = "ReadWrite" + create_option = "FromImage" + disk_size_gb = "50" + managed_disk_type = "Standard_LRS" + } + + storage_data_disk { + name = "acctmd-%d" + create_option = "Empty" + disk_size_gb = "1" + managed_disk_type = "Standard_LRS" + lun = 0 + } + + os_profile { + computer_name = "hn%d" + admin_username = "testadmin" + admin_password = "Password1234!" + } + + os_profile_linux_config { + disable_password_authentication = false + } + + tags { + environment = "Production" + cost-center = "Ops" + } +} +`, rInt, location, rInt, rInt, rInt, rInt, rInt, rInt, rInt) +} + func testAccAzureRMVirtualMachine_basicLinuxMachine_managedDisk_DestroyDisksBefore(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { From 2fa502a563879277305608f0170c3d4d43e29614 Mon Sep 17 00:00:00 2001 From: Thiago Caiubi Date: Thu, 10 Aug 2017 13:49:27 -0300 Subject: [PATCH 2/4] Remove disk deletion --- azurerm/resource_arm_virtual_machine_managed_disks_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/azurerm/resource_arm_virtual_machine_managed_disks_test.go b/azurerm/resource_arm_virtual_machine_managed_disks_test.go index 68fe38934cbc..2bc856dcd787 100644 --- a/azurerm/resource_arm_virtual_machine_managed_disks_test.go +++ b/azurerm/resource_arm_virtual_machine_managed_disks_test.go @@ -528,7 +528,6 @@ resource "azurerm_virtual_machine" "test" { vm_size = "Standard_D1_v2" delete_os_disk_on_termination = true - delete_data_disks_on_termination = true storage_image_reference { publisher = "Canonical" From 97ffc67718a5fd4b3012c8424c834e12c256fdda Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Fri, 11 Aug 2017 12:04:48 +0100 Subject: [PATCH 3/4] Upgrading to ubuntu 16 --- ..._arm_virtual_machine_managed_disks_test.go | 126 +++++++++--------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/azurerm/resource_arm_virtual_machine_managed_disks_test.go b/azurerm/resource_arm_virtual_machine_managed_disks_test.go index 2bc856dcd787..222d7cb750c0 100644 --- a/azurerm/resource_arm_virtual_machine_managed_disks_test.go +++ b/azurerm/resource_arm_virtual_machine_managed_disks_test.go @@ -490,82 +490,82 @@ resource "azurerm_virtual_machine" "test" { func testAccAzureRMVirtualMachine_basicLinuxMachine_managedDisk_empty(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { - name = "acctestRG-%d" - location = "%s" + name = "acctestRG-%d" + location = "%s" } resource "azurerm_virtual_network" "test" { - name = "acctvn-%d" - address_space = ["10.0.0.0/16"] - location = "${azurerm_resource_group.test.location}" - resource_group_name = "${azurerm_resource_group.test.name}" + name = "acctvn-%d" + address_space = ["10.0.0.0/16"] + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" } resource "azurerm_subnet" "test" { - name = "acctsub-%d" - resource_group_name = "${azurerm_resource_group.test.name}" - virtual_network_name = "${azurerm_virtual_network.test.name}" - address_prefix = "10.0.2.0/24" + name = "acctsub-%d" + resource_group_name = "${azurerm_resource_group.test.name}" + virtual_network_name = "${azurerm_virtual_network.test.name}" + address_prefix = "10.0.2.0/24" } resource "azurerm_network_interface" "test" { - name = "acctni-%d" - location = "${azurerm_resource_group.test.location}" - resource_group_name = "${azurerm_resource_group.test.name}" + name = "acctni-%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" - } + ip_configuration { + name = "testconfiguration1" + subnet_id = "${azurerm_subnet.test.id}" + private_ip_address_allocation = "dynamic" + } } resource "azurerm_virtual_machine" "test" { - name = "acctvm-%d" - location = "${azurerm_resource_group.test.location}" - resource_group_name = "${azurerm_resource_group.test.name}" - network_interface_ids = ["${azurerm_network_interface.test.id}"] - vm_size = "Standard_D1_v2" - - delete_os_disk_on_termination = true - - storage_image_reference { - publisher = "Canonical" - offer = "UbuntuServer" - sku = "14.04.2-LTS" - version = "latest" - } - - storage_os_disk { - name = "osd-%d" - caching = "ReadWrite" - create_option = "FromImage" - disk_size_gb = "50" - managed_disk_type = "Standard_LRS" - } - - storage_data_disk { - name = "acctmd-%d" - create_option = "Empty" - disk_size_gb = "1" - managed_disk_type = "Standard_LRS" - lun = 0 - } - - os_profile { - computer_name = "hn%d" - admin_username = "testadmin" - admin_password = "Password1234!" - } - - os_profile_linux_config { - disable_password_authentication = false - } - - tags { - environment = "Production" - cost-center = "Ops" - } + name = "acctvm-%d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + network_interface_ids = ["${azurerm_network_interface.test.id}"] + vm_size = "Standard_D1_v2" + + delete_os_disk_on_termination = true + + storage_image_reference { + publisher = "Canonical" + offer = "UbuntuServer" + sku = "16.04-LTS" + version = "latest" + } + + storage_os_disk { + name = "osd-%d" + caching = "ReadWrite" + create_option = "FromImage" + disk_size_gb = "50" + managed_disk_type = "Standard_LRS" + } + + storage_data_disk { + name = "acctmd-%d" + create_option = "Empty" + disk_size_gb = "1" + managed_disk_type = "Standard_LRS" + lun = 0 + } + + os_profile { + computer_name = "hn%d" + admin_username = "testadmin" + admin_password = "Password1234!" + } + + os_profile_linux_config { + disable_password_authentication = false + } + + tags { + environment = "Production" + cost-center = "Ops" + } } `, rInt, location, rInt, rInt, rInt, rInt, rInt, rInt, rInt) } From 6281b3ac7b4dd3695b69fdf00c3f94d123ee183a Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Fri, 11 Aug 2017 13:36:36 +0100 Subject: [PATCH 4/4] Verifying the create type --- ...esource_arm_virtual_machine_managed_disks_test.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/azurerm/resource_arm_virtual_machine_managed_disks_test.go b/azurerm/resource_arm_virtual_machine_managed_disks_test.go index 222d7cb750c0..aece85256717 100644 --- a/azurerm/resource_arm_virtual_machine_managed_disks_test.go +++ b/azurerm/resource_arm_virtual_machine_managed_disks_test.go @@ -227,9 +227,11 @@ func TestAccAzureRMVirtualMachine_bugAzureRM33(t *testing.T) { func TestAccAzureRMVirtualMachine_changeStorageDataDiskCreationOption(t *testing.T) { var afterCreate, afterUpdate compute.VirtualMachine + resourceName := "azurerm_virtual_machine.test" ri := acctest.RandInt() - preConfig := testAccAzureRMVirtualMachine_basicLinuxMachine_managedDisk_empty(ri, testLocation()) - postConfig := testAccAzureRMVirtualMachine_basicLinuxMachine_managedDisk_attach(ri, testLocation()) + location := testLocation() + preConfig := testAccAzureRMVirtualMachine_basicLinuxMachine_managedDisk_empty(ri, location) + postConfig := testAccAzureRMVirtualMachine_basicLinuxMachine_managedDisk_attach(ri, location) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -238,14 +240,16 @@ func TestAccAzureRMVirtualMachine_changeStorageDataDiskCreationOption(t *testing { Config: preConfig, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &afterCreate), + testCheckAzureRMVirtualMachineExists(resourceName, &afterCreate), + resource.TestCheckResourceAttr(resourceName, "storage_data_disk.0.create_option", "Empty"), ), }, { Config: postConfig, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &afterUpdate), + testCheckAzureRMVirtualMachineExists(resourceName, &afterUpdate), testAccCheckVirtualMachineRecreated(t, &afterCreate, &afterUpdate), + resource.TestCheckResourceAttr(resourceName, "storage_data_disk.0.create_option", "Attach"), ), }, },