fix handling of volume list filters

This commit is contained in:
Julien Gilli 2017-03-22 18:19:02 -07:00
parent 00f92d67a5
commit 984c692d2f
3 changed files with 57 additions and 31 deletions

View File

@ -125,6 +125,40 @@ function jsonStream(arr, stream) {
}); });
} }
/**
* Parses the string "filterComponent" of the form 'key=value' and returns an
* object that represents it with the form {'key': 'value'}. If "key"" in the
* "filterComponent" string is not included in the list "validKeys", it throws
* an error. It also throws an error if the string "filterComponent" is
* malformed.
*
* @param {String} filterComponent
* @param {arrayOfString} validKeys: Optional
*/
function _parseFilterKeyAndValue(filterComponent, validKeys) {
assert.string(filterComponent, 'filterComponent');
assert.optionalArrayOfString(validKeys, 'validKeys');
var idx = filterComponent.indexOf('=');
if (idx === -1) {
throw new errors.UsageError(format(
'invalid filter: "%s" (must be of the form "field=value")',
filterComponent));
}
var k = filterComponent.slice(0, idx);
var v = filterComponent.slice(idx + 1);
if (validKeys && validKeys.indexOf(k) === -1) {
throw new errors.UsageError(format(
'invalid filter name: "%s" (must be one of "%s")',
k, validKeys.join('", "')));
}
return {
key: k,
value: v
};
}
/** /**
* given an array of key=value pairs, break them into an object * given an array of key=value pairs, break them into an object
* *
@ -138,20 +172,11 @@ function kvToObj(kvs, valid) {
assert.optionalArrayOfString(valid, 'valid'); assert.optionalArrayOfString(valid, 'valid');
var o = {}; var o = {};
var parsedKeyValue;
for (var i = 0; i < kvs.length; i++) { for (var i = 0; i < kvs.length; i++) {
var kv = kvs[i]; parsedKeyValue = _parseFilterKeyAndValue(kvs[i], valid);
var idx = kv.indexOf('='); o[parsedKeyValue.key] = parsedKeyValue.value;
if (idx === -1)
throw new errors.UsageError(format(
'invalid filter: "%s" (must be of the form "field=value")',
kv));
var k = kv.slice(0, idx);
var v = kv.slice(idx + 1);
if (valid && valid.indexOf(k) === -1)
throw new errors.UsageError(format(
'invalid filter name: "%s" (must be one of "%s")',
k, valid.join('", "')));
o[k] = v;
} }
return o; return o;
} }
@ -174,24 +199,23 @@ function jsonPredFromKv(kvs, validKeys, compositionType) {
'compositionType'); 'compositionType');
var predicate = {}; var predicate = {};
predicate[compositionType] = []; var parsedKeyValue;
for (var i = 0; i < kvs.length; i++) { if (kvs.length === 0) {
var kv = kvs[i]; return predicate;
var idx = kv.indexOf('='); }
if (idx === -1) {
throw new errors.UsageError(format( if (kvs.length === 1) {
'invalid filter: "%s" (must be of the form "field=value")', parsedKeyValue = _parseFilterKeyAndValue(kvs[0], validKeys);
kv)); predicate.eq = [parsedKeyValue.key, parsedKeyValue.value];
} else {
predicate[compositionType] = [];
for (var i = 0; i < kvs.length; i++) {
parsedKeyValue = _parseFilterKeyAndValue(kvs[i], validKeys);
predicate[compositionType].push({
eq: [parsedKeyValue.key, parsedKeyValue.value]
});
} }
var k = kv.slice(0, idx);
var v = kv.slice(idx + 1);
if (validKeys && validKeys.indexOf(k) === -1) {
throw new errors.UsageError(format(
'invalid filter name: "%s" (must be one of "%s")',
k, validKeys.join('", "')));
}
predicate[compositionType].push({eq: [k, v]});
} }
return predicate; return predicate;

View File

@ -11,6 +11,7 @@
*/ */
var format = require('util').format; var format = require('util').format;
var jsprim = require('jsprim');
var tabula = require('tabula'); var tabula = require('tabula');
var common = require('../common'); var common = require('../common');
@ -64,7 +65,7 @@ function do_list(subcmd, opts, args, callback) {
} }
} }
if (opts.all === undefined) { if (jsprim.deepEqual(filterPredicate, {})) {
filterPredicate = { filterPredicate = {
and: [ and: [
{ ne: ['state', 'deleted']}, { ne: ['state', 'deleted']},

View File

@ -11,7 +11,8 @@
"bunyan": "1.5.1", "bunyan": "1.5.1",
"cmdln": "4.1.2", "cmdln": "4.1.2",
"extsprintf": "1.0.2", "extsprintf": "1.0.2",
"getpass": "0.1.6", "getpass": "0.1.6",
"jsprim": "^1.4.0",
"lomstream": "1.1.0", "lomstream": "1.1.0",
"mkdirp": "0.5.1", "mkdirp": "0.5.1",
"once": "1.3.2", "once": "1.3.2",