Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

devices: add device_tag to JSON output #2693

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
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']),
Copy link
Contributor

Choose a reason for hiding this comment

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

@thgreasi You probably have more context than I do on this, should the, now being expanded, device_tag have the pickAndRename applied to it? AFAIK this only applies in case of something being formatted with 'a => b' and we probably wouldn't want to change it if this is the tag value of the client?

Copy link
Member

Choose a reason for hiding this comment

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

@otaviojacobi I think that we only use pickAndRename in this file for consistency reasons and that in this file using pick or pickAndRename has the same outcome since there is no => used in the defined fields.

);
console.log(JSON.stringify(mapped, null, 4));
} else {
const _ = await import('lodash');
Expand Down
25 changes: 25 additions & 0 deletions tests/commands/device/devices.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
});
});
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"
}
]
}
]
}
Loading