示例#1
0
        private ManagementPath SetPortVlan(string vlan, EthernetPortAllocationSettingData portPath)
        {
            logger.DebugFormat("Setting VLAN to {0}", vlan);

            var vmVirtMgmtSvc = GetVirtualisationSystemManagementService();
            EthernetSwitchPortVlanSettingData.GetInstances();

            // Create NIC resource by cloning the default NIC
            var vlanSettings = EthernetSwitchPortVlanSettingData.GetInstances(vmVirtMgmtSvc.Scope, "InstanceID LIKE \"%Default\"");

            // Assert
            if (vlanSettings.Count != 1)
            {
                var errMsg = string.Format("Internal error, could not find default EthernetSwitchPortVlanSettingData instance");
                var ex = new WmiException(errMsg);
                logger.Error(errMsg, ex);
                throw ex;
            }
            var defaultVlanSettings = vlanSettings.OfType<EthernetSwitchPortVlanSettingData>().First();

            var newVlanSettings = new EthernetSwitchPortVlanSettingData((ManagementBaseObject)defaultVlanSettings.LateBoundObject.Clone());

            //  Assign configuration to new NIC
            newVlanSettings.LateBoundObject["AccessVlanId"] = vlan;
            newVlanSettings.LateBoundObject["OperationMode"] = 1; // Access=1, trunk=2, private=3 ;
            newVlanSettings.CommitObject();

            // Insert NIC into vm
            string[] newResources = new string[] { newVlanSettings.LateBoundObject.GetText(System.Management.TextFormat.CimDtd20) };
            ManagementPath[] newResourcePaths = AddFeatureSettings(newResources, portPath.Path);

            // assert
            if (newResourcePaths.Length != 1)
            {
                var errMsg = string.Format(
                    "Failed to properly set VLAN to {0} for NIC on port {1}",
                    vlan,
                    portPath.Path);
                var ex = new WmiException(errMsg);
                logger.Error(errMsg, ex);
                throw ex;
            }

            return newResourcePaths[0];
        }
 public EthernetSwitchPortVlanSettingData(System.Management.ManagementScope mgmtScope, string keyInstanceID)
 {
     this.InitializeObject(((System.Management.ManagementScope)(mgmtScope)), new System.Management.ManagementPath(EthernetSwitchPortVlanSettingData.ConstructPath(keyInstanceID)), null);
 }