// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft. All rights reserved.
namespace Microsoft.Samples.HyperV.ResourcePools
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Management;
using Microsoft.Samples.HyperV.Common;
static class MsvmResourceAllocationSettingData
{
///
/// Returns a MOB for the Msvm_ResourceAllocationSettingData
/// object associated with the specified resource pool.
///
/// The ManagementScope to use to connect to WMI.
/// The resource type to assign.
/// The resource subtype to assign.
/// The pool ID to assign.
/// The MOB for a Msvm_ResourceAllocationSettingData object.
internal static ManagementObject
GetAllocationSettingsForPool(
ManagementScope scope,
string resourceType,
string resourceSubType,
string poolId)
{
using (ManagementObject pool = WmiUtilities.GetResourcePool(
resourceType,
resourceSubType,
poolId,
scope))
{
if ((bool)pool.GetPropertyValue("Primordial"))
return null;
using (ManagementObjectCollection rasdCollection =
pool.GetRelated(
"CIM_ResourceAllocationSettingData",
"Msvm_SettingsDefineState",
null,
null,
"SettingData",
"ManagedElement",
false,
null))
{
foreach (ManagementObject rasd in rasdCollection)
{
return rasd;
}
return null;
}
}
}
///
/// Returns an embedded instance string of a Msvm_ResourceAllocationSettingData
/// object initialized with the specified pool ID, pool name, and resource
/// information.
///
/// The ManagementScope to use to connect to WMI.
/// The resource type to assign.
/// The resource subtype to assign.
/// The pool ID to assign.
/// An array of strings that indicates the
/// host resources to assign to the pool.
/// The embedded instance string of a
/// Msvm_ResourceAllocationSettingData object.
internal static string
GetNewPoolAllocationSettings(
ManagementScope scope,
string resourceType,
string resourceSubType,
string poolId,
string[] hostResources)
{
using (ManagementClass rasdClass = new ManagementClass("Msvm_ResourceAllocationSettingData"))
{
rasdClass.Scope = scope;
using (ManagementObject rasdMob = rasdClass.CreateInstance())
{
rasdMob["ResourceType"] = resourceType;
if (resourceType == "1")
{
rasdMob["OtherResourceType"] = resourceSubType;
rasdMob["ResourceSubType"] = string.Empty;
}
else
{
rasdMob["OtherResourceType"] = string.Empty;
rasdMob["ResourceSubType"] = resourceSubType;
}
rasdMob["PoolId"] = poolId;
rasdMob["HostResource"] = hostResources;
string rasdString = rasdMob.GetText(TextFormat.WmiDtd20);
return rasdString;
}
}
}
///
/// Returns a string array of embedded instance strings of Msvm_ResourceAllocationSettingData
/// objects for each specified pool ID.
///
/// The ManagementScope to use to connect to WMI.
/// The Resource Pool
/// Configuration Service MOB.
/// The resource type to assign.
/// The resource subtype to assign.
/// A string array of pool IDs.
/// An array of string arrays that specify host
/// resources.
/// An array of embedded instance strings of
/// Msvm_ResourceAllocationSettingData objects.
internal static string[]
GetNewPoolAllocationSettingsArray(
ManagementScope scope,
string resourceType,
string resourceSubType,
string[] poolIdArray,
string[][] hostResourcesArray)
{
List rasdList = new List();
for (uint index = 0; index < poolIdArray.Length; ++index)
{
rasdList.Add(GetNewPoolAllocationSettings(
scope,
resourceType,
resourceSubType,
poolIdArray[index],
hostResourcesArray[index]));
}
return rasdList.ToArray();
}
///
/// Returns the default Msvm_ResourceAllocationSettingData MOB.
///
/// The ManagementScope to use to connect to WMI.
/// Mvm_ResourcePool MOB.
///
///
/// Msvm_ResourceAllocationSettingData MOB.
public static ManagementObject GetDefaultAllocationSettings(
ManagementScope scope,
ManagementObject pool)
{
return GetPrototypeAllocationSettings(
scope,
pool,
"0",
"0");
}
///
/// Returns the minimum Msvm_ResourceAllocationSettingData MOB.
///
/// The ManagementScope to use to connect to WMI.
/// Mvm_ResourcePool MOB.
///
///
/// Msvm_ResourceAllocationSettingData MOB.
public static ManagementObject GetMinimumAllocationSettings(
ManagementScope scope,
ManagementObject pool)
{
return GetPrototypeAllocationSettings(
scope,
pool,
"3",
"1");
}
///
/// Returns the maximum Msvm_ResourceAllocationSettingData MOB.
///
/// The ManagementScope to use to connect to WMI.
/// Mvm_ResourcePool MOB.
///
///
/// Msvm_ResourceAllocationSettingData MOB.
public static ManagementObject GetMaximumAllocationSettings(
ManagementScope scope,
ManagementObject pool)
{
return GetPrototypeAllocationSettings(
scope,
pool,
"3",
"2");
}
///
/// Returns the incremental Msvm_ResourceAllocationSettingData MOB.
///
/// The ManagementScope to use to connect to WMI.
/// Mvm_ResourcePool MOB.
///
///
/// Msvm_ResourceAllocationSettingData MOB.
public static ManagementObject GetIncrementalAllocationSettings(
ManagementScope scope,
ManagementObject pool)
{
return GetPrototypeAllocationSettings(
scope,
pool,
"3",
"3");
}
///
/// Returns the specified Msvm_ResourceAllocationSettingData MOB.
///
/// Role/Range: 0/0 - Default; 3/1 Minimum; 3/2 Maximum; 3/3 Incremental
/// The ManagementScope to use to connect to WMI.
/// Mvm_ResourcePool MOB.
///
///
/// Msvm_ResourceAllocationSettingData MOB.
internal static ManagementObject GetPrototypeAllocationSettings(
ManagementScope scope,
ManagementObject pool,
string valueRole,
string valueRange)
{
foreach (ManagementObject allocationCapability in pool.GetRelated("Msvm_AllocationCapabilities",
"Msvm_ElementCapabilities",
null, null, null, null, false, null))
using (allocationCapability)
{
foreach (ManagementObject relationship in allocationCapability.GetRelationships("Cim_SettingsDefineCapabilities"))
using (relationship)
{
if (relationship["ValueRole"].ToString() == valueRole &&
relationship["ValueRange"].ToString() == valueRange)
{
ManagementObject rasd = new ManagementObject(
pool.Scope,
new ManagementPath(relationship["PartComponent"].ToString()),
null);
return rasd;
}
}
}
return null;
}
///
/// Displays the ResourceAllocationSettingData
/// object associated with the specified resource pool.
///
/// A Msvm_ResourceAllocationSettingData MOB.
internal static void
DisplayPoolResourceAllocationSettingData(
ManagementObject rasd)
{
Console.WriteLine("Msvm_ResourceAllocationSettingData:");
if (rasd == null)
{
Console.WriteLine("\tA primordial pool does not have an associated " +
"Msvm_ResourceAllocationSettingData object.");
return;
}
Console.WriteLine("\tElementName: {0}", rasd.GetPropertyValue("ElementName"));
Console.WriteLine("\tCaption: {0}", rasd.GetPropertyValue("Caption"));
Console.WriteLine("\tInstanceID: {0}", rasd.GetPropertyValue("InstanceID"));
Console.WriteLine("\tResourceType: {0}", rasd.GetPropertyValue("ResourceType"));
Console.WriteLine("\tResourceSubType: {0}", rasd.GetPropertyValue("ResourceSubType"));
string[] hostResources = (string[])rasd.GetPropertyValue("HostResource");
if (hostResources != null)
{
Console.WriteLine("\tHostResources:");
foreach (string resource in hostResources)
{
Console.WriteLine("\t\t" + resource);
}
}
}
///
/// Displays the ResourceAllocationSettingData
/// object associated with the specified resource pool.
///
/// The ManagementScope to use to connect to WMI.
/// Msvm_ResourcePool MOB.
internal static void
DisplayPoolResourceAllocationSettingData(
ManagementScope scope,
ManagementObject pool)
{
using (ManagementObject rasd =
MsvmResourceAllocationSettingData.GetAllocationSettingsForPool(
scope,
pool.GetPropertyValue("ResourceType").ToString(),
pool.GetPropertyValue("ResourceSubType").ToString(),
pool.GetPropertyValue("PoolId").ToString()))
{
DisplayPoolResourceAllocationSettingData(rasd);
}
}
///
/// Displays the Msvm_ResourceAllocationSettingData
/// object associated with the specified resource pool.
///
/// The display name for the resourcetype.
/// The pool ID.
internal static void
DisplayPoolResourceAllocationSettingData(
string resourceDisplayName,
string poolId)
{
Console.WriteLine(
"Displaying the Msvm_ResourceAllocationSettingData properties for the following " +
"resource pool:\n" +
"\tPool Id: " + poolId);
ResourceUtilities.DisplayResourceInformation(resourceDisplayName);
ManagementScope scope = ResourcePoolUtilities.GetManagementScope();
using (ManagementObject rasd =
MsvmResourceAllocationSettingData.GetAllocationSettingsForPool(
scope,
ResourceUtilities.GetResourceType(resourceDisplayName),
ResourceUtilities.GetResourceSubType(resourceDisplayName),
poolId))
{
DisplayPoolResourceAllocationSettingData(rasd);
}
}
///
/// Displays the valid default, minimum, maximum, and incremental
/// Msvm_ResourceAllocationSettingData objects for the specified resource.
///
/// The display name for the resource type.
/// The pool ID.
internal static void
DisplayValidResourceAllocationSettingDataSettings(
string resourceDisplayName,
string poolId)
{
ManagementScope scope = ResourcePoolUtilities.GetManagementScope();
using (ManagementObject pool = WmiUtilities.GetResourcePool(
ResourceUtilities.GetResourceType(resourceDisplayName),
ResourceUtilities.GetResourceSubType(resourceDisplayName),
poolId,
scope))
using (ManagementObject defaultRasd = GetDefaultAllocationSettings(
scope,
pool))
using (ManagementObject minimumRasd = GetMinimumAllocationSettings(
scope,
pool))
using (ManagementObject maximumRasd = GetMaximumAllocationSettings(
scope,
pool))
using (ManagementObject incrementalRasd = GetIncrementalAllocationSettings(
scope,
pool))
{
Console.WriteLine("(default)");
DisplayPoolResourceAllocationSettingData(defaultRasd);
Console.WriteLine("(minimum)");
DisplayPoolResourceAllocationSettingData(minimumRasd);
Console.WriteLine("(maximum)");
DisplayPoolResourceAllocationSettingData(maximumRasd);
Console.WriteLine("(incremental)");
DisplayPoolResourceAllocationSettingData(incrementalRasd);
}
}
}
}