|
|
|
@ -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 `<tag>:<value>` 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 `<tag>:<value>` 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, |
|
|
|
} |