From 7b1e351ac5b67e44fcbdddbc7cc40ce63649d7e7 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Mon, 2 Feb 2015 20:55:21 -0500 Subject: [PATCH] #9 add downtime api endpoints --- README.md | 10 +++++ lib/api/downtime.js | 102 ++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 lib/api/downtime.js diff --git a/README.md b/README.md index ce94489..26d7293 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,16 @@ DD_API_KEY=YOUR_KEY_HERE DD_APP_KEY=YOUR_KEY_HERE node app.js * method used to check the status of a datadog snapshot * `dogapi.service_check(status, check, host, [[extra], [callback]])` * method used to post a new service check (see `dogapi.constants.STATUSES`) +* `dogapi.schedule_downtime(scope, [options], [callback])` + * method to schedule a new downtime +* `dogapi.update_downtime(downtime_id, [options], [callback])` + * method to update an existing downtime +* `dogapi.cancel_downtime(downtime_id, [callback])` + * method to cancel an existing downtime +* `dogapi.get_downtime(downtime_id, [callback])` + * method to get an existing downtime +* `dogapi.get_all_downtimes([current_only], [callback])` + * method to get all downtimes ## Sample Usage: diff --git a/lib/api/downtime.js b/lib/api/downtime.js new file mode 100644 index 0000000..98dcf66 --- /dev/null +++ b/lib/api/downtime.js @@ -0,0 +1,102 @@ +var util = require('util'); + +var downtime_api = function(){}; + +downtime_api.prototype.schedule_downtime = function(scope, options, callback){ + /* + * downtime_api.schedule_downtime(scope, [options], [callback]) + * + * method to schedule a new downtime + * + * `scope` the scope to schedule downtime for + * `options` optional `start` `end` and `message` paramaters + * `callback` is an optional function to call with the results of the api call + * callback(error, results, status_code) + */ + if(typeof scope !== 'string'){ + throw new Error('scope parameter must be a string'); + } + + if(typeof options === 'function'){ + callback = options; + options = {}; + } + options.scope = scope; + + this.request('POST', '/downtime', {body: options}, callback); +}; + +downtime_api.prototype.update_downtime = function(downtime_id, options, callback){ + /* + * downtime_api.update_downtime(downtime_id, [options], [callback]) + * + * method to update an existing downtime + * + * `downtime_id` the id of the downtime + * `options` optional `scope` `start` `end` and `message` paramaters + * `callback` is an optional function to call with the results of the api call + * callback(error, results, status_code) + */ + + if(typeof options === 'function'){ + callback = options; + options = {}; + } + + this.request('PUT', util.format('/downtime/%s', downtime_id), {body: options}, callback); +}; + +downtime_api.prototype.get_downtime = function(downtime_id, callback){ + /* + * downtime_api.get_downtime(downtime_id, [callback]) + * + * method to get an existing downtime + * + * `downtime_id` the id of the downtime + * `callback` is an optional function to call with the results of the api call + * callback(error, results, status_code) + */ + + this.request('GET', util.format('/downtime/%s', downtime_id), callback); +}; + +downtime_api.prototype.cancel_downtime = function(downtime_id, callback){ + /* + * downtime_api.cancel_downtime(downtime_id, [callback]) + * + * method to cancel an existing downtime + * + * `downtime_id` the id of the downtime + * `callback` is an optional function to call with the results of the api call + * callback(error, results, status_code) + */ + + this.request('DELETE', util.format('/downtime/%s', downtime_id), callback); +}; + +downtime_api.prototype.get_all_downtimes = function(current_only, callback){ + /* + * downtime_api.get_all_downtimes(downtime_id, [current_only], [callback]) + * + * method to get all downtimes + * + * `downtime_id` the id of the downtime + * `current_only` whether or not to get the current downtime only + * `callback` is an optional function to call with the results of the api call + * callback(error, results, status_code) + */ + + if(typeof current_only === 'function'){ + callback = current_only; + current_only = false; + } + + query = {}; + if(current_only){ + query.current_only = true; + } + + this.request('GET', '/downtime', {query: query}, callback); +}; + +return module.exports = downtime_api; diff --git a/package.json b/package.json index fc6c58a..5129dab 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dogapi", - "version": "0.1.8", + "version": "0.2.0", "description": "Datadog API Node.JS Client", "main": "lib/index.js", "scripts": {