Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from akataz/updateSchemaFix
Browse files Browse the repository at this point in the history
Update schema fix
  • Loading branch information
americk0 authored Aug 24, 2018
2 parents b5223cd + 1abc030 commit 36835bf
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 9 deletions.
4 changes: 2 additions & 2 deletions components/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Account.prototype._list_cb = function(flags, cb, err, result) {
* @param {arrayCallback} cb - called with an array of accounts
*/
Account.prototype._list_all = function(cb) {
this.list({stats: true}, (err, stats) => {
this.list({stats: true, page_count: 200}, (err, stats) => {
if (err) return cb(err);
var calls = [];
for (var i=0, len=stats.page_count; i<len; i++) {
Expand Down Expand Up @@ -171,7 +171,7 @@ Account.prototype._find_by = function(field, value, list, cb) {
cb(null, matches);
}
else {
this.list({all: true}, (err, matches) => {
this.list({all: true, page_count: 200}, (err, matches) => {
if (err) return cb(err);
this.find_by(field, value, matches, cb);
});
Expand Down
31 changes: 25 additions & 6 deletions components/perspective.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ Perspective.prototype.get = function(flags, id, cb) {
}
// ensure id is a string
id = '' + id;

// if "id" is a name, lookup the name
if (!id.match(/^[0-9]+$/)) {
this._lookup_id(flags, id, (err, id) => {
Expand All @@ -75,7 +74,6 @@ Perspective.prototype.get = function(flags, id, cb) {
*/
Perspective.prototype._get = function(flags, id, cb) {
var options = this._options('GET', '/' + id, ['include_version=true']);

utils.send_request(options, null, this._get_cb.bind(this, flags, id, cb));
};

Expand Down Expand Up @@ -230,10 +228,13 @@ Perspective.prototype.add_to_group = function(pers, accts, group_name, cb) {
*/
Perspective.prototype._get_rule = function(pers, group_id) {
// get the rule specifying accounts that belong to this group
var rule = pers.rules.find(
(rule) => rule.asset === 'AwsAccount' && rule.to === group_id
);
var rule;

if (pers.rules.length) {
rule = pers.rules.find(
(rule) => rule.asset === 'AwsAccount' && rule.to === group_id && rule.from === undefined
);
}
// if the rule doesn't exist, make a rule
if (!rule) {
rule = {
Expand All @@ -246,10 +247,28 @@ Perspective.prototype._get_rule = function(pers, group_id) {
};
pers.rules.push(rule);
}

return rule;
};

/**
* remove previous references to an account within the rules of the schema
* @function module:cox-chapi.Perspective#remove_prev_refs
* @param {object} options options object of params
* @param {object} options.pers - a perspective containing rules
* @param {string} options.account_ref_id ref_id of the account
* @param {objectCallback} cb - called with the updated pers object
*/
Perspective.prototype.remove_prev_refs = function({ pers, account_ref_id }, cb) {
pers.rules.forEach((rule) => {
if (rule.asset === 'AwsAccount'){
rule.condition.clauses = rule.condition.clauses.filter(clause => clause.asset_ref != account_ref_id);
};
});
pers.rules = pers.rules.filter(rule => rule.condition.clauses.length);
cb(null, pers);
}

/**
* gets a JSON object containing all the perspectives
* @function module:cox-chapi.Perspective#list
Expand Down
111 changes: 111 additions & 0 deletions test/unit/components/perspective.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,56 @@ describe('Perspective', function() {
expect(test_rule).to.equal(rule);
});

it('should NOT return a rule if the rule has a defined from field', function() {
var group_id = '1234';
var rule = {
asset: 'AwsAccount',
to: group_id,
from: '4567',
type: 'filter',
condition: {
clauses: [],
},
};
var pers = {
rules: [
rule,
{
asset: 'AwsAccount',
to: '5678',
type: 'filter',
condition: {
clauses: [],
},
},
],
};

var test_rule = p._get_rule(pers, group_id);

expect(test_rule).to.not.equal(rule);
});

it('should return a new rule if the perspective has no rules', function() {
var group_id = '1234';
var rule = {
asset: 'AwsAccount',
to: group_id,
type: 'filter',
condition: {
clauses: [],
},
};
var pers = {
rules: [],
};

var test_rule = p._get_rule(pers, group_id);

expect(test_rule).to.eql(rule);
expect(pers.rules).to.contain(rule);
});

it('should return a new rule if no matching rule is found', function() {
var group_id = '1234';
var rule = {
Expand Down Expand Up @@ -854,6 +904,67 @@ describe('Perspective', function() {
});
});

describe('#remove_prev_refs', () => {
const account_ref_id = '1234';
const pers = {
rules: [
{
asset: 'AwsAccount',
to: '5678',
type: 'filter',
condition: {
clauses: [
{
asset_ref: '1234',
},
{
asset_ref: '2345',
}
],
},
},
{
asset: 'AwsAccount',
to: '5678',
type: 'filter',
condition: {
clauses: [
{
asset_ref: '1234',
}
],
},
},
]
};
const persOutput = {
rules: [
{
asset: 'AwsAccount',
to: '5678',
type: 'filter',
condition: {
clauses: [
{
asset_ref: '2345',
}
],
},
},
]
};
before(function() {
p = new Perspective();
});

it('should remove any clauses that contain refs to the account and then remove empty rules', (done) => {
p.remove_prev_refs({ pers, account_ref_id }, (err, data) => {
expect(data).to.be.eql(persOutput);
done();
});
});
})

describe('#list', function() {
before(function() {
p = new Perspective();
Expand Down
1 change: 0 additions & 1 deletion utils/chapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,6 @@ function set_cache(cache_name, cache, cb) {
if (cb) return cb(err1);
else throw err1;
}

settings.cache[cache_name] = cache;

this._set_settings(settings, (err2, settings) => {
Expand Down

0 comments on commit 36835bf

Please sign in to comment.