VMware Cloud Community
salil3
Contributor
Contributor
Jump to solution

Searching for datastore based on name

Hi

As part of the provision workflow I need to validate if the given datastore name is already in use (i.e. datastore with the same name already exist).

I can see that VcPlugin has an api, getAllDatastores() that takes filter object. Not sure how to use this filter object.

Can you please help me with the syntax for finding a datastore based on the name using filter object? In case there is other way to figure this out, instead of re-iterating through the list of all datastores in the system please let me know.

Thanks and Regards,

Salil

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Here is a sample code to dump all datastores whose name starts with a given prefix:

var prefix = "local_";

var xpath = "xpath:name[starts-with(.,'" + prefix + "')]";

var allDs = VcPlugin.getAllDatastores(null, xpath);

for each (var ds in allDs) {

  System.log(ds.name);

}

So, you need to prepare a XPath expression with your filtering criteria and pass it as a second parameter to method getAllDatastores(). Of course, instead of searching for datastore names prefix, you can prepare a XPath for other filtering criteria, eg. find exact match, find suffix, etc.

View solution in original post

0 Kudos
4 Replies
jacksonecac
Enthusiast
Enthusiast
Jump to solution

The best solution is to minimize your query to your subset of objects. For example if you are looking at a particular host... cluster... etc. You would want to use that criteria. //Case 1 have cluster.
//clusterObject is an input parameter
//DataStoreName is an input parameter
var dataStoreObject = null;
var hosts = clusterObject.host;
for each(var h in hosts)
{
    datastores = h.datastore;
    for each(var d in datastores)
    {
            if(d.name == DataStoreName)
                dataStoreObject = d;         
     }
}
//Case 2 have host
//hostObject is an input parameter
//DataStoreName is an input parameter
var dataStoreObject = null;
datastores = hostObject.datastore;
    for each(var d in datastores)
    {
            if(d.name == DataStoreName)
                dataStoreObject = d;         
     }
//Case 3 you have neither
var dataStoreObject = null;
var datastores = VcPlugin.getAllDatastores(DataStoreName); //Note this returns an array of found datastores
for each(var d in datastores)
{
    if(d.name == DataStoreName)
          dataStoreObject = d;
}
The getAllDatastores accepts a string, which is typically a regex filter however you can simply use the name and it will find it as well.

0 Kudos
salil3
Contributor
Contributor
Jump to solution

Thanks for you prompt response!!

I tried case 3, passing datastore name to VcPlugin.getAllDatastores(datastoreName) method, execution failed with the below error. Based on the error message tried passing the datastore name as array of String but it returns all  datastores without applying any filter.

[2016-10-18 04:34:39.692] [E] Error in (Workflow:Temp / Scriptable task (item1)#5) Cannot convert bbbb to java.lang.String[]

[2016-10-18 04:34:39.720] [E] Workfow execution stack:

***

item: 'Temp/item1', state: 'failed', business state: 'null', exception: 'Cannot convert bbbb to java.lang.String[] (Workflow:Temp / Scriptable task (item1)#5)'

workflow: 'Temp' (816b3915-dee8-4a6d-b121-b2e55d8a5e8c)

|  'input': name=datastoreName type=string value=bbbb

|  'no outputs'

|  'no attributes'

*** End of execution stack.

Here is the script

==============

var ds  = VcPlugin.getAllDatastores(datastoreName);

for each(var i in ds ) {

   System.log(i.name);

}

Thanks and Regards,

Salil

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Here is a sample code to dump all datastores whose name starts with a given prefix:

var prefix = "local_";

var xpath = "xpath:name[starts-with(.,'" + prefix + "')]";

var allDs = VcPlugin.getAllDatastores(null, xpath);

for each (var ds in allDs) {

  System.log(ds.name);

}

So, you need to prepare a XPath expression with your filtering criteria and pass it as a second parameter to method getAllDatastores(). Of course, instead of searching for datastore names prefix, you can prepare a XPath for other filtering criteria, eg. find exact match, find suffix, etc.

0 Kudos
salil3
Contributor
Contributor
Jump to solution

Thanks for you reply!!

I tried this and it worked. Just to check for exact datastore name I modified the xpath statement to below.

var xpath = "xpath:name='" + datastoreName + "'";           

Thanks once again!!

Salil

0 Kudos