diff --git a/src/models/device.supervisor-api.partial.ts b/src/models/device.supervisor-api.partial.ts index 14fe8de04..dc4131beb 100644 --- a/src/models/device.supervisor-api.partial.ts +++ b/src/models/device.supervisor-api.partial.ts @@ -22,11 +22,7 @@ import type { } from '..'; import type { Device } from '../types/models'; -import { - isNotFoundResponse, - treatAsMissingDevice, - withSupervisorLockedError, -} from '../util'; +import { withSupervisorLockedError } from '../util'; import { ensureVersionCompatibility } from '../util/device-os-version'; @@ -68,9 +64,6 @@ export const getSupervisorApiHelper = function ( } = deps; const { apiUrl } = opts; - const getId = (uuidOrId: string | number) => - sdkInstance.models.device._getId(uuidOrId); - const exports = { /** * @summary Ping a device @@ -166,37 +159,30 @@ export const getSupervisorApiHelper = function ( */ restartApplication: (uuidOrId: string | number): Promise => withSupervisorLockedError(async () => { - try { - const deviceOptions = { - $select: ['id', 'supervisor_version'], - $expand: { belongs_to__application: { $select: 'id' } }, - } satisfies PineOptions; - const device = (await sdkInstance.models.device.get( - uuidOrId, - deviceOptions, - )) as PineTypedResult; + const deviceOptions = { + $select: ['id', 'supervisor_version'], + $expand: { belongs_to__application: { $select: 'id' } }, + } satisfies PineOptions; + const device = (await sdkInstance.models.device.get( + uuidOrId, + deviceOptions, + )) as PineTypedResult; - const appId = device.belongs_to__application[0].id; - const { body } = await request.send({ - method: 'POST', - url: `/supervisor/v1/restart`, - baseUrl: apiUrl, - body: { - deviceId: device.id, + const appId = device.belongs_to__application[0].id; + const { body } = await request.send({ + method: 'POST', + url: `/supervisor/v1/restart`, + baseUrl: apiUrl, + body: { + deviceId: device.id, + appId, + data: { appId, - data: { - appId, - }, }, - timeout: CONTAINER_ACTION_ENDPOINT_TIMEOUT, - }); - return body; - } catch (err) { - if (isNotFoundResponse(err)) { - treatAsMissingDevice(uuidOrId, err); - } - throw err; - } + }, + timeout: CONTAINER_ACTION_ENDPOINT_TIMEOUT, + }); + return body; }), /** @@ -226,26 +212,19 @@ export const getSupervisorApiHelper = function ( options = {}; } - try { - const deviceId = await getId(uuidOrId); - const { body } = await request.send({ - method: 'POST', - url: '/supervisor/v1/reboot', - baseUrl: apiUrl, - body: { - deviceId, - data: { - force: Boolean(options?.force), - }, + const deviceId = (await sdkInstance.models.device.get(uuidOrId)).id; + const { body } = await request.send({ + method: 'POST', + url: '/supervisor/v1/reboot', + baseUrl: apiUrl, + body: { + deviceId, + data: { + force: Boolean(options?.force), }, - }); - return body; - } catch (err) { - if (isNotFoundResponse(err)) { - treatAsMissingDevice(uuidOrId, err); - } - throw err; - } + }, + }); + return body; }), /** diff --git a/src/models/device.ts b/src/models/device.ts index 3ea5672e0..23b3ac699 100644 --- a/src/models/device.ts +++ b/src/models/device.ts @@ -46,10 +46,8 @@ import memoizee from 'memoizee'; import { isId, - isNoDeviceForKeyResponse, isFullUuid, mergePineOptions, - treatAsMissingDevice, limitedMap, groupByMap, } from '../util'; @@ -222,17 +220,6 @@ const getDeviceModel = function ( return (await sdkInstance.models.config.getAll()).deviceUrlsBase; }); - // Internal method for uuid/id disambiguation - // Note that this throws an exception for missing uuids, but not missing ids - const getId = async (uuidOrId: string | number) => { - if (isId(uuidOrId)) { - return uuidOrId; - } else { - const { id } = await exports.get(uuidOrId, { $select: 'id' }); - return id; - } - }; - const getAppliedConfigVariableValue = async ( uuidOrId: string | number, name: string, @@ -433,7 +420,6 @@ const getDeviceModel = function ( } const exports = { - _getId: getId, OverallStatus, /** * @summary Get Dashboard URL for a specific device @@ -1456,25 +1442,18 @@ const getDeviceModel = function ( keyDescription?: string, keyExpiryDate?: string, ): Promise => { - try { - const deviceId = await getId(uuidOrId); - const { body } = await request.send({ - method: 'POST', - url: `/api-key/device/${deviceId}/device-key`, - baseUrl: apiUrl, - body: { - name: keyName, - description: keyDescription, - expiryDate: keyExpiryDate, - }, - }); - return body; - } catch (err) { - if (isNoDeviceForKeyResponse(err)) { - treatAsMissingDevice(uuidOrId, err); - } - throw err; - } + const deviceId = await sdkInstance.models.device.get(uuidOrId); + const { body } = await request.send({ + method: 'POST', + url: `/api-key/device/${deviceId}/device-key`, + baseUrl: apiUrl, + body: { + name: keyName, + description: keyDescription, + expiryDate: keyExpiryDate, + }, + }); + return body; }, /** diff --git a/src/util/index.ts b/src/util/index.ts index 0c30400d3..97aff3722 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -52,10 +52,6 @@ export const isUnauthorizedResponse = (err: Error) => export const isNotFoundResponse = (err: Error) => isBalenaRequestErrorResponseWithCode(err, 404); -export const isNoDeviceForKeyResponse = (err: Error) => - isBalenaRequestErrorResponseWithCode(err, 500) && - err.body === 'No device found to associate with the api key'; - export const isNoApplicationForKeyResponse = (err: Error) => isBalenaRequestErrorResponseWithCode(err, 500) && err.body === 'No application found to associate with the api key'; @@ -78,12 +74,6 @@ export const treatAsMissingApplication = ( throw replacementErr; }; -export const treatAsMissingDevice = (uuidOrId: string | number, err: Error) => { - const replacementErr = new errors.BalenaDeviceNotFound(uuidOrId); - replacementErr.stack = err.stack ?? ''; - throw replacementErr; -}; - // TODO: Make it so that it also infers the extras param export function mergePineOptionsTyped< R extends object,