1414using System . Collections . ObjectModel ;
1515using Swashbuckle . AspNetCore . SwaggerGen ;
1616using System . Web . Http . Description ;
17+ using SCOM_API . Models ;
1718
1819namespace SCOM_API . Controllers
1920{
@@ -79,7 +80,7 @@ public IList<MonitoringAlert> GetAlertById(Guid Id)
7980 /// <param name="ComputerName">FQDN of the windows computer</param>
8081 /// <param name="IncClosed">if true closed alert is also returned. Default is 'false'</param>
8182 [ HttpGet ]
82- [ Route ( "Alerts/{ComputerName}" ) ]
83+ [ Route ( "Alerts/Computer/ {ComputerName}" ) ]
8384 public IList < MonitoringAlert > GetAlertByComputerName ( string ComputerName , bool ? IncClosed = false )
8485 {
8586 if ( string . IsNullOrEmpty ( ComputerName ) )
@@ -93,7 +94,7 @@ public IList<MonitoringAlert> GetAlertByComputerName(string ComputerName, bool?
9394 {
9495 //Set alert criteria
9596 var Criteria = string . Format ( "MonitoringObjectPath = '{0}'" , ComputerName ) ;
96- /// Get alerts
97+ //Get alerts
9798 MonitoringAlertCriteria alertCriteria = new MonitoringAlertCriteria ( Criteria ) ;
9899 var Alert = mg . OperationalData . GetMonitoringAlerts ( alertCriteria , default ( DateTime ) ) ;
99100
@@ -113,31 +114,74 @@ public IList<MonitoringAlert> GetAlertByComputerName(string ComputerName, bool?
113114 }
114115
115116 /// <summary>
116- /// Updates the specified alert .
117+ /// Get all alerts related to the specific monitoring object .
117118 /// </summary>
118- /// <param name="ResolutionState">If specified as 255 (closed) and alert is raised by monitor. The corresponding monitor will be reset</param>
119- /// <param name="TicketId">set if you want to update alert with a ticket id</param>
120- /// <param name="Id">the alert GUID</param>
119+ /// <param name="MonitoringObjectId">SCOM GUID of the monitoring object</param>
120+ /// <param name="IncClosed">if true closed alert is also returned. Default is 'false'</param>
121+ [ HttpGet ]
122+ [ Route ( "Alerts/MonitoringObject/{MonitoringObjectId:Guid}" ) ]
123+ public IList < MonitoringAlert > GetAlertByMonitoringObjectId ( Guid MonitoringObjectId , bool ? IncClosed = false )
124+ {
125+ if ( MonitoringObjectId == Guid . Empty )
126+ {
127+ throw new HttpResponseException ( Request
128+ . CreateResponse ( HttpStatusCode . BadRequest ) ) ;
129+ }
130+
131+ //If include closed alerts
132+ if ( IncClosed == true )
133+ {
134+ //Set alert criteria
135+ var Criteria = string . Format ( "MonitoringObjectId = '{0}'" , MonitoringObjectId ) ;
136+ ///Get alerts
137+ MonitoringAlertCriteria alertCriteria = new MonitoringAlertCriteria ( Criteria ) ;
138+ var Alert = mg . OperationalData . GetMonitoringAlerts ( alertCriteria , default ( DateTime ) ) ;
139+
140+ return Alert ;
141+ }
142+
143+ else
144+ {
145+ //set alert criteria
146+ var Criteria = string . Format ( "MonitoringObjectId = '{0}' AND ResolutionState = 0" , MonitoringObjectId ) ;
147+ //Get alerts
148+ MonitoringAlertCriteria alertCriteria = new MonitoringAlertCriteria ( Criteria ) ;
149+ var Alert = mg . OperationalData . GetMonitoringAlerts ( alertCriteria , default ( DateTime ) ) ;
150+
151+ return Alert ;
152+ }
153+ }
154+
155+ /// <summary>
156+ /// Updates the specific alert.
157+ /// </summary>
158+ /// <param name="Properties">Json object to update alert properties.
159+ /// All set properties are available.
160+ /// For more information please see technet documentation "http://bit.ly/2zblZLh"</param>
161+ /// <param name="Id">Specify alert guid you want to update</param>
121162 [ HttpPut ]
122163 [ ResponseType ( typeof ( IEnumerable < MonitoringAlert > ) ) ]
123- [ Route ( "Alerts" ) ]
124- public IList < MonitoringAlert > UpdateAlertById ( Guid Id , byte ResolutionState , string TicketId = "" )
164+ [ Route ( "Alerts/{Id:Guid} " ) ]
165+ public IList < MonitoringAlert > UpdateAlertById ( [ FromUri ( ) ] Guid Id , [ FromBody ( ) ] SCOMAlertUpdateModel Properties )
125166 {
126- if ( string . IsNullOrEmpty ( ResolutionState . ToString ( ) ) )
167+ if ( Id == Guid . Empty )
127168 {
128169 throw new HttpResponseException ( Request
129170 . CreateResponse ( HttpStatusCode . BadRequest ) ) ;
130171 }
131172
173+ // Declare variables
174+ var ResolutionState = Properties . resolutionState ;
132175
133176 //alert criteria
134177 var Criteria = string . Format ( "Id = '{0}'" , Id ) ;
135178 MonitoringAlertCriteria alertCriteria = new MonitoringAlertCriteria ( Criteria ) ;
136179 var alerts = mg . OperationalData . GetMonitoringAlerts ( alertCriteria , default ( DateTime ) ) ;
180+
137181 foreach ( MonitoringAlert a in alerts )
138182 {
139183 //If resolution state is set to closed and alert is raised by a monitor. Reset the monitor
140- if ( a . IsMonitorAlert & ResolutionState . ToString ( ) == "255" )
184+ if ( a . IsMonitorAlert & ResolutionState == "255" )
141185 {
142186 //Get object and monitor that raised the alert
143187 Guid monitoringObjectId = a . MonitoringObjectId ;
@@ -146,27 +190,59 @@ public IList<MonitoringAlert> UpdateAlertById(Guid Id, byte ResolutionState, str
146190 var monObject = mg . EntityObjects . GetObject < MonitoringObject > ( monitoringObjectId , ObjectQueryOptions . Default ) ;
147191 //reset the monitor to 'close' the alert
148192 monObject . ResetMonitoringState ( monitor ) ;
149-
193+
150194 }
195+
196+
151197 else
152198 {
153-
154- if ( string . IsNullOrWhiteSpace ( TicketId ) )
199+
200+ if ( string . IsNullOrEmpty ( ResolutionState ) )
155201 {
156- a . ResolutionState = ResolutionState ;
157- string comment = "Changed resolution state (API)" ;
202+ a . TfsWorkItemId = Properties . tfsWorkItemId ;
203+ a . TfsWorkItemOwner = Properties . tfsWorkItemOwner ;
204+ a . Owner = Properties . owner ;
205+ a . CustomField1 = Properties . customField1 ;
206+ a . CustomField2 = Properties . customField2 ;
207+ a . CustomField3 = Properties . customField3 ;
208+ a . CustomField4 = Properties . customField4 ;
209+ a . CustomField5 = Properties . customField5 ;
210+ a . CustomField6 = Properties . customField6 ;
211+ a . CustomField7 = Properties . customField7 ;
212+ a . CustomField8 = Properties . customField8 ;
213+ a . CustomField9 = Properties . customField9 ;
214+ a . CustomField10 = Properties . customField10 ;
215+
216+ string comment = "Alert properties updated" ;
158217 a . Update ( comment ) ;
159218
160219 }
161- //If ticket id is specified.
220+ //If resolution state is specified
162221 else
163222 {
164- a . ResolutionState = ResolutionState ;
165- a . TicketId = TicketId ;
166- string comment = "Changed resolution state and ticket id (API)" ;
223+ a . TfsWorkItemId = Properties . tfsWorkItemId ;
224+ a . TfsWorkItemOwner = Properties . tfsWorkItemOwner ;
225+ a . Owner = Properties . owner ;
226+ a . CustomField1 = Properties . customField1 ;
227+ a . CustomField2 = Properties . customField2 ;
228+ a . CustomField3 = Properties . customField3 ;
229+ a . CustomField4 = Properties . customField4 ;
230+ a . CustomField5 = Properties . customField5 ;
231+ a . CustomField6 = Properties . customField6 ;
232+ a . CustomField7 = Properties . customField7 ;
233+ a . CustomField8 = Properties . customField8 ;
234+ a . CustomField9 = Properties . customField9 ;
235+ a . CustomField10 = Properties . customField10 ;
236+
237+ //Convert string resolution state to byte type
238+ var Res = Convert . ToByte ( ResolutionState ) ;
239+ a . ResolutionState = Res ;
240+ a . TicketId = Properties . ticketId ;
241+ var comments = $ "Updated and changed resolution state: { ResolutionState } ";
242+ string comment = comments ;
167243 a . Update ( comment ) ;
168244 }
169-
245+
170246 }
171247
172248 }
0 commit comments