Skip to content

Commit

Permalink
Add v7 read translation for device service environment variable servi…
Browse files Browse the repository at this point in the history
…ce install

Change-type: patch
  • Loading branch information
otaviojacobi committed Nov 29, 2024
1 parent 96a9072 commit 74befd7
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 79 deletions.
19 changes: 0 additions & 19 deletions src/balena-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,15 +937,6 @@ export interface ServiceInstall {
| { __id: Service['Read']['id'] }
| [Service['Read']];
is_installed_on__device: { __id: Device['Read']['id'] } | [Device['Read']];
is_of__device__has__application__has__service_name__has__env_var_name?: Array<
DeviceServiceEnvironmentVariable['Read']
>;
is_of__device__has__service__has__env_var_name?: Array<
DeviceServiceEnvironmentVariable['Read']
>;
is_of__device_service_environment_variable?: Array<
DeviceServiceEnvironmentVariable['Read']
>;
};
Write: {
created_at: Types['Date Time']['Write'];
Expand All @@ -965,15 +956,6 @@ export interface DeviceServiceEnvironmentVariable {
name: Types['Short Text']['Read'];
id: Types['Serial']['Read'];
value: Types['Text']['Read'];
service_install:
| { __id: ServiceInstall['Read']['id'] }
| [ServiceInstall['Read']];
device__installs__application__has__service_name:
| { __id: ServiceInstall['Read']['id'] }
| [ServiceInstall['Read']];
device__installs__service:
| { __id: ServiceInstall['Read']['id'] }
| [ServiceInstall['Read']];
application__has__service_name:
| { __id: Service['Read']['id'] }
| [Service['Read']];
Expand All @@ -986,7 +968,6 @@ export interface DeviceServiceEnvironmentVariable {
name: Types['Short Text']['Write'];
id: Types['Serial']['Write'];
value: Types['Text']['Write'];
service_install: ServiceInstall['Write']['id'];
};
}

Expand Down
2 changes: 0 additions & 2 deletions src/balena.sbvr
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,6 @@ Fact type: image environment variable has value

Fact type: device service environment variable has value
Necessity: each device service environment variable has exactly one value.
Fact type: device service environment variable has service install
Necessity: each device service environment variable has exactly one service install.


-- application tag
Expand Down
33 changes: 30 additions & 3 deletions src/features/cascade-delete/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ setupDeleteCascade('device', {
device_tag: 'device',
image_install: 'device',
service_install: 'device',
device_service_environment_variable: 'device',
});

setupDeleteCascade('image', {
Expand All @@ -45,11 +46,37 @@ setupDeleteCascade('service', {
service_install: 'installs__service',
image: 'is_a_build_of__service',
service_label: 'service',
device_service_environment_variable: 'service',
});

setupDeleteCascade('service_install', {
device_service_environment_variable: 'service_install',
});
// hooks.addPureHook('DELETE', 'resin', 'service install', {
// PRERUN: async (args) => {
// const serviceInstallIds = await getAffectedIds(args);
// if (serviceInstallIds.length === 0) {
// return;
// }

// // Delete all device_service_environment_variable (dsev) where
// // dsev.device = service_install.device and dsev.service = service_install.installs__service
// await args.api.delete({
// resource: 'device_service_environment_variable',
// passthrough: {
// tx: args.tx,
// req: permissions.root,
// },
// options: {
// $filter: {

// },
// },
// });

// },
// });

// setupDeleteCascade('service_install', {
// device_service_environment_variable: 'service_install',
// });

const deleteApiKeyHooks: hooks.Hooks = {
PRERUN: async (args) => {
Expand Down
38 changes: 38 additions & 0 deletions src/translations/v7/v7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,44 @@ export const getV7Translations = (_abstractSqlModel = v7AbstractSqlModel) => {
return {
'device-installs-application-has-service name-has-name': {
$toResource: 'device-has-application-has-service name-has-env var name',
'service install': [
'SelectQuery',
['Select', [['ReferencedField', 'si', 'id']]],
[
'From',
[
'Alias',
// TODO: should this be changed from table to resource? How?
['Table', 'device service environment variable'],
'dsev',
],
],
[
'Join',
[
'Alias',
// TODO: should this be changed from table to resource? How?
['Table', 'service install'],
'si',
],
[
'On',
[
'And',
[
'Equals',
['ReferencedField', 'dsev', 'device'],
['ReferencedField', 'si', 'device'],
],
[
'Equals',
['ReferencedField', 'dsev', 'service'],
['ReferencedField', 'si', 'installs-service'],
],
],
],
],
],
},
} satisfies ConfigLoader.Model['translations'];
};
112 changes: 57 additions & 55 deletions test/25_service-installs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,68 +275,70 @@ export default () => {
);
});

it('should be able to update device_service_environment_variable service_install', async () => {
const { body: serviceInstallService1 } = await pineUser
.get({
resource: 'service_install',
id: {
device: ctx.device.id,
installs__service: ctx.app2Service1.id,
},
})
.expect(200);
assertExists(serviceInstallService1);
if (versions.lte(version, 'v7')) {
it('should be able to update device_service_environment_variable service_install', async () => {
const { body: serviceInstallService1 } = await pineUser
.get({
resource: 'service_install',
id: {
device: ctx.device.id,
installs__service: ctx.app2Service1.id,
},
})
.expect(200);
assertExists(serviceInstallService1);

const { body: serviceInstallService2 } = await pineUser
.get({
resource: 'service_install',
id: {
device: ctx.device.id,
installs__service: ctx.app2Service2.id,
},
})
.expect(200);
assertExists(serviceInstallService2);
const { body: serviceInstallService2 } = await pineUser
.get({
resource: 'service_install',
id: {
device: ctx.device.id,
installs__service: ctx.app2Service2.id,
},
})
.expect(200);
assertExists(serviceInstallService2);

const {
body: [deviceServiceEnvVar],
} = await pineUser
.get({
resource: 'device_service_environment_variable',
options: {
$filter: {
service_install: serviceInstallService1.id,
const {
body: [deviceServiceEnvVar],
} = await pineUser
.get({
resource: 'device_service_environment_variable',
options: {
$filter: {
service_install: serviceInstallService1.id,
},
},
},
})
.expect(200);
assertExists(deviceServiceEnvVar);
})
.expect(200);
assertExists(deviceServiceEnvVar);

await pineUser
.patch({
resource: 'device_service_environment_variable',
id: deviceServiceEnvVar.id,
await pineUser
.patch({
resource: 'device_service_environment_variable',
id: deviceServiceEnvVar.id,
body: {
service_install: serviceInstallService2.id,
},
})
.expect(200);

const {
body: {
service_install: serviceInstallService2.id,
d: [dbDeviceServiceEnvVar],
},
})
.expect(200);

const {
body: {
d: [dbDeviceServiceEnvVar],
},
} = await supertest(ctx.admin)
.get(
`/resin/device_service_environment_variable(${deviceServiceEnvVar.id})?$select=device,service`,
)
.expect(200);
} = await supertest(ctx.admin)
.get(
`/resin/device_service_environment_variable(${deviceServiceEnvVar.id})?$select=device,service`,
)
.expect(200);

expect(dbDeviceServiceEnvVar.device.__id).to.equal(ctx.device.id);
expect(dbDeviceServiceEnvVar.service.__id).to.equal(
ctx.app2Service2.id,
);
});
expect(dbDeviceServiceEnvVar.device.__id).to.equal(ctx.device.id);
expect(dbDeviceServiceEnvVar.service.__id).to.equal(
ctx.app2Service2.id,
);
});
}
});
});
});
Expand Down

0 comments on commit 74befd7

Please sign in to comment.