diff --git a/lib/forwardingV2/ForwardingV2.js b/lib/forwardingV2/ForwardingV2.js new file mode 100644 index 0000000..455f914 --- /dev/null +++ b/lib/forwardingV2/ForwardingV2.js @@ -0,0 +1,84 @@ +"use strict"; + +const api = require('./../restApi'); + +/** + * https://docs.opsgenie.com/docs/forwarding-rule-api + * + * @returns {{get: get, create: create, list: list, delete: delete, updateRule: updateRule}} + */ +function forwardingV2() { + let baseURL = '/v2/forwarding-rules/'; + + return { + /** + * https://docs.opsgenie.com/docs/forwarding-rule-api + * Also, you can check /samples/forwardingV2/create.js for a complete example. + * + * @param data + * @param config + * @param cb + */ + create: function (data, config, cb) { + api.post(baseURL, data, config, cb) + }, + + /** + * Get forwarding rule by identifier + * https://docs.opsgenie.com/docs/forwarding-rule-api#section--get-forwarding-rule- + * One of id, tinyId or alias parameters should be specified with get alert request as identifier. + * Alias option can only be used for open alerts + * example identifier objects: + * {identifier : "123", identifierType : "tiny"} + * {identifier : "123-23-123", identifierType : "id"} + * {identifier : "alertAlias", identifierType : "alias"} + * + * @param identifier + * @param config + * @param cb + */ + get: function (identifier, config, cb) { + let path = api.getPath(baseURL, identifier, null); + api.get(path, config, cb) + }, + + /** + * Delete a forwarding rule + * https://docs.opsgenie.com/docs/forwarding-rule-api#section--delete-forwarding-rule- + * + * @param identifier + * @param config + * @param cb + */ + delete: function (identifier, config, cb) { + let path = api.getPath(baseURL, identifier, null); + api.delete(path, config, cb) + }, + + /** + * https://docs.opsgenie.com/docs/forwarding-rule-api#section--list-forwarding-rules- + * + * @param params + * @param config + * @param cb + */ + list: function (params, config, cb) { + let path = api.getPathWithListParams(baseURL, params); + api.get(path, config, cb) + }, + + /** + * https://docs.opsgenie.com/docs/forwarding-rule-api#section--delete-forwarding-rule- + * @param identifier + * @param data + * @param config + * @param cb + */ + updateRule: function (identifier, data, config, cb) { + let path = api.getPath(baseURL, identifier, null); + api.put(path, data, config, cb); + }, + }; +} + +module.exports = forwardingV2; diff --git a/lib/opsgenie.js b/lib/opsgenie.js index 10a4352..2fa40d4 100644 --- a/lib/opsgenie.js +++ b/lib/opsgenie.js @@ -26,5 +26,6 @@ module.exports = function () { incident: require('./resources/Indicent')(), escalationV2: require('./escalationV2/EscalationV2')(), scheduleV2: require('./scheduleV2/ScheduleV2')(), + forwardingV2: require('./forwardingV2/ForwardingV2')(), }; }; diff --git a/lib/restApi.js b/lib/restApi.js index 0b14356..a8ca00c 100644 --- a/lib/restApi.js +++ b/lib/restApi.js @@ -13,6 +13,10 @@ exports.post = function executePostRequest(path, data, http_options, cb) { execute('POST', path, data, http_options, cb); }; +exports.put = function executePutRequest(path, data, http_options, cb) { + execute('PUT', path, data, http_options, cb); +}; + exports.delete = function executeDeleteRequest(path, http_options, cb) { execute('DELETE', path, null, http_options, cb); }; diff --git a/samples/forwardingV2/create.js b/samples/forwardingV2/create.js new file mode 100644 index 0000000..e0539a3 --- /dev/null +++ b/samples/forwardingV2/create.js @@ -0,0 +1,30 @@ +"use strict"; + +let opsgenie = require('../../'); +require('../configure'); + +let todaysDate = new Date(); +todaysDate.setDate(todaysDate.getDate() + 1); +let startDate = todaysDate.toISOString(); +todaysDate.setDate(todaysDate.getDate() + 5); +let endDate = todaysDate.toISOString(); + +let create_rule_json = { + fromUser: { + id: 'c1754eea-7f80-4126-8a64-a55c42439947' + }, + toUser: { + id: '830e4738-fc50-4bd0-b789-00fc0db76418' + }, + startDate: startDate, + endDate: endDate +}; + +opsgenie.forwardingV2.create(create_rule_json, function (error, alert) { + if (error) { + console.error(error); + } else { + console.log("Create Forwarding Rule Response:"); + console.log(alert); + } +}); diff --git a/samples/forwardingV2/delete.js b/samples/forwardingV2/delete.js new file mode 100644 index 0000000..5e59284 --- /dev/null +++ b/samples/forwardingV2/delete.js @@ -0,0 +1,18 @@ +"use strict"; + +let opsgenie = require('../../'); +require('../configure'); + +let delete_rule_identifier = { + identifier: "7b55b16e-3de7-44b1-8c04-31e5966a7a31", + identifierType : "id" +}; + +opsgenie.forwardingV2.delete(delete_rule_identifier, function (error, result) { + if (error) { + console.error(error); + } else { + console.log("Delete Forwarding Rule Response:"); + console.log(result); + } +}); diff --git a/samples/forwardingV2/get.js b/samples/forwardingV2/get.js new file mode 100644 index 0000000..d26b52d --- /dev/null +++ b/samples/forwardingV2/get.js @@ -0,0 +1,18 @@ +"use strict"; + +let opsgenie = require('../../'); +require('../configure'); + +let get_rule_identifier = { + identifier: "ebb3c4e0-b9b9-4b6d-89d8-6b10032719db", + identifierType : "id" +}; + +opsgenie.forwardingV2.get(get_rule_identifier, function (error, rule) { + if (error) { + console.error(error); + } else { + console.log("Get Rule Response for id: "); + console.log(rule); + } +}); diff --git a/samples/forwardingV2/list.js b/samples/forwardingV2/list.js new file mode 100644 index 0000000..2e8d3ad --- /dev/null +++ b/samples/forwardingV2/list.js @@ -0,0 +1,17 @@ +"use strict"; + +let opsgenie = require('../../'); +require('../configure'); + +let list_params = { + order : "desc" +}; + +opsgenie.forwardingV2.list(list_params, function (error, rules) { + if (error) { + console.error(error); + } else { + console.log("List Rules Response"); + console.log(rules); + } +}); diff --git a/samples/forwardingV2/update.js b/samples/forwardingV2/update.js new file mode 100755 index 0000000..b260ffa --- /dev/null +++ b/samples/forwardingV2/update.js @@ -0,0 +1,30 @@ +"use strict"; + +let opsgenie = require('../../'); +require('../configure'); + +let todaysDate = new Date(); +todaysDate.setDate(todaysDate.getDate() + 1); +let startDate = todaysDate.toISOString(); +todaysDate.setDate(todaysDate.getDate() + 14); +let endDate = todaysDate.toISOString(); + +let update_rule_json = { + fromUser: { + id: 'c1754eea-7f80-4126-8a64-a55c42439947' + }, + toUser: { + id: '830e4738-fc50-4bd0-b789-00fc0db76418' + }, + startDate: startDate, + endDate: endDate +}; + +opsgenie.forwardingV2.updateRule("ebb3c4e0-b9b9-4b6d-89d8-6b10032719db", update_rule_json, function (error, rule) { + if (error) { + console.error(error); + } else { + console.log("Update Rule Response"); + console.log(rule); + } +});