This sets up some groundwork to send additional parameters to cloudapi.

This branch by itself does not break anything.  But, if you want breakage,
pull https://github.com/pccowboy/sdc-cloudapi, branch delegate_dataset.
This commit is contained in:
David Swift 2016-05-06 07:07:54 +00:00
parent c7f7f86126
commit dc65a481c1
2 changed files with 76 additions and 1 deletions

View File

@ -217,6 +217,19 @@ function do_create(subcmd, opts, args, cb) {
next();
});
},
function loadAddl(ctx, next) {
mat.addlFromCreateOpts(opts, log, function (err, addl) {
if (err) {
next(err);
return;
}
if (addl) {
log.trace({addl: addl}, 'additional parameters loaded from opts');
ctx.addl = addl;
}
next();
});
},
function getImg(ctx, next) {
var _opts = {
name: args[0],
@ -294,6 +307,11 @@ function do_create(subcmd, opts, args, cb) {
createOpts['tag.'+key] = ctx.tags[key];
});
}
if (ctx.addl) {
Object.keys(ctx.addl).forEach(function (key) {
createOpts[key] = ctx.addl[key];
});
}
for (var i = 0; i < opts._order.length; i++) {
var opt = opts._order[i];
@ -427,7 +445,17 @@ do_create.options = [
'`INST`. Use this option more than once for multiple rules.',
completionType: 'tritonaffinityrule'
},
{
names: ['addl'],
type: 'arrayOfString',
helpArg: 'ADDL',
help: 'Add specific properties when creating the instance. These are ' +
'key/value pairs available on the instance API object at the ' +
'top level. ADDL is one of: a "key=value" string (bool and ' +
'numeric "value" are converted to that type), a JSON object ' +
'(if first char is "{"), or a "@FILE" to have properties be ' +
'loaded from FILE. This option can be used multiple times.'
},
{
group: ''
},

View File

@ -123,6 +123,52 @@ function tagsFromCreateOpts(opts, log, cb) {
}
/*
* Load and validate additional parameters from these options:
* --addl-opts DATA
*
* <https://github.com/joyent/sdc-vmapi/blob/master/docs/index.md#vm-metadata>
* says values may be string, num or bool.
*/
function addlFromCreateOpts(opts, log, cb) {
assert.arrayOfObject(opts._order, 'opts._order');
assert.object(log, 'log');
assert.func(cb, 'cb');
var addl = {};
vasync.forEachPipeline({
inputs: opts._order,
func: function addlFromOpt(o, next) {
log.trace({opt: o}, 'addlFromOpt');
if (o.key === 'addl') {
if (!o.value) {
next(new errors.UsageError(
'empty additional parameters option value'));
return;
} else if (o.value[0] === '{') {
_addMetadataFromJsonStr('addl', addl, o.value, null, next);
} else if (o.value[0] === '@') {
_addMetadataFromFile('addl', addl, o.value.slice(1), next);
} else {
_addMetadataFromKvStr('addl', addl, o.value, null, next);
}
} else {
next();
}
}
}, function (err) {
if (err) {
cb(err);
} else if (Object.keys(addl).length) {
cb(null, addl);
} else {
cb();
}
});
}
/*
* Load and validate tags from (a) these options:
* -f,--file FILE
@ -342,6 +388,7 @@ function _addMetadatumFromFile(ilk, metadata, key, file, from, cb) {
module.exports = {
addlFromCreateOpts: addlFromCreateOpts,
metadataFromOpts: metadataFromOpts,
tagsFromCreateOpts: tagsFromCreateOpts,
tagsFromSetArgs: tagsFromSetArgs