From 0ccda0af10db851cd1734196f3655f06042c3d55 Mon Sep 17 00:00:00 2001 From: Dave Eddy Date: Wed, 26 Aug 2015 19:59:28 -0400 Subject: [PATCH] add listDatacenters --- lib/cli.js | 2 ++ lib/cloudapi2.js | 10 ++++++++ lib/do_datacenters.js | 58 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 lib/do_datacenters.js diff --git a/lib/cli.js b/lib/cli.js index 6104b68..74d8c16 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -62,6 +62,7 @@ function CLI() { { group: 'Operator Commands' }, 'account', 'services', + 'datacenters', 'info', 'keys', { group: 'Instances (aka VMs/Machines/Containers)' }, @@ -138,6 +139,7 @@ CLI.prototype.init = function (opts, args, callback) { // Operator CLI.prototype.do_account = require('./do_account'); CLI.prototype.do_services = require('./do_services'); +CLI.prototype.do_datacenters = require('./do_datacenters'); CLI.prototype.do_info = require('./do_info'); CLI.prototype.do_keys = require('./do_keys'); diff --git a/lib/cloudapi2.js b/lib/cloudapi2.js index e744e97..736bf4c 100644 --- a/lib/cloudapi2.js +++ b/lib/cloudapi2.js @@ -295,6 +295,16 @@ CloudAPI.prototype.listServices = function listServices(opts, cb) { this._passThrough(endpoint, opts, cb); }; +/** + * Get datacenters information + * + * @param {Function} callback of the form `function (err, datacenters, response)` + */ +CloudAPI.prototype.listDatacenters = function listDatacenters(opts, cb) { + var endpoint = sprintf('/%s/datacenters', this.user); + this._passThrough(endpoint, opts, cb); +}; + // ---- accounts /** diff --git a/lib/do_datacenters.js b/lib/do_datacenters.js new file mode 100644 index 0000000..d84699f --- /dev/null +++ b/lib/do_datacenters.js @@ -0,0 +1,58 @@ +/* + * Copyright 2015 Joyent Inc. + * + * `triton datacenters ...` + */ + +var common = require('./common'); + +function do_datacenters(subcmd, opts, args, callback) { + if (opts.help) { + this.do_help('help', {}, [subcmd], callback); + return; + } else if (args.length !== 0) { + callback(new Error('invalid args: ' + args)); + return; + } + + this.triton.cloudapi.listDatacenters(function (err, datacenters) { + if (err) { + callback(err); + return; + } + + if (opts.json) { + console.log(JSON.stringify(datacenters)); + } else { + // pretty print + Object.keys(datacenters).forEach(function (key) { + var val = datacenters[key]; + console.log('%s: %s', key, val); + }); + } + callback(); + }); +} + +do_datacenters.options = [ + { + names: ['help', 'h'], + type: 'bool', + help: 'Show this help.' + }, + { + names: ['json', 'j'], + type: 'bool', + help: 'JSON output.' + } +]; +do_datacenters.help = ( + 'Show datacenters information\n' + + '\n' + + 'Usage:\n' + + ' {{name}} datacenters\n' + + '\n' + + '{{options}}' +); + +module.exports = do_datacenters;