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: [
function getImg(ctx, next) {
var id = args[0];
if (common.isUUID(id)) {
ctx.img = {id: id};
next();
return;
}
self.tritonapi.getImage(id, function (err, img) {
var _opts = {
name: args[0],
useCache: true
};
self.tritonapi.getImage(_opts, function (err, img) {
if (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
* (by published_at) is returned.
*/
TritonApi.prototype.getImage = function getImage(name, cb) {
assert.string(name, 'name');
TritonApi.prototype.getImage = function getImage(opts, cb) {
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');
if (common.isUUID(name)) {
this.cloudapi.getImage({id: name}, function (err, img) {
var 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) {
cb(err);
} 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 {
cb(null, img);
}
});
}
);
} else {
var s = name.split('@');
name = s[0];
var s = opts.name.split('@');
var name = s[0];
var version = s[1];
var opts = {};
var _opts = {};
if (version) {
opts.name = name;
opts.version = version;
_opts.name = name;
_opts.version = version;
_opts.useCache = opts.useCache;
}
this.cloudapi.listImages(opts, function (err, imgs) {
this.cloudapi.listImages(_opts, function (err, imgs) {
if (err) {
return cb(err);
}
@ -285,7 +323,7 @@ TritonApi.prototype.getImage = function getImage(name, cb) {
var nameMatches = [];
var shortIdMatches = [];
for (var i = 0; i < imgs.length; i++) {
var img = imgs[i];
img = imgs[i];
if (img.name === name) {
nameMatches.push(img);
}