Example #1
0
        private void doRemoveVirtualSwitch()
        {
            ManagedObjectReference dcmor;
            ManagedObjectReference hostfoldermor;
            ManagedObjectReference hostmor = null;

            datacenter = cb.get_option("datacenter");
            host       = cb.get_option("host");
            vswitchId  = cb.get_option("vswitchid");
            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;
                    cb.getConnection()._service.RemoveVirtualSwitch(nwSystem, vswitchId);
                    Console.WriteLine(cb.getAppName() + " : Successful removing : "
                                      + vswitchId);
                }
                else
                {
                    Console.WriteLine("Host not found");
                }
            }
            catch (SoapException e)
            {
                if (e.Detail.FirstChild.LocalName.Equals("ResourceInUseFault"))
                {
                    Console.WriteLine(cb.getAppName() + " : Failed removing switch " + vswitchId);
                    Console.WriteLine("There are virtual network adapters "
                                      + "associated with the virtual switch.");
                }
                else if (e.Detail.FirstChild.LocalName.Equals("NotFoundFault"))
                {
                    Console.WriteLine(cb.getAppName() + " : Failed : virtual switch cannot be found. ");
                }
                else if (e.Detail.FirstChild.LocalName.Equals("HostConfigFault"))
                {
                    Console.WriteLine(cb.getAppName() + " : Failed : Configuration failures. ");
                }
                else
                {
                    throw e;
                }
            }
            catch (Exception e) {
                throw e;
            }
        }
Example #2
0
        private void doAddVirtualNic()
        {
            ManagedObjectReference dcmor;
            ManagedObjectReference hostfoldermor;
            ManagedObjectReference hostmor = null;

            datacenter = cb.get_option("datacenter");
            host       = cb.get_option("host");
            vswitchId  = cb.get_option("vswitchid");
            portGroup  = cb.get_option("portgroupname");
            ipAddr     = cb.get_option("ipaddress");

            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;

                    HostPortGroupSpec portgrp = new HostPortGroupSpec();
                    portgrp.name = portGroup;

                    HostVirtualNicSpec vNicSpec = createVNicSpecification();
                    cb.getConnection()._service.AddVirtualNic(nwSystem, portGroup, vNicSpec);

                    Console.WriteLine(cb.getAppName() + " : Successful creating nic on portgroup : "
                                      + portGroup);
                }
                else
                {
                    Console.WriteLine("Host not found");
                }
            }

            catch (SoapException e) {
                if (e.Detail.FirstChild.LocalName.Equals("ResourceInUseFault"))
                {
                    Console.WriteLine(cb.getAppName() + " : Failed to add nic "
                                      + ipAddr);
                }
                else if (e.Detail.FirstChild.LocalName.Equals("InvalidArgumentFault"))
                {
                    Console.WriteLine(cb.getAppName() + " : Failed to add nic " + ipAddr);
                    Console.WriteLine("PortGroup vlanId or network policy or ipaddress may be invalid .\n");
                }
                else if (e.Detail.FirstChild.LocalName.Equals("NotFoundFault"))
                {
                    Console.WriteLine(cb.getAppName() + " : Failed to add nic " + ipAddr);
                    Console.WriteLine(" Switch or portgroup not found.\n");
                }
            }

            catch (NullReferenceException e) {
                Console.WriteLine(cb.getAppName() + " : Failed to add nic " + ipAddr);
                Console.WriteLine("Datacenter or Host may be invalid \n");
                throw e;
            }
            catch (Exception e) {
                Console.WriteLine(cb.getAppName() + " : Failed to add nic " + ipAddr);
                throw e;
            }
        }
        private void doAddVirtualSwitchPortGroup()
        {
            ManagedObjectReference dcmor;
            ManagedObjectReference hostfoldermor;
            ManagedObjectReference hostmor = null;

            datacenter    = cb.get_option("datacenter");
            host          = cb.get_option("host");
            portGroupName = cb.get_option("portgroupname");
            vswitchId     = cb.get_option("vswitchid");
            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;

                    HostPortGroupSpec portgrp = new HostPortGroupSpec();
                    portgrp.name        = portGroupName;
                    portgrp.vswitchName = vswitchId;
                    portgrp.policy      = new HostNetworkPolicy();

                    cb.getConnection()._service.AddPortGroup(nwSystem, portgrp);

                    Console.WriteLine(cb.getAppName() + " : Successful creating : "
                                      + vswitchId + "/" + portGroupName);
                }
                else
                {
                    Console.WriteLine("Host not found");
                }
            }
            catch (SoapException e) {
                if (e.Detail.FirstChild.LocalName.Equals("AlreadyExistsFault"))
                {
                    Console.WriteLine(cb.getAppName() + " : Failed creating : "
                                      + vswitchId + "/" + portGroupName);
                    Console.WriteLine("Portgroup name already exists");
                }
                if (e.Detail.FirstChild.LocalName.Equals("InvalidArgumentFault"))
                {
                    Console.WriteLine(cb.getAppName() + " : Failed creating : "
                                      + vswitchId + "/" + portGroupName);
                    Console.WriteLine("PortGroup vlanId or network policy may be invalid.");
                }
                else if (e.Detail.FirstChild.LocalName.Equals("NotFoundFault"))
                {
                    Console.WriteLine(cb.getAppName() + " : Failed creating : "
                                      + vswitchId + "/" + portGroupName);
                    Console.WriteLine("Switch Not found.");
                }
            }

            catch (NullReferenceException e) {
                Console.WriteLine(cb.getAppName() + " : Failed creating : "
                                  + vswitchId + "/" + portGroupName);
                Console.WriteLine("Datacenter or Host may be invalid");
                throw e;
            }
            catch (Exception e) {
                Console.WriteLine(cb.getAppName() + " : Failed creating : "
                                  + vswitchId + "/" + portGroupName);
                throw e;
            }
        }
Example #4
0
        private void doAddVirtualSwitch()
        {
            ManagedObjectReference dcmor;
            ManagedObjectReference hostfoldermor;
            ManagedObjectReference hostmor = null;

            datacenter = cb.get_option("datacenter");
            host       = cb.get_option("host");
            vswitchId  = cb.get_option("vswitchid");
            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;
                    HostVirtualSwitchSpec  spec      = new HostVirtualSwitchSpec();
                    spec.numPorts = 8;
                    cb.getConnection()._service.AddVirtualSwitch(nwSystem, vswitchId, spec);
                    Console.WriteLine(cb.getAppName() + " : Successful creating : "
                                      + vswitchId);
                }
                else
                {
                    Console.WriteLine("Host not found");
                }
            }
            catch (SoapException e)
            {
                if (e.Detail.FirstChild.LocalName.Equals("InvalidArgumentFault"))
                {
                    Console.WriteLine(cb.getAppName() + "vswitchName exceeds the maximum "
                                      + "allowed length, or the number of ports "
                                      + "specified falls out of valid range, or the network "
                                      + "policy is invalid, or beacon configuration is invalid. ");
                }
                else if (e.Detail.FirstChild.LocalName.Equals("AlreadyExistsFault"))
                {
                    Console.WriteLine(cb.getAppName() + " : Failed : Switch already exists ");
                }
                else if (e.Detail.FirstChild.LocalName.Equals("HostConfigFault"))
                {
                    Console.WriteLine(cb.getAppName() + " : Failed : Configuration failures. ");
                }
                else if (e.Detail.FirstChild.LocalName.Equals("NotFoundFault"))
                {
                    Console.WriteLine(e.Message.ToString());
                }
                else
                {
                    throw e;
                }
            }

            catch (Exception e) {
                Console.WriteLine(cb.getAppName() + " : Failed adding switch: " + vswitchId);
                throw e;
            }
        }
        private void doRemoveVirtualNic()
        {
            ManagedObjectReference dcmor;
            ManagedObjectReference hostfoldermor;
            ManagedObjectReference hostmor = null;

            datacenter    = cb.get_option("datacenter");
            host          = cb.get_option("host");
            portGroupName = cb.get_option("portgroupname");

            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;

                    Object obj
                        = cb.getServiceUtil().GetDynamicProperty(nwSystem, "networkInfo.vnic");
                    HostVirtualNic[] hvns      = (HostVirtualNic[])obj;
                    Boolean          found_one = false;
                    if (hvns != null)
                    {
                        for (int i = 0; i < hvns.Length; i++)
                        {
                            HostVirtualNic nic       = hvns[i];
                            String         portGroup = nic.portgroup;
                            if (portGroup.Equals(portGroupName))
                            {
                                found_one = true;
                                cb.getConnection()._service.RemoveVirtualNic(nwSystem,
                                                                             nic.device);
                            }
                        }
                    }
                    if (found_one)
                    {
                        Console.WriteLine(cb.getAppName()
                                          + " : Successful removing : " + portGroupName);
                    }
                    else
                    {
                        Console.WriteLine(cb.getAppName()
                                          + " : PortGroupName not found failed removing : "
                                          + portGroupName);
                    }
                }
                else
                {
                    Console.WriteLine("Host not found");
                }
            }
            catch (SoapException e)
            {
                if (e.Detail.FirstChild.LocalName.Equals("NotFoundFault"))
                {
                    Console.WriteLine(cb.getAppName()
                                      + " : Failed : virtual network adapter cannot be found. ");
                }
                else if (e.Detail.FirstChild.LocalName.Equals("HostConfigFault"))
                {
                    Console.WriteLine(cb.getAppName()
                                      + " : Failed : Configuration falilures. ");
                }
            }
            catch (Exception e) {
                Console.WriteLine(cb.getAppName() + " : Failed removing nic: "
                                  + portGroupName);
                throw e;
            }
        }
Example #6
0
        private void doRemoveVirtualSwitchPortGroup()
        {
            ManagedObjectReference dcmor;
            ManagedObjectReference hostfoldermor;
            ManagedObjectReference hostmor = null;

            datacenter    = cb.get_option("datacenter");
            host          = cb.get_option("host");
            portGroupName = cb.get_option("portgroupname");
            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");
                    cb.getConnection()._service.RemovePortGroup(nwSystem, portGroupName);
                    Console.WriteLine(cb.getAppName() + " : Successful removing portgroup "
                                      + portGroupName);
                }
                else
                {
                    Console.WriteLine("Host not found");
                }
            }
            catch (SoapException e)
            {
                if (e.Detail.FirstChild.LocalName.Equals("InvalidArgumentFault"))
                {
                    Console.WriteLine(cb.getAppName() + " : Failed removing  " + portGroupName);
                    Console.WriteLine("PortGroup vlanId or network policy may be invalid .\n");
                }
                else if (e.Detail.FirstChild.LocalName.Equals("NotFoundFault"))
                {
                    Console.WriteLine(cb.getAppName() + " : Failed removing " + portGroupName);
                    Console.WriteLine(" Switch or portgroup not found.\n");
                }
            }

            catch (NullReferenceException e)
            {
                Console.WriteLine(cb.getAppName() + " : Failed removing " + portGroupName);
                Console.WriteLine("Datacenter or Host may be invalid \n");
                throw e;
            }
            catch (Exception e)
            {
                Console.WriteLine(cb.getAppName() + " : Failed removing " + portGroupName);
                throw e;
            }
        }