VMware Cloud Community
dharmendrarhce
Enthusiast
Enthusiast
Jump to solution

Java Scripts in VRO action

Hi ,

I created a java script which will to get network profile value as string .  but it is not working .i am not getting any value return .

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

if (clusterName == null || tenantName == null) return ;

var networkProfiles ;

var host = vCACCAFEHostManager.getDefaultHostForTenant(tenantName, true);

var reservations = vCACCAFEEntitiesFinder.getReservations(host);

var res;

for each (reservation in reservations) {

if (reservation.name == "Res-" + clusterName) res = reservation;

}

var data = res.getExtensionData();

var reservationNetworks = data.get("reservationNetworks");

for each(network in reservationNetworks.getValue()) {

if(network.getValue().get("networkProfile").label.indexOf('BUN') == 0) {

        var backupNetworkProfile = network.getValue().get("networkProfile").label;

        break

   

}

return networkProfiles;

}

Previously i was able to get value as Array not as a string . Can someone help ?

0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Here is the fixed code (I haven't ran/tested it, but it should work, assuming your code works for array):

if (clusterName == null || tenantName == null) return "";

var networkProfiles = [];

var host = vCACCAFEHostManager.getDefaultHostForTenant(tenantName, true);

var reservations = vCACCAFEEntitiesFinder.getReservations(host);

var res;

for each(reservation in reservations) {

    if (reservation.name == "Res-" + clusterName) res = reservation;

}

var data = res.getExtensionData();

var reservationNetworks = data.get("reservationNetworks");

for each(network in reservationNetworks.getValue()) {

    if (network.getValue().get("networkProfile").label.indexOf('BUN') == 0) {

        networkProfiles.push(network.getValue().get("networkProfile").label);

    }

}

if (networkProfiles.length < 1) return "";

return networkProfiles[0];

A couple of remarks:

1) If the inputs are incorrect (line 1) or there are no network profiles found (line 19), the script returns an empty string "". Depending on your use case, you may change this behavior to either return null value or throw an error.

2) If more than one network profile is found (line 20), the script returns the first one. Again, depending on your use case, you may consider the fact of having multiple profiles to be an error, and instead of returning the first profile, throw an error.

View solution in original post

13 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

There are too many errors in the code so it's normal for it to not work.

1) You said the action returns string, but the return statement on the first line doesn't return a value compatible with string type

2) The only other return statement in the code is near the end, but the value of networkProfiles variable is not populated anywhere in the code

3) I don't understand the purpose of the last for each loop. How many iterations this loop can have? As it's written, there will be exactly one iteration, and the loop (and action) will complete either because of the break operator (in this case the action won't have any return statements), or either because of the return statement (in this case the action will return unpopulated networkProfiles variable)

0 Kudos
dharmendrarhce
Enthusiast
Enthusiast
Jump to solution

Actually this script has been used with array , which return me a drop down menu to select the avilable Network profile from a reservation .

I have to modify the  code for BAU network profile which is backup network profile . One reservation is having only one BAU network profile .

I need  just a string value instead of array values .

Could you please fix it

0 Kudos
dharmendrarhce
Enthusiast
Enthusiast
Jump to solution

I was using below script which is returning the vallue in array , I just need value in string instead of array , Could you please fix that

if (clusterName == null || tenantName == null) return [];

var networkProfiles = [];

var host = vCACCAFEHostManager.getDefaultHostForTenant(tenantName, true);

var reservations = vCACCAFEEntitiesFinder.getReservations(host);

var res; for each (reservation in reservations) { if (reservation.name == "Res-" + clusterName) res = reservation;

}

var data = res.getExtensionData(); var reservationNetworks = data.get("reservationNetworks");

for each(network in reservationNetworks.getValue()) { if(network.getValue().get("networkProfile").label.indexOf('BUN') == 0) {

networkProfiles.push(network.getValue().get("networkProfile").label);

}

}

return networkProfiles

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Here is the fixed code (I haven't ran/tested it, but it should work, assuming your code works for array):

if (clusterName == null || tenantName == null) return "";

var networkProfiles = [];

var host = vCACCAFEHostManager.getDefaultHostForTenant(tenantName, true);

var reservations = vCACCAFEEntitiesFinder.getReservations(host);

var res;

for each(reservation in reservations) {

    if (reservation.name == "Res-" + clusterName) res = reservation;

}

var data = res.getExtensionData();

var reservationNetworks = data.get("reservationNetworks");

for each(network in reservationNetworks.getValue()) {

    if (network.getValue().get("networkProfile").label.indexOf('BUN') == 0) {

        networkProfiles.push(network.getValue().get("networkProfile").label);

    }

}

if (networkProfiles.length < 1) return "";

return networkProfiles[0];

A couple of remarks:

1) If the inputs are incorrect (line 1) or there are no network profiles found (line 19), the script returns an empty string "". Depending on your use case, you may change this behavior to either return null value or throw an error.

2) If more than one network profile is found (line 20), the script returns the first one. Again, depending on your use case, you may consider the fact of having multiple profiles to be an error, and instead of returning the first profile, throw an error.

dharmendrarhce
Enthusiast
Enthusiast
Jump to solution

Hi ,

Thanks for your response, really appreciate that. This code worked but it is returning two action result

Untitled.jpg

Actionresult ---- Array/string-------------- Blank

ActionResult ---  String -------------------  Values

How can i remove that 1st action result , i do not know from where is it calling . Could you please help .

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

You need to inspect the workflow's inputs/outputs/attributes/visual bindings to see where it is defined and to delete it. Sorry, I cannot help without having access to the whole workflow.

0 Kudos
dharmendrarhce
Enthusiast
Enthusiast
Jump to solution

Thanks for your response.

This is a VRO action code , so I can not define or bind the output

This action will be call in xaas blueprint

Where is catalog page this string value will populate .

Your code is working fine but only it is getting another return in array , if you can track which return have array . We can change that .

I really appreciate if you can help.

Is Second last line returning that array value in return.?

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Inputs/outputs/attributes/bindings are not defined via scripting code. You need to open the workflow in vRO Java UI client and fix them there.

0 Kudos
dharmendrarhce
Enthusiast
Enthusiast
Jump to solution

Thanks for your response, so you mean to say that we can not make any changes to remove extra  return array ??

Action result should be only one right ?

I am able to get the single result value in array if I use only till network profiles.push

I know you can only help in this just need to find which statement is retuning the result in array , that we can delete .

I am sorry to asking u again , I am just learning vCO and Java.

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

I cannot make any changes because I don't have this workflow. It is you who can make the change and remove this return array.

There is no statement in the code which is returning the array. Even if you remove all the code, the array will remain. That's why I'm saying that you need to use vRO Java client to find where is this array defined and delete it.

0 Kudos
dharmendrarhce
Enthusiast
Enthusiast
Jump to solution

As per your 1st remark of you code .

If no network profile found it return blank value

Is that value in array or string ?

I think this 19 line is returning the blank result along with main result

Can I use else after if stement so might be it return only one action result .

Any help

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

The variables shown on the screenshot in update# 5 ARE NOT DEFINED IN THE CODE.

Forget about the code. Just open the workflow for edit in vRO Java client and look around. There is a lot more stuff than the code; check the other tabs like Inputs, Outputs, General, Visual Bindings (this one is available on the items in the workflow schema). The array is defined somewhere there, not in the code.

0 Kudos
dharmendrarhce
Enthusiast
Enthusiast
Jump to solution

yes, You are correct . it worked , I just created a new action and in that use that code .

Thanks much for you support .

0 Kudos