VMware {code} Community
comiscience
Contributor
Contributor

How can i get VLAN ID of the port group of virtual Swtich?

Hello

How can i get VLAN ID of the port group of virtual Swtich?

Object cmobj
                        = cb.getServiceUtil().GetDynamicProperty(hostmor, "configManager");
                    HostConfigManager configMgr = (HostConfigManager)cmobj;
                    ManagedObjectReference nwSystem = configMgr.networkSystem;
                    HostNetworkInfo netInfo = (HostNetworkInfo)cb.getServiceUtil().GetDynamicProperty(nwSystem,
                                                                        "networkInfo");
                    Console.WriteLine("=======Configuration - Mise en réseau");
                    for (int i = 0; i < netInfo.vswitch.Length; i++)
                    {
                        Console.WriteLine(netInfo.vswitch[i].name);
                        for (int j = 0; j < netInfo.vswitch[i].portgroup.Length; j++)
                        {
                            Console.WriteLine("  " + netInfo.vswitch[i].portgroup[j]);
                        }
                    }
I have code above, i can read virtual switches, and its port group name. But how can i get VLAN ID?
Thanks
0 Kudos
9 Replies
togtog
Hot Shot
Hot Shot

Hi comiscience!

As you already have data object HostNetworkInfo at hand I would suggest the following:

     HostNetworkInfo.portgroup[].spec.vlanId

Hope this is what you are looking for ...

Thomas G.
0 Kudos
comiscience
Contributor
Contributor

Hi, togtog

I really wish it could help me.. But it cannot help me. Because there isn't a property "vlandId" in HostNetworkInfo.portgroup[].spec

0 Kudos
comiscience
Contributor
Contributor

http://poshcode.org/?show=872

Follow the example above, i found that we can get vlan id by HostNetworkConfig

HostNetworkConfig netConfig = (HostNetworkConfig)cb.getServiceUtil().GetDynamicProperty(nwSystem,
                                                                        "networkConfig");
0 Kudos
togtog
Hot Shot
Hot Shot

Hi comiscience!

Very strange, over here I can see property HostNetworkInfo.portgroup[].spec.vlanId - at least in the managed object browser. I have not tried to access it programmatically, but think this should be equivalent. I include some screenshots which show the vlanId property. HostNetworkConfig would have been my second choice as you had HostNetworkInfo already at hand.

The screen shots have been taken from the managed object browser of a "VMware ESXi 4.1.0 build-502767" install.

Thomas G.
comiscience
Contributor
Contributor

I've no idea about that. Because i don't know very well about VMware system...

But i use VMare-vSphere-SDK-5.0.0-429209, is it maybe the reason?

0 Kudos
togtog
Hot Shot
Hot Shot

Hi comiscience!

I do not think that VMware-vSphere-SDK-5.0.0-429209 is the culprit, though I have not checked. In order to make life easier I am using VI Java written by Steve Jin to access vSphere systems. Especially if you are not that familiar with VMware it might be worth taking a look at it. Steve has defined a clean and complete client side POJO based object model for accessing vSphere objects.

With respect to the screen shots. I have taken them from the so called "Managed Object Browser (MOB)" - a genius web based front to browse the inventory of a vCenter server or an ESX/ESXi server. I can only encourage you to get accustomed to it as it is a tremendous help to quickly unterstand how your application sees the contents of the inventory. In order to access the MOB simply open a web browser and type http://your.vcenter-server.hostname or http://your.esx-server.hostname - for sure you can also simply type the IP addresses if available. You will then be presented the vSphere or ESX/ESXi welcome page. At the right bottom corner you will find a link which enables you to browse the managed objects. Once clicked you have to provide your credentials. Use the same as in your code.

In order to check that the vlanId property is acutally available I would suggest looking for it in the MOB. If you can find it there it is likely that there is something wrong with your code.

By the way how do you exactly try to access the missing vlanId in your code?

Thomas G.
comiscience
Contributor
Contributor

Hi

Thanks for your suggestion. I think i should follow your suggestions and really have a look about what you say.

And in fact, i just changed a little bit of code of sample of "VMware-vSphere-SDK-5.0.0-429209\SDK\vsphere-ws\dotnet\"

In the sample "RemoveVirtualSwitch",

(I'm sorry i cannot give you a capture of my code because of problemes of my computer) But here is the code which i changed

ManagedObjectReference dcmor;
            ManagedObjectReference hostfoldermor;
            ManagedObjectReference hostmor = null;
            datacenter = cb.get_option("datacenter");
            host = cb.get_option("host");
            try
            {
                if (((datacenter != null) && (host != null))
                    || ((datacenter != null) && (host == null)))
                {
                    dcmor
                        = cb.getServiceUtil().GetDecendentMoRef(null, "Datacenter", datacenter);
                    if (dcmor == null)
                    {
                        Console.WriteLine("Datacenter not found");
                        return;
                    }
                    hostfoldermor = vmUtils.getHostFolder(dcmor);
                    hostmor = vmUtils.getHost(hostfoldermor, host);
                }
                else if ((datacenter == null) && (host != null))
                {
                    hostmor = vmUtils.getHost(null, host);
                }
                if (hostmor != null)
                {
                    Object cmobj
                        = cb.getServiceUtil().GetDynamicProperty(hostmor, "configManager");
                    HostConfigManager configMgr = (HostConfigManager)cmobj;
                    ManagedObjectReference nwSystem = configMgr.networkSystem;
                    HostNetworkInfo netInfo = (HostNetworkInfo)cb.getServiceUtil().GetDynamicProperty(nwSystem,
                                                                        "networkInfo");
                    HostNetworkConfig netConfig = (HostNetworkConfig)cb.getServiceUtil().GetDynamicProperty(nwSystem,
                                                                        "networkConfig");
                    for (int i = 0; i < netConfig.portgroup.Length; i++)
                    {
                        switchList.Add(new HostSwitchInfoDataStructure()
                        {
                            Host = host,
                            PortgroupName = netConfig.portgroup[i].spec.name,
                            SwitchId = netConfig.portgroup[i].spec.vswitchName,
                            VlanId = "VLAN ID" + netConfig.portgroup[i].spec.vlanId.ToString()
                        });
                    }
                    ////////////////////Stockage////////////////////////////////
                    Object cobj = cb.getServiceUtil().GetDynamicProperty(hostmor, "datastore");
                    ManagedObjectReference[] datastores = (ManagedObjectReference[])cobj;
                    for (int i = 0; i < datastores.Length; i++)
                    {
                        VimApi.DatastoreSummary storageSummaryInfo = (VimApi.DatastoreSummary)cb.getServiceUtil().GetObjectProperties(cb.getConnection().ServiceContent.propertyCollector, datastores[i], new String[] { "summary" })[0].propSet[0].val;
                        storageList.Add(new HostStorageInfoDataStructure() {
                            StorageName = storageSummaryInfo.name,
                             Host = host
                        });
                    }
                }
                else
                {
                    Console.WriteLine("Host not found");
                }
            }
            catch (SoapException e)
0 Kudos
comiscience
Contributor
Contributor

Hi always Smiley Happy

http://your.vcenter-server.hostname or http://your.esx-server.hostname

Can you give me a example of this kind of addresse?

I don't know  'your' and 'hostname'  mean

Something like xxx.vcenter-server.xxxx ?

Thanks a lot have a nice day Smiley Happy

0 Kudos
togtog
Hot Shot
Hot Shot

Hi comiscience!

Sorry for being not clear enough, I will give it another try:

Assume the following example: You have installed one ESXi server and one vCenter Server managing that ESXi. The IP address assinged to the ESXi server is 192.168.0.10 and its fully qualified domain name (FQDN) is esx1.comisience.org. The IP address assinged to the vCenter server is 192.168.0.20 and its FQDN is vcenter1.comisience.org.

To access the MOB on the ESXi you can use:

http://192.168.0.10/mob or http://esx1.comisience.org/mob

To access the MOB on the vCenter server you can use:

http://192.168.0.20/mob or http://vcenter1.comisience.org/mob

That is the FQDNs or the IP addresses depend on your actual setup and you will have to lookup them up and then type the respective URLs in your web browser.

Thomas G.
0 Kudos