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

feat(deviceManagerEngine): free devices after engine deletion #382

Open
wants to merge 10 commits into
base: 2-dev
Choose a base branch
from
Open
37 changes: 37 additions & 0 deletions lib/modules/device/DeviceService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,43 @@ export class DeviceService extends DigitalTwinService {
await this.attachEngine(engineId, deviceId, request);
},
);

/**
* On deletion of a tenant, remove association for all of
* its formerly linked devices in the admin index.
*/
this.app.hook.register(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this should not be done inside the kuzzle-device-manager plugins because here you create a dependency with the multi-tenancy plugin witch is not the purpose of this plugin. It is suppose to be autonomous. You should put this behaviour in the iot platform backend

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, this was the first implementation but supposed to be removed for the reason you gave
My bad, I messed up my last commit...

"multi-tenancy/tenant:afterDelete",
async (request) => {
const { group, name } = request.input.args;
const tenantIndex = `tenant-${group}-${name}`;
const devices = [];

let result = await this.sdk.document.search(
this.config.adminIndex,
"devices",
{
_source: false,
query: { bool: { must: { term: { engineId: tenantIndex } } } },
},
{
scroll: "2s",
size: 100,
},
);
while (result !== null) {
devices.push(...result.hits);
result = await result.next();
}
void this.sdk.document.mUpdate(
this.config.adminIndex,
"devices",
devices.map((device) => {
return { _id: device._id, body: { assetId: null, engineId: null } };
}),
);
},
);
}

/**
Expand Down
39 changes: 37 additions & 2 deletions lib/modules/plugin/DeviceManagerEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,14 @@ export class DeviceManagerEngine extends AbstractEngine<DeviceManagerPlugin> {
InternalCollection.DEVICES,
InternalCollection.MEASURES,
];

await Promise.all(
collections.map(async (collection) => {
if (await this.sdk.collection.exists(index, collection)) {
await this.sdk.collection.delete(index, collection);
}
}),
);

await this.detachDevicesFromAdminIndex(index);
return {
collections,
};
Expand Down Expand Up @@ -654,4 +653,40 @@ export class DeviceManagerEngine extends AbstractEngine<DeviceManagerPlugin> {

return result.hits.map((elt) => elt._source);
}

/**
* Detach all devices of an index in the admin index
*
* @param engineId The target engine Id
* @returns {any}
*/
private async detachDevicesFromAdminIndex(engineId: string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this method should be named detachDevicesFromTenantIndex

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think tenantIndex could lead to confusion as this functions is related only to the 'platform' index which is referred as adminIndex in KDM.

const devices = [];

let result = await this.sdk.document.search(
this.adminIndex,
"devices",
{
_source: false,
query: { bool: { must: { term: { engineId } } } },
},
{
scroll: "2s",
size: 100,
},
);
while (result !== null) {
devices.push(...result.hits);
result = await result.next();
}
if (devices.length > 0) {
void this.sdk.document.mUpdate(
this.adminIndex,
"devices",
devices.map((device) => {
return { _id: device._id, body: { assetId: null, engineId: null } };
}),
);
}
}
}
Loading