diff --git a/src/abstract-sql-utils.ts b/src/abstract-sql-utils.ts index aa40e3d6a..be0de9642 100644 --- a/src/abstract-sql-utils.ts +++ b/src/abstract-sql-utils.ts @@ -168,7 +168,13 @@ export const renameResourceField = ( }; export const renameVarResourcesName = (abstractSql: AbstractSqlModel) => { - for (const resource of ['device', 'application']) { + for (const resource of [ + 'device', + 'application', + 'device-installs-application-has-service name', + 'application-has-service name', + 'image-is part of-release', + ]) { renameResourceField(abstractSql, resource, 'config var name', 'name'); renameResourceField(abstractSql, resource, 'env var name', 'name'); } diff --git a/src/balena.sbvr b/src/balena.sbvr index ec352c508..6c35fd6a6 100644 --- a/src/balena.sbvr +++ b/src/balena.sbvr @@ -346,10 +346,14 @@ Term: application Term Form: service label Database Table Name: service label - Fact type: service has name (Auth) + Fact type: service has env var name Term Form: service environment variable Database Table Name: service environment variable + Fact type: service has config var name + Term Form: service config variable + Database Table Name: service config variable + Fact type: application has tag key Term Form: application tag Database Table Name: application tag @@ -380,10 +384,14 @@ Term: device Term Form: service install Database Table Name: service install - Fact type: service install has name (Auth) + Fact type: service install has env var name Term Form: device service environment variable Database Table Name: device service environment variable + Fact type: service install has config var name + Term Form: device service config variable + Database Table Name: device service config variable + Fact type: device has tag key Term Form: device tag Database Table Name: device tag @@ -404,10 +412,14 @@ Fact type: image is part of release Term Form: image label Database Table Name: image label - Fact type: release image has name (Auth) + Fact type: release image has env var name Term Form: image environment variable Database Table Name: image environment variable + Fact type: release image has config var name + Term Form: image config variable + Database Table Name: image config variable + Fact type: user (Auth) is member of organization Synonymous Form: organization includes user (Auth) Database Table Name: organization membership @@ -711,6 +723,12 @@ Fact type: service environment variable has value Necessity: each service environment variable has exactly one value. +-- service config variable + +Fact type: service config variable has value + Necessity: each service config variable has exactly one value. + + -- image Fact type: image has start timestamp @@ -774,12 +792,24 @@ Fact type: image environment variable has value Necessity: each image environment variable has exactly one value. +-- image config variable + +Fact type: image config variable has value + Necessity: each image config variable has exactly one value. + + -- device service environment variable Fact type: device service environment variable has value Necessity: each device service environment variable has exactly one value. +-- device service config variable + +Fact type: device service config variable has value + Necessity: each device service config variable has exactly one value. + + -- application tag Fact type: application tag has value diff --git a/src/features/vars-schema/hooks/config-var-validation.ts b/src/features/vars-schema/hooks/config-var-validation.ts index 7ae70752c..6787d87e8 100644 --- a/src/features/vars-schema/hooks/config-var-validation.ts +++ b/src/features/vars-schema/hooks/config-var-validation.ts @@ -21,6 +21,9 @@ const configVarHook: hooks.Hooks = { for (const resource of [ 'application_config_variable', 'device_config_variable', + 'service_config_variable', + 'device_service_config_variable', + 'image_config_variable', ]) { for (const method of ['POST', 'PATCH', 'PUT'] as const) { hooks.addPureHook(method, 'resin', resource, configVarHook); diff --git a/src/features/vars-schema/hooks/vars-update-trigger.ts b/src/features/vars-schema/hooks/vars-update-trigger.ts index 553642469..8aec3fc3d 100644 --- a/src/features/vars-schema/hooks/vars-update-trigger.ts +++ b/src/features/vars-schema/hooks/vars-update-trigger.ts @@ -191,55 +191,57 @@ const addDeviceEnvHooks = (resource: string) => addDeviceEnvHooks('device_config_variable'); addDeviceEnvHooks('device_environment_variable'); -addEnvHooks( - 'service_environment_variable', - async ( - args: hooks.HookArgs & { - tx: Tx; - }, - ) => { - if (args.req.body.service != null) { - return { - service_install: { - $any: { - $alias: 'si', - $expr: { - si: { - service: { - $any: { - $alias: 's', - $expr: { s: { id: args.req.body.service } }, +const addServiceEnvHooks = (resource: string) => + addEnvHooks( + resource, + async ( + args: hooks.HookArgs & { + tx: Tx; + }, + ) => { + if (args.req.body.service != null) { + return { + service_install: { + $any: { + $alias: 'si', + $expr: { + si: { + service: { + $any: { + $alias: 's', + $expr: { s: { id: args.req.body.service } }, + }, }, }, }, }, }, - }, - }; - } + }; + } - const envVarIds = await sbvrUtils.getAffectedIds(args); - if (envVarIds.length === 0) { - return; - } - return [ - envVarIds, - (envVarIdsChunk) => ({ - service_install: { - $any: { - $alias: 'si', - $expr: { - si: { - service: { - $any: { - $alias: 's', - $expr: { - s: { - service_environment_variable: { - $any: { - $alias: 'e', - $expr: { - e: { id: { $in: envVarIdsChunk } }, + const envVarIds = await sbvrUtils.getAffectedIds(args); + if (envVarIds.length === 0) { + return; + } + return [ + envVarIds, + (envVarIdsChunk) => ({ + service_install: { + $any: { + $alias: 'si', + $expr: { + si: { + service: { + $any: { + $alias: 's', + $expr: { + s: { + [resource]: { + $any: { + $alias: 'e', + $expr: { + e: { id: { $in: envVarIdsChunk } }, + }, }, }, }, @@ -250,79 +252,85 @@ addEnvHooks( }, }, }, - }, - }), - ]; - }, -); - -addEnvHooks( - 'device_service_environment_variable', - async ( - args: hooks.HookArgs & { - tx: Tx; + }), + ]; }, - ) => { - if (args.req.body.service_install != null) { - return { - service_install: { - $any: { - $alias: 's', - $expr: { s: { id: args.req.body.service_install } }, + ); +addServiceEnvHooks('service_environment_variable'); +addServiceEnvHooks('service_config_variable'); + +const addDeviceServiceEnvHooks = (resource: string) => + addEnvHooks( + resource, + async ( + args: hooks.HookArgs & { + tx: Tx; + }, + ) => { + if (args.req.body.service_install != null) { + return { + service_install: { + $any: { + $alias: 's', + $expr: { s: { id: args.req.body.service_install } }, + }, }, - }, - }; - } + }; + } - const envVarIds = await sbvrUtils.getAffectedIds(args); - if (envVarIds.length === 0) { - return; - } - return [ - envVarIds, - (envVarIdsChunk) => ({ - service_install: { - $any: { - $alias: 's', - $expr: { - s: { - device_service_environment_variable: { - $any: { - $alias: 'e', - $expr: { e: { id: { $in: envVarIdsChunk } } }, + const envVarIds = await sbvrUtils.getAffectedIds(args); + if (envVarIds.length === 0) { + return; + } + return [ + envVarIds, + (envVarIdsChunk) => ({ + service_install: { + $any: { + $alias: 's', + $expr: { + s: { + [resource]: { + $any: { + $alias: 'e', + $expr: { e: { id: { $in: envVarIdsChunk } } }, + }, }, }, }, }, }, - }, - }), - ]; - }, -); - -addEnvHooks( - 'image_environment_variable', - async ( - args: hooks.HookArgs & { - tx: Tx; + }), + ]; }, - ) => { - if (args.req.body.release_image != null) { - return { - image_install: { - $any: { - $alias: 'ii', - $expr: { - installs__image: { - $any: { - $alias: 'i', - $expr: { - i: { - release_image: { - $any: { - $alias: 'ri', - $expr: { ri: { id: args.req.body.release_image } }, + ); +addDeviceServiceEnvHooks('device_service_environment_variable'); +addDeviceServiceEnvHooks('device_service_config_variable'); + +const addImageEnvHooks = (resource: string) => + addEnvHooks( + resource, + async ( + args: hooks.HookArgs & { + tx: Tx; + }, + ) => { + if (args.req.body.release_image != null) { + return { + image_install: { + $any: { + $alias: 'ii', + $expr: { + installs__image: { + $any: { + $alias: 'i', + $expr: { + i: { + release_image: { + $any: { + $alias: 'ri', + $expr: { ri: { id: args.req.body.release_image } }, + }, }, }, }, @@ -331,35 +339,37 @@ addEnvHooks( }, }, }, - }, - }; - } + }; + } - const envVarIds = await sbvrUtils.getAffectedIds(args); - if (envVarIds.length === 0) { - return; - } - return [ - envVarIds, - (envVarIdsChunk) => ({ - image_install: { - $any: { - $alias: 'ii', - $expr: { - installs__image: { - $any: { - $alias: 'i', - $expr: { - i: { - release_image: { - $any: { - $alias: 'ri', - $expr: { - ri: { - image_environment_variable: { - $any: { - $alias: 'e', - $expr: { e: { id: { $in: envVarIdsChunk } } }, + const envVarIds = await sbvrUtils.getAffectedIds(args); + if (envVarIds.length === 0) { + return; + } + return [ + envVarIds, + (envVarIdsChunk) => ({ + image_install: { + $any: { + $alias: 'ii', + $expr: { + installs__image: { + $any: { + $alias: 'i', + $expr: { + i: { + release_image: { + $any: { + $alias: 'ri', + $expr: { + ri: { + [resource]: { + $any: { + $alias: 'e', + $expr: { + e: { id: { $in: envVarIdsChunk } }, + }, + }, }, }, }, @@ -372,8 +382,9 @@ addEnvHooks( }, }, }, - }, - }), - ]; - }, -); + }), + ]; + }, + ); +addImageEnvHooks('image_environment_variable'); +addImageEnvHooks('image_config_variable');