Skip to content

Instantly share code, notes, and snippets.

@holyketzer
Last active December 22, 2015 00:29
Show Gist options
  • Save holyketzer/6389747 to your computer and use it in GitHub Desktop.
Save holyketzer/6389747 to your computer and use it in GitHub Desktop.
IDataObject client implementation
<ManagementPackFragment SchemaVersion="2.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TypeDefinitions>
<SchemaTypes>
<SchemaType ID="VIAcode.AwsExtension.Schema.ClassHealthStatisticsDataType" Accessibility="Public">
<xs:complexType name="ClassHealthStatistics" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:sc="http://schemas.microsoft.com/SystemCenter/Common/TypeExtensions>">
<xs:sequence minOccurs="1" maxOccurs="1">
<xs:element name="ClassName" type="xsd:string" minOccurs="1" maxOccurs="1" />
<xs:element name="HealthState" type="xsd:string" minOccurs="1" maxOccurs="1" />
<xs:element name="EntitiesCount" type="xsd:integer" minOccurs="1" maxOccurs="1" />
<xs:element name="Color" type="xs:anyType" sc:type="xsd://Microsoft.SystemCenter.Visualization.Library!Microsoft.SystemCenter.Visualization.ChartDataTypes/HexColor" minOccurs="1" maxOccurs="1" />
</xs:sequence>
</xs:complexType>
</SchemaType>
</SchemaTypes>
</TypeDefinitions>
</ManagementPackFragment>
using System;
using Microsoft.EnterpriseManagement.Presentation.DataAccess;
...
{
class DataObjectsFactory
{
private const string COLOUR_TYPE = "xsd://Microsoft.SystemCenter.Visualization.Library!Microsoft.SystemCenter.Visualization.ChartDataTypes/HexColor";
private IDataModel model;
public DataObjectsFactory(IDataModel dataModel)
{
model = dataModel;
}
public TObject Create<TObject>() where TObject : DataObjectWrapperBase, new()
{
var obj = new TObject();
obj.Wrapped = model.CreateInstance(obj.GetDataType(model));
var particularObj = obj as ClassHealthStatisticsDataObject;
if (particularObj != null)
{
particularObj.Color = CreateByName(COLOUR_TYPE);
}
return obj;
}
public IDataObject CreateByName(string typeName)
{
var objectType = model.Types[typeName];
if (objectType == null)
{
objectType = model.Types.Create(typeName);
objectType.Properties.Create("HexString", typeof(String));
};
return model.CreateInstance(objectType);
}
public IDataObjectCollection WrapWithCollection<TObject>(TObject obj) where TObject : DataObjectWrapperBase
{
var collection = model.CreateCollection(obj.DataType);
collection.Add(obj.Wrapped);
return collection;
}
public IDataObjectCollection WrapWithCollection<TObject>(TObject[] objArray) where TObject : DataObjectWrapperBase
{
IDataObjectCollection collection = null;
foreach (var obj in objArray)
{
if (collection == null)
{
collection = model.CreateCollection(obj.DataType);
}
collection.Add(obj.Wrapped);
}
return collection;
}
}
}
using Microsoft.EnterpriseManagement.Presentation.DataAccess;
public abstract class DataObjectWrapperBase
{
public abstract string DataType { get; }
public IDataObject Wrapped { get; set; }
public IDataType GetDataType(IDataModel dataModel)
{
return dataModel.Types[DataType] ?? PopulateTypeProperties(dataModel.Types.Create(DataType));
}
protected abstract IDataType PopulateTypeProperties(IDataType dataType);
}
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
using Microsoft.EnterpriseManagement.Monitoring;
using Microsoft.EnterpriseManagement.Presentation.DataAccess;
using Microsoft.EnterpriseManagement.Presentation.Security;
using Microsoft.Practices.Unity;
using Microsoft.EnterpriseManagement;
...
{
[Export]
[DataProvider]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class GetHealthStatistics
{
[Dependency]
public IOMConnectionManager ConnectionManager { get; set; }
[Dependency]
public IDataCommandContext DataContext { get; set; }
private const string ERROR_COLOUR = "#FF0000";
private const string WARNING_COLOUR = "#FFFF00";
private const string HEALTHY_COLOUR = "#00FF00";
private const string UNINITIALIZED_COLOUR = "#c9c9c9";
private const string HEALTHY_STATE = "OK";
private const string WARNING_STATE = "Warning";
private const string ERROR_STATE = "Critical";
private const string UNINITIALIZED_STATE = "Uninitialized";
private const string AMAZON_MP_NAME = "Amazon.AmazonWebServices";
private const string AMAZON_MP_PUBLIC_TOKEN = "8edc8f0ed2e0ec7b";
private const string AMAZON_REGION_PROPERTY_NAME = "RegionName";
private const string ARGUMENT_EXCEPTION_MESSAGE = "'Class Name' argument is required!";
[DataCommand]
public IEnumerable<IDataObject> GetEntitiesCount(string className, string amazonRegionName)
{
if (string.IsNullOrEmpty(className))
{
throw new ArgumentException(ARGUMENT_EXCEPTION_MESSAGE);
}
var connection = ConnectionManager.GetConnection(DataContext.ConnectionSessionTickets["Default"]);
var amazonMp = connection.GetManagementPack(AMAZON_MP_NAME, AMAZON_MP_PUBLIC_TOKEN, new Version(1, 0));
var amazonMpClass = connection.EntityTypes.GetClass(className, amazonMp);
var classObjects = connection.EntityObjects.GetObjectReader<MonitoringObject>(amazonMpClass, ObjectQueryOptions.Default);
var healthyCount = 0;
var warningCount = 0;
var errorCount = 0;
var uninitializedCount = 0;
foreach (var classObject in classObjects)
{
var objectRegionName = classObject.Values.FirstOrDefault(value => value.Type.Name == AMAZON_REGION_PROPERTY_NAME);
// if entity is from different Amazon Region we skip it
// if Amazon Region is empty or null we take all entities into account
if (objectRegionName == null || (!string.IsNullOrEmpty(amazonRegionName) && !amazonRegionName.Equals(objectRegionName.Value))) continue;
switch (classObject.HealthState)
{
case HealthState.Success:
healthyCount++;
break;
case HealthState.Warning:
warningCount++;
break;
case HealthState.Error:
errorCount++;
break;
case HealthState.Uninitialized:
uninitializedCount++;
break;
}
}
//TODO: Replace repeating code
//throw new Exception("Debug exception");
//EventLog.WriteEntry("Health Service Modules", "GetHealthStatistics 1.0.0.3", EventLogEntryType.Information, 4222);
var result = CreateDataObjectCollection(new []
{
CreateStatisticsObject(className, HEALTHY_COLOUR, healthyCount, HEALTHY_STATE),
CreateStatisticsObject(className, WARNING_COLOUR, warningCount, WARNING_STATE),
CreateStatisticsObject(className, ERROR_COLOUR, errorCount, ERROR_STATE),
CreateStatisticsObject(className, UNINITIALIZED_COLOUR, uninitializedCount, UNINITIALIZED_STATE)
});
return result;
}
private DataObjectWrapperBase CreateStatisticsObject(string className, string color, int entitiesCount, string healthState)
{
var factory = new DataObjectsFactory(DataContext.DataModel);
var stats = factory.Create<ClassHealthStatisticsDataObject>();
stats.ClassName = className;
stats.HexColor = color;
stats.EntitiesCount = entitiesCount;
stats.HealthState = healthState;
return stats;
}
private IDataObjectCollection CreateDataObjectCollection(DataObjectWrapperBase[] dataObjectArray)
{
var factory = new DataObjectsFactory(DataContext.DataModel);
return factory.WrapWithCollection(dataObjectArray);
}
public static ManagementPackTypeProjection GetRelationshipTypeProjection(EnterpriseManagementGroup mg, string relationshipName)
{
return (from typeProjection in mg.EntityTypes.GetTypeProjections()
where typeProjection.Name == relationshipName
select typeProjection).First();
}
public static ManagementPackRelationship GetRelationshipClasses(ManagementGroup mg, string relationshipName)
{
return (from managementPackRelationship in mg.EntityTypes.GetRelationshipClasses()
where managementPackRelationship.Name == relationshipName
select managementPackRelationship).First();
}
}
}
using Microsoft.EnterpriseManagement.Presentation.DataAccess;
...
{
public class ClassHealthStatisticsDataObject : DataObjectWrapperBase
{
private const string CLASS_NAME_PROPERTY = "ClassName";
private const string HEALTH_STATE_PROPERTY = "HealthState";
private const string ENTITIES_COUNT_PROPERTY = "EntitiesCount";
private const string COLOR_PROPERTY = "Color";
private const string COLOUR_PROPERTY = "HexString";
public override string DataType
{
get { return "xsd://full_MP_name!full_schema_name/ClassHealthStatistics"; }
}
#region Properties
public string ClassName
{
get { return Wrapped[CLASS_NAME_PROPERTY] as string; }
set { Wrapped[CLASS_NAME_PROPERTY] = value; }
}
public string HealthState
{
get { return Wrapped[HEALTH_STATE_PROPERTY] as string; }
set { Wrapped[HEALTH_STATE_PROPERTY] = value; }
}
public int EntitiesCount
{
get { return (int)Wrapped[ENTITIES_COUNT_PROPERTY]; }
set { Wrapped[ENTITIES_COUNT_PROPERTY] = value; }
}
public IDataObject Color
{
get { return Wrapped[COLOR_PROPERTY] as IDataObject; }
set { Wrapped[COLOR_PROPERTY] = value; }
}
internal string HexColor
{
get { return Color[COLOUR_PROPERTY] as string; }
set { Color[COLOUR_PROPERTY] = value; }
}
#endregion Properties
protected override IDataType PopulateTypeProperties(IDataType dataType)
{
dataType.Properties.Create(CLASS_NAME_PROPERTY, typeof(string));
dataType.Properties.Create(HEALTH_STATE_PROPERTY, typeof(string));
dataType.Properties.Create(ENTITIES_COUNT_PROPERTY, typeof(int));
dataType.Properties.Create(COLOR_PROPERTY, typeof(IDataObject));
return dataType;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment