Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move backfill device service env var hook to a v7 translation hook #1845

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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 @@ -317,7 +317,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 @@ -331,14 +333,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 @@ -382,9 +387,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 @@ -394,14 +404,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
Loading