Skip to content

Commit

Permalink
Update service implementation to read new columns while continuing to…
Browse files Browse the repository at this point in the history
… write to old columns

Change-type: minor
  • Loading branch information
Andrea Rosci authored and Andrea Rosci committed Jun 19, 2024
1 parent d5c2231 commit 7c68b5b
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/features/ci-cd/hooks/app-update-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ hooks.addPureHook('PATCH', 'resin', 'application', {
is_running__release: {
$ne: request.values.should_be_running__release,
},
should_be_running__release: null,
is_pinned_on__release: null,
},
wait: false,
});
Expand Down
2 changes: 1 addition & 1 deletion src/features/ci-cd/hooks/device-update-trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ hooks.addPureHook('PATCH', 'resin', 'device', {
// * target release changed
// * device name changed - so a user can restart their service and it will pick up the change
if (
(request.values.should_be_running__release !== undefined ||
(request.values.is_pinned_on__release !== undefined ||
request.values.belongs_to__application != null ||
request.values.device_name != null) &&
affectedIds.length !== 0
Expand Down
8 changes: 4 additions & 4 deletions src/features/ci-cd/hooks/service-installs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ hooks.addPureHook('PATCH', 'resin', 'application', {
api,
{
belongs_to__application: { $in: affectedIds },
should_be_running__release: null,
is_pinned_on__release: null,
},
{
id: request.values.should_be_running__release,
Expand Down Expand Up @@ -236,14 +236,14 @@ hooks.addPureHook('PATCH', 'resin', 'device', {
POSTRUN: async ({ api, request }) => {
const affectedIds = request.affectedIds!;
if (
request.values.should_be_running__release !== undefined &&
request.values.is_pinned_on__release !== undefined &&
affectedIds.length !== 0
) {
// If the device was preloaded, and then pinned, service_installs do not exist
// for this device+release combination. We need to create these
if (request.values.should_be_running__release != null) {
if (request.values.is_pinned_on__release != null) {
await createReleaseServiceInstalls(api, affectedIds, {
id: request.values.should_be_running__release,
id: request.values.is_pinned_on__release,
});
} else {
const devices = (await api.get({
Expand Down
4 changes: 2 additions & 2 deletions src/features/device-state/state-get-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export const getConfig = (
export const getReleaseForDevice = (
device: AnyObject,
): AnyObject | undefined => {
if (device.should_be_running__release[0] != null) {
return device.should_be_running__release[0];
if (device.is_pinned_on__release[0] != null) {
return device.is_pinned_on__release[0];
}
return device.belongs_to__application[0]?.should_be_running__release[0];
};
Expand Down
16 changes: 8 additions & 8 deletions src/features/devices/hooks/defaults-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ hooks.addPureHook('POST', 'resin', 'device', {
}

// TODO[device management next step]: Drop this after re-migrating all data on step 2:
if (request.values.should_be_running__release !== undefined) {
if (request.values.is_pinned_on__release !== undefined) {
// Add an async boundary so that value updates,
// and doesn't remove the properties that we add.
await null;
request.values.is_pinned_on__release =
request.values.should_be_running__release;
request.values.should_be_running__release =
request.values.is_pinned_on__release;
}
},
});
Expand Down Expand Up @@ -69,9 +69,9 @@ hooks.addPureHook('PATCH', 'resin', 'device', {
// build has been targeted, instead of pointing to a build of the wrong application
if (
request.values.belongs_to__application != null &&
request.values.should_be_running__release === undefined
request.values.is_pinned_on__release === undefined
) {
request.values.should_be_running__release = null;
request.values.is_pinned_on__release = null;
}

if (request.values.is_connected_to_vpn != null) {
Expand All @@ -84,12 +84,12 @@ hooks.addPureHook('PATCH', 'resin', 'device', {
}

// TODO[device management next step]: Drop this after re-migrating all data on step 2:
if (request.values.should_be_running__release !== undefined) {
if (request.values.is_pinned_on__release !== undefined) {
// Add an async boundary so that value updates,
// and doesn't remove the properties that we add.
await null;
request.values.is_pinned_on__release =
request.values.should_be_running__release;
request.values.should_be_running__release =
request.values.is_pinned_on__release;
}
},
});
2 changes: 1 addition & 1 deletion src/features/releases/hooks/restrict-release-deletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ hooks.addPureHook('DELETE', 'resin', 'release', {
$top: 1,
$select: 'id',
$filter: {
should_be_running__release: { $in: affectedIds },
is_pinned_on__release: { $in: affectedIds },
},
},
})) as Array<PickExpanded<Device['Read'], 'id'>>;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const DEVICE_API_KEY_PERMISSIONS = [
`device/any(d:${matchesNonFrozenDeviceActor('d')})`,
),
'resin.application_config_variable.read?application/canAccess()',
'resin.release.read?should_be_running_on__device/canAccess() or belongs_to__application/canAccess()',
'resin.release.read?is_pinned_to__device/canAccess() or belongs_to__application/canAccess()',
'resin.release_tag.read?release/canAccess()',
'resin.device_environment_variable.read?device/canAccess()',
...writePerms(
Expand Down
13 changes: 8 additions & 5 deletions test/14_release-pinning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export default () => {
let device4: fakeDevice.Device;
let device5: fakeDevice.Device;
let applicationToDelete: Application['Read'];
const pinnedOnReleaseField = versions.gt(version, 'v6')
? 'is_pinned_on__release'
: 'should_be_running__release';

before(async () => {
fx = await fixtures.load('14-release-pinning');
Expand Down Expand Up @@ -85,7 +88,7 @@ export default () => {
await supertest(admin)
.patch(`/${version}/device(${device.id})`)
.send({
should_be_running__release: pinnedRelease.id,
[pinnedOnReleaseField]: pinnedRelease.id,
})
.expect(200);

Expand All @@ -106,7 +109,7 @@ export default () => {
await supertest(admin)
.patch(`/${version}/device(${device.id})`)
.send({
should_be_running__release: null,
[pinnedOnReleaseField]: null,
})
.expect(200);
});
Expand Down Expand Up @@ -464,7 +467,7 @@ export default () => {
await supertest(device4)
.patch(`/${version}/device(${device4.id})`)
.send({
should_be_running__release: app3ReleaseId,
[pinnedOnReleaseField]: app3ReleaseId,
})
.expect(200);

Expand All @@ -491,7 +494,7 @@ export default () => {
await supertest(device4)
.patch(`/${version}/device(${device4.id})`)
.send({
should_be_running__release: app3ReleaseId,
[pinnedOnReleaseField]: app3ReleaseId,
})
.expect(200);
},
Expand Down Expand Up @@ -579,7 +582,7 @@ export default () => {
it('should not be able to delete a release if a device is pinned to it', async function () {
await supertest(admin)
.patch(`/${version}/device(${device4.id})`)
.send({ should_be_running__release: app4ReleaseId });
.send({ [pinnedOnReleaseField]: app4ReleaseId });
await supertest(admin)
.delete(`/${version}/release(${app4ReleaseId})`)
.expect(
Expand Down
4 changes: 2 additions & 2 deletions test/scenarios/unpin-device-after-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default () => {
await supertest(admin)
.patch(`/${version}/device(${device.id})`)
.send({
should_be_running__release: releases['deadbeef'],
is_pinned_on__release: releases['deadbeef'],
})
.expect(200);

Expand Down Expand Up @@ -163,7 +163,7 @@ export default () => {
await supertest(admin)
.patch(`/${version}/device(${device.id})`)
.send({
should_be_running__release: null,
is_pinned_on__release: null,
})
.expect(200);
});
Expand Down

0 comments on commit 7c68b5b

Please sign in to comment.