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

fix #130: Resize disk on clone #137

Merged
merged 6 commits into from
Jun 24, 2020
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
35 changes: 12 additions & 23 deletions nutanix/resource_nutanix_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1261,9 +1261,9 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{
if err != nil {
return err
}
if res.DiskList, err = expandDiskListUpdate(d, response); err != nil {
return err
}

res.DiskList = expandDiskListUpdate(d, response)

postCdromCount, err := CountDiskListCdrom(res.DiskList)
if err != nil {
return err
Expand Down Expand Up @@ -1643,11 +1643,11 @@ func getVMResources(d *schema.ResourceData, vm *v3.VMResources) error {
}
vm.SerialPortList = expandSerialPortList(d)

vmDiskList, err := expandDiskList(d, true)
vmDiskList := expandDiskList(d)

vm.DiskList = vmDiskList

return err
return nil
}

func expandNicList(d *schema.ResourceData) []*v3.VMNic {
Expand Down Expand Up @@ -1719,12 +1719,9 @@ func expandIPAddressList(ipl []interface{}) []*v3.IPAddress {
return nil
}

func expandDiskListUpdate(d *schema.ResourceData, vm *v3.VMIntentResponse) ([]*v3.VMDisk, error) {
var eDiskList []*v3.VMDisk
var err error
if eDiskList, err = expandDiskList(d, false); err != nil {
return eDiskList, err
}
func expandDiskListUpdate(d *schema.ResourceData, vm *v3.VMIntentResponse) []*v3.VMDisk {
eDiskList := expandDiskList(d)

if vm.Spec != nil && vm.Spec.Resources != nil {
for _, disk := range vm.Spec.Resources.DiskList {
if disk.DeviceProperties != nil && disk.DeviceProperties.DiskAddress != nil {
Expand All @@ -1736,17 +1733,16 @@ func expandDiskListUpdate(d *schema.ResourceData, vm *v3.VMIntentResponse) ([]*v
}
}
}
return eDiskList, nil
return eDiskList
}

func expandDiskList(d *schema.ResourceData, isCreation bool) ([]*v3.VMDisk, error) {
func expandDiskList(d *schema.ResourceData) []*v3.VMDisk {
if v, ok := d.GetOk("disk_list"); ok {
dsk := v.([]interface{})
if len(dsk) > 0 {
dls := make([]*v3.VMDisk, len(dsk))

for k, val := range dsk {
hasDSRef := false
v := val.(map[string]interface{})
dl := &v3.VMDisk{}

Expand All @@ -1764,7 +1760,6 @@ func expandDiskList(d *schema.ResourceData, isCreation bool) ([]*v3.VMDisk, erro
}
// data_source_reference
if v1, ok := v["data_source_reference"]; ok && len(v1.(map[string]interface{})) != 0 {
hasDSRef = true
dsref := v1.(map[string]interface{})
dl.DataSourceReference = validateShortRef(dsref)
}
Expand All @@ -1775,24 +1770,18 @@ func expandDiskList(d *schema.ResourceData, isCreation bool) ([]*v3.VMDisk, erro
}
// disk_size_bytes
if v1, ok1 := v["disk_size_bytes"]; ok1 && v1.(int) != 0 {
if hasDSRef && isCreation {
return nil, fmt.Errorf(`"disk_list.%[1]d.disk_size_bytes": conflicts with disk_list.%[1]d.data_source_reference`, k)
}
dl.DiskSizeBytes = utils.Int64Ptr(int64(v1.(int)))
}
// disk_size_mib
if v1, ok := v["disk_size_mib"]; ok && v1.(int) != 0 {
if hasDSRef && isCreation {
return nil, fmt.Errorf(`"disk_list.%[1]d.disk_size_mib": conflicts with disk_list.%[1]d.data_source_reference`, k)
}
dl.DiskSizeMib = utils.Int64Ptr(int64(v1.(int)))
}
dls[k] = dl
}
return dls, nil
return dls
}
}
return nil, nil
return nil
}

func expandStorageConfig(storageConfig []interface{}) *v3.VMStorageConfig {
Expand Down
73 changes: 73 additions & 0 deletions nutanix/resource_nutanix_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ func TestAccNutanixVirtualMachine_withDiskContainer(t *testing.T) {
r := acctest.RandInt()

resourceName := "nutanix_virtual_machine.vm-disk"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand All @@ -381,6 +382,44 @@ func TestAccNutanixVirtualMachine_withDiskContainer(t *testing.T) {
}})
}

func TestAccNutanixVirtualMachine_resizeDiskClone(t *testing.T) {
resourceName := "nutanix_virtual_machine.vm"
imgName := acctest.RandomWithPrefix("test-dou-IMG")
vmName := acctest.RandomWithPrefix("test-dou-VM")
diskSize := 90 * 1024 * 1024
diskSizeUpdated := 90 * 1024 * 1024 * 1024

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNutanixVirtualMachineDestroy,
Steps: []resource.TestStep{
{
Config: testAccNutanixVMConfigResizeDiskClone(imgName, vmName, diskSize),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixVirtualMachineExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", vmName),
resource.TestCheckResourceAttr(resourceName, "power_state", "ON"),
resource.TestCheckResourceAttr(resourceName, "memory_size_mib", "186"),
resource.TestCheckResourceAttr(resourceName, "num_sockets", "1"),
resource.TestCheckResourceAttr(resourceName, "num_vcpus_per_socket", "1"),
),
},
{
Config: testAccNutanixVMConfigResizeDiskClone(imgName, vmName, diskSizeUpdated),
Check: resource.ComposeTestCheckFunc(
testAccCheckNutanixVirtualMachineExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", vmName),
resource.TestCheckResourceAttr(resourceName, "power_state", "ON"),
resource.TestCheckResourceAttr(resourceName, "memory_size_mib", "186"),
resource.TestCheckResourceAttr(resourceName, "num_sockets", "1"),
resource.TestCheckResourceAttr(resourceName, "num_vcpus_per_socket", "1"),
),
},
},
})
}

func testAccCheckNutanixVirtualMachineExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -979,3 +1018,37 @@ func testAccNutanixVMConfigWithDiskContainer(r int) string {
}
`, r)
}

func testAccNutanixVMConfigResizeDiskClone(imgName, vmName string, diskSize int) string {
return fmt.Sprintf(`
data "nutanix_clusters" "clusters" {}

locals {

cluster1 = "${data.nutanix_clusters.clusters.entities.0.service_list.0 == "PRISM_CENTRAL"
? data.nutanix_clusters.clusters.entities.1.metadata.uuid : data.nutanix_clusters.clusters.entities.0.metadata.uuid}"
}

resource "nutanix_image" "img" {
name = "%s"
source_uri = "http://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img"
description = "heres a tiny linux image, not an iso, but a real disk!"
}

resource "nutanix_virtual_machine" "vm" {
name = "%s"
num_vcpus_per_socket = 1
num_sockets = 1
memory_size_mib = 186
cluster_uuid = "${local.cluster1}"

disk_list {
data_source_reference = {
kind = "image"
uuid = nutanix_image.img.id
}
disk_size_bytes = %d
}
}
`, imgName, vmName, diskSize)
}