1414using System . Collections . ObjectModel ;
1515using SCOM_API . Models ;
1616using System . Configuration ;
17+ using System . Web . Http . Description ;
1718
1819namespace 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
0 commit comments