Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/IAMClient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ declare class VaultClient {
* @returns {undefined}
*/
getAccountLimits(accountName: string, callback: any): undefined;
/**
* A getter for account rate limit configuration given a list of canonicalIDs
* @param {String[]} canonicalIds - list of canonicalIDs
* @param {Object} options - additional arguments
* @param {string} options.reqUid - the request UID
* @param {werelogs.RequestLogger} [options.logger] - Logger instance
* @param {Function} callback - the callback handling the response object
* and the error, if there is one
* @returns {undefined}
*/
getAccountLimitsByCanonicalIds(canonicalIds: string[], options: {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be too late for this comment:
Are we gonna support more types of limits other than rate limit.
Limits might confuse the end user/admin, getAccountRateLimitByCanonicalIds might be more descriptive.

reqUid: string;
logger?: werelogs.RequestLogger;
}, callback: Function): undefined;
/**
* Get Quota of an account
*
Expand Down
2 changes: 1 addition & 1 deletion lib/IAMClient.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 41 additions & 16 deletions lib/IAMClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,10 @@ class VaultClient {
limits: JSON.stringify(limits),
};
assert(!this.parameterValidation || (limits && typeof limits === 'object'),
'Limits must be an object');
if (accountName) {
assert(!this.parameterValidation || typeof accountName === 'string',
'the account name, if set, should be a string');
data.AccountName = accountName;
}
'limits must be an object');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be too late here as well, rate limit instead of limits

assert(!this.parameterValidation || typeof accountName === 'string',
'the account name should be a string');
data.AccountName = accountName;
this.request('POST', '/', true, callback, data);
}

Expand All @@ -370,11 +368,9 @@ class VaultClient {
Version: '2010-05-08',
};

if (accountName) {
assert(!this.parameterValidation || typeof accountName === 'string',
'the account name, if set, should be a string');
data.AccountName = accountName;
}
assert(!this.parameterValidation || typeof accountName === 'string',
'the account name should be a string');
data.AccountName = accountName;
this.request('POST', '/', true, callback, data);
}

Expand All @@ -391,14 +387,43 @@ class VaultClient {
Version: '2010-05-08',
};

if (accountName) {
assert(!this.parameterValidation || typeof accountName === 'string',
'the account name, if set, should be a string');
data.AccountName = accountName;
}
assert(!this.parameterValidation || typeof accountName === 'string',
'the account name should be a string');
data.AccountName = accountName;
this.request('POST', '/', true, callback, data);
}

/**
* A getter for account rate limit configuration given a list of canonicalIDs
* @param {String[]} canonicalIds - list of canonicalIDs
* @param {Object} options - additional arguments
* @param {string} options.reqUid - the request UID
* @param {werelogs.RequestLogger} [options.logger] - Logger instance
* @param {Function} callback - the callback handling the response object
* and the error, if there is one
* @returns {undefined}
*/
getAccountLimitsByCanonicalIds(canonicalIds, options, callback) {
this.getAccounts(null, null, canonicalIds, options, (err, res, code) => {
if (err) {
return callback(err);
}
const body = res.reduce((accounts, account) => {
// eslint-disable-next-line no-param-reassign
accounts[account.canId] = account.limits;
return accounts;
}, {});

return callback(null, {
message: {
body,
code,
message: 'Attributes retrieved',
},
});
});
}

/**
* Get Quota of an account
*
Expand Down
16 changes: 5 additions & 11 deletions tests/unit/deleteAccountLimits.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,19 @@ describe('IAMClient - deleteAccountLimits', () => {
});
});

it('should call the request method with the correct parameters when accountName is not provided', () => {
it('should throw an error when accountName is not provided', () => {
client = createClient(true);
client.deleteAccountLimits(undefined, () => {});

assert.strictEqual(lastRequestData.method, 'POST');
assert.strictEqual(lastRequestData.path, '/');
assert.strictEqual(lastRequestData.iamAuthenticate, true);
assert.deepStrictEqual(lastRequestData.data, {
Action: 'DeleteAccountLimits',
Version: '2010-05-08',
});
assert.throws(() => {
client.deleteAccountLimits(undefined, () => {});
}, /the account name should be a string/);
});

it('should throw an error if accountName is not a string', () => {
client = createClient(true);
const accountName = 123;
assert.throws(() => {
client.deleteAccountLimits(accountName, () => {});
}, /the account name, if set, should be a string/);
}, /the account name should be a string/);
});

it('should call the request method when wrong options are provided and parameterValidation is false', () => {
Expand Down
16 changes: 5 additions & 11 deletions tests/unit/getAccountLimits.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,12 @@ describe('GetAccountLimits', () => {
assert.deepStrictEqual(lastRequestData.data, expectedData);
});

it('should call the request method with default parameters when accountName is not provided', () => {
it('should throw an error when accountName is not provided', () => {
client = createClient(true);
const callback = () => {};
client.getAccountLimits(undefined, callback);
const expectedData = {
Action: 'GetAccountLimits',
Version: '2010-05-08',
};
assert.strictEqual(lastRequestData.method, 'POST');
assert.strictEqual(lastRequestData.path, '/');
assert.strictEqual(lastRequestData.iamAuthenticate, true);
assert.deepStrictEqual(lastRequestData.data, expectedData);
assert.throws(() => {
client.getAccountLimits(undefined, callback);
}, /the account name should be a string/);
});

it('should throw an error if accountName is not a string', () => {
Expand All @@ -52,7 +46,7 @@ describe('GetAccountLimits', () => {
const callback = () => {};
assert.throws(() => {
client.getAccountLimits(accountName, callback);
}, /the account name, if set, should be a string/);
}, /the account name should be a string/);
});

it('should not throw an error if accountName is not a string and parameterValidation is false', () => {
Expand Down
49 changes: 49 additions & 0 deletions tests/unit/getAccountLimitsByCanonicalId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

const assert = require('assert');
const IAMClient = require('../../lib/IAMClient');

const canonicalIds = ['canId1', 'canId2'];
const opt = { reqUid: 'test.getAccounts.reqUid' };
const mockCB = () => {};

const expectedData = {
Action: 'GetAccounts',
Version: '2010-05-08',
};

describe('getAccountLimitsByCanonicalIds', () => {
let client;
let spyArg = null;

beforeEach('spy on request', done => {
client = new IAMClient('127.0.0.1', 8500);
client.request = function spy(...args) {
spyArg = args;
};
done();
});

afterEach('reset spyArg', () => { spyArg = null; });

it('should send request with correct arguments', () => {
client.getAccountLimitsByCanonicalIds(canonicalIds, opt, mockCB);
const [method, path, auth, cb, data, reqUid, contentType] = spyArg;
assert.strictEqual(method, 'GET');
assert.strictEqual(path, '/');
assert.strictEqual(auth, false);
assert.strictEqual(typeof cb, 'function');
assert.strictEqual(reqUid, opt.reqUid);
assert.strictEqual(contentType, null);
assert.deepStrictEqual(
data,
{ ...expectedData, canonicalIds },
);
});


it('should throw with invalid arguments', () => {
assert.throws(() => client.getAccountLimitsByCanonicalIds('foobar', opt, mockCB),
assert.AssertionError);
});
});
24 changes: 8 additions & 16 deletions tests/unit/updateAccountLimits.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,13 @@ describe('updateAccountLimits', () => {
assert.deepStrictEqual(lastRequestData.data, expectedData);
});

it('should call the request method with the correct parameters when accountName is not provided', () => {
it('should throw when accountName is not provided', () => {
client = createClient(true);
const limits = { rateLimit: 1000 };

const expectedData = {
Action: 'UpdateAccountLimits',
Version: '2010-05-08',
limits: JSON.stringify(limits),
};

client.updateAccountLimits(undefined, limits, () => {});
assert.strictEqual(lastRequestData.method, 'POST');
assert.strictEqual(lastRequestData.path, '/');
assert.strictEqual(lastRequestData.iamAuthenticate, true);
assert.deepStrictEqual(lastRequestData.data, expectedData);
assert.throws(
() => client.updateAccountLimits(undefined, limits, () => {})
, /the account name should be a string/
);
});

it('should handle complex limits object correctly', () => {
Expand Down Expand Up @@ -82,7 +74,7 @@ describe('updateAccountLimits', () => {

assert.throws(() => {
client.updateAccountLimits(accountName, limits, () => {});
}, /Limits must be an object/);
}, /limits must be an object/);
});

it('should throw an error if limits is null', () => {
Expand All @@ -92,7 +84,7 @@ describe('updateAccountLimits', () => {

assert.throws(() => {
client.updateAccountLimits(accountName, limits, () => {});
}, /Limits must be an object/);
}, /limits must be an object/);
});

it('should not throw an error if limits is not an object and parameterValidation is false', () => {
Expand Down Expand Up @@ -121,7 +113,7 @@ describe('updateAccountLimits', () => {

assert.throws(() => {
client.updateAccountLimits(accountName, limits, () => {});
}, /the account name, if set, should be a string/);
}, /the account name should be a string/);
});

it('should not throw an error if accountName is not a string and parameterValidation is false', () => {
Expand Down
Loading