From ad5841aa5300cd211a366eeb300a5f20fd7952a9 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Wed, 18 Mar 2015 08:52:47 -0400 Subject: [PATCH] setup new dogapi base and port over the tag api --- lib/api/index.js | 9 ++ lib/api/tag.js | 294 +++++++++++++++++++++++++++-------------------- lib/index.js | 65 ++++++----- 3 files changed, 210 insertions(+), 158 deletions(-) create mode 100644 lib/api/index.js diff --git a/lib/api/index.js b/lib/api/index.js new file mode 100644 index 0000000..04d809b --- /dev/null +++ b/lib/api/index.js @@ -0,0 +1,9 @@ +var api = { + tag: require("./tag") +}; + +module.exports = function(obj){ + for(key in api){ + obj[key] = api[key]; + } +}; diff --git a/lib/api/tag.js b/lib/api/tag.js index f155430..e376b8c 100644 --- a/lib/api/tag.js +++ b/lib/api/tag.js @@ -1,169 +1,213 @@ +var client = require("../client"); var util = require('util'); -var tag_api = function(){}; - -tag_api.prototype.all_tags = function(source, callback){ - /* - * tag_api.all_tags([[source], callback]) - * - * method to get all the tags in datadog - * - * `source` a source to limit to - * `callback` is an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - if(arguments.length < 2 && typeof arguments[0] == 'function'){ - callback = arguments[0]; +/*section: tag + *comment: | + * get all host tags + *params: + * source: | + * optional, only show tags for a particular source [default: null] + * callback: | + * function callback(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.tag.get_all(function(err, results){ + * console.dir(results); + * }); + * ``` + */ +function get_all(source, callback){ + if(arguments.length < 2 && typeof arguments[0] === "function"){ + callback = source; source = undefined; } - params = { + var params = { query: { source: source } }; - this.request('GET', '/tags/hosts', params, callback); -}; - -tag_api.prototype.host_tags = function(host, source, callback){ - /* - * tag_api.host_tags(host, [[source], callback]) - * - * method to get the tags associated with a given `host` - * - * `host` the hostname or id to get tags for - * `source` a source to limit the results to - * `callback` is an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - if(arguments.length < 3 && typeof arguments[1] == 'function'){ - callback = arguments[1]; - source = undefined; + client.request("GET", "/tags/hosts", params, callback); +} + +/*section: tag + *comment: | + * get the host tags for a provided host name or host id + *params: + * hostname: | + * the hostname or host id + * options: + * | + * optional, an object of options for the query allowing the following + * * source: the source of the tags (e.g. chef, puppet, users, etc) [default: null] + * * by_source: whether or not to group the results by source [default: false] + * callback: | + * function callback(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.tag.get("host.name", function(err, results){ + * console.dir(results); + * }); + * ``` + */ +function get(hostname, options, callback){ + if(arguments.length < 3 && typeof arguments[1] === "function"){ + callback = options; + options = {}; } + options = options || {}; - params = { + var params = { query: { - source: source, } }; - this.request('GET', util.format('/tags/hosts/%s', host), params, callback); -}; - -tag_api.prototype.host_tags_by_source = function(host, source, callback){ - /* - * tag_api.host_tags_by_source(host, [[source], callback]) - * - * method to return the tags associated with a host, arranged by source - * - * `host` the hostname of id to get tags for - * `source` a source to limit the lookup for - * `callback` is an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - if(arguments.length < 3 && typeof arguments[1] == 'function'){ - callback = arguments[1]; - source = undefined; + if(options.source){ + params.query.source = options.source; } - - params = { - query: { - source: source, - by_source: true, - } - }; - this.request('GET', util.format('/tags/hosts/%s', host), params, callback); + if(options.by_source){ + params.query.by_source = options.by_source; + } + client.request("GET", "/tags/hosts/" + hostname, params, callback); }; -tag_api.prototype.add_tags = function(host, tags, source, callback){ - /* - * tag_api.add_tags(host, tags, [[source], callback]) - * - * add new tags to given `host` - * - * `host` the hostname or id of the machine to add tags for - * `tags` an array of tags to add to the `host` - * `source` the source to associate the tags with, default: user - * `callback` is an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - if(typeof tags != 'object'){ - throw new Error('`tags` parameter must be an array'); - } - if(arguments.length < 4 && typeof arguments[2] == 'function'){ - callback = arguments[2]; +/*section: tag + *comment: | + * assign new host tags to the provided host name or host id + *params: + * hostname: | + * the hostname or host id + * tags: | + * list of `:` tags to assign to the server + * source: | + * optional, the source of the tags (e.g. chef, puppet, etc) [default: users] + * callback: | + * function callback(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.tag.create("host.name", ["role:webserver"], function(err, results){ + * console.dir(results); + * }); + * ``` + */ +function create(hostname, tags, source, callback){ + if(arguments.length < 4 && typeof arguments[2] === "function"){ + callback = source; source = undefined; } - params = { - query: { - source: source, - }, + var params = { body: { tags: tags, - } + source: source + }, }; - this.request('POST', util.format('/tags/hosts/%s', host), params, callback); + client.request("POST", "/tags/hosts/" + hostname, params, callback); }; -tag_api.prototype.update_tags = function(host, tags, source, callback){ - /* - * tag_api.update_tags(host, tags, [[source], callback]) - * - * update the tags associated with the given `host` - * - * `host` is the hostname or id of the machine to update tags for - * `tags` an array of tags to associate with the `host` - * `source` the source to associate the tags with, default: user - * `callback` is an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - if(typeof tags != 'object'){ - throw new Error('`tags` parameter must be an array'); - } - if(arguments.length < 4 && typeof arguments[2] == 'function'){ - callback = arguments[2]; +/*section: tag + *comment: | + * update the host tags for the provided host name or host id + *params: + * hostname: | + * the hostname or host id + * tags: | + * list of `:` tags to assign to the server + * source: | + * optional, the source of the tags (e.g. chef, puppet, etc) [default: users] + * callback: | + * function callback(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.tag.update("host.name", function(err, results){ + * console.dir(results); + * }); + * ``` + */ +function update(hostname, tags, source, callback){ + if(arguments.length < 4 && typeof arguments[2] === "function"){ + callback = source; source = undefined; } - params = { - query: { - source: source, - }, + var params = { body: { tags: tags, - } + source: source + }, }; - - this.request('PUT', util.format('/tags/hosts/%s', host), params, callback); + client.request("PUT", "/tags/hosts/" + hostname, params, callback); }; -tag_api.prototype.detach_tags = function(host, source, callback){ - /* - * tag_api.detach_tags(host, [[source], callback]) - * - * method to remove tags for a given `host` - * - * `host` the hostname or id of the machine to remove the tags for - * `source` the source of the tags - * `callback` is an optional function to call with the results of the api call - * callback(error, result, status_code) - */ - if(arguments.length < 3 && typeof arguments[1] == 'function'){ - callback = arguments[1]; +/*section: tag + *comment: | + * delete the host tags for the provided host name or host id + *params: + * hostname: | + * the hostname or host id + * source: | + * optional, the source of the tags (e.g. chef, puppet, etc) [default: users] + * callback: | + * function callback(err, res) + *example: | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "api_key", + * app_key: "app_key" + * }; + * dogapi.initialize(options); + * dogapi.tag.delete("host.name", function(err, results){ + * console.dir(results); + * }); + * ``` + */ +function delete_tags(hostname, source, callback){ + if(arguments.length < 3 && typeof arguments[1] === "function"){ + callback = source; source = undefined; } - params = { + var params = { query: { - source: source, - }, + source: source + } }; - - this.request('DELETE', util.format('/tags/hosts/%s', host), params, callback); + client.request("DELETE", "/tags/hosts/" + hostname, params, callback); }; -return module.exports = tag_api; +module.exports = { + _client: client, + get_all: get_all, + get: get, + create: create, + update: update, + "delete": delete_tags, +} diff --git a/lib/index.js b/lib/index.js index 7cfc672..2f2234f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,36 +1,35 @@ -var extend = require('extend'); +require("./api")(module.exports); -var http_client = require('./http_client.js'); -var constants = require('./constants.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'); -var search_api = require('./api/search.js'); -var service_check_api = require('./api/service_check.js'); -var snapshot_api = require('./api/snapshot.js'); -var downtime_api = require('./api/downtime.js'); - - -var dogapi = function(options){ - http_client.call(this, options); +/*section: dogapi + *params: + * options: + * | + * An object which allows you to override the default set parameters for interacting + * with the datadog api. The available options are. + * * api_key: your api key + * * app_key: your app key + * * api_version: the version of the api [default: `v1`] + * * api_host: the host to call [default: `api.datadoghq.com`] + *example: + * | + * ```javascript + * var dogapi = require("dogapi"); + * var options = { + * api_key: "", + * app_key: "" + * }; + * dogapi.initialize(options); + * dogapi.event.create(...); + * ``` + */ +function initialize(options){ + options = options || {}; + for(var key in options){ + if(module.exports.client.hasOwnProperty(key)){ + module.exports.client[key] = options[key]; + } + } }; -extend(dogapi.prototype, - http_client.prototype, - alert_api.prototype, - dash_api.prototype, - screen_api.prototype, - event_api.prototype, - tag_api.prototype, - metric_api.prototype, - search_api.prototype, - service_check_api.prototype, - snapshot_api.prototype, - downtime_api.prototype); - -dogapi.constants = constants; - -return module.exports = dogapi; +module.exports.client = require("./client"), +module.exports.initialize = initialize;