Example #1
0
        private void displayScsiLuns()
        {
            String hostname;
            ManagedObjectReference hmor;
            ManagedObjectReference hostfoldermor;

            if (!validate())
            {
                vmUtils  = new VMUtils(cb);
                hostname = null;
                ManagedObjectReference dcmor = cb.getServiceUtil().GetDecendentMoRef
                                                   (null, "Datacenter", "ha-datacenter");
                hostfoldermor = vmUtils.getHostFolder(dcmor);
                hmor          = vmUtils.getHost(hostfoldermor, hostname);
            }
            else
            {
                hostname = cb.get_option("hostname");
                hmor     = cb.getServiceUtil().GetDecendentMoRef(null, "HostSystem", hostname);
            }
            if (hmor != null)
            {
                DynamicProperty[] scsiArry =
                    getDynamicProarray(hmor, "config.storageDevice.scsiLun");

                ScsiLun[] scsiLun = ((ScsiLun[])(scsiArry[0]).val);

                try{
                    if (scsiLun != null && scsiLun.Length > 0)
                    {
                        for (int j = 0; j < scsiLun.Length; j++)
                        {
                            Console.WriteLine("\nSCSI LUN " + (j + 1));
                            Console.WriteLine("--------------");
                            String             canName            = scsiLun[j].canonicalName;
                            String             vendor             = scsiLun[j].vendor;
                            String             model              = scsiLun[j].model;
                            ScsiLunDurableName scsiLunDurableName = scsiLun[j].durableName;
                            if (scsiLunDurableName != null)
                            {
                                sbyte[] data          = scsiLunDurableName.data;
                                String  scsinamespace = scsiLunDurableName.@namespace;
                                sbyte   namespaceId   = scsiLunDurableName.namespaceId;
                                Console.Write("\nData            : ");
                                for (int i = 0; i < data.Length; i++)
                                {
                                    Console.Write(data[i] + " ");
                                }
                                Console.WriteLine("Namespace       : " + scsinamespace);
                                Console.WriteLine("Namespace ID    : " + namespaceId);
                            }
                            Console.WriteLine("\nCanonical Name  : " + canName);

                            Console.WriteLine("\nVMFS Affected ");
                            getVMFS(hmor, canName);
                            Console.WriteLine("Virtual Machines ");
                            getVMs(hmor, canName);
                        }
                    }
                }
                catch (Exception e) {
                    Console.WriteLine("error" + e);
                    e.StackTrace.ToString();
                }
            }
            else
            {
                Console.WriteLine("Host " + cb.get_option("hostname") + " not found");
            }
        }
Example #2
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 #3
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 #5
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 #7
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;
            }
        }