This code for a scriptable task will first get all the available datastores of a given cluster, then select only those where the name is matching a given pattern and then select the first one with enough space.
INPUTS:
vmCluster (VC:ClusterComputeResource) //Your Cluster
storageSum (number) //A previously calculated value for the needed storage
OUTPUTS:
vmDatastore (VC:Datastore) //A datastore with enough free space
CODE:
allStorage = vmCluster.datastore;
var vmStorage = new Array();
var storageName;
for (ii in allStorage)
{
storageName = allStorage[ii].name
if (storageName.substring(6,9) == "CLS")
{
vmStorage.push(allStorage[ii]);
}
}
var ii = 0;var vmStorageInfo;
var vmStorageLength = vmStorage.length;
while(vmDatastore == null && ii < vmStorageLength)
{//extract storageinfo and compare free space to needed space
vmStorageInfo = vmStorage[ii].info;
if ((vmStorageInfo.freeSpace / 1073741824) >= storageSum) //1073741824 = 1024*1024*1024 = 1GB{
vmDatastore = vmStorage[ii];
}
ii++;
}
If you have any questions or suggestions, feel free to comment.
Regards
Andreas
Hi Andreas,
var allStorage = new Array();
var allStorage = vmCluster.datastore; //this line initialize the array also
var vmStorage = new Array();
var ii = 0;
var kk = 0;
var storageName;
for each (VcDatastore in allStorage)
for (ii in allStorage)
{
//extract the name of the storage and compare part of it to a string
storageName = allStorage[ii].name
if (storageName.substring(6,9) == "CLS")
{
vmStorage.push(allStorage[ii]);
kk++;
}
ii++;
}
Less code, more readable
Regards, Andreas
Hi Andreas,
i wrote it in about 15 minutes and it worked for me. Didn't have the time to optimize.
Oh and i didn't know, how to iterate without the ii when using a for each.
And i did forget about the push-method.
Thanks for the corrections
Hi Andreas,
no problem - I don't want to teach you - just to give some hints, because it is a community document and so it should / could be a blueprint for other user / starters.
Regards, Andreas
i am trying to implement this but i feel like im missing a few steps...i create a scriptable task and enter this info...is the storagesum a parameter or attribute?
Aramaki, I would like to use this workflow, but I am very new to vOrch and JavaScript. I understand that you are using the substring call to specifically look for "CLS." That is too specific for my environment today. Do you have a sample line that I can use to do a pattern matching (regex) way to find the appropriate DS. For example I would like to match on something like this: "vm_"_"*"_ where we pass variables ba=business area, perftier=performance tier, and continuitytier=continuity tier.
To add to this it might be helpful for some users to take a piece off the top of the datastore. By that I mean, sometimes datastores cannot be filled to capacity to account for certain apps or backup processes. Therefore something like
(((vmStorageInfo.freeSpace - (vmStorageInfo.freeSpace * 0.10)) / 1073741824) >= storageSum))
would make sure that you always maintain 10% of the datastore to reserve for other running processes.