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 19, 2024
1 parent a7fcfdf commit 9872311
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 47 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;
},
},
);
25 changes: 19 additions & 6 deletions test/25_service-installs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,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 All @@ -202,14 +204,17 @@ export default () => {
},
} = await supertest(ctx.admin)
.get(
`/resin/device_service_environment_variable(${deviceServiceEnvVar.id})?$select=device,service`,
`/resin/device_service_environment_variable(${deviceServiceEnvVar.id})?$select=device,service,service_install`,
)
.expect(200);

expect(dbDeviceServiceEnvVar.device.__id).to.equal(ctx.device.id);
expect(dbDeviceServiceEnvVar.service.__id).to.equal(
ctx.app2Service1.id,
);
expect(dbDeviceServiceEnvVar.service_install.__id).to.equal(
serviceInstall.id,
);
});

it('should be able to update device_service_environment_variable service_install', async () => {
Expand Down Expand Up @@ -253,9 +258,14 @@ export default () => {
.patch({
resource: 'device_service_environment_variable',
id: deviceServiceEnvVar.id,
body: {
service_install: serviceInstallService2.id,
},
body: versions.lte(version, 'v7')
? {
service_install: serviceInstallService2.id,
}
: {
device: ctx.device.id,
service: ctx.app2Service2.id,
},
})
.expect(200);

Expand All @@ -265,14 +275,17 @@ export default () => {
},
} = await supertest(ctx.admin)
.get(
`/resin/device_service_environment_variable(${deviceServiceEnvVar.id})?$select=device,service`,
`/resin/device_service_environment_variable(${deviceServiceEnvVar.id})?$select=device,service,service_install`,
)
.expect(200);

expect(dbDeviceServiceEnvVar.device.__id).to.equal(ctx.device.id);
expect(dbDeviceServiceEnvVar.service.__id).to.equal(
ctx.app2Service2.id,
);
expect(dbDeviceServiceEnvVar.service_install.__id).to.equal(
serviceInstallService2.id,
);
});
});
});
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 9872311

Please sign in to comment.