sorted stable fields for 'triton rbac role ROLE' output; use 'add' for added fields in 'triton rbac apply' resource updates

This commit is contained in:
Trent Mick 2015-12-16 10:47:46 -08:00
parent 432c962f82
commit 5ea3b1862a
3 changed files with 31 additions and 5 deletions

View File

@ -3,7 +3,8 @@
## 3.4.1 (not yet released)
- Add `-d,--data <data>` option to `triton cloudapi`.
- Fix `triton rbac role ROLE`.
- Fix `triton rbac role ROLE`. Also get that command to have a stable order for the
displayed fields.
## 3.4.0

View File

@ -34,6 +34,10 @@ var CREATE_ROLE_FIELDS = [
{key: 'policies', array: true}
];
var SHOW_ORDER_FIELDS = [
'id', 'name', 'default_members', 'members', 'policies'
];
var _isArrayFromKey = {};
UPDATABLE_ROLE_FIELDS.forEach(function (field) {
_isArrayFromKey[field.key] = Boolean(field.array);
@ -61,7 +65,24 @@ function _showRole(opts, cb) {
if (opts.json) {
console.log(JSON.stringify(role));
} else {
Object.keys(role).forEach(function (key) {
var keys = Object.keys(role);
keys.sort(function cmpKeys(a, b) {
var idxA = SHOW_ORDER_FIELDS.indexOf(a);
var idxB = SHOW_ORDER_FIELDS.indexOf(b);
if (idxA === -1 && idxB === -1) {
return 0;
} else if (idxA === -1) {
return -1;
} else if (idxB === -1) {
return 1;
} else if (idxA < idxB) {
return -1;
} else if (idxA > idxB) {
return 1;
}
});
keys.forEach(function (key) {
var val = role[key];
if (Array.isArray(val)) {
val = val.join(', ');

View File

@ -149,7 +149,7 @@ function crudChangesForThings(opts) {
});
Object.keys(wantThing_).forEach(function (field) {
if (! haveThing_.hasOwnProperty(field)) {
diff[field] = 'create';
diff[field] = 'add';
}
});
if (opts.compareFields) {
@ -370,7 +370,7 @@ function loadRbacState(ctx, cb) {
next();
return;
}
// XXX Q! or concurrency forEachParallel
// XXX Limit concurrency here!
// TODO: Optimization: could avoid getting keys for users that are
// going to be deleted, for the `triton rbac apply` use case.
vasync.forEachParallel({
@ -540,7 +540,11 @@ function createRbacUpdatePlan(ctx, cb) {
idField: 'name',
have: ctx.rbacState.roles || [],
want: ctx.rbacConfig.roles || [],
compareFields: ['members', 'default_members', 'policies'],
compareFields: [
'members',
'default_members',
'policies'
],
normThing: function normRole(role) {
role.members.sort();
role.default_members.sort();