VMware {code} Community
comiscience
Contributor
Contributor

The resource is in use when try to remove a port group

Hello

                    Object cmobj = cb.getServiceUtil().GetDynamicProperty(hostmor, "configManager");
                    HostConfigManager configMgr = (HostConfigManager)cmobj;
                    ManagedObjectReference nwSystem = configMgr.networkSystem;
                    HostNetworkInfo netInfo = (HostNetworkInfo)cb.getServiceUtil().GetDynamicProperty(nwSystem,
                                                                                "networkInfo");
                    cb.getConnection()._service.RemovePortGroup(nwSystem, portGroupName);

The code above showed how did i remove a port group. The last week, my code worked verywell.

But when i came back from my vacation today, i found that it dosen't work anymore.

After i successfully added a new port group, i would remove it, but it appears always a Exception:  "The resource "xxxx" is in use"

What should i do to avoid this problem? Thanks

0 Kudos
3 Replies
comiscience
Contributor
Contributor

But i can remove my port group with using "vSphere Client Tool"

0 Kudos
comiscience
Contributor
Contributor

Object cmobj
                       = cb.getServiceUtil().GetDynamicProperty(hostmor, "configManager");
                    HostConfigManager configMgr = (HostConfigManager)cmobj;
                    ManagedObjectReference nwSystem = configMgr.networkSystem;
                    HostPortGroupSpec portgrp = new HostPortGroupSpec();
                    portgrp.name = portGroupName;
                    portgrp.vswitchName = vswitchId;
                    portgrp.policy = new HostNetworkPolicy();
                    portgrp.vlanId = Convert.ToInt32(vlanId);
                    cb.getConnection()._service.AddPortGroup(nwSystem, portgrp);

The code above for just add a PortGroup is ok, i can remove this port group with my code

But if i add code below, i get the execption of 'The resource xxx is in use'

// The problem is here!

                    if (this.networkType == VMNetworkType.VMKernel)

                    {

                        HostVirtualNicSpec vNicSpec = new HostVirtualNicSpec();

                        HostIpConfig ipConfig = new HostIpConfig();

                        ipConfig.dhcp = false;

                        string ipAddr = cb.get_option("ipaddress");

                        ipConfig.ipAddress = ipAddr;

                        ipConfig.subnetMask = "255.255.255.0";

                        vNicSpec.ip = ipConfig;

                        cb.getConnection()._service.AddVirtualNic(nwSystem, portGroupName, vNicSpec);

                    }

0 Kudos
comiscience
Contributor
Contributor

I got it!!

Must remove VNIC first before remove portgroup    lol

0 Kudos