From da2ddd002712ecd593502d3585028a383d08a936 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Wed, 7 Aug 2013 19:43:00 -0400 Subject: [PATCH] fix #1: add support for screenboards --- README.md | 10 ++++ lib/api/screen.js | 117 ++++++++++++++++++++++++++++++++++++++++++++++ lib/index.js | 2 + package.json | 2 +- 4 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 lib/api/screen.js diff --git a/README.md b/README.md index 75540f8..4f78992 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,16 @@ DD_API_KEY=YOUR_KEY_HERE DD_APP_KEY=YOUR_KEY_HERE node app.js * method used to update the dashboard with the provided `dash_id` * `dogapi.delete_dashboard(dash_id, [callback])` * method to remove a dashboard from datadog +* `dogapi.get_screenboard(screen_id, [callback])` + * method to get a single screenboard information +* `dogapi.get_all_screenboards([callback])` + * method to retrieve all screenboards in datadog +* `dogapi.create_screenboard(screenboard, [callback])` + * method used to create a new screenboard in datadog +* `dogapi.update_screenboard(screen_id, screenboard, [callback])` + * method used to update the screenboard with the provided `screen_id` +* `dogapi.delete_screenboard(screen_id, [callback])` + * method to remove a screenboard from datadog * `dogapi.search(query, [callback])` * method used to query the api for `metrics` or `hosts` * `dogapi.add_metric(metric, [callback])` diff --git a/lib/api/screen.js b/lib/api/screen.js new file mode 100644 index 0000000..a379482 --- /dev/null +++ b/lib/api/screen.js @@ -0,0 +1,117 @@ +var util = require('util'); +var v8type = require('v8type'); + +var validate_screenboard = function(screenboard){ + if(!v8type.of(screenboard, v8type.OBJECT)){ + throw new Error('`screenboard` parameter must be an object'); + } + + if(!v8type.of(screenboard['board_title'], v8type.STRING)){ + throw new Error('`screenboard["board_title"]` must be a string'); + } + + if(screenboard['width'] != undefined && !v8type.of(screenboard['width'], v8type.NUMBER)){ + throw new Error('`screenboard["width"]` must be a number'); + } + + if(screenboard['height'] != undefined && !v8type.of(screenboard['height'], v8type.NUMBER)){ + throw new Error('`screenboard["height"]` must be a number'); + } + + if(!v8type.of(screenboard['widgets'], v8type.ARRAY)){ + throw new Error('`screenboard["widgets"]` must be an array'); + } + + for(var i in screenboard['widgets']){ + if(!screenboard['widgets'][i]['type']){ + throw new Error(util.format('`screenboard["widgets"][%s]["type"]` is missing', i)); + } + if(!screenboard['widgets'][i]['width']){ + throw new Error(util.format('`screenboard["widgets"][%s]["width"]` is missing', i)); + } + if(!screenboard['widgets'][i]['height']){ + throw new Error(util.format('`screenboard["widgets"][%s]["height"]` is missing', i)); + } + if(!screenboard['widgets'][i]['x']){ + throw new Error(util.format('`screenboard["widgets"][%s]["x"]` is missing', i)); + } + if(!screenboard['widgets'][i]['y']){ + throw new Error(util.format('`screenboard["widgets"][%s]["y"]` is missing', i)); + } + } +} + +var screen_api = function(){}; + +screen_api.prototype.get_screenboard = function(screen_id, callback){ + /* + * screen_api.get_screenboard(screen_id, [callback]) + * + * method to get a single screenboard information + * + * `screen_id` is the id of the screenboard to get information for + * `callback` is an optional function to call with the results of the api call + * callback(error, results, status_code) + */ + this.request('GET', util.format('/screen/%s', screen_id), callback) +}; + +screen_api.prototype.get_all_screenboards = function(callback){ + /* + * screen_api.get_all_screenboards([callback]) + * + * method to retrieve all screenboards in datadog + * + * `callback` is an optional function to call with the results of the api call + * callback(error, result, status_code) + */ + this.request('GET', '/screen', callback) +}; + +screen_api.prototype.create_screenboard = function(screenboard, callback){ + /* + * screen_api.create_screenboard(screenboard, [callback]) + * + * method used to create a new screenboard in datadog + * + * `screenboard` is the definition for the screenboard + * please see (http://docs.datadoghq.com/api/) for more information + * + * `callback` is an optional function to call with the results of the api call + * callback(error, result, status_code) + */ + validate_screenboard(screenboard); + this.request('POST', '/screen', {body: screenboard}, callback); +}; + +screen_api.prototype.update_screenboard = function(screen_id, screenboard, callback){ + /* + * screen_api.update_screenboard(screen_id, screenboard, [callback]) + * + * method used to update the screenboard with the provided `screen_id` + * + * `screen_id` the id of the screenboard to update + * `screenboard` is a definition for the datadog screenboard + * please see (http://docs.datadoghq.com/api/) for more information + * + * `callback` an optional function to call with the results of the api call + * callback(error, result, status_code) + */ + validate_screenboard(screenboard); + this.request('PUT', util.format('/screen/%s', screen_id), {body: screenboard}, callback); +}; + +screen_api.prototype.delete_screenboard = function(screen_id, callback){ + /* + * screen_api.delete_screenboard(screen_id, [callback]) + * + * method to remove a screenboard from datadog + * + * `screen_id` the id for the screenboard to remove + * `callback` an optional function to call with the results of the api call + * callback(error, result, status_code) + */ + this.request('DELETE', util.format('/screen/%s', screen_id), callback) +}; + +return module.exports = screen_api; diff --git a/lib/index.js b/lib/index.js index fed130b..f92fe96 100644 --- a/lib/index.js +++ b/lib/index.js @@ -3,6 +3,7 @@ var extend = require('extend'); var http_client = require('./http_client.js'); var alert_api = require('./api/alert.js'); var dash_api = require('./api/dash.js'); +var screen_api = require('./api/screen.js'); var event_api = require('./api/event.js'); var tag_api = require('./api/tag.js'); var metric_api = require('./api/metric.js'); @@ -16,6 +17,7 @@ extend(dogapi.prototype, http_client.prototype, alert_api.prototype, dash_api.prototype, + screen_api.prototype, event_api.prototype, tag_api.prototype, metric_api.prototype, diff --git a/package.json b/package.json index a9f3174..2a56ee7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dogapi", - "version": "0.1.1", + "version": "0.1.2", "description": "Datadog API Node.JS Client", "main": "lib/index.js", "scripts": {