Skip to content
Open
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
18 changes: 14 additions & 4 deletions cli/src/commands/alarms.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const { getClient, getClientV1, resolveBoxGid } = require('../api/client');

const buildAlarmQuery = (gid, query) => {
const boxQuery = `box.id:${gid}`;
return query ? `${boxQuery} ${query}` : boxQuery;
};

const Alarms = {
archive: async (aid, options) => {
const gid = await resolveBoxGid(options.box, options);
Expand Down Expand Up @@ -28,13 +33,12 @@ const Alarms = {
list: async (options) => {
const gid = await resolveBoxGid(options.box, options);
const client = getClient(options);
let apiParams = { gid };

const apiParams = {};

// Only pass through supported API parameters
if (options.params) {
const parsedParams = JSON.parse(options.params);
// Filter to only supported parameters (limit, cursor, etc.)
const supportedParams = ['limit', 'cursor', 'query', 'groupBy', 'sortBy'];
supportedParams.forEach(param => {
if (parsedParams[param] !== undefined) {
Expand All @@ -43,6 +47,11 @@ const Alarms = {
});
}

// GET /v2/alarms is MSP-wide. Enforce the selected box using the
// documented alarm search qualifier instead of an unsupported `gid`
// collection parameter.
apiParams.query = buildAlarmQuery(gid, apiParams.query);

try {
const { data } = await client.get('/alarms', { params: apiParams });
console.log(JSON.stringify(data, null, 2));
Expand All @@ -53,3 +62,4 @@ const Alarms = {
};

module.exports = Alarms;
module.exports.buildAlarmQuery = buildAlarmQuery;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"fw": "./src/index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node --test"
},
"dependencies": {
"axios": "^1.6.0",
Expand Down
25 changes: 25 additions & 0 deletions test/alarms.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const test = require('node:test');
const assert = require('node:assert/strict');

const { buildAlarmQuery } = require('../cli/src/commands/alarms');

test('scopes an alarm list query to the selected box', () => {
assert.equal(
buildAlarmQuery('box-a', undefined),
'box.id:box-a'
);
});

test('preserves additional alarm filters while enforcing the selected box', () => {
assert.equal(
buildAlarmQuery('box-a', 'status:active type:1'),
'box.id:box-a status:active type:1'
);
});

test('does not allow a caller-supplied box qualifier to replace the selected box', () => {
assert.equal(
buildAlarmQuery('box-a', 'box.id:box-b'),
'box.id:box-a box.id:box-b'
);
});