Skip to content
This repository was archived by the owner on Mar 24, 2023. It is now read-only.

Commit 9eae0b2

Browse files
committed
added endpoint to retrieve all monitoring objects of a class
1 parent 477be94 commit 9eae0b2

3 files changed

Lines changed: 89 additions & 7 deletions

File tree

SCOM API/Controllers/SCOMObjectController.cs

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
using System.Collections.ObjectModel;
1515
using SCOM_API.Models;
1616
using System.Configuration;
17+
using System.Web.Http.Description;
1718

1819
namespace SCOM_API.Controllers
1920
{
21+
[RoutePrefix("API/MonitoringObject")]
2022
public class SCOMObjectController : ApiController
2123
{
2224
ManagementGroup mg = null;
@@ -38,7 +40,7 @@ public SCOMObjectController()
3840
/// <response code="400">Bad request check Id</response>
3941
/// <response code="404">Object not found</response>
4042
[HttpGet]
41-
[Route("API/MonitoringObject/{id}")]
43+
[Route("{id}")]
4244
public IHttpActionResult GetMonitoringObject(Guid Id)
4345
{
4446
//Check if guid is not empty
@@ -53,11 +55,11 @@ public IHttpActionResult GetMonitoringObject(Guid Id)
5355

5456
List<SCOMMonitoringObjectModel> MonitoringObject = new List<SCOMMonitoringObjectModel>();
5557

56-
if (monObject !=null)
58+
if (monObject != null)
5759
{
5860
//Get related objects
5961
ReadOnlyCollection<PartialMonitoringObject> RelatedObjects = monObject.GetRelatedPartialMonitoringObjects();
60-
62+
6163
//Add properties
6264
SCOMMonitoringObjectModel mObject = new SCOMMonitoringObjectModel();
6365
mObject.id = monObject.Id;
@@ -86,20 +88,84 @@ public IHttpActionResult GetMonitoringObject(Guid Id)
8688
mObject.relatedObjects = SCOMMonitoringObjectChildObjects;
8789

8890
MonitoringObject.Add(mObject);
89-
91+
9092
//Return object
9193
return Json(MonitoringObject);
9294
}
9395

9496
//If no object found return error code
9597
else
96-
{
97-
98+
{
99+
98100
HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.NotFound);
99101
message.Content = new StringContent("Monitoring object not found");
100102
throw new HttpResponseException(message);
101103
}
102104
}
103105

106+
/// <summary>
107+
///Gets all monitoring objects of specified class
108+
///Limited properties returned. Use MonitoringObject/Id endpoint for more details
109+
/// </summary>
110+
///<param name="classId">Your class guid</param>
111+
/// <response code="200">OK: returns member of class</response>
112+
/// <response code="204">class found but no objects / no members exist</response>
113+
/// <response code="400">Bad request check classId</response>
114+
/// <response code="404">No such class</response>
115+
116+
[Route("class/{classId}")]
117+
[HttpGet]
118+
[ResponseType(typeof(IEnumerable<SCOMClassObjectModel>))]
119+
public IHttpActionResult GetClassPartialMonitoringObject(Guid classId)
120+
{
121+
122+
if (classId == Guid.Empty)
123+
{
124+
throw new HttpResponseException(Request
125+
.CreateResponse(HttpStatusCode.BadRequest));
126+
}
127+
128+
//Use the input class id to create a criteria for our query
129+
ManagementPackClassCriteria classCriteria = new ManagementPackClassCriteria(string.Format("Id = '{0}'", classId.ToString()));
130+
IList<ManagementPackClass> monitoringClasses = mg.EntityTypes.GetClasses(classCriteria);
131+
132+
133+
List<PartialMonitoringObject> inputClassObjects = new List<PartialMonitoringObject>();
134+
135+
IObjectReader<PartialMonitoringObject> reader = mg.EntityObjects.GetObjectReader<PartialMonitoringObject>(monitoringClasses[0], ObjectQueryOptions.Default);
136+
137+
inputClassObjects.AddRange(reader);
138+
139+
List<SCOMClassObjectModel> classObjects = new List<SCOMClassObjectModel>();
140+
141+
//If objects are found add them to list and return
142+
if (inputClassObjects.Count > 0)
143+
{
144+
145+
foreach (PartialMonitoringObject classObject in inputClassObjects)
146+
{
147+
SCOMClassObjectModel inputClassObject = new SCOMClassObjectModel();
148+
inputClassObject.id = classObject.Id;
149+
inputClassObject.displayName = classObject.DisplayName;
150+
inputClassObject.healthState = classObject.HealthState.ToString();
151+
inputClassObject.path = classObject.Path;
152+
153+
classObjects.Add(inputClassObject);
154+
}
155+
156+
return Json(classObjects);
157+
}
158+
159+
//if class does not have any monitoring objects return 'no content'
160+
else
161+
{
162+
HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.NoContent);
163+
message.Content = new StringContent("Class found but no object exist");
164+
throw new HttpResponseException(message);
165+
}
166+
167+
}
104168
}
105-
}//END
169+
170+
}
171+
//END
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
6+
namespace SCOM_API.Models
7+
{
8+
public class SCOMClassObjectModel
9+
{
10+
public string displayName { get; set; }
11+
public string healthState { get; set; }
12+
public string path { get; set; }
13+
public Guid id { get; set; }
14+
}
15+
}

SCOM API/SCOM API.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@
275275
</Compile>
276276
<Compile Include="Models\SCOMAlertUpdateModel.cs" />
277277
<Compile Include="Models\SCOMComputerModelDetailed.cs" />
278+
<Compile Include="Models\SCOMClassObjectModel.cs" />
278279
<Compile Include="Models\SCOMObjectScheduleMaintenanceModel.cs" />
279280
<Compile Include="Models\SCOMObjectMaintenanceModel.cs" />
280281
<Compile Include="Models\SCOMComputerMaintenanceModel.cs" />

0 commit comments

Comments
 (0)