When I first started with vRO I struggled with how to use elements that the plugins discover. It was really a struggle because I was used to PowerShell elements. The real challenge was figuring out how to use the elements provided by the plugins. The easiest way to explain how to mine this information is an example. For example, I want to generate some basic information on all my datastores from all my connected vCenters. We want to gather capacity, free space, percent free space and name.
We start by creating a basic workflow with and drag over a scriptable task:
Edit the scriptable task and you will be presented with:
This allows you to review the elements provided by the plugins to vRO. Let’s do a quick search for datastores using the magnifying glass. We locate a number of methods, attributes and scripting classes.
Inside here it’s important to understand the difference between Method, attribute, scripting class
- Method – output’s an object of attributes
- Attribute – specific data element
- Scripting Class – Collections of objects and their available methods
For this section we are going to choose vcplugin.getAllDatastores() and choose to go to selection which will change our top pane to:
This tells us that this method will return an array of the object VcDatastore. To see the individual elements in VcDatastore click on the blue VcDatastore
Each of the individual key value’s are listed you can click them for additional information. Let’s create a array of object VcDatastore
var datastores = VcPlugin.getAllDatastores();
Now let’s identify the values I want
- Name
- Capacity
- Free Space
- Percent free
Browsing the available values in the list I locate name which is the datastore name. Let’s loop through the object’s and write all the names out to log.
var datastores = VcPlugin.getAllDatastores(); for each (datastore in datastores){ System.log("Datastore : " + datastore.name); }
Output on my home lab looks like this:
It worked. Now we have to locate capacity of drive. As you browse the available fields you will notice that some fields like info and summary return objects with additional fields. You can click the blue link to learn more about available information for example summary returns object VcDatastoreSummary which if clicked has a ton of values:
Including two fields I want capacity and freespace. Lets make sure they are correct with some easy code inserted inside our loop:
System.log("Capacity : " + datastore.summary.capacity + " Free space : " + datastore.summary.freeSpace);
The output from the whole thing combined looks like this:
As you can see I have my required information. The data is really not human readable so I want to create a function at the top to convert the data into GB’s. Like this:
function convert_to_gb(size) { gbsize = size/1024/1024/1024; return gbsize; }
And let’s add it to our output inline:
System.log("Capacity : " + convert_to_gb(datastore.summary.capacity) + " Free space : " + convert_to_gb(datastore.summary.freeSpace));
Now my output is a lot more readable:
But wait I hate all that decimal point mess lets just round it up
System.log("Capacity : " + Math.ceil(convert_to_gb(datastore.summary.capacity)) + " Free space : " + Math.ceil(convert_to_gb(datastore.summary.freeSpace)));
Now the output looks much better:
Now we just need a percent of free space. This one is not built in but it’s easy math. (freespace / capacity *100 = percent free) let’s do it inline:
System.log("Percent free : " + Math.ceil((datastore.summary.freeSpace/datastore.summary.capacity)*100));
The output has everything we need.
Now I understand that outputting this to log does not help you at all. But from here you can feed this information into arrays or objects to be passed to additional workflow items. I hope it helps you understand how to work with the methods provided. If you want the whole script download it here: datastore
Hi , Thanks for good article.
Have you used “listDatastores” which requires host name?
When I give host as string, it gives me error. I think it requires input as type VC:hostname and I don’t know how to send this from vRA property.
Thank you
Srini
Great question. It does require the object. You need to create an object of VC:Hostname’s then limit down to your specific host or hosts. I think you will find some helpful code to this process in this article http://blog.jgriffiths.org/?p=1240