-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move backfill device service env var hook to a v7 translation hook
Change-type: patch
- Loading branch information
1 parent
39ab00d
commit d2f9fce
Showing
8 changed files
with
163 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
src/features/service-install/hooks/backfill-device-service-environment-variable.ts
This file was deleted.
Oops, something went wrong.
58 changes: 58 additions & 0 deletions
58
src/features/service-install/hooks/backfill-service-install-on-device-service-env-var.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters