VMware Cloud Community
raphael83
Contributor
Contributor
Jump to solution

Convert string to VC:Network

Hi all,

Can someone help me how to convert a String to VC:NetworkType?


I'm receiving the portgroup name as a string via POST and I need to convert it to a VC:Network on my Workflow.

I tried the "GetNetworksForLabel" action but it return only Standard portgroup. I need to find the Dvs Portgroups.


Thanks!

Reply
0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

Perhaps the easiest way is to fetch all DVS portgroups and find which one matches the desired name. Here is some sample code:

var pgName = "dvPortGroup123"; // portgroup name to find

var allDvpg = Server.findAllForType("VC:DistributedVirtualPortgroup");

var foundDvpg = null;

for each (var dvpg in allDvpg) {

  if (dvpg.name == pgName) {

    foundDvpg = dvpg;

    break;

  }

}

if (foundDvpg != null) {

  System.log("Found matching DVS portgroup -> " + foundDvpg);

}

One thing I'm not sure and that should be validated if whether the portgroup name alone is sufficient to uniquely identify the DVS portgroup (eg. if you have multiple vCenters managed by single vRO instance, is it possible to have DVS portgroups with the same name in each vCenter)?. If this is possible, in production code you may want to use additional criteria to distinguish between DVS portgroups having the same name.

View solution in original post

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

Hi,

Perhaps the easiest way is to fetch all DVS portgroups and find which one matches the desired name. Here is some sample code:

var pgName = "dvPortGroup123"; // portgroup name to find

var allDvpg = Server.findAllForType("VC:DistributedVirtualPortgroup");

var foundDvpg = null;

for each (var dvpg in allDvpg) {

  if (dvpg.name == pgName) {

    foundDvpg = dvpg;

    break;

  }

}

if (foundDvpg != null) {

  System.log("Found matching DVS portgroup -> " + foundDvpg);

}

One thing I'm not sure and that should be validated if whether the portgroup name alone is sufficient to uniquely identify the DVS portgroup (eg. if you have multiple vCenters managed by single vRO instance, is it possible to have DVS portgroups with the same name in each vCenter)?. If this is possible, in production code you may want to use additional criteria to distinguish between DVS portgroups having the same name.

Reply
0 Kudos
raphael83
Contributor
Contributor
Jump to solution

Hi iilievIlian Iliev,

Thank you for your answer. It was a good advice but I still can't get the dvs Portgroup.

I tried to change the :

  1. var allDvpg = Server.findAllForType("VC:DistributedVirtualPortgroup"); 

to:

  1. var allDvpg = Server.findAllForType("VC:Network"); 

and find the Standar Portgroup, it work perfectly but when I tried to find a dvs Portgroup it doesn't work.

any ideia?

thanks

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

By 'doesn't work' do you mean you are getting some errors, or just you are getting empty/no results?

Which vRO/vCenter versions do you use? I tried the sample code in my environment - vRO 7.0.1 + vCenter 6.0 + vCenter 5.1. It worked and returned the existing DistributedVirtualPortgroup objects.

Reply
0 Kudos
raphael83
Contributor
Contributor
Jump to solution

Hi,

     No errors. Just getting no results.

     The search is working, when I removed the condition, the script shows all dvs Portgroups.

var allDvpg = Server.findAllForType("VC:DistributedVirtualPortgroup");

System.log(allDvpg);

     The script returned all dvs Portgroup in this format:

DynamicWrapper (Instance) : [VcDistributedVirtualPortgroup]-[class com.vmware.vmo.plugin.vi4.model.VimDistributedVirtualPortgroup] -- VALUE : DistributedVirtualPortgroup<dvportgroup-426>'PORTGROUPNAME'

     But when I try to find a specific portgroup name, it shows nothing.

     I'm using vRO 6.0.2+vCenter 5.5

thanks

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Yes, that's the proper format when you log a DVS portgroup object.

So the script finds the portgroups, but among them is no portgroup with a name you search for, is that correct? Then how do you know the name is correct? Do you see a DVS portgroup with this name in vRO inventory tree?

Reply
0 Kudos
raphael83
Contributor
Contributor
Jump to solution

Finally! It works!!

The problems was how I was entering the string. I need to enter "DistributedPortgroupName (dvSwitchName)" not only DistributedPortgroupName. Just how it is in vRO inventory tree, like you said.

Thank you very much! Smiley Happy

Reply
0 Kudos
Hazenet
Enthusiast
Enthusiast
Jump to solution

If you do

dvpg.config.name

you get the actual name of the DVS Portgroup, instead of the "display name" in vRO.

Reply
0 Kudos
raphael83
Contributor
Contributor
Jump to solution

Hi HazenetMads Fog Albrechtslund

     This also worked.

   

Thank you!

Reply
0 Kudos