From bd194e6b63f9ca9a5a30fbf6e21fc897b9e5d8b8 Mon Sep 17 00:00:00 2001 From: Brian Bugh <438465+bbugh@users.noreply.github.com> Date: Fri, 27 Oct 2023 12:21:29 -0500 Subject: [PATCH] devices: add device_tag to JSON output change-type: minor --- lib/commands/devices/index.ts | 13 ++++++++++-- tests/commands/device/devices.spec.ts | 25 +++++++++++++++++++++++ tests/test-data/api-response/devices.json | 16 +++++++++++++-- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/commands/devices/index.ts b/lib/commands/devices/index.ts index e7196c42bf..0b0ecb59ef 100644 --- a/lib/commands/devices/index.ts +++ b/lib/commands/devices/index.ts @@ -71,7 +71,14 @@ export default class DevicesCmd extends Command { const balena = getBalenaSdk(); const devicesOptions = { ...devicesSelectFields, - ...expandForAppName, + $expand: { + ...(options.json && { + device_tag: { + $select: ['tag_key', 'value'], + }, + }), + ...expandForAppName.$expand, + }, $orderby: { device_name: 'asc' }, } satisfies PineOptions; @@ -116,7 +123,9 @@ export default class DevicesCmd extends Command { if (options.json) { const { pickAndRename } = await import('../../utils/helpers'); - const mapped = devices.map((device) => pickAndRename(device, fields)); + const mapped = devices.map((device) => + pickAndRename(device, [...fields, 'device_tag']), + ); console.log(JSON.stringify(mapped, null, 4)); } else { const _ = await import('lodash'); diff --git a/tests/commands/device/devices.spec.ts b/tests/commands/device/devices.spec.ts index 5c0ac5baa9..2ddab235e3 100644 --- a/tests/commands/device/devices.spec.ts +++ b/tests/commands/device/devices.spec.ts @@ -59,4 +59,29 @@ describe('balena devices', function () { // e.g. When user has a device associated with app that user is no longer a collaborator of. expect(lines.some((l) => l.includes('N/a'))).to.be.true; }); + + it('should list devices from own and collaborator apps', async () => { + api.scope + .get( + '/v6/device?$orderby=device_name%20asc&$select=id,uuid,device_name,status,is_online,supervisor_version,os_version&$expand=device_tag($select=tag_key,value),belongs_to__application($select=app_name,slug),is_of__device_type($select=slug),is_running__release($select=commit)', + ) + .replyWithFile(200, path.join(apiResponsePath, 'devices.json'), { + 'Content-Type': 'application/json', + }); + + const { out, err } = await runCommand('devices --json'); + expect(err).to.be.empty; + const json = JSON.parse(out.join('')); + expect(json[0].device_name).to.equal('sparkling-wood'); + expect(json[0].device_tag[0]).to.deep.equal({ + tag_key: 'example', + value: '0', + }); + + expect(json[1].device_name).to.equal('dashing-spruce'); + expect(json[1].device_tag[0]).to.deep.equal({ + tag_key: 'example', + value: '1', + }); + }); }); diff --git a/tests/test-data/api-response/devices.json b/tests/test-data/api-response/devices.json index e6403c4a05..cdd7878292 100644 --- a/tests/test-data/api-response/devices.json +++ b/tests/test-data/api-response/devices.json @@ -22,7 +22,13 @@ "supervisor_version": "10.3.7", "__metadata": { "uri": "/resin/device(@id)?@id=1747415" - } + }, + "device_tag": [ + { + "tag_key": "example", + "value": "0" + } + ] }, { "belongs_to__application": [], @@ -41,7 +47,13 @@ "supervisor_version": "10.3.7", "__metadata": { "uri": "/resin/device(@id)?@id=1747415" - } + }, + "device_tag": [ + { + "tag_key": "example", + "value": "1" + } + ] } ] }