1616using System . Configuration ;
1717using System . Web . Http . Description ;
1818using Microsoft . EnterpriseManagement . Monitoring . MaintenanceSchedule ;
19+ using Swashbuckle . Swagger . Annotations ;
1920
2021namespace SCOM_API . Controllers
2122{
@@ -59,7 +60,7 @@ public IHttpActionResult EnableComputerMaintenance(SCOMComputerMaintenanceModel
5960
6061 List < SCOMComputerMaintenanceModel > MaintenanceComputers = new List < SCOMComputerMaintenanceModel > ( ) ;
6162
62- /// travers trough all classes to get monitoring objects
63+ //travers trough all classes to get monitoring objects
6364 foreach ( ManagementPackClass monClass in monClasses )
6465 {
6566 monObjects . AddRange ( mg . EntityObjects . GetObjectReader < MonitoringObject > ( criteria , ObjectQueryOptions . Default ) ) ;
@@ -111,8 +112,6 @@ public IHttpActionResult EnableComputerMaintenance(SCOMComputerMaintenanceModel
111112
112113
113114
114-
115-
116115 /// <summary>
117116 /// Puts the specified monitoring object in maintenance mode.
118117 /// </summary>
@@ -190,14 +189,92 @@ public IHttpActionResult EnableObjectMaintenance(SCOMObjectMaintenanceModel Data
190189
191190 }
192191
192+ /// <summary>
193+ /// Updates or ends existing maintenance mode for object.
194+ /// </summary>
195+ /// <param name="Data">Json string with object id and new endTime</param>
196+ /// <param name="EndNow">If true, maintenance will end</param>
197+ /// <example>
198+ /// {
199+ /// "id": "Guid",
200+ /// "EndTime": "2017-07-07T19:00:00.000Z"
201+ /// }
202+ /// </example>
203+ /// <response code="200">Successfully updated maintenance mode</response>
204+ /// <response code="400">Bad request. Check json input</response>
205+ /// <response code="304">Object not in maintenance. Nothing to update</response>
206+
207+ [ HttpPut ]
208+ [ SwaggerResponse ( HttpStatusCode . OK , "Object maintenance mode updated" ) ]
209+ [ ResponseType ( typeof ( HttpResponseMessage ) ) ]
210+ [ Route ( "API/ObjectMaintenance" ) ]
211+ public IHttpActionResult UpdateObjectMaintenance ( SCOMUpdateObjectMaintenanceModel Data , bool EndNow = false )
212+ {
213+ //Validate input
214+ if ( ModelState . IsValid )
215+ {
216+ //create a Guid from the json input
217+ var ObjectId = new Guid ( Data . id ) ;
218+ //get the monitoring object by Guid
219+ var monObject = mg . EntityObjects . GetObject < MonitoringObject > ( ObjectId , ObjectQueryOptions . Default ) ;
220+
221+ //If object not in maintenance not modified
222+ if ( ! monObject . InMaintenanceMode )
223+ {
224+ {
225+ HttpResponseMessage res = new HttpResponseMessage ( HttpStatusCode . NotModified ) ;
226+ res . Content = new StringContent ( "Specified object not in maintenance mode. Nothing to update..." ) ;
227+ throw new HttpResponseException ( res ) ;
228+ }
229+ }
230+
231+ //If object in maintenanance update
232+ else
233+ {
234+ //If endNow parameter validate true. End maintenance mode
235+ if ( EndNow . Equals ( true ) )
236+ {
237+ monObject . StopMaintenanceMode ( DateTime . UtcNow , TraversalDepth . Recursive ) ;
238+
239+ }
240+
241+ // Get the maintenance window
242+ MaintenanceWindow MaintenanceWindow = monObject . GetMaintenanceWindow ( ) ;
243+
244+ //If user specifies an end date
245+ if ( Data . EndTime > DateTime . MinValue )
246+ {
247+
248+ //Compare specified end time with current maintenance end time
249+ int TimeCompare = DateTime . Compare ( Data . EndTime , MaintenanceWindow . ScheduledEndTime ) ;
250+ //Update end time but use same reason and comment
251+ monObject . UpdateMaintenanceMode ( Data . EndTime , MaintenanceWindow . Reason , MaintenanceWindow . Comments ) ;
252+ }
253+
254+ }
255+ // creating OK response
256+ return Ok ( new { message = "Updated maintenance mode" , monitoringObjectId = Data . id } ) ;
257+
258+ }
259+
260+ // throw error message
261+ else
262+ {
263+ HttpResponseMessage res = new HttpResponseMessage ( HttpStatusCode . BadRequest ) ;
264+ res . Content = new StringContent ( "Please check request body" ) ;
265+ throw new HttpResponseException ( res ) ;
266+ }
267+ }
268+
269+
193270 /// <summary>
194271 /// Creates a new maintenance schedule with the specified monitoring objects.
195272 /// </summary>
196- /// <param name="Data">Json string scheduleName, object ids, StartTime, EndTime, comment</param>
273+ /// <param name="Data">scheduleName, object ids, StartTime, EndTime, comment are mandatory </param>
197274 /// <example>
198275 /// {
199276 /// "scheduleName": "string",
200- /// "id": "[monitoringObjectId]",
277+ /// "id": "[monitoringObjectId's ]",
201278 /// "StartTime": "2017-05-22T07:01:00.374Z",
202279 /// "EndTime": "2017-05-22T08:01:00.374Z",
203280 /// "comment": "doing maintenance"
@@ -233,7 +310,6 @@ public IHttpActionResult ScheduleObjectMaintenance(SCOMObjectSchedMaintenanceMod
233310
234311 ObjectList . Add ( item ) ;
235312 }
236- //
237313
238314 //create a recurrencePattern this is 'sourced' from OmCommands.10.dll ( new-scommaintenanceSchedule CMDLET )
239315 //read more: https://docs.microsoft.com/en-us/powershell/systemcenter/systemcenter2016/operationsmanager/vlatest/new-scommaintenanceschedule
@@ -263,10 +339,11 @@ public IHttpActionResult ScheduleObjectMaintenance(SCOMObjectSchedMaintenanceMod
263339 var shed = MaintenanceSchedule . GetMaintenanceScheduleById ( guid , mg ) ;
264340 List < SCOMObjectSchedMaintenanceModel > MaintenanceScheduleList = new List < SCOMObjectSchedMaintenanceModel > ( ) ;
265341 SCOMObjectSchedMaintenanceModel mSched = new SCOMObjectSchedMaintenanceModel ( ) ;
342+ mSched . scheduleId = guid ;
343+ mSched . scheduleName = shed . ScheduleName ;
266344 mSched . id = array ;
267345 mSched . StartTime = shed . ActiveStartTime ;
268346 mSched . EndTime = shed . ScheduledEndTime ;
269- mSched . scheduleName = shed . ScheduleName ;
270347 mSched . comment = shed . Comments ;
271348
272349 MaintenanceScheduleList . Add ( mSched ) ;
@@ -276,5 +353,6 @@ public IHttpActionResult ScheduleObjectMaintenance(SCOMObjectSchedMaintenanceMod
276353
277354 }
278355
356+
279357 }
280358} //END
0 commit comments