Skip to content

Commit

Permalink
Move backfill device service env var hook to a v7 translation hook
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
otaviojacobi committed Nov 29, 2024
1 parent 74befd7 commit 2813558
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 42 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { hooks, errors, type sbvrUtils } from '@balena/pinejs';

async function backfillServiceInstall({
request,
api,
}: sbvrUtils.HookArgs<'resin'>) {
const { device: deviceId, service: serviceId } = request.values;

if (deviceId == null && serviceId == null) {
return;
}

if (
(deviceId == null && serviceId != null) ||
(deviceId != null && serviceId == null)
) {
throw new errors.BadRequestError(
'Both or none of device and service must be specified',
);
}

let si = await api.get({
resource: 'service_install',
id: {
device: deviceId,
installs__service: serviceId,
},
options: {
$select: ['id'],
},
});

if (si == null) {
si = await api.post({
resource: 'service_install',
body: {
device: deviceId,
installs__service: serviceId,
},
});
}

if (si == null) {
throw new errors.BadRequestError(
`No service install exists for device: ${deviceId} and service ${serviceId} and one could not be created`,
);
}

request.values.service_install = si.id;
}

hooks.addPureHook('POST', 'resin', 'device_service_environment_variable', {
POSTPARSE: backfillServiceInstall,
});

hooks.addPureHook('PATCH', 'resin', 'device_service_environment_variable', {
POSTPARSE: backfillServiceInstall,
});
2 changes: 1 addition & 1 deletion src/features/service-install/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import './backfill-device-service-environment-variable.js';
import './backfill-service-install-on-device-service-env-var.js';
45 changes: 45 additions & 0 deletions src/translations/v7/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { hooks, sbvrUtils, errors } from '@balena/pinejs';

const addReadOnlyHook = (
methods: Array<Parameters<typeof hooks.addHook>[0]>,
resource: string,
hook: sbvrUtils.Hooks<'v7'>,
) => {
methods.map((method) => {
hooks.addHook(method, 'v7', resource, {
...hook,
sideEffects: false,
readOnlyTx: true,
});
});
};

addReadOnlyHook(
['POST', 'PATCH', 'PUT'],
'device_service_environment_variable',
{
POSTPARSE: async ({ request, api }) => {
const { service_install: siId } = request.values;

if (siId == null) {
return;
}

const si = await sbvrUtils.api.resin.get({
resource: 'service_install',
passthrough: api.passthrough,
id: siId,
options: {
$select: ['device', 'service'],
},
});

if (si == null) {
throw new errors.UnauthorizedError();
}

request.values.device = si.device.__id;
request.values.service = si.service.__id;
},
},
);
4 changes: 3 additions & 1 deletion test/25_service-installs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ export default () => {
.post({
resource: 'device_service_environment_variable',
body: {
service_install: serviceInstall.id,
...(versions.lte(version, 'v7')
? { service_install: serviceInstall.id }
: { device: ctx.device.id, service: ctx.app2Service1.id }),
name: 'test',
value: '123',
},
Expand Down
10 changes: 5 additions & 5 deletions test/test-lib/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,23 +508,23 @@ const loaders: types.Dictionary<LoaderFunc> = {
logErrorAndThrow(`Could not find service: ${jsonData.service}`);
}

const si = await expectToEventually(async () => {
const $si = await api.resin.get({
await expectToEventually(async () => {
const si = await api.resin.get({
resource: 'service_install',
passthrough: { req: permissions.rootRead },
id: {
device: device.id,
installs__service: service.id,
},
});
assertExists($si);
return $si;
assertExists(si);
});

return await createResource({
resource: 'device_service_environment_variable',
body: {
service_install: si.id,
device: device.id,
service: service.id,
name: jsonData.name,
value: jsonData.value,
},
Expand Down

0 comments on commit 2813558

Please sign in to comment.