Skip to content

Commit

Permalink
devices: add device_tag to JSON output
Browse files Browse the repository at this point in the history
change-type: minor
  • Loading branch information
bbugh authored and otaviojacobi committed Nov 13, 2023
1 parent f44fa38 commit 4eb1aef
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/commands/devices/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Device>;

Expand Down Expand Up @@ -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');
Expand Down
26 changes: 26 additions & 0 deletions tests/commands/device/devices.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,30 @@ 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(''));
console.log(json);
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',
});
});
});
16 changes: 14 additions & 2 deletions tests/test-data/api-response/devices.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand All @@ -41,7 +47,13 @@
"supervisor_version": "10.3.7",
"__metadata": {
"uri": "/resin/device(@id)?@id=1747415"
}
},
"device_tag": [
{
"tag_key": "example",
"value": "1"
}
]
}
]
}

0 comments on commit 4eb1aef

Please sign in to comment.