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/add m detach #33

Merged
merged 59 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
aeb5c04
feat/add-mAttachTenant
rolljee Mar 2, 2021
5550d7e
Lint
rolljee Mar 2, 2021
573245b
Lint
rolljee Mar 2, 2021
469efd1
Remove ?. syntax
rolljee Mar 2, 2021
bd1c5c4
Add documentation
rolljee Mar 3, 2021
529e622
Update doc/1/controllers/sensor/mattach-tenant/index.md
rolljee Mar 3, 2021
b798874
Update features/SensorController.feature
rolljee Mar 3, 2021
27ba94f
Update lib/controllers/SensorController.ts
rolljee Mar 3, 2021
23fa990
Update lib/controllers/SensorController.ts
rolljee Mar 3, 2021
259b5f4
Update lib/controllers/SensorController.ts
rolljee Mar 3, 2021
b896327
Update lib/controllers/SensorController.ts
rolljee Mar 3, 2021
98410a6
Update lib/controllers/SensorController.ts
rolljee Mar 3, 2021
db06c76
Update lib/controllers/SensorController.ts
rolljee Mar 3, 2021
5404b69
wip feat/add-mDetach
rolljee Mar 3, 2021
cf1f49c
Requested changes
rolljee Mar 3, 2021
8e809d5
Merge branch 'feat/add-m-attach-tenant' into feat/add-m-detach
rolljee Mar 4, 2021
65a1283
wip add-m-attach
rolljee Mar 4, 2021
5f4adc2
wip add-m-attach
rolljee Mar 4, 2021
bd42756
Add function to parse mRequest
rolljee Mar 4, 2021
035251e
merge
rolljee Mar 4, 2021
a02037b
Rename a function
rolljee Mar 4, 2021
2ee1834
Merge branch 'feat/add-m-attach-tenant' into feat/add-m-detach
rolljee Mar 4, 2021
f5899d7
feature and tests are now working properly
rolljee Mar 4, 2021
5a72e6d
Add documentation
rolljee Mar 5, 2021
46af50d
use good return values
rolljee Mar 5, 2021
96515b9
merge 1-dev
rolljee Mar 5, 2021
d5943dd
Merge branch 'feat/add-m-attach-tenant' into feat/add-m-detach
rolljee Mar 5, 2021
4a97ad3
Requested changes
rolljee Mar 8, 2021
0d7e32d
Merge ok
rolljee Mar 8, 2021
50d4ecd
Fix issue with mAttatchSensors tests
rolljee Mar 8, 2021
ae0ac60
Remove debug leftover
rolljee Mar 8, 2021
8fb0279
Fix tests
rolljee Mar 8, 2021
10fae65
Use global.kuzzle.config.limits.documentsWriteCount to avoid issue wi…
rolljee Mar 8, 2021
6b560c2
Requested changes
rolljee Mar 8, 2021
2af753f
Merge ok and wip mDetach
rolljee Mar 8, 2021
a61cefa
still wip on refactoring
rolljee Mar 8, 2021
e04e093
Make writeToDatabase function more generic
rolljee Mar 8, 2021
d442755
enhancement of writeToDatabase using kuzzle limits & Add custom test …
rolljee Mar 9, 2021
c469729
Update docs & add a part in the guide for mAttach
rolljee Mar 9, 2021
453b230
merge 1-dev
rolljee Mar 10, 2021
b666fe1
Adds laius about format, correct some spelling mistakes and modify fi…
rolljee Mar 10, 2021
177730c
Merge ok + rename case of mdettach directory
rolljee Mar 10, 2021
ec44401
Add laius about dettach and mDettach in the guide
rolljee Mar 10, 2021
27effa4
wip on mDelete query
rolljee Mar 10, 2021
14dfe31
Update docs and rename mAttach to m-attach in directory structure
rolljee Mar 10, 2021
7af8b8c
Merge branch 'feat/add-m-attach-tenant' into feat/add-m-detach
rolljee Mar 10, 2021
4b45c46
Make writer function of mDettach work properly
rolljee Mar 10, 2021
a2d0242
Remove debug leftover
rolljee Mar 10, 2021
de92bb7
Update doc/1/controllers/sensor/m-attach/index.md
rolljee Mar 10, 2021
465110f
Update doc/1/controllers/sensor/m-attach/index.md
rolljee Mar 10, 2021
7e0e2cb
Update doc/1/controllers/sensor/m-attach/index.md
rolljee Mar 10, 2021
ac9ffb1
fix left mAttachtenant to mAttach
rolljee Mar 10, 2021
eb706cd
Fix wrong identation
rolljee Mar 10, 2021
2231023
Merge branch 'feat/add-m-attach-tenant' into feat/add-m-detach
rolljee Mar 10, 2021
c5b3297
Steps are more generic to tests controller and action while exeeding …
rolljee Mar 12, 2021
bdcbd6d
Merge OK
rolljee Mar 16, 2021
70b3dee
requested changes
rolljee Mar 18, 2021
98702fc
Remove debug leftover
rolljee Mar 18, 2021
742b209
Merge ok
rolljee Mar 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions doc/1/controllers/device/m-detach/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
code: true
type: page
title: mDetach
description: Detach multiple devices from multiple tenants
---

# mDetach

Detach multiple devices from multiple tenants.

---

## Query Syntax

### HTTP

``` http
URL: http://kuzzle:7512/_/device-manager/device-manager/devices/_mDetach[?refresh=wait_for]
Method: PUT
Body:
```

``` js
{
// Using JSON
"sensorIds" ["test-id"],
// Using CSV syntax
"csv": "sensorId\ntest-id"
}
```

### Other protocols

``` js
{
"controller": "device-manager/sensor",
"action": "mAttachTenant",
"body": {
// Using JSON
"sensorIds" ["test-id"],
// Using CSV syntax
"csv": "sensorId\ntest-id"
}
}
```

---

## Body properties

Body properties, must contain at least one of

* `sensorIds`: an array of string containing identifiers of a sensor already attached
* `csv`: a csv syntax compatible containing at least one header `sensorId` with his corresponding values
* `strict`: a boolean value that indicate if the process should fail at first error

---

## Arguments

### Optional:

* `refresh`: if set to `wait_for`, Kuzzle will not respond until the documents are indexed

---

## Response

``` js
{
"status": 200,
"error": null,
"index": "<index>",
"controller": "device-manager/sensor",
"action": "mDetach",
"requestId": "<unique request identifier>",
"result": {
"errors": [],
"successes": []
}
}
```
17 changes: 17 additions & 0 deletions doc/1/guides/devices/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ The format used can be either __CSV__ in the form of a string in the format `ten

When attached, all devices documents are copied inside the `devices` collections of all different tenant index.

## Detach a sensor from a tenant

Sensors can be detached to tenant by using the [device-manager/sensor:detach](/kuzzle-iot-platform/device-manager/1/controllers/sensor/detach) API action.

When detached, the sensor document is deleted from the `sensors` collection of the tenant index.

the `tenantId` property is set to `null` in the `device-manager` index.
## Detach multiple sensors from multiple tenants

Multiple different Sensors can also be detached to multiple defferents tenant by using the [device-manager/sensor:mDetach](/kuzzle-iot-platform/device-manager/1/controllers/sensor/mDetach) API action.

The format used can be either __CSV__ in the form of a string in the format "tenantId,sensorId,mysensorId" or __JSON__ in the form of an array of objects.

When detached, all sensors documents are deleted from the `sensors` collections of all different tenant index.

The `tenantId` property is set to `null` for all of them in the `device-manager` index

## Link to an asset

Devices can be linked to an asset by using the [device-manager/device:link](/kuzzle-iot-platform/device-manager/1/controllers/device/link) API action.
Expand Down
46 changes: 41 additions & 5 deletions features/DeviceController.feature
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ Feature: Device Manager device controller

Scenario: Attach multiple device to a tenant while exceeding documentsWriteCount limit
Given an engine on index "tenant-kuzzle"
When I attach multiple devices while exeding documentsWriteCount limit
Then All attached devices have the correct tenantId
Then All tenant devices documents exists
When I succesfully execute "device-manager/device":"mAttach" while exeding documentsWriteCount limit
Then All devices in "device-manager" "devices" have the property "tenantId" to "tenant-kuzzle"
And All documents "tenant-kuzzle":"devices" exists

Scenario: Error when assigning a device to a tenant
Given an engine on index "tenant-kuzzle"
Expand Down Expand Up @@ -67,19 +67,55 @@ Feature: Device Manager device controller
| tenantId | null |
And The document "tenant-kuzzle":"devices":"DummyTemp_detached" does not exists

Scenario: Detach multiple devices to a tenant using JSON
Given an engine on index "tenant-kuzzle"
When I successfully execute the action "device-manager/device":"mAttach" with args:
| body.records.0.tenantId | "tenant-kuzzle" |
| body.records.0.deviceId | "DummyTemp_detached" |
| body.records.1.tenantId | "tenant-kuzzle" |
| body.records.1.deviceId | "DummyTemp_attached-ayse-unlinked" |
When I successfully execute the action "device-manager/device":"mDetach" with args:
| body.deviceIds | ["DummyTemp_detached","DummyTemp_attached-ayse-unlinked"] |
Then The document "device-manager":"devices":"DummyTemp_detached" content match:
| tenantId | null |
Then The document "device-manager":"devices":"DummyTemp_attached-ayse-unlinked" content match:
| tenantId | null |
And The document "tenant-kuzzle":"devices":"DummyTemp_detached" does not exists
And The document "tenant-kuzzle":"devices":"DummyTemp_attached-ayse-unlinked" does not exists

Scenario: Detach multiple devices to a tenant using CSV
Given an engine on index "tenant-kuzzle"
When I successfully execute the action "device-manager/device":"mAttach" with args:
| body.csv | "tenantId,deviceId\\ntenant-kuzzle,DummyTemp_detached\\ntenant-kuzzle,DummyTemp_attached-ayse-unlinked," |
When I successfully execute the action "device-manager/device":"mDetach" with args:
| body.csv | "deviceId\\nDummyTemp_detached\\nDummyTemp_attached-ayse-unlinked," |
Then The document "device-manager":"devices":"DummyTemp_detached" content match:
| tenantId | null |
Then The document "device-manager":"devices":"DummyTemp_attached-ayse-unlinked" content match:
| tenantId | null |
And The document "tenant-kuzzle":"devices":"DummyTemp_detached" does not exists
And The document "tenant-kuzzle":"devices":"DummyTemp_attached-ayse-unlinked" does not exists

Scenario: Detach multiple device to a tenant while exceeding documentsWriteCount limit
Given an engine on index "tenant-kuzzle"
When I succesfully execute "device-manager/device":"mAttach" while exeding documentsWriteCount limit
When I succesfully execute "device-manager/device":"mDetach" while exeding documentsWriteCount limit
Then All devices in "device-manager" "devices" have the property "tenantId" to "null"
And All documents "tenant-kuzzle":"devices" does not exists

Scenario: Error when detaching from a tenant
Given an engine on index "tenant-kuzzle"
When I execute the action "device-manager/device":"detach" with args:
| _id | "DummyTemp_detached" |
Then I should receive an error matching:
| message | "Device \"DummyTemp_detached\" is not attached to a tenant" |
| message | "Devices \"DummyTemp_detached\" are not attached to a tenant" |
Given I successfully execute the action "device-manager/device":"linkAsset" with args:
| _id | "DummyTemp_attached-ayse-unlinked" |
| assetId | "PERFO-unlinked" |
When I execute the action "device-manager/device":"detach" with args:
| _id | "DummyTemp_attached-ayse-unlinked" |
Then I should receive an error matching:
| message | "Device \"DummyTemp_attached-ayse-unlinked\" is still linked to an asset" |
| message | "Devices \"DummyTemp_attached-ayse-unlinked\" are still linked to an asset" |

Scenario: Link device to an asset
When I successfully execute the action "device-manager/device":"linkAsset" with args:
Expand Down
4 changes: 3 additions & 1 deletion features/fixtures/devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ for (let i = 0; i < 50; i++) {
});
}

module.exports = devices;
const devicesTests = devices.filter(test => test.model);

module.exports = { devices, devicesTests };
2 changes: 1 addition & 1 deletion features/fixtures/fixtures.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const devices = require('./devices');
const { devices } = require('./devices');

module.exports = {
'device-manager': {
Expand Down
46 changes: 33 additions & 13 deletions features/step_definitions/device-controller-steps.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,71 @@
const { When, Then } = require('cucumber');

When('I attach multiple devices while exeding documentsWriteCount limit', async function () {
const { devicesTests } = require('../fixtures/devices');

When('I succesfully execute {string}:{string} while exeding documentsWriteCount limit', async function (controller, action) {
const records = [];
for (let i = 0; i < 50; i++) {
for (let i = 0; i < devicesTests.length; i++) {
records.push({ deviceId: `DummyTemp_detached-${i}`, tenantId: 'tenant-kuzzle' });
}


await this.sdk.query({
controller: "device-manager/device",
action: "mAttach",
controller,
action,
body: {
records
}
});
});

Then('All attached devices have the correct tenantId', async function () {
Then('All devices in {string} {string} have the property {string} to {string}', async function (index, collection, key, value) {
const deviceIds = [];
for (let i = 0; i < 50; i++) {
for (let i = 0; i < devicesTests.length; i++) {
deviceIds.push(`DummyTemp_detached-${i}`);
}

const { successes, errors } = await this.sdk.document.mGet('device-manager', 'devices', deviceIds);
const { successes, errors } = await this.sdk.document.mGet(index, collection, deviceIds);

if (errors.length > 0) {
throw new Error(errors);
}

for (let i = 0; i < successes.length; i++) {
const { _source } = successes[i];
if (_source.tenantId !== 'tenant-kuzzle') {
throw new Error('tenantId should be tenant-kuzzle but current value is: ', _source.tenantId);
const theValue = value === "null" ? null : value;

if (_source[key] !== theValue) {
throw new Error(`tenantId should be ${value} but current value is: ${_source.tenantId}`);
}
}
});


Then('All tenant devices documents exists', async function () {
Then('All {string} devices documents exists', async function (tenant) {
const deviceIds = [];
for (let i = 0; i < 50; i++) {
for (let i = 0; i < devicesTests.length; i++) {
deviceIds.push(`DummyTemp_detached-${i}`);
}

const { errors } = await this.sdk.document.mGet('tenant-kuzzle', 'devices', deviceIds);
const { errors } = await this.sdk.document.mGet(tenant, 'devices', deviceIds);

if (errors.length > 0) {
throw new Error(errors);
}
});

Then(/All documents "(.*?)":"(.*?)" (does not)? exists/, async function (index, collection, not) {
const deviceIds = [];
for (let i = 0; i < devicesTests.length; i++) {
deviceIds.push(`DummyTemp_detached-${i}`);
}

const { successes, errors } = await this.sdk.document.mGet(index, collection, deviceIds);

if (not && successes.length > 0) {
throw new Error(`Documents exists, but it shoudn't`);
}

if (!not && errors.length) {
throw new Error(`Expected documents to exist`);
}
});
25 changes: 22 additions & 3 deletions lib/controllers/DeviceController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export class DeviceController extends CRUDController {
handler: this.detach.bind(this),
http: [{ verb: 'delete', path: 'device-manager/devices/:_id/_detach' }]
},
mDetach: {
handler: this.mDetach.bind(this),
http: [{ verb: 'put', path: 'device-manager/devices/_mDetach' }]
},
linkAsset: {
handler: this.linkAsset.bind(this),
http: [{ verb: 'put', path: 'device-manager/:index/devices/:_id/_link/:assetId' }]
Expand Down Expand Up @@ -94,9 +98,21 @@ export class DeviceController extends CRUDController {
async detach (request: KuzzleRequest) {
const deviceId = this.getId(request);

const device = await this.getDevice(deviceId);
const document: DeviceBulkContent = { deviceId };
const devices = await this.mGetDevice([document]);

await this.deviceService.detach(device);
await this.deviceService.mDetach(devices, [document], { strict: true });
}

/**
* Unattach multiple devices from multiple tenants
*/
async mDetach (request: KuzzleRequest) {
const { bulkData, strict } = await this.mParseRequest(request);

const devices = await this.mGetDevice(bulkData);

return this.deviceService.mDetach(devices, bulkData, { strict });
}

/**
Expand Down Expand Up @@ -155,8 +171,11 @@ export class DeviceController extends CRUDController {
else if (body.records) {
bulkData = body.records;
}
else if (body.deviceIds) {
bulkData = body.deviceIds.map((deviceId: string) => ({ deviceId }));
}
else {
throw new BadRequestError(`Malformed request missing property csv or records`);
throw new BadRequestError(`Malformed request missing property csv, records, deviceIds`);
}

const strict = body.strict || false;
Expand Down
Loading