diff --git a/lib/cloudapi2.js b/lib/cloudapi2.js index 6949e56..eab9822 100644 --- a/lib/cloudapi2.js +++ b/lib/cloudapi2.js @@ -728,6 +728,31 @@ CloudApi.prototype.getMachine = function getMachine(opts, cb) { }); }; +/** + * rename a machine by id. + * + * @param {Object} opts + * - id {UUID} Required. The machine id. + * - {String} name. The machine name + * @param {Function} callback of the form `function (err, res)` + */ +CloudApi.prototype.renameMachine = function renameMachine(opts, callback) { + assert.uuid(opts.id, 'opts.id'); + assert.string(opts.name, 'opts.name'); + var data = { + action: 'rename', + name: opts.name + }; + + this._request({ + method: 'POST', + path: format('/%s/machines/%s', this.account, opts.id), + data: data + }, function (err, req, res, body) { + callback(err, res); + }); +}; + /** * delete a machine by id. * diff --git a/lib/do_instance/do_rename.js b/lib/do_instance/do_rename.js new file mode 100644 index 0000000..a89c306 --- /dev/null +++ b/lib/do_instance/do_rename.js @@ -0,0 +1,62 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +var common = require('../common'); +var errors = require('../errors'); + + +function perror(err) { + console.error('error: %s', err.message); +} + +function do_rename(subcmd, opts, args, callback) { + var self = this; + if (opts.help) { + this.do_help('help', {}, [subcmd], callback); + return; + } else if (args.length < 1) { + callback(new errors.UsageError('missing INST arg')); + return; + } else if (args.length < 2) { + callback(new errors.UsageError('missing NEWNAME arg')); + return; + } + var cOpts = {id: args[0], name: args[1]}; + self.top.tritonapi.renameInstance(cOpts, function (err) { + if (err) { + callback(err); + return; + } + console.log('Renamed instance %s to "%s"', cOpts.id, cOpts.name); + callback(); + }); +} + + +do_rename.options = [ + { + names: ['help', 'h'], + type: 'bool', + help: 'Show this help.' + } +]; + + +do_rename.synopses = ['{{name}} rename [OPTIONS] INST NEWNAME']; +do_rename.help = [ + 'Rename an instance.', + '', + '{{usage}}', + '', + '{{options}}', + 'Where "INST" is an instance name, id, or short id', + 'and "NEWNAME" is an instance name.' +].join('\n'); + +do_rename.completionArgtypes = ['tritoninstance', 'none']; + + +module.exports = do_rename; diff --git a/lib/do_instance/index.js b/lib/do_instance/index.js index 9b0abbe..1165d22 100644 --- a/lib/do_instance/index.js +++ b/lib/do_instance/index.js @@ -34,6 +34,7 @@ function InstanceCLI(top) { 'get', 'create', 'delete', + 'rename', { group: '' }, 'start', 'stop', @@ -63,6 +64,7 @@ InstanceCLI.prototype.do_list = require('./do_list'); InstanceCLI.prototype.do_get = require('./do_get'); InstanceCLI.prototype.do_create = require('./do_create'); InstanceCLI.prototype.do_delete = require('./do_delete'); +InstanceCLI.prototype.do_rename = require('./do_rename'); InstanceCLI.prototype.do_start = require('./do_start'); InstanceCLI.prototype.do_stop = require('./do_stop'); diff --git a/lib/tritonapi.js b/lib/tritonapi.js index e749132..36e39fc 100644 --- a/lib/tritonapi.js +++ b/lib/tritonapi.js @@ -2121,6 +2121,35 @@ TritonApi.prototype.deletePolicy = function deletePolicy(opts, cb) { }); }; +/** + * rename a machine by id. + * + * @param {Object} opts + * - id {UUID} or The machine name or shortID Required. + * - {String} name. The machine name + * @param {Function} callback of the form `function (err, res)` + */ +TritonApi.prototype.renameInstance = function renameInstance(opts, cb) { + assert.string(opts.id, 'opts.id'); + assert.string(opts.name, 'opts.name'); + assert.func(cb, 'cb'); + var self = this; + var res; + + vasync.pipeline({arg: {client: self, id: opts.id}, funcs: [ + _stepInstId, + + function renameMachine(arg, next) { + self.cloudapi.renameMachine({id: arg.instId, name: opts.name}, + function (err, _res) { + res = _res; + next(err); + }); + } + ]}, function (err) { + cb(err, res); + }); +}; //---- exports diff --git a/test/integration/cli-subcommands.test.js b/test/integration/cli-subcommands.test.js index d13f439..4723668 100644 --- a/test/integration/cli-subcommands.test.js +++ b/test/integration/cli-subcommands.test.js @@ -45,6 +45,7 @@ var subs = [ ['instance delete', 'instance rm', 'delete', 'rm'], ['instance enable-firewall'], ['instance disable-firewall'], + ['instance rename'], ['instance ssh'], ['instance ip'], ['instance wait'],