add instance (alias and uuid)

This commit is contained in:
Dave Eddy 2015-08-25 20:27:46 -04:00
parent f2dee08b46
commit b5f9cbe054
5 changed files with 103 additions and 21 deletions

View File

@ -61,6 +61,7 @@ function CLI() {
{ group: 'Instances (aka VMs/Machines/Containers)' },
'create',
'instances',
'instance',
'instance-audit',
{ group: 'Images' },
'images',
@ -108,6 +109,7 @@ CLI.prototype.do_image = require('./do_image');
// Instances (aka VMs/containers/machines)
CLI.prototype.do_create = require('./do_create');
CLI.prototype.do_instance = require('./do_instance');
CLI.prototype.do_instances = require('./do_instances');
CLI.prototype.do_instance_audit = require('./do_instance_audit');

View File

@ -357,29 +357,14 @@ CloudAPI.prototype.getPackage = function getPackage(options, callback) {
* - {String} id (required) The machine id.
* @param {Function} callback of the form `function (err, machine, response)`
*/
CloudAPI.prototype.getMachine = function getMachine(options, callback) {
CloudAPI.prototype.getMachine = function getMachine(uuid, callback) {
var self = this;
assert.object(options, 'options');
assert.string(options.id, 'options.id');
assert.string(uuid, 'uuid');
assert.func(callback, 'callback');
var path = sprintf('/%s/machines/%s', self.user, options.id);
self._getAuthHeaders(function (hErr, headers) {
if (hErr) {
callback(hErr);
return;
}
var opts = {
path: path,
headers: headers
};
self.client.get(opts, function (err, req, res, body) {
if (err) {
callback(err, null, res);
} else {
callback(null, body, res);
}
});
var endpoint = sprintf('/%s/machines/%s', self.user, uuid);
this._request(endpoint, function (err, req, res, body) {
callback(err, body, res);
});
};

View File

@ -134,6 +134,14 @@ function longAgo(when, now) {
return '0s';
}
/**
* checks a string and returns a boolean based on if it
* is a UUID or not
*/
function isUUID(s) {
return /^([a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}?)$/i.test(s);
}
//---- exports
module.exports = {
@ -143,6 +151,7 @@ module.exports = {
boolFromString: boolFromString,
jsonStream: jsonStream,
kvToObj: kvToObj,
longAgo: longAgo
longAgo: longAgo,
isUUID: isUUID,
};
// vim: set softtabstop=4 shiftwidth=4:

60
lib/do_instance.js Normal file
View File

@ -0,0 +1,60 @@
/*
* Copyright 2015 Joyent Inc.
*
* `triton instance ...`
*/
var common = require('./common');
function do_instance(subcmd, opts, args, callback) {
if (opts.help) {
this.do_help('help', {}, [subcmd], callback);
return;
} else if (args.length !== 1) {
callback(new Error('invalid args: ' + args));
return;
}
var id = args[0];
if (common.isUUID(id)) {
this.triton.cloudapi.getMachine(id, cb);
} else {
this.triton.getMachineByAlias(id, cb);
}
function cb(err, machine) {
if (err) {
callback(err);
return;
}
console.log(JSON.stringify(machine, null, 4));
callback();
}
}
do_instance.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
},
{
names: ['json', 'j'],
type: 'bool',
help: 'JSON output.'
}
];
do_instance.help = (
'Show a single instance.\n'
+ '\n'
+ 'Usage:\n'
+ ' {{name}} instance <alias|id>\n'
+ '\n'
+ '{{options}}'
);
do_instance.aliases = ['inst'];
module.exports = do_instance;

View File

@ -232,6 +232,32 @@ Triton.prototype.machineAudit = function machineAudit(options, callback) {
};
/**
* getMachine for an alias
*
* @param {String} alias - the machine alias
* @param {Function} callback `function (err, machine)`
*/
Triton.prototype.getMachineByAlias = function getMachineByAlias(alias, callback) {
this.cloudapi.listMachines({name: alias}, function (err, machines) {
if (err) {
callback(err);
return;
}
var found = false;
machines.forEach(function (machine) {
if (!found && machine.name === alias) {
callback(null, machine);
found = true;
}
});
if (!found) {
callback(new Error('machine ' + alias + ' not found'));
return;
}
});
};
//---- exports