Example #1
0
        /// <summary>
        /// Deprecated
        /// </summary>
        /// <param name="nic"></param>
        /// <returns></returns>
        public static SwitchPort GetSwitchPort(SyntheticEthernetPort nic)
        {
            // An ASSOCIATOR object provides the cross reference between WMI objects,
            // but generated wrappers do not expose a ASSOCIATOR OF query as a method.
            // Instead, we use the System.Management to code the equivalant of
            //  string query = string.Format( "ASSOCIATORS OF {{{0}}} WHERE ResultClass = {1}", wmiObject.path, resultclassName);
            //
            var wmiObjQuery = new RelatedObjectQuery(nic.Path.Path, VmLANEndpoint.CreatedClassName);

            // NB: default scope of ManagementObjectSearcher is '\\.\root\cimv2', which does not contain
            // the virtualisation objects.
            var wmiObjectSearch = new ManagementObjectSearcher(nic.Scope, wmiObjQuery);
            var wmiObjCollection = new VmLANEndpoint.VmLANEndpointCollection(wmiObjectSearch.Get());

            // assert
            if (wmiObjCollection.Count < 1)
            {
                var errMsg = string.Format("No VmLANEndpoint for external NIC {0} on Hyper-V server", nic.Name);
                var ex = new WmiException(errMsg);
                logger.Error(errMsg, ex);
                throw ex;
            }

            VmLANEndpoint vmEndPoint = wmiObjCollection.OfType<VmLANEndpoint>().First();
            var switchPortQuery = new RelatedObjectQuery(vmEndPoint.Path.Path, SwitchPort.CreatedClassName);
            var switchPortSearch = new ManagementObjectSearcher(nic.Scope, switchPortQuery);
            var switchPortCollection = new SwitchPort.SwitchPortCollection(switchPortSearch.Get());

            // assert
            if (switchPortCollection.Count < 1)
            {
                var errMsg = string.Format("No SwitchPort for external NIC {0} on Hyper-V server", nic.Name);
                var ex = new WmiException(errMsg);
                logger.Error(errMsg, ex);
                throw ex;
            }

            SwitchPort switchPort = wmiObjCollection.OfType<SwitchPort>().First();

            return switchPort;
        }
Example #2
0
        /// <summary>
        /// External VSwitch has an external NIC, and we assume there is only one external NIC
        /// </summary>
        /// <param name="vmSettings"></param>
        /// <returns></returns>
        /// <throw>Throws if there is no vswitch</throw>
        public static VirtualSwitch GetExternalVirtSwitch()
        {
            // Work back from the first *bound* external NIC we find.
            var externNICs = ExternalEthernetPort.GetInstances("IsBound = TRUE");

            if (externNICs.Count == 0 )
            {
                var errMsg = "No ExternalEthernetPort available to Hyper-V";
                var ex = new WmiException(errMsg);
                logger.Error(errMsg, ex);
                throw ex;
            }

            ExternalEthernetPort externNIC = externNICs.OfType<ExternalEthernetPort>().First();

            // A sequence of ASSOCIATOR objects need to be traversed to get from external NIC the vswitch.
            // We use ManagementObjectSearcher objects to execute this sequence of questions
            // NB: default scope of ManagementObjectSearcher is '\\.\root\cimv2', which does not contain
            // the virtualisation objects.
            var endpointQuery = new RelatedObjectQuery(externNIC.Path.Path, SwitchLANEndpoint.CreatedClassName);
            var endpointSearch = new ManagementObjectSearcher(externNIC.Scope, endpointQuery);
            var endpointCollection = new SwitchLANEndpoint.SwitchLANEndpointCollection(endpointSearch.Get());

            // assert
            if (endpointCollection.Count < 1 )
            {
                var errMsg = string.Format("No SwitchLANEndpoint for external NIC {0} on Hyper-V server", externNIC.Name);
                var ex = new WmiException(errMsg);
                logger.Error(errMsg, ex);
                throw ex;
            }

            SwitchLANEndpoint endPoint = endpointCollection.OfType<SwitchLANEndpoint>().First();
            var switchPortQuery = new RelatedObjectQuery(endPoint.Path.Path, SwitchPort.CreatedClassName);
            var switchPortSearch = new ManagementObjectSearcher(externNIC.Scope, switchPortQuery);
            var switchPortCollection = new SwitchPort.SwitchPortCollection(switchPortSearch.Get());

            // assert
            if (switchPortCollection.Count < 1 )
            {
                var errMsg = string.Format("No SwitchPort for external NIC {0} on Hyper-V server", externNIC.Name);
                var ex = new WmiException(errMsg);
                logger.Error(errMsg, ex);
                throw ex;
            }

            SwitchPort switchPort = switchPortCollection.OfType<SwitchPort>().First();
            var vSwitchQuery = new RelatedObjectQuery(switchPort.Path.Path, VirtualSwitch.CreatedClassName);
            var vSwitchSearch = new ManagementObjectSearcher(externNIC.Scope, vSwitchQuery);
            var vSwitchCollection = new VirtualSwitch.VirtualSwitchCollection(vSwitchSearch.Get());

            // assert
            if (vSwitchCollection.Count < 1)
            {
                var errMsg = string.Format("No virtual switch for external NIC {0} on Hyper-V server", externNIC.Name);
                var ex = new WmiException(errMsg);
                logger.Error(errMsg, ex);
                throw ex;
            }

            VirtualSwitch vSwitch = vSwitchCollection.OfType<VirtualSwitch>().First();

            return vSwitch;
        }