Skip to content

Commit

Permalink
Stop silencing Not Found errors for devices, applications, and organi…
Browse files Browse the repository at this point in the history
…zations

Resolves: #577
Resolves: #649
Change-type: major
  • Loading branch information
myarmolinsky committed Dec 12, 2024
1 parent 12d7522 commit 2a14629
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 262 deletions.
167 changes: 71 additions & 96 deletions src/models/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@ import * as url from 'url';
import once from 'lodash/once';
import * as errors from 'balena-errors';

import {
isId,
isNoApplicationForKeyResponse,
isNotFoundResponse,
mergePineOptions,
treatAsMissingApplication,
withSupervisorLockedError,
} from '../util';
import { isId, mergePineOptions, withSupervisorLockedError } from '../util';

import {
getCurrentServiceDetailsPineExpand,
Expand Down Expand Up @@ -732,18 +725,15 @@ const getApplicationModel = function (
slugOrUuidOrIdOrIds: string | number | number[],
): Promise<void> => {
if (typeof slugOrUuidOrIdOrIds === 'string') {
try {
const applicationId = await getId(slugOrUuidOrIdOrIds);
await pine.delete({
resource: 'application',
id: applicationId,
});
} catch (err) {
if (isNotFoundResponse(err)) {
treatAsMissingApplication(slugOrUuidOrIdOrIds, err);
}
throw err;
}
const applicationId = (
await sdkInstance.models.application.get(slugOrUuidOrIdOrIds, {
$select: 'id',
})
).id;
await pine.delete({
resource: 'application',
id: applicationId,
});
return;
}
await batchApplicationOperation()({
Expand Down Expand Up @@ -783,21 +773,18 @@ const getApplicationModel = function (
slugOrUuidOrId: string | number,
newAppName: string,
): Promise<void> => {
try {
const applicationId = await getId(slugOrUuidOrId);
await pine.patch({
resource: 'application',
id: applicationId,
body: {
app_name: newAppName,
},
});
} catch (err) {
if (isNotFoundResponse(err)) {
treatAsMissingApplication(slugOrUuidOrId, err);
}
throw err;
}
const applicationId = (
await sdkInstance.models.application.get(slugOrUuidOrId, {
$select: 'id',
})
).id;
await pine.patch({
resource: 'application',
id: applicationId,
body: {
app_name: newAppName,
},
});
},

/**
Expand All @@ -818,20 +805,17 @@ const getApplicationModel = function (
*/
restart: (slugOrUuidOrId: string | number): Promise<void> =>
withSupervisorLockedError(async () => {
try {
const applicationId = await getId(slugOrUuidOrId);
const applicationId = (
await sdkInstance.models.application.get(slugOrUuidOrId, {
$select: 'id',
})
).id;

await request.send({
method: 'POST',
url: `/application/${applicationId}/restart`,
baseUrl: apiUrl,
});
} catch (err) {
if (isNotFoundResponse(err)) {
treatAsMissingApplication(slugOrUuidOrId, err);
}
throw err;
}
await request.send({
method: 'POST',
url: `/application/${applicationId}/restart`,
baseUrl: apiUrl,
});
}),

/**
Expand Down Expand Up @@ -869,28 +853,25 @@ const getApplicationModel = function (
keyDescription?: string,
keyExpiryDate?: string,
): Promise<string> => {
try {
const applicationId = await getId(slugOrUuidOrId);
const { body } = await request.send({
method: 'POST',
url: '/api-key/v1/',
baseUrl: apiUrl,
body: {
actorType: 'application',
actorTypeId: applicationId,
roles: ['provisioning-api-key'],
name: keyName,
description: keyDescription,
expiryDate: keyExpiryDate,
},
});
return body;
} catch (err) {
if (isNoApplicationForKeyResponse(err)) {
treatAsMissingApplication(slugOrUuidOrId, err);
}
throw err;
}
const applicationId = (
await sdkInstance.models.application.get(slugOrUuidOrId, {
$select: 'id',
})
).id;
const { body } = await request.send({
method: 'POST',
url: '/api-key/v1/',
baseUrl: apiUrl,
body: {
actorType: 'application',
actorTypeId: applicationId,
roles: ['provisioning-api-key'],
name: keyName,
description: keyDescription,
expiryDate: keyExpiryDate,
},
});
return body;
},

/**
Expand Down Expand Up @@ -1318,19 +1299,16 @@ const getApplicationModel = function (
);
}

try {
const applicationId = await getId(slugOrUuidOrId);
await pine.patch({
resource: 'application',
id: applicationId,
body: { is_accessible_by_support_until__date: expiryTimestamp },
});
} catch (err) {
if (isNotFoundResponse(err)) {
treatAsMissingApplication(slugOrUuidOrId, err);
}
throw err;
}
const applicationId = (
await sdkInstance.models.application.get(slugOrUuidOrId, {
$select: 'id',
})
).id;
await pine.patch({
resource: 'application',
id: applicationId,
body: { is_accessible_by_support_until__date: expiryTimestamp },
});
},

/**
Expand All @@ -1352,19 +1330,16 @@ const getApplicationModel = function (
revokeSupportAccess: async (
slugOrUuidOrId: string | number,
): Promise<void> => {
try {
const applicationId = await getId(slugOrUuidOrId);
await pine.patch({
resource: 'application',
id: applicationId,
body: { is_accessible_by_support_until__date: null },
});
} catch (err) {
if (isNotFoundResponse(err)) {
treatAsMissingApplication(slugOrUuidOrId, err);
}
throw err;
}
const applicationId = (
await sdkInstance.models.application.get(slugOrUuidOrId, {
$select: 'id',
})
).id;
await pine.patch({
resource: 'application',
id: applicationId,
body: { is_accessible_by_support_until__date: null },
});
},

/**
Expand Down
89 changes: 34 additions & 55 deletions src/models/device.supervisor-api.partial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -166,37 +159,30 @@ export const getSupervisorApiHelper = function (
*/
restartApplication: (uuidOrId: string | number): Promise<void> =>
withSupervisorLockedError(async () => {
try {
const deviceOptions = {
$select: ['id', 'supervisor_version'],
$expand: { belongs_to__application: { $select: 'id' } },
} satisfies PineOptions<Device>;
const device = (await sdkInstance.models.device.get(
uuidOrId,
deviceOptions,
)) as PineTypedResult<Device, typeof deviceOptions>;
const deviceOptions = {
$select: ['id', 'supervisor_version'],
$expand: { belongs_to__application: { $select: 'id' } },
} satisfies PineOptions<Device>;
const device = (await sdkInstance.models.device.get(
uuidOrId,
deviceOptions,
)) as PineTypedResult<Device, typeof deviceOptions>;

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;
}),

/**
Expand Down Expand Up @@ -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;
}),

/**
Expand Down
Loading

0 comments on commit 2a14629

Please sign in to comment.