Skip to content

Commit 2813558

Browse files
committed
Move backfill device service env var hook to a v7 translation hook
Change-type: patch
1 parent 74befd7 commit 2813558

File tree

6 files changed

+112
-42
lines changed

6 files changed

+112
-42
lines changed

src/features/service-install/hooks/backfill-device-service-environment-variable.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { hooks, errors, type sbvrUtils } from '@balena/pinejs';
2+
3+
async function backfillServiceInstall({
4+
request,
5+
api,
6+
}: sbvrUtils.HookArgs<'resin'>) {
7+
const { device: deviceId, service: serviceId } = request.values;
8+
9+
if (deviceId == null && serviceId == null) {
10+
return;
11+
}
12+
13+
if (
14+
(deviceId == null && serviceId != null) ||
15+
(deviceId != null && serviceId == null)
16+
) {
17+
throw new errors.BadRequestError(
18+
'Both or none of device and service must be specified',
19+
);
20+
}
21+
22+
let si = await api.get({
23+
resource: 'service_install',
24+
id: {
25+
device: deviceId,
26+
installs__service: serviceId,
27+
},
28+
options: {
29+
$select: ['id'],
30+
},
31+
});
32+
33+
if (si == null) {
34+
si = await api.post({
35+
resource: 'service_install',
36+
body: {
37+
device: deviceId,
38+
installs__service: serviceId,
39+
},
40+
});
41+
}
42+
43+
if (si == null) {
44+
throw new errors.BadRequestError(
45+
`No service install exists for device: ${deviceId} and service ${serviceId} and one could not be created`,
46+
);
47+
}
48+
49+
request.values.service_install = si.id;
50+
}
51+
52+
hooks.addPureHook('POST', 'resin', 'device_service_environment_variable', {
53+
POSTPARSE: backfillServiceInstall,
54+
});
55+
56+
hooks.addPureHook('PATCH', 'resin', 'device_service_environment_variable', {
57+
POSTPARSE: backfillServiceInstall,
58+
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import './backfill-device-service-environment-variable.js';
1+
import './backfill-service-install-on-device-service-env-var.js';

src/translations/v7/hooks.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { hooks, sbvrUtils, errors } from '@balena/pinejs';
2+
3+
const addReadOnlyHook = (
4+
methods: Array<Parameters<typeof hooks.addHook>[0]>,
5+
resource: string,
6+
hook: sbvrUtils.Hooks<'v7'>,
7+
) => {
8+
methods.map((method) => {
9+
hooks.addHook(method, 'v7', resource, {
10+
...hook,
11+
sideEffects: false,
12+
readOnlyTx: true,
13+
});
14+
});
15+
};
16+
17+
addReadOnlyHook(
18+
['POST', 'PATCH', 'PUT'],
19+
'device_service_environment_variable',
20+
{
21+
POSTPARSE: async ({ request, api }) => {
22+
const { service_install: siId } = request.values;
23+
24+
if (siId == null) {
25+
return;
26+
}
27+
28+
const si = await sbvrUtils.api.resin.get({
29+
resource: 'service_install',
30+
passthrough: api.passthrough,
31+
id: siId,
32+
options: {
33+
$select: ['device', 'service'],
34+
},
35+
});
36+
37+
if (si == null) {
38+
throw new errors.UnauthorizedError();
39+
}
40+
41+
request.values.device = si.device.__id;
42+
request.values.service = si.service.__id;
43+
},
44+
},
45+
);

test/25_service-installs.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ export default () => {
251251
.post({
252252
resource: 'device_service_environment_variable',
253253
body: {
254-
service_install: serviceInstall.id,
254+
...(versions.lte(version, 'v7')
255+
? { service_install: serviceInstall.id }
256+
: { device: ctx.device.id, service: ctx.app2Service1.id }),
255257
name: 'test',
256258
value: '123',
257259
},

test/test-lib/fixtures.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -508,23 +508,23 @@ const loaders: types.Dictionary<LoaderFunc> = {
508508
logErrorAndThrow(`Could not find service: ${jsonData.service}`);
509509
}
510510

511-
const si = await expectToEventually(async () => {
512-
const $si = await api.resin.get({
511+
await expectToEventually(async () => {
512+
const si = await api.resin.get({
513513
resource: 'service_install',
514514
passthrough: { req: permissions.rootRead },
515515
id: {
516516
device: device.id,
517517
installs__service: service.id,
518518
},
519519
});
520-
assertExists($si);
521-
return $si;
520+
assertExists(si);
522521
});
523522

524523
return await createResource({
525524
resource: 'device_service_environment_variable',
526525
body: {
527-
service_install: si.id,
526+
device: device.id,
527+
service: service.id,
528528
name: jsonData.name,
529529
value: jsonData.value,
530530
},

0 commit comments

Comments
 (0)