I have an array of disks on a VM and I want to create a property set of Hard Disk Name and Hard Disk UUID. But when I print out the properties set it only prints out the last Hard Disk Information Below is my code:
for each (var vmdkInfo in vmdkDisks){
input = vmdkDisks (array of vm disks (lets say 3))
var vmdkProperties = new Properties ();
vmdkProperties.put("Hard Disk Name", vmdkInfo.deviceInfo.label);
vmdkProperties.put("VMDK UUID", vmdkInfo.backing.uuid);
}
System.log(vmdkProperties);
current output
<string>Hard disk 3</string>
</property>
<string>6000C298-eb25-7aab-86f8-423565cd28e9</string>
</property>
There are 2 issues in your code:
1) var vmdkProperties = new Properties (); should be moved before the 'for each' loop. Right now, you are trying to instantiate a new properties object in each loop iteration.
2) In each loop iteration, you put 2 new values (label and uuid) using the same keys. Putting a value with the same key during the next iteration will override the value you put during the previous iterations. Keys have to be unique.
Something like the following code should work:
var vmdkProperties = new Properties();
var uniquekey = 1;
for each (var vmdkInfo in vmdkDisks) {
vmdkProperties.put(uniquekey++, {"Hard Disk Name":vmdkInfo.label, "VMDK UUID":vmdkInfo.uuid});
}
In the above sample:
uniquekey++
generates a new unique value for each loop iteration (1, 2, 3, ...)
{"Hard Disk Name":vmdkInfo.label, "VMDK UUID":vmdkInfo.uuid}
creates a new Javascript object with 2 attributes ("Hard Disk Name" and "VMDK UUID") and assigns them the corresponding values.
There are 2 issues in your code:
1) var vmdkProperties = new Properties (); should be moved before the 'for each' loop. Right now, you are trying to instantiate a new properties object in each loop iteration.
2) In each loop iteration, you put 2 new values (label and uuid) using the same keys. Putting a value with the same key during the next iteration will override the value you put during the previous iterations. Keys have to be unique.
Something like the following code should work:
var vmdkProperties = new Properties();
var uniquekey = 1;
for each (var vmdkInfo in vmdkDisks) {
vmdkProperties.put(uniquekey++, {"Hard Disk Name":vmdkInfo.label, "VMDK UUID":vmdkInfo.uuid});
}
In the above sample:
uniquekey++
generates a new unique value for each loop iteration (1, 2, 3, ...)
{"Hard Disk Name":vmdkInfo.label, "VMDK UUID":vmdkInfo.uuid}
creates a new Javascript object with 2 attributes ("Hard Disk Name" and "VMDK UUID") and assigns them the corresponding values.
Thank You so Much
Hello,
I had one more question for some reason I am unable to pull out those properties in to an array of values Do you know why that is? I am only attempting to do this because for some reason when somebody is trying to do a getResult api call from a soap services it returns a hash like map of properties instead of a clean version like it does with a rest Call Do you know why that is?