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 55 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
88 changes: 88 additions & 0 deletions doc/1/controllers/sensor/m-attach/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
code: true
type: page
title: mAttach
description: Attach multiple sensors to multiple tenants index
---

# mAttachTenant

Attach multiple sensors to multiple tenants.

The sensor document will be duplicated inside the tenant "sensors" collection.

---

## Query Syntax

### HTTP

``` http
URL: http://kuzzle:7512/_/device-manager/sensors/_mAttach
Method: PUT
Body:
```

``` js
{
// Using JSON
"records" [{
"tenantId": "tenant-kuzzle",
"sensorId": "test-id"
}],
// Using CSV syntax
"csv": "tenant,id\ntenant-kuzzle,test-id"
}
```

### Other protocols

``` js
{
"controller": "device-manager/sensor",
"action": "mAttach",
"body": {
// Using JSON
"records" [{
"tenantId": "tenant-kuzzle",
"sensorId": "test-id"
}],
// Using CSV syntax
"csv": "tenantId,sensorId\ntenant-kuzzle,test-id",
}
}
```

---

## Body properties

Body properties, must contain at least one of

- `records`: an array of object containing `tenantId` and `sensorId`
- `csv`: a csv syntax compatible containing at least this two headers `tenantId,sensorId` with their corresponding values
- `strict`: a boolean value that indicate if the process should fail at first error

---

### Optional:

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

---

## Response

``` js
{
"status": 200,
"error": null,
"controller": "device-manager/sensor",
"action": "mAttach",
"requestId": "<unique request identifier>",
"result": {
"errors": [],
"successes": []
}
}
```
89 changes: 89 additions & 0 deletions doc/1/controllers/sensor/mDettach/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---
Copy link
Contributor

Choose a reason for hiding this comment

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

This file should be name m-detach

code: true
type: page
title: detach
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
title: detach
title: mDetach

description: Detach multiple sensors from multiple tenants
---

# detach
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# detach
# mDetach


Detach multiple sensors from multiple tenants.

---

## Query Syntax

### HTTP

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

``` js
{
// Using JSON
"records" [{
Copy link
Contributor

Choose a reason for hiding this comment

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

The mDetach action should only takes an array of sensorId

"tenant": "tenant-kuzzle",
"id": "test-id"
}],
// Using CSV syntax
"csv": "tenant,id\ntenant-kuzzle,test-id"
}
```

### Other protocols

``` js
{
"controller": "device-manager/sensor",
"action": "mAttachTenant",
"body": {
// Using JSON
"records" [{
"tenantId": "tenant-kuzzle",
"sensorId": "test-id"
}],
// Using CSV syntax
"csv": "tenantId,sensorId\ntenant-kuzzle,test-id",
}
}
```

---

## Body properties

Body properties, must contain at least one of

* `records`: an array of object containing `tenantId` and `sensorId`
* `csv`: a csv syntax compatible containing at least this two headers `tenantId,sensorId` with their 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": []
}
}
```
25 changes: 25 additions & 0 deletions doc/1/guides/sensors/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,31 @@ Sensors can be attached to tenant by using the [device-manager/sensor:attach](/k

When attached, the sensor document is copied inside the `sensors` collection of the tenant index.

## Attach to multiple tenant

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

The format used can be either __CSV__ in the form of a string in the format `tenantId,sensorId\nmytenantId,mysensorId` or __JSON__ in the form of an array of objects `"records": [{ "tenantId": "mytenantId", "sensorId": "mysensorId"}]`.

When attached, all sensors documents are copied inside the `sensors` 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

Sensors can be linked to an asset by using the [device-manager/sensor:link](/kuzzle-iot-platform/device-manager/1/controllers/sensor/link) API action.
Expand Down
77 changes: 74 additions & 3 deletions features/SensorController.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,37 @@ Feature: Device Manager sensor controller
| tenantId | "tenant-kuzzle" |
And The document "tenant-kuzzle":"sensors":"DummyTemp_detached" exists

Scenario: Attach multiple sensors to a tenant using JSON
Given an engine on index "tenant-kuzzle"
When I successfully execute the action "device-manager/sensor":"mAttach" with args:
| body.records.0.tenantId | "tenant-kuzzle" |
| body.records.0.sensorId | "DummyTemp_detached" |
| body.records.1.tenantId | "tenant-kuzzle" |
| body.records.1.sensorId | "DummyTemp_attached-ayse-unlinked" |
Then The document "device-manager":"sensors":"DummyTemp_detached" content match:
| tenantId | "tenant-kuzzle" |
Then The document "device-manager":"sensors":"DummyTemp_attached-ayse-unlinked" content match:
| tenantId | "tenant-kuzzle" |
And The document "tenant-kuzzle":"sensors":"DummyTemp_detached" exists
And The document "tenant-kuzzle":"sensors":"DummyTemp_attached-ayse-unlinked" exists

Scenario: Attach multiple sensor to a tenant using CSV
Given an engine on index "tenant-kuzzle"
When I successfully execute the action "device-manager/sensor":"mAttach" with args:
| body.csv | "tenantId,sensorId\\ntenant-kuzzle,DummyTemp_detached\\ntenant-kuzzle,DummyTemp_attached-ayse-unlinked," |
Then The document "device-manager":"sensors":"DummyTemp_detached" content match:
| tenantId | "tenant-kuzzle" |
Then The document "device-manager":"sensors":"DummyTemp_attached-ayse-unlinked" content match:
| tenantId | "tenant-kuzzle" |
And The document "tenant-kuzzle":"sensors":"DummyTemp_detached" exists
And The document "tenant-kuzzle":"sensors":"DummyTemp_attached-ayse-unlinked" exists

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

Scenario: Error when assigning a sensor to a tenant
Given an engine on index "tenant-kuzzle"
When I execute the action "device-manager/sensor":"attachTenant" with args:
Expand All @@ -23,7 +54,7 @@ Feature: Device Manager sensor controller
| _id | "DummyTemp_detached" |
| index | "tenant-kuzzle" |
Then I should receive an error matching:
| message | "Sensor \"DummyTemp_detached\" is already attached to a tenant" |
| message | "These sensors \"DummyTemp_detached\" are already attached to a tenant" |

Scenario: Detach sensor from a tenant
Given an engine on index "tenant-kuzzle"
Expand All @@ -36,19 +67,59 @@ Feature: Device Manager sensor controller
| tenantId | null |
And The document "tenant-kuzzle":"sensors":"DummyTemp_detached" does not exists

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

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

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

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

Scenario: Link sensor to an asset
When I successfully execute the action "device-manager/sensor":"linkAsset" with args:
Expand Down
1 change: 1 addition & 0 deletions features/fixtures/application/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ app.hook.register('request:onError', async (request: KuzzleRequest) => {
});

app.config.set('plugins.kuzzle-plugin-logger.services.stdout.level', 'debug');
app.config.set('limits.documentsWriteCount', 20);

app.start()
.then(() => {
Expand Down
3 changes: 3 additions & 0 deletions features/fixtures/fixtures.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const { sensors } = require('./sensors');

module.exports = {
'device-manager': {
sensors: [
...sensors,
{ index: { _id: 'DummyTemp_detached' } },
{
reference: 'detached',
Expand Down
16 changes: 16 additions & 0 deletions features/fixtures/sensors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const sensors = [];
for (let i = 0; i < 50; i++) {
sensors.push({ index: { _id: `DummyTemp_detached-${i}` } });
sensors.push({
reference: 'detached',
model: `DummyTemp-${i}`,
measures: {},
metadata: {},
tenantId: null,
assetId: null
});
}

const sensorsTests = sensors.filter(test => test.model);

module.exports = { sensors, sensorsTests };
Loading