devices: add device_tag to JSON output

change-type: minor
This commit is contained in:
Brian Bugh 2023-10-27 12:21:29 -05:00
parent 801a25995c
commit bd194e6b63
No known key found for this signature in database
GPG Key ID: 22398F30E3B701CF
3 changed files with 50 additions and 4 deletions

View File

@ -71,7 +71,14 @@ export default class DevicesCmd extends Command {
const balena = getBalenaSdk(); const balena = getBalenaSdk();
const devicesOptions = { const devicesOptions = {
...devicesSelectFields, ...devicesSelectFields,
...expandForAppName, $expand: {
...(options.json && {
device_tag: {
$select: ['tag_key', 'value'],
},
}),
...expandForAppName.$expand,
},
$orderby: { device_name: 'asc' }, $orderby: { device_name: 'asc' },
} satisfies PineOptions<Device>; } satisfies PineOptions<Device>;
@ -116,7 +123,9 @@ export default class DevicesCmd extends Command {
if (options.json) { if (options.json) {
const { pickAndRename } = await import('../../utils/helpers'); 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)); console.log(JSON.stringify(mapped, null, 4));
} else { } else {
const _ = await import('lodash'); const _ = await import('lodash');

View File

@ -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. // 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; 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',
});
});
}); });

View File

@ -22,7 +22,13 @@
"supervisor_version": "10.3.7", "supervisor_version": "10.3.7",
"__metadata": { "__metadata": {
"uri": "/resin/device(@id)?@id=1747415" "uri": "/resin/device(@id)?@id=1747415"
} },
"device_tag": [
{
"tag_key": "example",
"value": "0"
}
]
}, },
{ {
"belongs_to__application": [], "belongs_to__application": [],
@ -41,7 +47,13 @@
"supervisor_version": "10.3.7", "supervisor_version": "10.3.7",
"__metadata": { "__metadata": {
"uri": "/resin/device(@id)?@id=1747415" "uri": "/resin/device(@id)?@id=1747415"
} },
"device_tag": [
{
"tag_key": "example",
"value": "1"
}
]
} }
] ]
} }