using System; using System.Collections.Generic; using System.Linq; using System.Text; using Intel.Management.Wsman; using System.Collections.ObjectModel; using System.Management.Automation; namespace Intel.Management.PSModule.Amt { class StorageAdmin : DriveContainer { IManagedReference _storageAdminRef; bool _isCached; static string[] _properties = {"TotalStorage", "TotalAllocatedStorage", "MaxPartnerStorage", "TotalPartnerAllocatedStorage", "MaxNonPartnerStorage", "MaxFpaclEntries", "MaxAslEntries", "MaxEaclEntries", "MaxGroupsPerBlock", "MaxMembersPerGroup", "MaxNonPartnerTotalAllocationSize",}; uint _totalStorage; uint _totalAllocatedStorage; uint _maxPartnerStorage; uint _totalPartnerAllocatedStorage; uint _maxNonPartnerStorage; uint _maxFpaclEntries; uint _maxAslEntries; uint _maxEaclEntires; uint _maxGroupsPerBlock; uint _maxMembersPerGroup; uint _maxNonPartnerTotalAllocationSize; public StorageAdmin(DriveContainer parent) : base("3PDStorage", parent) { //_items = null; IWsmanConnection conn = ((AmtRootService)parent.GetRoot()).Connection; _storageAdminRef = conn.NewReference("AMT_ThirdPartyDataStorageAdministrationService"); //_root = new Root(this, parent); } public override void GetChildItems(ChildWriter writer) { writer.Add(new Enterprises( this)); writer.Add(new Allocations( this)); writer.Add(new Registrations(this)); } public override string[] Properties { get { return _properties; } } public uint TotalStorage { get { LoadProperties(); return _totalStorage; } } public uint TotalAllocatedStorage { get { LoadProperties(); return _totalAllocatedStorage; } } public uint MaxPartnerStorage { get { LoadProperties(); return _maxPartnerStorage; } set { SetGlobalStorageAttributes("MaxPartnerStorage", value.ToString()); } } public uint TotalPartnerAllocatedStorage { get { LoadProperties(); return _totalPartnerAllocatedStorage; } } public uint MaxNonPartnerStorage { get { LoadProperties(); return _maxNonPartnerStorage; } } public uint MaxFpaclEntries { get { LoadProperties(); return _maxFpaclEntries; } } public uint MaxAslEntries { get { LoadProperties(); return _maxAslEntries; } } public uint MaxEaclEntries { get { LoadProperties(); return _maxEaclEntires; } } public uint MaxGroupsPerBlock { get { LoadProperties(); return _maxGroupsPerBlock; } } public uint MaxMembersPerGroup { get { LoadProperties(); return _maxMembersPerGroup; } } public uint MaxNonPartnerTotalAllocationSize { get { LoadProperties(); return _maxNonPartnerTotalAllocationSize; } set { SetGlobalStorageAttributes("MaxNonPartnerTotalAllocationSize", value.ToString()); } } private void LoadProperties() { if (!_isCached) { IManagedInstance outObj = GetGlobalStorageAttributes(); _totalStorage = uint.Parse(outObj.GetProperty("TotalStorage").ToString()); _totalAllocatedStorage = uint.Parse(outObj.GetProperty("TotalAllocatedStorage").ToString()); _maxPartnerStorage = uint.Parse(outObj.GetProperty("MaxPartnerStorage").ToString()); _totalPartnerAllocatedStorage = uint.Parse(outObj.GetProperty("TotalPartnerAllocatedStorage").ToString()); _maxNonPartnerStorage = uint.Parse(outObj.GetProperty("MaxNonPartnerStorage").ToString()); _maxFpaclEntries = uint.Parse(outObj.GetProperty("MaxFpaclEntries").ToString()); _maxAslEntries = uint.Parse(outObj.GetProperty("MaxAslEntries").ToString()); _maxEaclEntires = uint.Parse(outObj.GetProperty("MaxEaclEntries").ToString()); _maxGroupsPerBlock = uint.Parse(outObj.GetProperty("MaxGroupsPerBlock").ToString()); _maxMembersPerGroup = uint.Parse(outObj.GetProperty("MaxMembersPerGroup").ToString()); _maxNonPartnerTotalAllocationSize = uint.Parse(outObj.GetProperty("MaxNonPartnerTotalAllocationSize").ToString()); _isCached = true; } } public override object GetProperty(Collection propList, DriveProvider provider) { if (provider != null && provider.Force.IsPresent) _isCached = false; return base.GetProperty(propList, provider); } class EnterpriseAcl : DriveItem { StorageAdmin _stgAdmin; uint _handle; static string[] _properties = { "Handle" }; public EnterpriseAcl(StorageAdmin stgAdmin,string name,uint handle,DriveItem parent) : base(name,new Container(),parent) { _stgAdmin = stgAdmin; _handle = handle; } public override string[] Properties { get {return _properties;} } public uint Handle { get { return _handle; } } public override DriveItem NewItem(string name, string type, object newItem, DriveProvider provider) { if (name.Equals(string.Empty)) { return this; } return base.NewItem(name, type, newItem, provider); } public override void RemoveItem(DriveProvider provider) { _stgAdmin.RemoveEaclEntry(_handle.ToString()); ((DriveContainer)_parent).RemoveChildItem(this); } } class Enterprises : DriveContainer { StorageAdmin _stgAdmin; public Enterprises(DriveItem parent) : base("Enterprises", parent) { _stgAdmin = (StorageAdmin)parent; _items = null; } public override void GetChildItems(ChildWriter writer) { IManagedInstance outObj = _stgAdmin.GetEaclList(); IWsmanItem handles = outObj.GetProperty("Handles"); if (handles != null) { foreach (IWsmanItem handle in handles) { IManagedInstance entryObj = _stgAdmin.GetEaclEntry(handle.ToString()); string name = entryObj.GetProperty("EnterpriseName").ToString(); writer.Add(new EnterpriseAcl(_stgAdmin, name, uint.Parse(handle.ToString()), this)); } } } public override DriveItem NewItem(string name, string type, object newItem, DriveProvider provider) { string handle = _stgAdmin.AddEaclEntry(name); EnterpriseAcl result = new EnterpriseAcl( _stgAdmin,name,uint.Parse(handle),this); AddChildItem(result); return result; } } public class PartnerAclDynamicParameters { public PartnerAclDynamicParameters() { _maxSize=48*1024; //default to 48K storage } [Parameter(Mandatory = false, Position = 0, HelpMessage = "Maximum size in bytes of the allocation")] public uint TotalAllocationSize { get { return _maxSize; } set {_maxSize = StorageManager.GetAllocationSize(value);} } uint _maxSize; } class AllocationEntry : DriveItem { StorageAdmin _stgAdmin; static string[] _properties = {"VendorName","ApplicationName","TotalAllocationSize","IsPartner","AttrType","Handle","StoragePath"}; uint _totalAllocationSize; string _vendorName; string _applicationName; bool _isPartner; uint _attrType; uint _handle; public AllocationEntry(StorageAdmin stgAdmin,IManagedInstance entryObj, string handle, DriveItem parent) : base(entryObj.GetProperty("ApplicationName").ToString(), new Object(),parent) { _stgAdmin = stgAdmin; _vendorName = entryObj.GetProperty("VendorName").ToString(); _applicationName = entryObj.GetProperty("ApplicationName").ToString(); _totalAllocationSize = uint.Parse(entryObj.GetProperty("TotalAllocationSize").ToString()); _isPartner=bool.Parse(entryObj.GetProperty("IsPartner").ToString()); _attrType = uint.Parse(entryObj.GetProperty("AttrType").ToString()); _handle = uint.Parse(handle); } public override string[] Properties { get { return _properties; } } public override void RemoveItem( DriveProvider provider) { //find and remove all applications associated witht his allocation IManagedInstance appsObj = _stgAdmin.GetRegisteredApps(); IWsmanItem handles = appsObj.GetProperty("ApplicationHandles"); if (handles != null) { foreach (IWsmanItem handle in handles) { IManagedInstance entryObj = _stgAdmin.GetApplicationAttributes(handle.ToString()); if (entryObj.GetProperty("ApplicationName").ToString().Equals(_applicationName) && entryObj.GetProperty("VendorName").ToString().Equals(_vendorName)) { _stgAdmin.RemoveApplication(handle.ToString()); } } } _stgAdmin.RemoveFpaclEntry(_handle.ToString()); ((DriveContainer)_parent).RemoveChildItem(this); } public override DriveItem NewItem(string name, string type, object newItem, DriveProvider provider) { PartnerAclDynamicParameters localParams = (PartnerAclDynamicParameters)provider.GetDynamicParameters(); if (name.Equals(string.Empty)) { if (localParams.TotalAllocationSize != _totalAllocationSize) { _stgAdmin.UpdateFpaclEntry(_handle.ToString(), localParams.TotalAllocationSize.ToString()); _totalAllocationSize = localParams.TotalAllocationSize; } return this; } return base.NewItem(name, type, newItem, provider); } public override object NewItemDynamicParameters(string itemTypeName, object newItemValue) { return new PartnerAclDynamicParameters(); } public string VendorName { get { return _vendorName; } } public uint TotalAllocationSize { get { return _totalAllocationSize; } set { _stgAdmin.UpdateFpaclEntry(_handle.ToString(), value.ToString()); _totalAllocationSize = value; } } public bool IsPartner { get { return _isPartner; } } public uint AttrType { get { return _attrType; } } public uint Handle { get { return _handle; } } public string ApplicationName { get { return Name; } } public string StoragePath { get { StringBuilder builder =new StringBuilder(); builder.Append("\\"); builder.Append(EncodeName(_vendorName)); builder.Append("\\"); builder.Append(Name); return builder.ToString(); } } } class Vendor : DriveContainer { StorageAdmin _stgAdmin; public Vendor(StorageAdmin stgAdmin,string name,DriveItem parent) : base(name,parent) { _stgAdmin = stgAdmin; } public override DriveItem NewItem(string name, string type, object newItem, DriveProvider provider) { PartnerAclDynamicParameters localParams = (PartnerAclDynamicParameters)provider.GetDynamicParameters(); string handle = _stgAdmin.AddFpaclEntry(name, _name, localParams.TotalAllocationSize.ToString()); IManagedInstance entryObj = _stgAdmin.GetNewAllocEntry(); entryObj.SetProperty("ApplicationName", name); entryObj.SetProperty("TotalAllocationSize", localParams.TotalAllocationSize.ToString()); entryObj.SetProperty("VendorName", _name); entryObj.SetProperty("IsPartner", "true"); entryObj.SetProperty("AttrType", "1"); DriveItem item = new AllocationEntry(_stgAdmin, entryObj, handle, this); this.AddChildItem(item); return item; } public override object NewItemDynamicParameters(string itemTypeName, object newItemValue) { return new PartnerAclDynamicParameters(); } } class Allocations : DriveContainer { StorageAdmin _stgAdmin; public Allocations(DriveItem parent) : base("Allocations",parent) { _stgAdmin = (StorageAdmin)parent; _items = null; } public override void GetChildItems(ChildWriter writer) { IManagedInstance outObj = _stgAdmin.GetFpaclList(); IWsmanItem handles = outObj.GetProperty("Handles"); if (handles != null) { List list = new List(); foreach (IWsmanItem handle in handles) { IManagedInstance entryObj = _stgAdmin.GetFpaclEntry(handle.ToString()); AddEntry(list, handle.ToString(), entryObj); } writer.Add(list.ToArray()); } } private AllocationEntry AddEntry(List list,string handle, IManagedInstance entryObj) { AllocationEntry result = null; string vendorName = entryObj.GetProperty("VendorName").ToString(); //find the vendor container using anynomous delegate DriveContainer vendorItem = (DriveContainer)list.Find( delegate(DriveItem item) { return string.Compare(item.Name, vendorName, true) == 0; } ); // No vendor container so add one if (vendorItem == null) { vendorItem = new Vendor( _stgAdmin,vendorName,this); list.Add(vendorItem); } result = new AllocationEntry(_stgAdmin, entryObj,handle, vendorItem); vendorItem.AddChildItem(result); return result; } public override DriveItem NewItem(string name, string type, object newItem, DriveProvider provider) { PartnerAclDynamicParameters localParams = (PartnerAclDynamicParameters)provider.GetDynamicParameters(); char[] pathChar = { '\\', '/' }; string[] names = name.Split(pathChar, StringSplitOptions.RemoveEmptyEntries); if (names.Length == 2) { string handle = _stgAdmin.AddFpaclEntry(names[1], names[0], localParams.TotalAllocationSize.ToString()); IManagedInstance entryObj = _stgAdmin.GetNewAllocEntry(); entryObj.SetProperty("ApplicationName", names[1]); entryObj.SetProperty("TotalAllocationSize", localParams.TotalAllocationSize.ToString()); entryObj.SetProperty("VendorName", names[0]); entryObj.SetProperty("IsPartner", "true"); entryObj.SetProperty("AttrType", "1"); List list = new List(); if (_items != null) list.AddRange(_items); AllocationEntry result= AddEntry(list, handle, entryObj); //list.Sort(_defaultComparer); _items = list.ToArray(); return result; } return base.NewItem(name, type, newItem, provider); } public override object NewItemDynamicParameters(string itemTypeName, object newItemValue) { return new PartnerAclDynamicParameters(); } } class Application : DriveItem { StorageAdmin _stgAdmin; uint _attrType; Guid _uuid; string _vendorName; string _applicationName; string _enterpriseName; uint _currentAllocationSize; bool _activeSession; bool _partner; bool _cached; static string[] _properties = {"AttrType", "UUID", "VendorName", "ApplicationName", "EnterpriseName", "CurrentAllocationSize", "ActiveSession", "Partner", "Handle", "StoragePath" }; public Application(StorageAdmin stgAdmin, string name, DriveItem parent) : base(name, "StorgeApplication",parent) { _stgAdmin = stgAdmin; } public uint AttrType { get { LoadProperties(); return _attrType; } } public Guid UUID { get { LoadProperties(); return _uuid; } } public string VendorName { get { LoadProperties(); return _vendorName; } } public string ApplicationName { get { LoadProperties(); return _applicationName; } } public string EnterpriseName { get { LoadProperties(); return _enterpriseName; } } public uint CurrentAllocationSize { get { LoadProperties(); return _currentAllocationSize; } } public bool ActiveSession { get { LoadProperties(); return _activeSession; } } public bool Partner { get { LoadProperties(); return _activeSession; } } public uint Handle { get { LoadProperties(); return uint.Parse(_name); } } public string StoragePath { get { StringBuilder builder = new StringBuilder(); builder.Append("\\\\"); builder.Append(EncodeName(EnterpriseName)); builder.Append("\\"); builder.Append(EncodeName(VendorName)); builder.Append("\\"); builder.Append(EncodeName(ApplicationName)); return builder.ToString(); } } public override string[] Properties { get { return _properties; } } private void LoadProperties() { if (_cached==false) { IManagedInstance outObj = _stgAdmin.GetApplicationAttributes(_name); _vendorName = outObj.GetProperty("VendorName").ToString(); //convert UUID to string _uuid = new Guid(Convert.FromBase64String(outObj.GetProperty("UUID").ToString())); _applicationName = outObj.GetProperty("ApplicationName").ToString(); _enterpriseName = outObj.GetProperty("EnterpriseName").ToString(); _currentAllocationSize = uint.Parse(outObj.GetProperty("CurrentAllocationSize").ToString()); _activeSession = bool.Parse(outObj.GetProperty("ActiveSession").ToString()); _partner = bool.Parse(outObj.GetProperty("Partner").ToString()); _attrType = uint.Parse(outObj.GetProperty("AttrType").ToString()); _cached = true; } } public override object GetProperty(Collection propList, DriveProvider provider) { if (provider !=null && provider.Force.IsPresent) _cached = false; return base.GetProperty(propList, provider); } public override void RemoveItem(DriveProvider provider) { _stgAdmin.RemoveApplication(_name); ((DriveContainer)_parent).RemoveChildItem(this); } } class Registrations : DriveContainer { StorageAdmin _stgAdmin; public Registrations(DriveItem parent) : base("Registrations",parent) { _stgAdmin = (StorageAdmin)parent; _items = null; } public override void GetChildItems(ChildWriter writer) { IManagedInstance outObj = _stgAdmin.GetRegisteredApps(); IWsmanItem handles = outObj.GetProperty("ApplicationHandles"); if (handles != null) { foreach (IWsmanItem handle in handles) { writer.Add(new Application(_stgAdmin, handle.ToString(),this)); } } } } private IManagedInstance GetNewAllocEntry() { IManagedInstance inputObj = _storageAdminRef.CreateMethodInput("GetStorageAllocEntry"); return inputObj; } private IManagedInstance GetGlobalStorageAttributes() { IManagedInstance inputObj = _storageAdminRef.CreateMethodInput("GetGlobalStorageAttributes"); IManagedInstance outObj = _storageAdminRef.InvokeMethod(inputObj); StorageManager.StorageOperationException.CheckResult(outObj); return outObj; } private IManagedInstance SetGlobalStorageAttributes(string name, string value) { IManagedInstance inputObj = _storageAdminRef.CreateMethodInput("SetGlobalStorageAttributes"); inputObj.SetProperty(name, value); IManagedInstance outObj = _storageAdminRef.InvokeMethod(inputObj); StorageManager.StorageOperationException.CheckResult(outObj); return outObj; } private IManagedInstance GetEaclEntry(string handle) { IManagedInstance inputObj = _storageAdminRef.CreateMethodInput("GetStorageEaclEntry"); inputObj.SetProperty("Handle", handle); IManagedInstance outObj = _storageAdminRef.InvokeMethod(inputObj); StorageManager.StorageOperationException.CheckResult(outObj); return outObj; } private IManagedInstance GetFpaclEntry(string handle) { IManagedInstance inputObj = _storageAdminRef.CreateMethodInput("GetStorageAllocEntry"); inputObj.SetProperty("Handle", handle); IManagedInstance outObj = _storageAdminRef.InvokeMethod(inputObj); StorageManager.StorageOperationException.CheckResult(outObj); return outObj; } private IManagedInstance GetEaclList() { IManagedInstance inputObj = _storageAdminRef.CreateMethodInput("EnumerateStorageEaclEntries"); IManagedInstance outObj = _storageAdminRef.InvokeMethod(inputObj); StorageManager.StorageOperationException.CheckResult(outObj); return outObj; } private IManagedInstance GetFpaclList() { IManagedInstance inputObj = _storageAdminRef.CreateMethodInput("EnumerateStorageAllocEntries"); IManagedInstance outObj = _storageAdminRef.InvokeMethod(inputObj); StorageManager.StorageOperationException.CheckResult(outObj); return outObj; } private IManagedInstance GetRegisteredApps() { IManagedInstance inputObj = _storageAdminRef.CreateMethodInput("AdminGetRegisteredApplications"); IManagedInstance outObj = _storageAdminRef.InvokeMethod(inputObj); StorageManager.StorageOperationException.CheckResult(outObj); return outObj; } private string AddEaclEntry(string name) { IManagedInstance inputObj = _storageAdminRef.CreateMethodInput("AddStorageEaclEntry"); inputObj.SetProperty("EnterpriseName",name); IManagedInstance outObj = _storageAdminRef.InvokeMethod(inputObj); StorageManager.StorageOperationException.CheckResult(outObj); return outObj.GetProperty("Handle").ToString(); } private string AddFpaclEntry(string appName, string vendor, string size) { IManagedInstance inputObj = _storageAdminRef.CreateMethodInput("AddStorageFpaclEntry"); inputObj = _storageAdminRef.CreateMethodInput("AddStorageFpaclEntry"); inputObj.SetProperty("AttrType","1"); inputObj.SetProperty("ApplicationName",appName); inputObj.SetProperty("VendorName",vendor); inputObj.SetProperty("IsPartner", "true"); inputObj.SetProperty("TotalAllocationSize",size); IManagedInstance outObj = _storageAdminRef.InvokeMethod(inputObj); StorageManager.StorageOperationException.CheckResult(outObj); return outObj.GetProperty("Handle").ToString(); } private void RemoveEaclEntry(string handle) { IManagedInstance inputObj = _storageAdminRef.CreateMethodInput("RemoveStorageEaclEntry"); inputObj.SetProperty("Handle", handle); IManagedInstance outObj = _storageAdminRef.InvokeMethod(inputObj); StorageManager.StorageOperationException.CheckResult(outObj); } private void RemoveFpaclEntry(string handle) { IManagedInstance inputObj = _storageAdminRef.CreateMethodInput("RemoveStorageFpaclEntry"); inputObj.SetProperty("Handle", handle); IManagedInstance outObj = _storageAdminRef.InvokeMethod(inputObj); StorageManager.StorageOperationException.CheckResult(outObj); } private void UpdateFpaclEntry(string handle, string newSize) { IManagedInstance inputObj = _storageAdminRef.CreateMethodInput("UpdateStorageFpaclEntry"); inputObj.SetProperty("Handle", handle); inputObj.SetProperty("NewAllocationSize", newSize); IManagedInstance outObj = _storageAdminRef.InvokeMethod(inputObj); StorageManager.StorageOperationException.CheckResult(outObj); } private void RemoveApplication(string handle) { IManagedInstance inputObj = _storageAdminRef.CreateMethodInput("AdminRemoveApplication"); inputObj.SetProperty("Handle", handle); IManagedInstance outObj = _storageAdminRef.InvokeMethod(inputObj); StorageManager.StorageOperationException.CheckResult(outObj); } private IManagedInstance GetApplicationAttributes(string handle) { IManagedInstance inputObj = _storageAdminRef.CreateMethodInput("AdminGetApplicationAttributes"); inputObj.SetProperty("Handle", handle); IManagedInstance outObj = _storageAdminRef.InvokeMethod(inputObj); StorageManager.StorageOperationException.CheckResult(outObj); return outObj; } } }