VMware Cloud Community
AureusStone
Expert
Expert
Jump to solution

How do I sort VcHostSystem

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?

Reply
0 Kudos
1 Solution

Accepted Solutions
Andreas_Diemer
Enthusiast
Enthusiast
Jump to solution

Hi Aureus,

here a very small example for using in an action or workflow.

  • Input: ArrDS [Array/Datastore)
  • Output: DS [Datastore]

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

------ for correct and / or useful answers please award points visit http://www.vcoteam.info & http://mighty-virtualization.blogspot.com

View solution in original post

Reply
0 Kudos
8 Replies
Andreas_Diemer
Enthusiast
Enthusiast
Jump to solution

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

------ for correct and / or useful answers please award points visit http://www.vcoteam.info & http://mighty-virtualization.blogspot.com
Andreas_Diemer
Enthusiast
Enthusiast
Jump to solution

Hi Aureus,

here a very small example for using in an action or workflow.

  • Input: ArrDS [Array/Datastore)
  • Output: DS [Datastore]

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

------ for correct and / or useful answers please award points visit http://www.vcoteam.info & http://mighty-virtualization.blogspot.com
Reply
0 Kudos
AureusStone
Expert
Expert
Jump to solution

Awesome that worked perfectly. Smiley Happy  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. Smiley Happy

http://communities.vmware.com/docs/DOC-14684

Reply
0 Kudos
tschoergez
Leadership
Leadership
Jump to solution

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

AureusStone
Expert
Expert
Jump to solution

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.

Reply
0 Kudos
AureusStone
Expert
Expert
Jump to solution

Managed to get it working with an regular expression. Smiley Happy

Reply
0 Kudos
Andreas_Diemer
Enthusiast
Enthusiast
Jump to solution

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

------ for correct and / or useful answers please award points visit http://www.vcoteam.info & http://mighty-virtualization.blogspot.com
Reply
0 Kudos
AureusStone
Expert
Expert
Jump to solution

Thanks.  That is a real interesting site. Smiley Happy

Bookmarked. Smiley Happy

Reply
0 Kudos