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

Commit 4140ac6

Browse files
committed
minor changes to response on put operations
1 parent 66206ba commit 4140ac6

4 files changed

Lines changed: 125 additions & 14 deletions

File tree

SCOM API/Controllers/SCOMAlertController.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,10 @@ public IList<MonitoringAlert> GetAlertByMonitoringObjectId(Guid MonitoringObject
160160
/// For more information please see technet documentation "http://bit.ly/2zblZLh"</param>
161161
/// <param name="Id">Specify alert guid you want to update</param>
162162
[HttpPut]
163-
[ResponseType(typeof(IEnumerable<MonitoringAlert>))]
163+
[ResponseType(typeof(HttpResponseMessage))]
164164
[Route("Alerts/{Id:Guid}")]
165-
public IList<MonitoringAlert> UpdateAlertById([FromUri()]Guid Id, [FromBody()] SCOMAlertUpdateModel Properties)
165+
//public IList<MonitoringAlert> UpdateAlertById([FromUri()]Guid Id, [FromBody()] SCOMAlertUpdateModel Properties)
166+
public IHttpActionResult UpdateAlertById([FromUri()]Guid Id, [FromBody()] SCOMAlertUpdateModel Properties)
166167
{
167168
if (Id == Guid.Empty)
168169
{
@@ -246,7 +247,8 @@ public IList<MonitoringAlert> UpdateAlertById([FromUri()]Guid Id, [FromBody()] S
246247
}
247248

248249
}
249-
return alerts;
250+
// creating OK response
251+
return Ok(new { message = "Updated ticket", ticketId = Id.ToString() });
250252

251253
}
252254

SCOM API/Controllers/SCOMMaintenanceController.cs

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Configuration;
1717
using System.Web.Http.Description;
1818
using Microsoft.EnterpriseManagement.Monitoring.MaintenanceSchedule;
19+
using Swashbuckle.Swagger.Annotations;
1920

2021
namespace 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

SCOM API/Models/SCOMObjectMaintenanceModel.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class SCOMObjectMaintenanceModel
1515
public string id { get; set; }
1616

1717
public string displayName { get; set; }
18+
1819
/// <summary>
1920
/// Minutes to maintenance
2021
/// </summary>
@@ -27,4 +28,24 @@ public class SCOMObjectMaintenanceModel
2728
/// </summary>
2829
public string comment { get; set; }
2930
}
31+
32+
public class SCOMUpdateObjectMaintenanceModel
33+
{
34+
/// <summary>
35+
/// Monitoring object id
36+
/// </summary>
37+
[Required(ErrorMessage = "Please provide object guid")]
38+
public string id { get; set; }
39+
40+
/// <summary>
41+
/// New endtime for the maintenance
42+
/// </summary>
43+
[Required(ErrorMessage = "End time is required")]
44+
public DateTime EndTime { get; set; }
45+
46+
/// <summary>
47+
/// comment for the maintenance
48+
/// </summary>
49+
public string comment { get; set; }
50+
}
3051
}

SCOM API/Models/SCOMObjectScheduleMaintenanceModel.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,40 @@ namespace SCOM_API.Models
88
{
99
public class SCOMObjectSchedMaintenanceModel
1010
{
11+
/// <summary>
12+
/// Guid for the maintenance schedule
13+
/// </summary>
14+
/// remarks GUID will be created
15+
public Guid scheduleId { get; set; }
16+
1117
/// <summary>
1218
/// Name of the maintenance schedule
1319
/// </summary>
1420
[Required]
1521
public string scheduleName { get; set; }
16-
[Required]
22+
1723
/// <summary>
1824
/// Monitoring object ID(s)
1925
/// </summary>
20-
public string[] id { get; set; }
2126
[Required]
27+
public string[] id { get; set; }
28+
2229
/// <summary>
2330
/// Start time and date
2431
/// </summary>
25-
public DateTime StartTime { get; set; }
2632
[Required]
33+
public DateTime StartTime { get; set; }
34+
2735
/// <summary>
2836
/// End time and date
2937
/// </summary>
30-
public DateTime EndTime { get; set; }
3138
[Required]
39+
public DateTime EndTime { get; set; }
40+
3241
/// <summary>
3342
/// Comment
3443
/// </summary>
44+
[Required]
3545
public string comment { get; set; }
3646
}
3747
}

0 commit comments

Comments
 (0)