Skip to content

Commit

Permalink
Add new Opsgenie Alert API as AlertV2 and add new samples that uses t…
Browse files Browse the repository at this point in the history
…his.
  • Loading branch information
mustafanacar committed Jun 8, 2017
1 parent 89294b2 commit 30d8bb3
Show file tree
Hide file tree
Showing 32 changed files with 1,371 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function (grunt) {
},
jshint: {
all: {
src: ["lib/*.js", "test/*.js", "samples/**/*.js", "lib/resources/*.js"],
src: ["lib/*.js", "test/*.js", "samples/**/*.js", "lib/resources/*.js", "lib/alertV2/*.js"],
options: {
jshintrc: ".jshintrc"
}
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ OpsGenie Node.js SDK aims to access OpsGenie Web API through HTTP calls from Nod

OpsGenie Node.js SDK covers:

* Alert API (TODO: only attachFile method is missing)
* Alert API v2
* Alert API
* User API
* Group API
* Team API
Expand Down Expand Up @@ -41,7 +42,12 @@ opsgenie.configure({
```

```js
opsgenie.alert.get({id: "alert_id"}, function (error, alert) {
var get_alert_identifier = {
identifier : "alert_id",
identifierType : "id"
};

opsgenie.alertV2.get(get_alert_identifier, function (error, alert) {
if (error) {
// handle error
} else {
Expand Down
417 changes: 417 additions & 0 deletions lib/alertV2/AlertV2.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/opsgenie.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = function () {
configure: configure,
headers: headers,
configuration: configuration.default_options,
alertV2: require('./alertV2/AlertV2')(),
alert: require('./resources/Alert')(),
user: require('./resources/User')(),
group: require('./resources/Group')(),
Expand Down
42 changes: 34 additions & 8 deletions lib/resources/Alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,62 +20,82 @@ function alert() {
* One of id or alias parameters should be specified with close alert request.
* Alias option can only be used for open alerts
* example data: {id: 'id1'} or {alias: 'alias1'} etc.
*
* @deprecated
*/
close: function (data, config, cb) {
api.post(this.baseURL + 'close', data, config, cb);
},
/**
* Count alerts request is used to counts alerts in OpsGenie
*
* @deprecated
*/
count: function (data, config, cb) {
api.get(this.baseURL + 'count', data, config, cb);
},
/**
* data should be either {id: 'id1'} or {alias: 'alias1'}
*
* @deprecated
*/
listAlertNotes: function (data, config, cb) {
api.get(this.baseURL + 'note', data, config, cb);
},
/**
* data should be either {id: 'id1'} or {alias: 'alias1'}
*
* @deprecated
*/
listAlertLogs: function (data, config, cb) {
api.get(this.baseURL + 'log', data, config, cb);
},
/**
* data should be either {id: 'id1'} or {alias: 'alias1'}
*
* @deprecated
*/
listAlertRecipients: function (data, config, cb) {
api.get(this.baseURL + 'recipient', data, config, cb);
},
/**
* data should be either {id: 'id1'} or {alias: 'alias1'}
*
* @deprecated
*/
acknowledge: function (data, config, cb) {
api.post(this.baseURL + 'acknowledge', data, config, cb);
},
/**
* One of id or alias parameters should be specified with snooze alert request. Alias option can only be used for open alerts
* Also, endDate param should be provided - "endDate": ""2016-01-28 08:00""
*
* @deprecated
*/
snooze: function (data, config, cb) {
api.post(this.baseURL + 'snooze', data, config, cb);
},
/**
* data should be either {id: 'id1'} or {alias: 'alias1'}
*
* @deprecated
*/
renotify: function (data, config, cb) {
api.post(this.baseURL + 'renotify', data, config, cb);
},
/**
* data should be either {id: 'id1'} or {alias: 'alias1'}
*
* @deprecated
*/
takeOwnership: function (data, config, cb) {
api.post(this.baseURL + 'takeOwnership', data, config, cb);
},
/**
* One of id or alias parameters should be specified with assign request. Alias option can only be used for open alerts
* Also, owner param should be provided "owner":"[email protected]"
*
* @deprecated
*/
assign: function (data, config, cb) {
api.post(this.baseURL + 'assign', data, config, cb);
Expand All @@ -84,6 +104,8 @@ function alert() {
* Add team request is used to add new teams to alerts in OpsGenie. It takes the following parameters
* One of id or alias parameters should be specified with add team request. Alias option can only be used for open alerts
* Also, the new team that will be added should be added to parameters as "team": "operations"
*
* @deprecated
*/
addTeam: function (data, config, cb) {
api.post(this.baseURL + 'team', data, config, cb);
Expand All @@ -92,6 +114,8 @@ function alert() {
* Add recipient request is used to add new recipients(user or group) to alerts in OpsGenie
* One of id or alias parameters should be specified with add recipient request. Alias option can only be used for open alerts
* Also, the new recipient that will be added as "recipient":"[email protected]"
*
* @deprecated
*/
addRecipient: function (data, config, cb) {
api.post(this.baseURL + 'recipient', data, config, cb);
Expand All @@ -100,6 +124,8 @@ function alert() {
* Add note request is used to add notes to alerts in OpsGenie
* One of id or alias parameters should be specified with add note request. Alias option can only be used for open alerts
* Also, note param is mandatory as "note": "custom note"
*
* @deprecated
*/
addNote: function (data, config, cb) {
api.post(this.baseURL + 'note', data, config, cb);
Expand All @@ -109,6 +135,7 @@ function alert() {
* One of id or alias parameters should be specified with add tags request. Alias option can only be used for open alerts
* Also, tags param is mandatory as "tags": "emergency, delay"
*
* @deprecated
*/
addTags: function (data, config, cb) {
api.post(this.baseURL + 'tags', data, config, cb);
Expand All @@ -117,6 +144,8 @@ function alert() {
* Remove tags request is used to remove tags from alerts in OpsGenie
* One of id or alias parameters should be specified with add tags request. Alias option can only be used for open alerts
* Also, tags param is mandatory as "tags": "emergency, delay"
*
* @deprecated
*/
removeTags: function (data, config, cb) {
if (data.tags === undefined) {
Expand All @@ -129,6 +158,7 @@ function alert() {
* One of id or alias parameters should be specified with add details request. Alias option can only be used for open alerts
* Also, details param is mandatory as "details": {"prop1" : "val1", "prop2" : "val2"}
*
* @deprecated
*/
addDetails: function (data, config, cb) {
api.post(this.baseURL + 'details', data, config, cb);
Expand All @@ -137,6 +167,8 @@ function alert() {
* Remove details request is used to remove details from alerts in OpsGenie
* One of id or alias parameters should be specified with add details request. Alias option can only be used for open alerts
* Also, details param is mandatory as "keys" : { "prop1", "prop2" }
*
* @deprecated
*/
removeDetails: function (data, config, cb) {
if (data.keys === undefined) {
Expand All @@ -148,18 +180,12 @@ function alert() {
* Execute action request is used to execute predefined actions on alerts in OpsGenie
* One of id or alias parameters should be specified with execute action request. Alias option can only be used for open alerts
* Also, action param is mandatory as "action": "acknowledge"
*
* @deprecated
*/
executeAction: function (data, config, cb) {
api.post(this.baseURL + 'executeAction', data, config, cb);
}
/**
* Attach file request is used to attach files to alerts in OpsGenie. It should be sent as multipart HTTP request
* One of id or alias parameters should be specified with attach file request. Alias option can only be used for open alerts
* TODO: will be implemented
*/
/*attachFile: function (data, config, cb) {
api.post(this.baseURL + 'attachFile', data, config, cb);
}*/
};
ops = operations.attach(ops, defaultOperations);
return ops;
Expand Down
136 changes: 136 additions & 0 deletions lib/restApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
"use strict";

var utils = require('./utils');
var configuration = require('./configure');
var request = require('requestretry');
var qs = require('querystring');

exports.get = function executeGetRequest(path, http_options, cb) {
execute('GET', path, null, http_options, cb);
};

exports.post = function executePostRequest(path, data, http_options, cb) {
execute('POST', path, data, http_options, cb);
};

exports.delete = function executeDeleteRequest(path, http_options, cb) {
execute('DELETE', path, null, http_options, cb);
};

exports.patch = function executePostRequest(path, data, http_options, cb) {
execute('PATCH', path, data, http_options, cb);
};

function setRetryPolicy() {
request.defaults({
json: true,
maxAttempts: configuration.default_options.maxAttempts,
retryDelay: configuration.default_options.retryDelay,
retryStrategy: configuration.default_options.retryStrategy
});
}

function getHost() {
return configuration.default_options.host;
}

function getBasicHttpOptions(http_options) {
var apiKey = configuration.default_options.api_key;
configuration.default_http_options.headers['Authorization'] = 'GenieKey ' + apiKey;

if (!http_options) {
http_options = configuration.default_http_options;
} else {
http_options = utils.merge(http_options, configuration.default_http_options, true);
}

http_options.host = getHost();
return http_options;
}

function getPathWithIdentifier(path, params, actionPath) {
var pathParams;
var actPath = actionPath ? actionPath : "";

if (typeof params !== 'string') {
pathParams = params["identifier"] + actPath;
if (params["identifierType"] != null) {
pathParams += "?identifierType=" + params["identifierType"];
}
} else {
pathParams = params;
}

return params ? (path + pathParams) : (path);
}

exports.getPath = function (path, params, actionPath) {
return getPathWithIdentifier(path, params, actionPath)
};

exports.getPathWithData = function (path, params, actionPath, data) {
var pathWithData = getPathWithIdentifier(path, params, actionPath);
var dataParam = "";
if (typeof data !== 'string') {
dataParam = qs.stringify(data);
} else {
dataParam = data;
}

return data ? (pathWithData + "&" + dataParam) : pathWithData;
};

exports.getPathWithListParams = function (path, params) {
if (typeof params !== 'string') {
params = qs.stringify(params);
}

return params ? (path + "?" + params) : path;
};

function execute(method, path, data, http_options, cb) {
setRetryPolicy();
if (typeof http_options === "function") {
cb = http_options;
http_options = null;
}

http_options = getBasicHttpOptions(http_options);
http_options.method = method;

if (method === 'GET' || method === 'DELETE') {
http_options.uri = http_options.host + path;
http_options.body = null;
} else {
http_options.uri = http_options.host + path;
http_options.body = data;
http_options.json = true
}
request(http_options, function (error, response, body) {
handleResponse(error, response, body, cb)
});
}

function handleResponse(error, response, body, cb) {
if (error) {
console.log('Problem with request: ', error);
cb(error, null);
return;
}

var err = null, res = null;
if (response.statusCode < 200 || response.statusCode >= 300) {
if (process.env.NODE_ENV === 'development') {
err.response_stringified = JSON.stringify(response);
}
err = JSON.parse(body);
err.httpStatusCode = response.statusCode;
response = null;
} else {
var bodyJson = JSON.stringify(body);
res = JSON.parse(bodyJson);
}

cb(err, res);
}

31 changes: 31 additions & 0 deletions samples/alertV2/acknowledge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";

require('../configure');
var opsgenie = require('../../');



opsgenie.configure({
'host': 'https://api.opsgenie.com',
'api_key': "yourApiKey"
});

var acknowledge_alert_identifier = {
identifier: "5", // should be custom to your created alert
identifierType : "tiny"
};

var acknowledge_alert_data = {
note : "some note for acknowledge action",
user : "[email protected]",
source : "source of the acknowledge request"
};

opsgenie.alertV2.acknowledge(acknowledge_alert_identifier, acknowledge_alert_data, function (error, result) {
if (error) {
console.error(error);
} else {
console.log("Acknowledge Alert Response");
console.log(result);
}
});
Loading

0 comments on commit 30d8bb3

Please sign in to comment.