VMware Cloud Community
WJPConway
Contributor
Contributor
Jump to solution

xpath filtering VcPlugin.getAllDatacenters(); VcPlugin.getAllClusterComputeResources()

Hello all,

I have a workflow that is creating Datacenters and Clusters in a vCenter.

It takes inputs through csv file and I use the a loop through the functions VcPlugin.allSdkConnections; VcPlugin.getAllDatacenters(); VcPlugin.getAllClusterComputeResources() to see if the vCenter, Datacenter or Cluster exists and creates it if it doesn't. (except for vCenter obviously)

This is all working fine - however now I have an additional requirement in the form of there will be multiple vCenters connected to vRO - and its possible that the Datacenter/Cluster exists but not in the right vCenter. I know I can use xpath to filter as per documentation below but am struggling to get it to work and don't fully get the examples the documentation includes.

VMware vRealize Orchestrator 7.2 Documentation Center

This is my basic code

//Datacenter Section

var value1 = "SomeDatacenter"

var allDcs = VcPlugin.getAllDatacenters();

System.log ("Datacenter Requested = " + value1);

for (var i in allDcs) { 

//Check to see if the Datacenter Exists

  if (allDcs[i].name.match(new RegExp(value1, "i"))) { 

//Datacenter exists set the DatacenterExists attribute to true

DatacenterExists = "True";

  }

//Create Datacenter here

}

//Cluster Section

var value2 = "SomeCluster"

var allClus = VcPlugin.getAllClusterComputeResources();

System.log ("Cluster Requested = " + value2);

for (var i in allClus) { 

//Check to see if the Cluster Exists

  if (allClus[i].name.match(new RegExp(value2, "i"))) { 

//Cluster exists set the ClusterExists attribute to true

ClusterExists = "True";

  }

//Create Cluster here

}

For Step 1 The filter would ensure that the returned Datacenter from VcPlugin.getAllDatacenters(); would be from the right vCenter,

For Step 2 The filter would ensure that the returned Clusters from VcPlugin.getAllClusterComputeResources(); would be from the right Datacenter and vCenter,

Reply
0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

I'm not sure that xpath filtering will help in this case.

The good news is that you don't need to use VcPlugin.getAllDatacenters() or VcPlugin.getAllClusterComputeResources(). what you need is a few nested loops like in the following pseudo code:

for each (var vcenter in VcPlugin.allSdkConnections) { // iterate over all vCenter servers

    for each (var datacenter in vcenter.allDatacenters) { // iterate over all datacenters inside the given vCenter

        var clusters = getClustersInDatacenter(datacenter); // get all clusters inside the given datacenter

        for each (var cluster in clusters) {

            // use the cluster object

        }

    }

}

That is, you won't iterate over all possible vCenters/datacenters/clusters; you would iterate over the objects within the desired scope, so no filtering is needed.

What will remain in the code above is to implement the function getClustersInDatacenter(datacenter) to return Array/VC:ClusterComputeResource with all the clusters inside the given datacenter. Here is some sample code for vRO scripting action to do so:

var result = new Array();

findComputeResourceInHostFolder(datacenter.hostFolder);

return result;

function findComputeResourceInHostFolder (hostFolder) {

  for each (var item in hostFolder.childEntity) {

    if (item instanceof VcClusterComputeResource) {

      result.push(item);

    }

  }

  for each (var folder in hostFolder.childEntity) {

    if (folder instanceof VcFolder) {

      findComputeResourceInHostFolder(folder);

    }

  }

}

View solution in original post

Reply
0 Kudos
2 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

I'm not sure that xpath filtering will help in this case.

The good news is that you don't need to use VcPlugin.getAllDatacenters() or VcPlugin.getAllClusterComputeResources(). what you need is a few nested loops like in the following pseudo code:

for each (var vcenter in VcPlugin.allSdkConnections) { // iterate over all vCenter servers

    for each (var datacenter in vcenter.allDatacenters) { // iterate over all datacenters inside the given vCenter

        var clusters = getClustersInDatacenter(datacenter); // get all clusters inside the given datacenter

        for each (var cluster in clusters) {

            // use the cluster object

        }

    }

}

That is, you won't iterate over all possible vCenters/datacenters/clusters; you would iterate over the objects within the desired scope, so no filtering is needed.

What will remain in the code above is to implement the function getClustersInDatacenter(datacenter) to return Array/VC:ClusterComputeResource with all the clusters inside the given datacenter. Here is some sample code for vRO scripting action to do so:

var result = new Array();

findComputeResourceInHostFolder(datacenter.hostFolder);

return result;

function findComputeResourceInHostFolder (hostFolder) {

  for each (var item in hostFolder.childEntity) {

    if (item instanceof VcClusterComputeResource) {

      result.push(item);

    }

  }

  for each (var folder in hostFolder.childEntity) {

    if (folder instanceof VcFolder) {

      findComputeResourceInHostFolder(folder);

    }

  }

}

Reply
0 Kudos
WJPConway
Contributor
Contributor
Jump to solution

This makes a lot more sense and is a better approach. Thanks Ilian.

Reply
0 Kudos