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/issue 285 cloudinit v2 #292

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
78 changes: 1 addition & 77 deletions nutanix/resource_nutanix_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,88 +132,12 @@ resource "nutanix_role" "test" {
description = "%[2]s"
permission_reference_list {
kind = "permission"
uuid = "d08ea95c-8221-4590-a77a-52d69639959a"
}
permission_reference_list {
kind = "permission"
uuid = "1a8a65c0-4333-42c6-9039-fd2585ceead7"
}
permission_reference_list {
kind = "permission"
uuid = "bea75573-e8fe-42a3-817a-bd1bd98ab110"
}
permission_reference_list {
kind = "permission"
uuid = "93e1cc93-d799-4f44-84ad-534814f6db0d"
}
permission_reference_list {
kind = "permission"
uuid = "62f53a1a-324c-4da6-bcb8-2cecc07b2cb7"
}
permission_reference_list {
kind = "permission"
uuid = "62f53a1a-324c-4da6-bcb8-2cecc07b2cb7"
}
permission_reference_list {
kind = "permission"
uuid = "ef38a553-a20f-4a2b-b12d-bb9cca03cbdd"
}
permission_reference_list {
kind = "permission"
uuid = "6e768a07-21ef-4615-84d0-7ec442ec942f"
}
permission_reference_list {
kind = "permission"
uuid = "91b77724-b163-473f-94a8-d016e75c18bd"
}
permission_reference_list {
kind = "permission"
uuid = "491ae1d0-5a8f-4bcc-9cee-068cd01c9274"
}
permission_reference_list {
kind = "permission"
uuid = "1dbfb7b4-9896-4c2a-b6fe-fbf113bae306"
}
permission_reference_list {
kind = "permission"
uuid = "740d29f7-18ae-4d07-aeef-3fc901c1887a"
}
permission_reference_list {
kind = "permission"
uuid = "b28f35be-6561-4a4a-9d90-a298d2de33d7"
}
permission_reference_list {
kind = "permission"
uuid = "428fad6c-8735-4a7d-bad3-8497bef051c8"
}
permission_reference_list {
kind = "permission"
uuid = "ea445ec5-f9bb-4af6-92e8-0d72d11ada85"
}
permission_reference_list {
kind = "permission"
uuid = "85a24ad8-67b6-4b63-b30f-96da1baca161"
}
permission_reference_list {
kind = "permission"
uuid = "d370823b-82d8-4518-a486-b75ba8e130d6"
}
permission_reference_list {
kind = "permission"
uuid = "2e9988df-47ae-44ae-9114-ada346657b90"
uuid = "16b81a55-2bca-48c6-9fab-4f82c6bb4284"
}
permission_reference_list {
kind = "permission"
uuid = "4e8e9007-8fbe-4709-a069-278259238e55"
}
permission_reference_list {
kind = "permission"
uuid = "2e9988df-47ae-44ae-9114-ada346657b90"
}
permission_reference_list {
kind = "permission"
uuid = "2e9988df-47ae-44ae-9114-ada346657b90"
}
}
`, name, description)
}
Expand Down
125 changes: 81 additions & 44 deletions nutanix/resource_nutanix_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ var (

func resourceNutanixVirtualMachine() *schema.Resource {
return &schema.Resource{
Create: resourceNutanixVirtualMachineCreate,
Read: resourceNutanixVirtualMachineRead,
Update: resourceNutanixVirtualMachineUpdate,
Delete: resourceNutanixVirtualMachineDelete,
Exists: resourceNutanixVirtualMachineExists,
Create: resourceNutanixVirtualMachineCreate,
Read: resourceNutanixVirtualMachineRead,
Update: resourceNutanixVirtualMachineUpdate,
Delete: resourceNutanixVirtualMachineDelete,
Exists: resourceNutanixVirtualMachineExists,
CustomizeDiff: resourceNutanixVirtualMachineDiff,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Expand Down Expand Up @@ -666,6 +667,7 @@ func resourceNutanixVirtualMachine() *schema.Resource {
"disk_list": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"uuid": {
Expand Down Expand Up @@ -1313,7 +1315,6 @@ func resourceNutanixVirtualMachineUpdate(d *schema.ResourceData, meta interface{
return err
}
res.DiskList = expandDiskListUpdate(d, response)

postCdromCount, err := CountDiskListCdrom(res.DiskList)
if err != nil {
return err
Expand Down Expand Up @@ -1846,48 +1847,53 @@ func expandDiskListUpdate(d *schema.ResourceData, vm *v3.VMIntentResponse) []*v3

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))
return expandDiskListRaw(v)
}
return nil
}

for k, val := range dsk {
v := val.(map[string]interface{})
dl := &v3.VMDisk{}
func expandDiskListRaw(diskList interface{}) []*v3.VMDisk {
dsk := diskList.([]interface{})
if len(dsk) > 0 {
dls := make([]*v3.VMDisk, len(dsk))

// uuid
if v1, ok1 := v["uuid"]; ok1 && v1.(string) != "" {
dl.UUID = utils.StringPtr(v1.(string))
}
// storage_config
if v, ok1 := v["storage_config"]; ok1 {
dl.StorageConfig = expandStorageConfig(v.([]interface{}))
}
// device_properties
if v1, ok1 := v["device_properties"]; ok1 {
dl.DeviceProperties = expandDeviceProperties(v1.([]interface{}))
}
// data_source_reference
if v1, ok := v["data_source_reference"]; ok && len(v1.(map[string]interface{})) != 0 {
dsref := v1.(map[string]interface{})
dl.DataSourceReference = validateShortRef(dsref)
}
// volume_group_reference
if v1, ok := v["volume_group_reference"]; ok {
volgr := v1.(map[string]interface{})
dl.VolumeGroupReference = validateRef(volgr)
}
// disk_size_bytes
if v1, ok1 := v["disk_size_bytes"]; ok1 && v1.(int) != 0 {
dl.DiskSizeBytes = utils.Int64Ptr(int64(v1.(int)))
}
// disk_size_mib
if v1, ok := v["disk_size_mib"]; ok && v1.(int) != 0 {
dl.DiskSizeMib = utils.Int64Ptr(int64(v1.(int)))
}
dls[k] = dl
for k, val := range dsk {
v := val.(map[string]interface{})
dl := &v3.VMDisk{}

// uuid
if v1, ok1 := v["uuid"]; ok1 && v1.(string) != "" {
dl.UUID = utils.StringPtr(v1.(string))
}
// storage_config
if v, ok1 := v["storage_config"]; ok1 {
dl.StorageConfig = expandStorageConfig(v.([]interface{}))
}
return dls
// device_properties
if v1, ok1 := v["device_properties"]; ok1 {
dl.DeviceProperties = expandDeviceProperties(v1.([]interface{}))
}
// data_source_reference
if v1, ok := v["data_source_reference"]; ok && len(v1.(map[string]interface{})) != 0 {
dsref := v1.(map[string]interface{})
dl.DataSourceReference = validateShortRef(dsref)
}
// volume_group_reference
if v1, ok := v["volume_group_reference"]; ok {
volgr := v1.(map[string]interface{})
dl.VolumeGroupReference = validateRef(volgr)
}
// disk_size_bytes
if v1, ok1 := v["disk_size_bytes"]; ok1 && v1.(int) != 0 {
dl.DiskSizeBytes = utils.Int64Ptr(int64(v1.(int)))
}
// disk_size_mib
if v1, ok := v["disk_size_mib"]; ok && v1.(int) != 0 {
dl.DiskSizeMib = utils.Int64Ptr(int64(v1.(int)))
}
dls[k] = dl
}
return dls
}
return nil
}
Expand All @@ -1903,6 +1909,7 @@ func expandStorageConfig(storageConfig []interface{}) *v3.VMStorageConfig {
URL: cast.ToString(scr["url"]),
Kind: cast.ToString(scr["kind"]),
UUID: cast.ToString(scr["uuid"]),
Name: cast.ToString(scr["name"]),
},
}
}
Expand Down Expand Up @@ -2185,6 +2192,36 @@ func setVMTimeout(meta interface{}) {
}
}

func resourceNutanixVirtualMachineDiff(d *schema.ResourceDiff, m interface{}) error {
if cloudInitCdromUUID, ok := d.GetOk("cloud_init_cdrom_uuid"); !ok {
usesGuestCustomization := usesGuestCustomizationDiff(d)
if usesGuestCustomization {
stateFileDiskList, apiDiskList := d.GetChange("disk_list")
newlyGeneratedDiff := expandDiskListRaw(stateFileDiskList)
apiDiskListExpanded := expandDiskListRaw(apiDiskList)
cloudInitHelper, _, err := flattenDiskListFilterCloudInitHelper(cloudInitCdromUUID.(string), usesGuestCustomization, apiDiskListExpanded, newlyGeneratedDiff, false)
if err != nil {
return err
}
newDiskList := make([]interface{}, 0)
added := false
for i, d := range apiDiskList.([]interface{}) {
if i == cloudInitHelper.Index && cloudInitHelper.UUID != "" {
newDiskList = append(newDiskList, flattenDisk(cloudInitHelper.CloudInitCDrom))
added = true
}
newDiskList = append(newDiskList, d)
}
if !added && cloudInitHelper.UUID != "" {
newDiskList = append(newDiskList, flattenDisk(cloudInitHelper.CloudInitCDrom))
}
d.SetNew("cloud_init_cdrom_uuid", cloudInitHelper.UUID)
d.SetNew("disk_list", newDiskList)
}
}
return nil
}

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