show image name and version when UUID is specified, fixes #29

This commit is contained in:
Dave Eddy 2015-10-05 16:34:24 -04:00
parent a67341b1b0
commit 3cbf85a121
2 changed files with 61 additions and 26 deletions

View File

@ -33,14 +33,11 @@ function do_create_instance(subcmd, opts, args, callback) {
vasync.pipeline({arg: {}, funcs: [ vasync.pipeline({arg: {}, funcs: [
function getImg(ctx, next) { function getImg(ctx, next) {
var id = args[0]; var _opts = {
if (common.isUUID(id)) { name: args[0],
ctx.img = {id: id}; useCache: true
next(); };
return; self.tritonapi.getImage(_opts, function (err, img) {
}
self.tritonapi.getImage(id, function (err, img) {
if (err) { if (err) {
return next(err); return next(err);
} }

View File

@ -253,31 +253,69 @@ TritonApi.prototype.listImages = function listImages(opts, cb) {
* If there is more than one image with that name, then the latest * If there is more than one image with that name, then the latest
* (by published_at) is returned. * (by published_at) is returned.
*/ */
TritonApi.prototype.getImage = function getImage(name, cb) { TritonApi.prototype.getImage = function getImage(opts, cb) {
assert.string(name, 'name'); var self = this;
if (typeof (opts) === 'string')
opts = {name: opts};
assert.object(opts, 'opts');
assert.string(opts.name, 'opts.name');
assert.optionalBool(opts.useCache, 'opts.useCache');
assert.func(cb, 'cb'); assert.func(cb, 'cb');
if (common.isUUID(name)) { var img;
this.cloudapi.getImage({id: name}, function (err, img) { if (common.isUUID(opts.name)) {
vasync.pipeline({funcs: [
function tryCache(_, next) {
if (!opts.useCache) {
next();
return;
}
self._cacheGetJson('images.json', function (err, images) {
if (err) {
next(err);
return;
}
var _img = images.filter(function (i) {
return i.id === opts.name;
});
if (_img.length === 1)
img = _img[0];
next();
});
},
function cloudApiGetImage(_, next) {
if (img !== undefined) {
next();
return;
}
self.cloudapi.getImage({id: opts.name}, function (err, _img) {
img = _img;
next(err);
});
}
]}, function done(err) {
if (err) { if (err) {
cb(err); cb(err);
} else if (img.state !== 'active') { } else if (img.state !== 'active') {
cb(new Error(format('image %s is not active', name))); cb(new Error(format('image %s is not active', opts.name)));
} else { } else {
cb(null, img); cb(null, img);
} }
}); }
);
} else { } else {
var s = name.split('@'); var s = opts.name.split('@');
name = s[0]; var name = s[0];
var version = s[1]; var version = s[1];
var opts = {}; var _opts = {};
if (version) { if (version) {
opts.name = name; _opts.name = name;
opts.version = version; _opts.version = version;
_opts.useCache = opts.useCache;
} }
this.cloudapi.listImages(opts, function (err, imgs) { this.cloudapi.listImages(_opts, function (err, imgs) {
if (err) { if (err) {
return cb(err); return cb(err);
} }
@ -285,7 +323,7 @@ TritonApi.prototype.getImage = function getImage(name, cb) {
var nameMatches = []; var nameMatches = [];
var shortIdMatches = []; var shortIdMatches = [];
for (var i = 0; i < imgs.length; i++) { for (var i = 0; i < imgs.length; i++) {
var img = imgs[i]; img = imgs[i];
if (img.name === name) { if (img.name === name) {
nameMatches.push(img); nameMatches.push(img);
} }