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

Bugfix/fix 159 imagereplace v3 #177

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
14 changes: 3 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
module github.com/terraform-providers/terraform-provider-nutanix

require (
github.com/apparentlymart/go-cidr v1.1.0 // indirect
github.com/aws/aws-sdk-go v1.31.9 // indirect
github.com/client9/misspell v0.3.4
github.com/gogo/protobuf v1.2.2-0.20190723190241-65acae22fc9d // indirect
github.com/golangci/golangci-lint v1.25.0
github.com/hashicorp/go-getter v1.4.2-0.20200106182914-9813cbd4eb02 // indirect
github.com/hashicorp/go-plugin v1.3.0 // indirect
github.com/hashicorp/hcl/v2 v2.6.0 // indirect
github.com/hashicorp/terraform-config-inspect v0.0.0-20191212124732-c6ae6269b9d7 // indirect
github.com/hashicorp/terraform v0.13.5 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.7.0
github.com/mitchellh/gox v1.0.1
github.com/pkg/sftp v1.12.0 // indirect
github.com/spf13/cast v1.3.1
github.com/zclconf/go-cty v1.5.1 // indirect
github.com/zclconf/go-cty-yaml v1.0.2 // indirect
golang.org/x/net v0.0.0-20200602114024-627f9648deb9 // indirect
github.com/stretchr/testify v1.6.1 // indirect
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a // indirect
golang.org/x/tools v0.0.0-20200530233709-52effbd89c51 // indirect
)

Expand Down
245 changes: 188 additions & 57 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion nutanix/data_source_nutanix_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ func dataSourceNutanixVirtualMachineRead(d *schema.ResourceData, meta interface{
if err := d.Set("parent_reference", flattenReferenceValues(resp.Status.Resources.ParentReference)); err != nil {
return err
}
if err := d.Set("disk_list", flattenDiskList(resp.Status.Resources.DiskList)); err != nil {
if err := d.Set("disk_list", flattenDiskList(resp.Status.Resources.DiskList, resp.Status.Resources.DiskList)); err != nil {
return err
}

Expand Down
31 changes: 30 additions & 1 deletion nutanix/data_source_nutanix_virtual_machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestAccNutanixVirtualMachineDataSource_WithDisk(t *testing.T) {
}

func TestAccNutanixVirtualMachineDataSource_withDiskContainer(t *testing.T) {
t.Skip()
datasourceName := "data.nutanix_virtual_machine.nutanix_virtual_machine"
vmName := acctest.RandomWithPrefix("test-dou-vm")

Expand Down Expand Up @@ -89,10 +90,17 @@ func testAccVMDataSourceWithDiskContainer(vmName string) string {
memory_size_mib = 186

disk_list {
# disk_size_mib = 300
disk_size_bytes = 68157440
disk_size_mib = 65

device_properties {
device_type = "DISK"
disk_address = {
device_index = 0
adapter_type = "SCSI"
}
}

storage_config {
storage_container_reference {
kind = "storage_container"
Expand Down Expand Up @@ -172,14 +180,35 @@ func testAccVMDataSourceConfigWithDisk(r int) string {

disk_list {
disk_size_mib = 100
device_properties {
device_type = "DISK"
disk_address = {
device_index = 1
adapter_type = "IDE"
}
}
}

disk_list {
disk_size_mib = 200
device_properties {
device_type = "DISK"
disk_address = {
device_index = 2
adapter_type = "IDE"
}
}
}

disk_list {
disk_size_mib = 300
device_properties {
device_type = "DISK"
disk_address = {
device_index = 3
adapter_type = "IDE"
}
}
}
}

Expand Down
61 changes: 49 additions & 12 deletions nutanix/resource_nutanix_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
vmDelay = 3 * time.Second
vmMinTimeout = 3 * time.Second
IDE = "IDE"
CDROOMIndex = 3
useHotAdd = true
)

Expand Down Expand Up @@ -710,31 +711,26 @@ func resourceNutanixVirtualMachine() *schema.Resource {
},
"device_properties": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"device_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Required: true,
},
"disk_address": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"device_index": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Required: true,
},
"adapter_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Required: true,
},
},
},
Expand Down Expand Up @@ -956,7 +952,7 @@ func resourceNutanixVirtualMachineRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("error setting nic_list_status for Virtual Machine %s: %s", d.Id(), err)
}

if err := d.Set("disk_list", flattenDiskList(resp.Spec.Resources.DiskList)); err != nil {
if err := d.Set("disk_list", flattenDiskList(resp.Spec.Resources.DiskList, expandDiskList(d))); err != nil {
return fmt.Errorf("error setting disk_list for Virtual Machine %s: %s", d.Id(), err)
}

Expand Down Expand Up @@ -1277,6 +1273,16 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{
}

res.DiskList = expandDiskListUpdate(d, response)
imageMismatch := parseDiskImageChange(response, res.DiskList)

//check if a disk is removed.
if len(res.DiskList) < len(response.Status.Resources.DiskList) {
hotPlugChange = false
}

if imageMismatch {
hotPlugChange = false
}

postCdromCount, err := CountDiskListCdrom(res.DiskList)
if err != nil {
Expand Down Expand Up @@ -1763,7 +1769,8 @@ func expandDiskListUpdate(d *schema.ResourceData, vm *v3.VMIntentResponse) []*v3
if disk.DeviceProperties != nil && disk.DeviceProperties.DiskAddress != nil {
index := disk.DeviceProperties.DiskAddress.DeviceIndex
adapterType := disk.DeviceProperties.DiskAddress.AdapterType
if *index == 3 && *adapterType == IDE {
deviceType := disk.DeviceProperties.DeviceType
if *deviceType == "CDROM" && *index == 3 && *adapterType == IDE {
eDiskList = append(eDiskList, disk)
}
}
Expand Down Expand Up @@ -2101,6 +2108,36 @@ func setVMTimeout(meta interface{}) {
}
}

func parseDiskImageChange(vmOutput *v3.VMIntentResponse, expandedDiskList []*v3.VMDisk) bool {
foundImageMismatch := false
if vmOutput.Status.Resources.DiskList != nil {
currentDiskList := vmOutput.Status.Resources.DiskList
// Loop the disks to be updated via PUT
for _, nDisk := range expandedDiskList {
// check if disk uuid is not nil => is existing disk
if nDisk.UUID != nil {
// Loop over the current VM disks
for _, cDisk := range currentDiskList {
// perform a match... Need to check ndisk again since we put it to nil late rin the loop
if cDisk.UUID != nil && nDisk.UUID != nil && *nDisk.UUID == *cDisk.UUID {
// check if all attrs are not nil and compare the image UUIDs
if nDisk.DataSourceReference != nil &&
nDisk.DataSourceReference.UUID != nil &&
cDisk.DataSourceReference != nil &&
cDisk.DataSourceReference.UUID != nil &&
*cDisk.DataSourceReference.UUID != *nDisk.DataSourceReference.UUID {
// clear UUID so the API sees it as new disk
nDisk.UUID = nil
foundImageMismatch = true
}
}
}
}
}
}
return foundImageMismatch
}

func resourceNutanixVirtualMachineInstanceResourceV0() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down
Loading