From 0d4e411777dd53d83c475da3653ab94176e07d7d Mon Sep 17 00:00:00 2001 From: Otavio Jacobi Date: Thu, 27 Mar 2025 15:25:06 -0300 Subject: [PATCH] Fix device detail for open balena Change-type: patch --- npm-shrinkwrap.json | 12 +-- src/commands/device/index.ts | 146 ++++++++++----------------- tests/commands/device/device.spec.ts | 8 ++ 3 files changed, 65 insertions(+), 101 deletions(-) diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index ab60e3fc..b587773a 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -2682,9 +2682,9 @@ } }, "node_modules/@inquirer/core/node_modules/@types/node": { - "version": "22.13.13", - "resolved": "https://registry.npmjs.org/@types/node/-/node-22.13.13.tgz", - "integrity": "sha512-ClsL5nMwKaBRwPcCvH8E7+nU4GxHVx1axNvMZTFHMEfNI7oahimt26P5zjVCRrjiIWj6YFXfE1v3dEp94wLcGQ==", + "version": "22.13.14", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.13.14.tgz", + "integrity": "sha512-Zs/Ollc1SJ8nKUAgc7ivOEdIBM8JAKgrqqUYi2J997JuKO7/tpQC+WCetQ1sypiKCQWHdvdg9wBNpUPEWZae7w==", "dev": true, "license": "MIT", "dependencies": { @@ -5829,9 +5829,9 @@ } }, "node_modules/@types/node": { - "version": "20.17.27", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.17.27.tgz", - "integrity": "sha512-U58sbKhDrthHlxHRJw7ZLiLDZGmAUOZUbpw0S6nL27sYUdhvgBLCRu/keSd6qcTsfArd1sRFCCBxzWATGr/0UA==", + "version": "20.17.28", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.17.28.tgz", + "integrity": "sha512-DHlH/fNL6Mho38jTy7/JT7sn2wnXI+wULR6PV4gy4VHLVvnrV/d3pHAMQHhc4gjdLmK2ZiPoMxzp6B3yRajLSQ==", "license": "MIT", "dependencies": { "undici-types": "~6.19.2" diff --git a/src/commands/device/index.ts b/src/commands/device/index.ts index 4d421b3f..11acc7b0 100644 --- a/src/commands/device/index.ts +++ b/src/commands/device/index.ts @@ -77,103 +77,59 @@ export default class DeviceCmd extends Command { const balena = getBalenaSdk(); - const device = (await balena.models.device.get( - params.uuid, - options.json - ? { - $select: [ - 'id', - 'belongs_to__application', - 'belongs_to__user', - 'actor', - 'is_pinned_on__release', - 'device_name', - 'is_of__device_type', - 'uuid', - 'is_running__release', - 'note', - 'local_id', - 'status', - 'update_status', - 'last_update_status_event', - 'is_online', - 'last_connectivity_event', - 'is_connected_to_vpn', - 'last_vpn_event', - 'ip_address', - 'mac_address', - 'public_address', - 'os_version', - 'os_variant', - 'supervisor_version', - 'should_be_managed_by__release', - 'should_be_operated_by__release', - 'is_managed_by__service_instance', - 'provisioning_progress', - 'provisioning_state', - 'download_progress', - 'is_web_accessible', - 'longitude', - 'latitude', - 'location', - 'custom_longitude', - 'custom_latitude', - 'is_locked_until__date', - 'is_accessible_by_support_until__date', - 'created_at', - 'modified_at', - 'is_active', - 'api_heartbeat_state', - 'changed_api_heartbeat_state_on__date', - 'memory_usage', - 'memory_total', - 'storage_block_device', - 'storage_usage', - 'storage_total', - 'cpu_temp', - 'cpu_usage', - 'cpu_id', - 'is_undervolted', - // explicit read - 'overall_status', - 'overall_progress', - 'should_be_running__release', - ], - $expand: { - device_tag: { - $select: ['tag_key', 'value'], - }, - ...expandForAppName.$expand, + let device: ExtendedDevice; + if (options.json) { + const [deviceBase, deviceComputed] = await Promise.all([ + balena.models.device.get(params.uuid, { + $expand: { + device_tag: { + $select: ['tag_key', 'value'], }, - } - : { - $select: [ - 'device_name', - 'id', - 'overall_status', - 'is_online', - 'ip_address', - 'mac_address', - 'last_connectivity_event', - 'uuid', - 'supervisor_version', - 'is_web_accessible', - 'note', - 'os_version', - 'memory_usage', - 'memory_total', - 'public_address', - 'storage_block_device', - 'storage_usage', - 'storage_total', - 'cpu_usage', - 'cpu_temp', - 'cpu_id', - 'is_undervolted', - ], - ...expandForAppName, + ...expandForAppName.$expand, }, - )) as ExtendedDevice; + }), + balena.models.device.get(params.uuid, { + $select: [ + 'overall_status', + 'overall_progress', + 'should_be_running__release', + ], + }), + ]); + + device = { + ...deviceBase, + ...deviceComputed, + } as ExtendedDevice; + } else { + device = (await balena.models.device.get(params.uuid, { + $select: [ + 'device_name', + 'id', + 'overall_status', + 'is_online', + 'ip_address', + 'mac_address', + 'last_connectivity_event', + 'uuid', + 'supervisor_version', + 'is_web_accessible', + 'note', + 'os_version', + 'memory_usage', + 'memory_total', + 'public_address', + 'storage_block_device', + 'storage_usage', + 'storage_total', + 'cpu_usage', + 'cpu_temp', + 'cpu_id', + 'is_undervolted', + ], + ...expandForAppName, + })) as ExtendedDevice; + } if (options.view) { const open = await import('open'); diff --git a/tests/commands/device/device.spec.ts b/tests/commands/device/device.spec.ts index 3f283a2d..7bdab261 100644 --- a/tests/commands/device/device.spec.ts +++ b/tests/commands/device/device.spec.ts @@ -114,6 +114,14 @@ describe('balena device', function () { 'Content-Type': 'application/json', }); + api.scope + .get( + /^\/v\d+\/device\?.+&\$select=overall_status,overall_progress,should_be_running__release$/, + ) + .replyWithFile(200, path.join(apiResponsePath, 'device.json'), { + 'Content-Type': 'application/json', + }); + const { out, err } = await runCommand('device 27fda508c --json'); expect(err).to.be.empty; const json = JSON.parse(out.join(''));