Hi,
I am new to both Orchestrator and javascript. I am attempting to build a script to automate building virtuals in my environment.
I am having a bit of trouble with sorting.
I can't seem to work out how to sort VcHostSystem.datastore.summary.freeSpace
I would like to be able to select the lun with the most free space. This is really easy in PowerCLI, but I can't work it out in Orchestrator.
Does any one know?
Hi Aureus,
here a very small example for using in an action or workflow.
DS is set to the Datastore in ArrDS with most free space. There is no error handling and no distinction of datastore type (local / shared)!
function sortDS (a, b)
{
return (a.summary.freeSpace < b.summary.freeSpace);
}ArrDS.sort(sortDS);
DS = ArrDS[0];
If you look in vCO API browser Array->sort you will find a very short explanation of implementing sort.
If you are using sort without any function like sort() the array members are sorted by there primary type. In case of scalars (number, string, ...) this will work fine. But on complex types like datastore you want to sort on a special criteria and therefore you have to define an own sort function.
Hope this is heplful.
Regards, Andreas
Hi Aureus,
just use the sort method of Array, by implementing an adapted function sort(). Simple Example could be found here: http://mighty-virtualization.blogspot.com/2010/09/vco-sorting-arrays-in-vmware.html
If you want, I can build you a small example for this datastore sorting.
Regards, Andreas
Hi Aureus,
here a very small example for using in an action or workflow.
DS is set to the Datastore in ArrDS with most free space. There is no error handling and no distinction of datastore type (local / shared)!
function sortDS (a, b)
{
return (a.summary.freeSpace < b.summary.freeSpace);
}ArrDS.sort(sortDS);
DS = ArrDS[0];
If you look in vCO API browser Array->sort you will find a very short explanation of implementing sort.
If you are using sort without any function like sort() the array members are sorted by there primary type. In case of scalars (number, string, ...) this will work fine. But on complex types like datastore you want to sort on a special criteria and therefore you have to define an own sort function.
Hope this is heplful.
Regards, Andreas
Awesome that worked perfectly. Thanks Andreas.
One other question. How do I filter out certain LUN names. I want it to look at only the LUNs with "Production" in the name.
Actually. Looks like this in my answer.
yeah, have a look at this discussion:
http://communities.vmware.com/thread/301381
You could use the xpath-filter for VC.getAllDatastores(), but there is not really documentation about it 😞
The other way: You can use the Javascript String comparison and find mehtods to filter your array for certain names.
There are match() and search()-methods for Strings, which can be used with the (very powerful) Regular expressions.
Some infos:
http://www.regular-expressions.info/javascriptexample.html
http://www.w3schools.com/js/js_obj_regexp.asp
Regards,
Joerg
I have attempted to follow the link but it doesn't seem to be working for me
bestHost is just a VC:HostSystem var that points to the host that I am going to build on.
var allStorage = bestHost.datastore;
var vmStorage = new Array();
car storageName;
for (ii in allStorage)
{
storageName = allStorage[ii].name
if (storageName.subsring(14,24) == "Production")
{
vmStorage.push(allStorage[ii]);
}
}
Which is pretty much copy paste from the example
I will have a look at those links.
Managed to get it working with an regular expression.
Hi,
after this and struggeling again through RegExp (im Native .NET) I wrote down some memos for RegExp in vCO:
http://mighty-virtualization.blogspot.com/2011/02/vco-use-and-examples-of-regexp-regular.html
Regards, Andreas
Thanks. That is a real interesting site.
Bookmarked.