Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VOLAPI-15: Support "predicate" querystring parameter for ListVolumes …
…endpoint
- Loading branch information
Julien Gilli
committed
Jul 18, 2016
1 parent
950f1ab
commit 3211c88
Showing
8 changed files
with
604 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
/* | ||
* Copyright (c) 2016, Joyent, Inc. | ||
*/ | ||
|
||
var assert = require('assert-plus'); | ||
var krill = require('krill'); | ||
|
||
var volumes = require('../volumes'); | ||
var volumesValidation = require('./volumes'); | ||
|
||
var VOLUME_PREDICATE_TYPES = { | ||
name: 'string', | ||
type: 'string', | ||
state: 'string' | ||
}; | ||
|
||
function validatePredicate(predicateString) { | ||
assert.string(predicateString, 'predicateString'); | ||
|
||
var predicateObject; | ||
var predicate; | ||
var validationErrs = []; | ||
var error; | ||
var predicateFieldsAndValues; | ||
var predicateField; | ||
var VALIDATION_FUNCS = { | ||
state: volumesValidation.validateVolumeState, | ||
name: volumesValidation.validateVolumeName, | ||
type: volumesValidation.validateVolumeType | ||
}; | ||
|
||
try { | ||
predicateObject = JSON.parse(predicateString); | ||
} catch (parseErr) { | ||
error = parseErr; | ||
} | ||
|
||
if (!error) { | ||
try { | ||
predicate = krill.createPredicate(predicateObject, | ||
VOLUME_PREDICATE_TYPES); | ||
} catch (predicateValidationErr) { | ||
error = predicateValidationErr; | ||
} | ||
} | ||
|
||
if (!error) { | ||
predicateFieldsAndValues = predicate.fieldsAndValues(); | ||
|
||
for (predicateField in predicateFieldsAndValues) { | ||
var validationFunc = VALIDATION_FUNCS[predicateField]; | ||
var predicateValues = predicateFieldsAndValues[predicateField]; | ||
|
||
assert.func(validationFunc, 'validationFunc'); | ||
|
||
predicateValues.forEach(function validatePredicateValue(value) { | ||
var validationError = validationFunc(value); | ||
if (validationError) { | ||
validationErrs.push(validationError); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
if (validationErrs.length > 0) { | ||
error = new Error('Invalid values in predicate: ' + validationErrs); | ||
} | ||
|
||
return error; | ||
} | ||
|
||
module.exports = { | ||
validatePredicate: validatePredicate | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.