implemented attaching existing managed disks on vm creation

This commit is contained in:
Brandon Tosch 2017-03-03 21:50:32 -08:00
parent a084aa08c2
commit ac8eae9eff
2 changed files with 170 additions and 36 deletions

View File

@ -152,7 +152,7 @@ func resourceArmVirtualMachine() *schema.Resource {
Optional: true, Optional: true,
Elem: &schema.Resource{ Elem: &schema.Resource{
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"id": { "managed_disk_id": {
Type: schema.TypeString, Type: schema.TypeString,
Optional: true, Optional: true,
ForceNew: true, ForceNew: true,
@ -221,6 +221,11 @@ func resourceArmVirtualMachine() *schema.Resource {
Optional: true, Optional: true,
Elem: &schema.Resource{ Elem: &schema.Resource{
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"managed_disk_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"storage_account_type": { "storage_account_type": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
@ -810,6 +815,9 @@ func resourceArmVirtualMachineStorageOsDiskHash(v interface{}) int {
if managedDisk["storage_account_type"] != nil { if managedDisk["storage_account_type"] != nil {
buf.WriteString(fmt.Sprintf("%s-", managedDisk["storage_account_type"].(string))) buf.WriteString(fmt.Sprintf("%s-", managedDisk["storage_account_type"].(string)))
} }
if managedDisk["managed_disk_id"] != nil {
buf.WriteString(fmt.Sprintf("%s-", managedDisk["managed_disk_id"].(string)))
}
} }
return hashcode.String(buf.String()) return hashcode.String(buf.String())
} }
@ -1272,16 +1280,18 @@ func expandAzureRmVirtualMachineDataDisk(d *schema.ResourceData) ([]compute.Data
Lun: &lun, Lun: &lun,
CreateOption: compute.DiskCreateOptionTypes(createOption), CreateOption: compute.DiskCreateOptionTypes(createOption),
} }
if vhd != "" && managedDisk != nil { if vhd != "" && len(managedDisk) != 0 {
return nil, fmt.Errorf("[ERROR] Conflict between `vhd_uri` and `managed_disk` (only one or the other can be used)") return nil, fmt.Errorf("[ERROR] Conflict between `vhd_uri` and `managed_disk` (only one or the other can be used)")
} else if vhd != "" { } else if vhd != "" {
data_disk.Vhd = &compute.VirtualHardDisk{ data_disk.Vhd = &compute.VirtualHardDisk{
URI: &vhd, URI: &vhd,
} }
} else if managedDisk != nil { } else if managedDisk != nil {
data_disk.ManagedDisk = expandAzureRmVirtualMachineManagedDisk(managedDisk) managedDisk, err := expandAzureRmVirtualMachineManagedDisk(managedDisk, &data_disk.CreateOption)
} else { if err != nil {
return nil, fmt.Errorf("[ERROR] A value must be specified for either `vhd_uri` or `managed_disk`") return nil, err
}
data_disk.ManagedDisk = managedDisk
} }
if v := config["caching"].(string); v != "" { if v := config["caching"].(string); v != "" {
@ -1372,14 +1382,18 @@ func expandAzureRmVirtualMachineOsDisk(d *schema.ResourceData) (*compute.OSDisk,
CreateOption: compute.DiskCreateOptionTypes(createOption), CreateOption: compute.DiskCreateOptionTypes(createOption),
} }
if vhdURI != "" { if vhdURI != "" && len(managedDisk) != 0 {
return nil, fmt.Errorf("[ERROR] Conflict between `vhd_uri` and `managed_disk` (only one or the other can be used)")
} else if vhdURI != "" {
osDisk.Vhd = &compute.VirtualHardDisk{ osDisk.Vhd = &compute.VirtualHardDisk{
URI: &vhdURI, URI: &vhdURI,
} }
} else if managedDisk != nil { } else if managedDisk != nil {
osDisk.ManagedDisk = expandAzureRmVirtualMachineManagedDisk(managedDisk) managedDisk, err := expandAzureRmVirtualMachineManagedDisk(managedDisk, &osDisk.CreateOption)
} else { if err != nil {
return nil, fmt.Errorf("[ERROR] must specify value for either vhd_uri or managed_disk") return nil, err
}
osDisk.ManagedDisk = managedDisk
} }
if v := disk["image_uri"].(string); v != "" { if v := disk["image_uri"].(string); v != "" {
@ -1410,11 +1424,19 @@ func expandAzureRmVirtualMachineOsDisk(d *schema.ResourceData) (*compute.OSDisk,
return osDisk, nil return osDisk, nil
} }
func expandAzureRmVirtualMachineManagedDisk(managedDisk map[string]interface{}) *compute.ManagedDiskParameters { func expandAzureRmVirtualMachineManagedDisk(managedDisk map[string]interface{}, createOption *compute.DiskCreateOptionTypes) (*compute.ManagedDiskParameters, error) {
managedDiskParameters := &compute.ManagedDiskParameters{ managedDiskParameters := &compute.ManagedDiskParameters{}
StorageAccountType: compute.StorageAccountTypes(managedDisk["storage_account_type"].(string)), if storageAccountType := managedDisk["storage_account_type"]; storageAccountType != nil {
managedDiskParameters.StorageAccountType = compute.StorageAccountTypes(storageAccountType.(string))
} }
return managedDiskParameters if managedDisk["managed_disk_id"] != nil {
managedDiskId := managedDisk["managed_disk_id"].(string)
managedDiskParameters.ID = &managedDiskId
}
if *createOption == compute.Attach && managedDiskParameters.ID == nil {
return nil, fmt.Errorf("[ERROR] A value is required for `managed_disk_id` when `create_option` is %s", compute.Attach)
}
return managedDiskParameters, nil
} }
func findStorageAccountResourceGroup(meta interface{}, storageAccountName string) (string, error) { func findStorageAccountResourceGroup(meta interface{}, storageAccountName string) (string, error) {

View File

@ -50,6 +50,25 @@ func TestAccAzureRMVirtualMachine_basicLinuxMachine_managedDisk(t *testing.T) {
}) })
} }
func TestAccAzureRMVirtualMachine_basicLinuxMachine_managedDisk_attach(t *testing.T) {
var vm compute.VirtualMachine
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMVirtualMachine_basicLinuxMachine_managedDisk_attach, ri, ri, ri, ri, ri, ri, ri)
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),
),
},
},
})
}
func TestAccAzureRMVirtualMachine_basicLinuxMachine_disappears(t *testing.T) { func TestAccAzureRMVirtualMachine_basicLinuxMachine_disappears(t *testing.T) {
var vm compute.VirtualMachine var vm compute.VirtualMachine
ri := acctest.RandInt() ri := acctest.RandInt()
@ -167,7 +186,7 @@ func TestAccAzureRMVirtualMachine_updateMachineSize(t *testing.T) {
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm), testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"azurerm_virtual_machine.test", "vm_size", "Standard_A0"), "azurerm_virtual_machine.test", "vm_size", "Standard_D1_v2"),
), ),
}, },
{ {
@ -175,7 +194,7 @@ func TestAccAzureRMVirtualMachine_updateMachineSize(t *testing.T) {
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm), testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"azurerm_virtual_machine.test", "vm_size", "Standard_A1"), "azurerm_virtual_machine.test", "vm_size", "Standard_D2_v2"),
), ),
}, },
}, },
@ -200,7 +219,7 @@ func TestAccAzureRMVirtualMachine_updateMachineSize_managedDisk(t *testing.T) {
Check: resource.ComposeTestCheckFunc( Check: resource.ComposeTestCheckFunc(
testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm), testCheckAzureRMVirtualMachineExists("azurerm_virtual_machine.test", &vm),
resource.TestCheckResourceAttr( resource.TestCheckResourceAttr(
"azurerm_virtual_machine.test", "vm_size", "Standard_A0"), "azurerm_virtual_machine.test", "vm_size", "Standard_D1_v2"),
), ),
}, },
{ {
@ -707,7 +726,7 @@ resource "azurerm_virtual_machine" "test" {
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
storage_image_reference { storage_image_reference {
publisher = "Canonical" publisher = "Canonical"
@ -778,7 +797,7 @@ resource "azurerm_virtual_machine" "test" {
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
storage_image_reference { storage_image_reference {
publisher = "Canonical" publisher = "Canonical"
@ -815,6 +834,99 @@ resource "azurerm_virtual_machine" "test" {
} }
` `
var testAccAzureRMVirtualMachine_basicLinuxMachine_managedDisk_attach = `
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "West US 2"
}
resource "azurerm_virtual_network" "test" {
name = "acctvn-%d"
address_space = ["10.0.0.0/16"]
location = "West US 2"
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 = "West US 2"
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_managed_disk" "test" {
name = "acctmd-%d"
location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}"
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = "1"
}
resource "azurerm_virtual_machine" "test" {
name = "acctvm-%d"
location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_D1_v2"
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "14.04.2-LTS"
version = "latest"
}
storage_os_disk {
name = "myosdisk1"
caching = "ReadWrite"
create_option = "FromImage"
disk_size_gb = "10"
managed_disk {
storage_account_type = "Standard_LRS"
}
}
storage_data_disk {
name = "${azurerm_managed_disk.test.name}"
create_option = "Attach"
disk_size_gb = "1"
lun = 0
managed_disk = {
managed_disk_id = "${azurerm_managed_disk.test.id}"
}
}
os_profile {
computer_name = "hostname%d"
admin_username = "testadmin"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = false
}
tags {
environment = "Production"
cost-center = "Ops"
}
}
`
var testAccAzureRMVirtualMachine_machineNameBeforeUpdate = ` var testAccAzureRMVirtualMachine_machineNameBeforeUpdate = `
resource "azurerm_resource_group" "test" { resource "azurerm_resource_group" "test" {
name = "acctestRG-%d" name = "acctestRG-%d"
@ -870,7 +982,7 @@ resource "azurerm_virtual_machine" "test" {
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
delete_os_disk_on_termination = true delete_os_disk_on_termination = true
storage_image_reference { storage_image_reference {
@ -964,7 +1076,7 @@ resource "azurerm_virtual_machine" "test" {
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
storage_image_reference { storage_image_reference {
publisher = "Canonical" publisher = "Canonical"
@ -1171,7 +1283,7 @@ resource "azurerm_virtual_machine" "test" {
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
storage_image_reference { storage_image_reference {
publisher = "Canonical" publisher = "Canonical"
@ -1353,7 +1465,7 @@ resource "azurerm_virtual_machine" "test" {
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
storage_image_reference { storage_image_reference {
publisher = "Canonical" publisher = "Canonical"
@ -1512,7 +1624,7 @@ resource "azurerm_virtual_machine" "test" {
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A1" vm_size = "Standard_D2_v2"
storage_image_reference { storage_image_reference {
publisher = "Canonical" publisher = "Canonical"
@ -1663,7 +1775,7 @@ resource "azurerm_virtual_machine" "test" {
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
storage_image_reference { storage_image_reference {
publisher = "MicrosoftWindowsServer" publisher = "MicrosoftWindowsServer"
@ -1747,7 +1859,7 @@ resource "azurerm_virtual_machine" "test" {
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
storage_image_reference { storage_image_reference {
publisher = "MicrosoftWindowsServer" publisher = "MicrosoftWindowsServer"
@ -1837,7 +1949,7 @@ resource "azurerm_virtual_machine" "test" {
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
storage_image_reference { storage_image_reference {
publisher = "MicrosoftWindowsServer" publisher = "MicrosoftWindowsServer"
@ -1928,7 +2040,7 @@ resource "azurerm_virtual_machine" "test" {
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
storage_image_reference { storage_image_reference {
publisher = "MicrosoftWindowsServer" publisher = "MicrosoftWindowsServer"
@ -2019,7 +2131,7 @@ var testAccAzureRMVirtualMachine_withAvailabilitySet = `
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
availability_set_id = "${azurerm_availability_set.test.id}" availability_set_id = "${azurerm_availability_set.test.id}"
delete_os_disk_on_termination = true delete_os_disk_on_termination = true
@ -2110,7 +2222,7 @@ var testAccAzureRMVirtualMachine_updateAvailabilitySet = `
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
availability_set_id = "${azurerm_availability_set.test.id}" availability_set_id = "${azurerm_availability_set.test.id}"
delete_os_disk_on_termination = true delete_os_disk_on_termination = true
@ -2195,7 +2307,7 @@ var testAccAzureRMVirtualMachine_updateMachineName = `
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
delete_os_disk_on_termination = true delete_os_disk_on_termination = true
storage_image_reference { storage_image_reference {
@ -2279,7 +2391,7 @@ resource "azurerm_virtual_machine" "test" {
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
delete_os_disk_on_termination = true delete_os_disk_on_termination = true
storage_image_reference { storage_image_reference {
@ -2369,7 +2481,7 @@ resource "azurerm_virtual_machine" "test" {
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
delete_os_disk_on_termination = true delete_os_disk_on_termination = true
storage_image_reference { storage_image_reference {
@ -2459,7 +2571,7 @@ resource "azurerm_virtual_machine" "test" {
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
storage_image_reference { storage_image_reference {
publisher = "Canonical" publisher = "Canonical"
@ -2548,7 +2660,7 @@ resource "azurerm_virtual_machine" "test" {
location = "West US 2" location = "West US 2"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
license_type = "Windows_Server" license_type = "Windows_Server"
storage_image_reference { storage_image_reference {
@ -2724,7 +2836,7 @@ resource "azurerm_virtual_machine" "test" {
location = "southcentralus" location = "southcentralus"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
storage_image_reference { storage_image_reference {
publisher = "Canonical" publisher = "Canonical"
@ -2808,7 +2920,7 @@ resource "azurerm_virtual_machine" "test" {
location = "southcentralus" location = "southcentralus"
resource_group_name = "${azurerm_resource_group.test.name}" resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${azurerm_network_interface.test.id}"] network_interface_ids = ["${azurerm_network_interface.test.id}"]
vm_size = "Standard_A0" vm_size = "Standard_D1_v2"
storage_image_reference { storage_image_reference {
publisher = "Canonical" publisher = "Canonical"