/** * Usage: */ var EVENT_STATUS = { ACTIVE: 'ACTIVE', SUSPENDED: 'SUSPENDED', TERMINATED: 'TERMINATED' }; var isValidStatusCode = function(statusCode) { return statusCode >= 200 && statusCode <= 299; }; var getEvents = function(status, propertyNames, propertyValues, formName) { var formClause = formName ? '&form=' + encodeURIComponent(formName) : ''; // Prepare the HTTP request to locate existing active events var getEvents = http.request({ 'endpoint': 'xMatters', 'method': 'GET', 'path': '/api/xm/1/events?status=' + status + '&propertyName=' + propertyNames.join(',') + '&propertyValue=' + propertyValues.join(',') + '&propertyValueOperator=EQUALS' + formClause, 'headers': { 'Content-Type': 'application/json' } }); var response = getEvents.write(); if(isValidStatusCode(response.statusCode)) { var events = []; if(response.body) { events = JSON.parse(response.body).data; } return events; } else { throw 'getEvents returned an error: ' + response.statusCode; } }; var terminateEvent = function(eventId) { var response = http.request({ 'endpoint': 'xMatters', 'method': 'POST', 'path': '/api/xm/1/events', 'headers': { 'Content-Type': 'application/json' } }).write({ id: eventId, status: EVENT_STATUS.TERMINATED }); if(isValidStatusCode(response.statusCode)) { return true; } else { return false; } }; /** * Determine if an HTTP status code represents success * * @param {number} statusCode from an HTTP request * @returns {boolean} true if successful; false otherwise */ exports.isValidStatusCode = isValidStatusCode; /** * Terminate active events matching a set of properties * * Note: When property is a string type, be sure to include the language * with the property name (e.g. "alert_id#en"). If the property is * a numeric, however, do not include the language (e.g. * "alert_count"). * * @param {Object} properties a set of properties to search * @param {string} formName name of the form that created the events * @returns {void} */ exports.terminateEvents = function (properties, formName) { // Prepare the search criteria for matching events var propNames = []; var propValues = []; for (var key in properties) { propNames.push(encodeURIComponent(key)); propValues.push(encodeURIComponent(properties[key])); } if(propNames.length <= 0) { console.log('Skipping terminate events because it was called without any search properties'); return false; } var isSuccessful = true; try { var activeEvents = getEvents(EVENT_STATUS.ACTIVE, propNames, propValues, formName); for(var i = 0; i < activeEvents.length; i++) { isSuccessful = terminateEvent(activeEvents[i].id) && isSuccessful; } } catch(e) { console.log('A problem occurred while terminating active events: ' + JSON.stringify(e)); isSuccessful = false; } try { var suspendedEvents = getEvents(EVENT_STATUS.SUSPENDED, propNames, propValues, formName); for(var j = 0; j < suspendedEvents.length; j++) { isSuccessful = terminateEvent(suspendedEvents[j].id) && isSuccessful; } } catch(e) { console.log('A problem occurred while terminating suspended events: ' + JSON.stringify(e)); isSuccessful = false; } return isSuccessful; };