joyent/node-triton#64 support instance renaming

Reviewed by: Trent Mick <trentm@gmail.com>
Approved by: Trent Mick <trentm@gmail.com>
This commit is contained in:
Yang Yong 2016-11-01 13:54:07 +09:00 committed by Trent Mick
parent 6c7944f2c2
commit a2c83aab56
5 changed files with 119 additions and 0 deletions

View File

@ -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. * delete a machine by id.
* *

View File

@ -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;

View File

@ -34,6 +34,7 @@ function InstanceCLI(top) {
'get', 'get',
'create', 'create',
'delete', 'delete',
'rename',
{ group: '' }, { group: '' },
'start', 'start',
'stop', 'stop',
@ -63,6 +64,7 @@ InstanceCLI.prototype.do_list = require('./do_list');
InstanceCLI.prototype.do_get = require('./do_get'); InstanceCLI.prototype.do_get = require('./do_get');
InstanceCLI.prototype.do_create = require('./do_create'); InstanceCLI.prototype.do_create = require('./do_create');
InstanceCLI.prototype.do_delete = require('./do_delete'); InstanceCLI.prototype.do_delete = require('./do_delete');
InstanceCLI.prototype.do_rename = require('./do_rename');
InstanceCLI.prototype.do_start = require('./do_start'); InstanceCLI.prototype.do_start = require('./do_start');
InstanceCLI.prototype.do_stop = require('./do_stop'); InstanceCLI.prototype.do_stop = require('./do_stop');

View File

@ -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 //---- exports

View File

@ -45,6 +45,7 @@ var subs = [
['instance delete', 'instance rm', 'delete', 'rm'], ['instance delete', 'instance rm', 'delete', 'rm'],
['instance enable-firewall'], ['instance enable-firewall'],
['instance disable-firewall'], ['instance disable-firewall'],
['instance rename'],
['instance ssh'], ['instance ssh'],
['instance ip'], ['instance ip'],
['instance wait'], ['instance wait'],