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(operate): add multitenant support and lossless Json parsing #82

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/multitenancy.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Run Local Integration Tests Multitenant
name: Multitenant Integration Tests

on: [push]

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/saas.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Integration Tests
name: SaaS Integration Tests

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/singletenant.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Run Local Integration Tests
name: Single Tenant Integration Tests

on: [push]

Expand Down
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"tasklist",
"zeebe",
"github",
"modeler"
"modeler",
"operate"
],
"editor.formatOnSave": true,

Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![NPM](https://nodei.co/npm/@camunda8/sdk.png)](https://www.npmjs.com/package/@camunda8/sdk)

This is the official Camunda 8 JavaScript SDK.
This is the official Camunda 8 JavaScript SDK. It is written in TypeScript and runs on NodeJS ([why not in a web browser?](https://github.com/camunda-community-hub/camunda-8-js-sdk/issues/79)).

## Using the SDK in your project

Expand All @@ -12,6 +12,10 @@ Install the SDK as a dependency:
npm i @camunda8/sdk
```

## A note on entity key types in the JavaScript SDK

Entity keys in Camunda 8 are stored and represented as int64 numbers. The range of int64 extends to numbers that cannot be represented by the JavaScript `number` type. To deal with this, int64 keys are serialised by the SDK as the JavaScript `string` type. See [this issue](https://github.com/camunda-community-hub/camunda-8-js-sdk/issues/78) for more details.

## Usage

In this release, the functionality of the Camunda Platform 8 is exposed via dedicated clients for the component APIs.
Expand Down
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,11 @@
"got": "^11.8.6",
"lodash.mergewith": "^4.6.2",
"long": "^4.0.0",
"lossless-json": "^4.0.1",
"neon-env": "^0.1.3",
"node-fetch": "^2.7.0",
"promise-retry": "^1.1.1",
"reflect-metadata": "^0.2.1",
"stack-trace": "0.0.10",
"typed-duration": "^1.0.12",
"uuid": "^7.0.3"
Expand Down
73 changes: 73 additions & 0 deletions src/__tests__/lib/LosslessJsonParser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import {
ChildDto,
Int32,
Int64,
LosslessDto,
parseArrayWithAnnotations,
parseWithAnnotations,
} from 'lib'
import { ProcessInstanceStatistics } from 'operate/lib/OperateDto'

describe('LosslessJSONParser methods', () => {
test('It correctly handles nested Dtos', () => {
class DecisionInstanceOutput extends LosslessDto {
@Int32
public someInt32Field!: number
}

class ProcessDefinitionDto extends LosslessDto {
@ChildDto(DecisionInstanceOutput)
public decisionsOutputs!: DecisionInstanceOutput[]

@Int64
public total!: string // Example of another field
}

const json = `{
"total": 3,
"decisionsOutputs": [
{
"someInt32Field": 123
}
]
}`

const parsedDto = parseWithAnnotations(json, ProcessDefinitionDto)
expect(parsedDto.decisionsOutputs[0].someInt32Field).toBe(123) // 123 (number)
expect(parsedDto.total).toBe('3') // 3 (string)
})

test('it can handle an array', () => {
class ProcessInstanceStatisticsWithInt32 extends ProcessInstanceStatistics {
@Int32
smallNumber!: number
}
const json = `[
{
"activityId": "activityId",
"active": 1,
"canceled": 2,
"incidents": 3,
"completed": 4,
"smallNumber": 100
},
{
"activityId": "activityId2",
"active": 11,
"canceled": 12,
"incidents": 13,
"completed": 14
}
]`
const parsedDto = parseArrayWithAnnotations(
json,
ProcessInstanceStatisticsWithInt32
)
expect(Array.isArray(parsedDto)).toBe(true)
expect(parsedDto[0].activityId).toBe('activityId')
expect(parsedDto[0].active).toBe('1')
expect(parsedDto[0].smallNumber).toBe(100)
expect(parsedDto[1].activityId).toBe('activityId2')
expect(parsedDto[1].active).toBe('11')
})
})
65 changes: 65 additions & 0 deletions src/__tests__/operate/multitenancy/operate-mt.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { delay, restoreZeebeLogging, suppressZeebeLogging } from 'lib'
import { ZeebeGrpcClient } from 'zeebe'

import { OperateApiClient } from '../../../operate'

jest.setTimeout(15000)
suppressZeebeLogging()

afterAll(() => restoreZeebeLogging())

describe('Operate multi-tenancy', () => {
test('It can get the process instance from green tenant and not the red tenant', async () => {
const operateGreen = new OperateApiClient({
config: {
CAMUNDA_TENANT_ID: 'green',
},
})
const operateRed = new OperateApiClient({
config: {
CAMUNDA_TENANT_ID: 'red',
ZEEBE_CLIENT_ID: 'redzeebe',
ZEEBE_CLIENT_SECRET: 'redzecret',
},
})
const zbc = new ZeebeGrpcClient()

// Deploy to green tenant
await zbc.deployResource({
processFilename: 'src/__tests__/testdata/OperateMultitenancy.bpmn',
tenantId: 'green',
})

// Start an instance in green tenant
const p = await zbc.createProcessInstance({
bpmnProcessId: 'operate-mt',
tenantId: 'green',
variables: {},
})

await delay(8000)
// Get the process instance from Operate green tenant

const greenprocess = await operateGreen.searchProcessInstances({
filter: { key: p.processInstanceKey },
})

// getProcessInstance(
// p.processInstanceKey
// )

expect(greenprocess).toBeDefined()
expect(greenprocess.items[0].key.toString()).toBe(p.processInstanceKey)

// Can't find it in red tenant
const redprocess = await operateRed
.getProcessInstance(p.processInstanceKey)
.catch((e) => {
expect(e.message.includes('404')).toBe(true)
return false
})
expect(redprocess).toBe(false)
// Cancel the instance in green tenant
await zbc.cancelProcessInstance(p.processInstanceKey)
})
})
8 changes: 5 additions & 3 deletions src/__tests__/operate/operate-integrate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { LosslessNumber } from 'lossless-json'

import { OperateApiClient } from '../../operate'
import { ProcessDefinition, Query } from '../../operate/lib/APIObjects'
import { ProcessDefinition, Query } from '../../operate/lib/OperateDto'

jest.setTimeout(15000)
describe('Operate Integration', () => {
Expand All @@ -8,7 +10,7 @@ describe('Operate Integration', () => {

const res = await c.searchIncidents({
filter: {
processInstanceKey: 2251799816400111,
processInstanceKey: new LosslessNumber('2251799816400111'),
},
})
console.log(JSON.stringify(res, null, 2))
Expand All @@ -19,7 +21,7 @@ describe('Operate Integration', () => {

const query: Query<ProcessDefinition> = {
filter: {},
size: 50,
size: 5,
sort: [
{
field: 'bpmnProcessId',
Expand Down
48 changes: 48 additions & 0 deletions src/__tests__/operate/operate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EnvironmentSetup } from 'lib'
import { TestableOperateApiClient } from 'operate/lib/TestableOperateApiClient'

import { OperateApiClient } from '../../operate'

Expand Down Expand Up @@ -29,3 +30,50 @@ test('Can get construct a client', () => {
})
expect(client).toBeTruthy()
})

test('Can add tenant id to filter', () => {
const client = new TestableOperateApiClient({
config: {
CAMUNDA_OAUTH_DISABLED: true,
CAMUNDA_OPERATE_BASE_URL: 'http://localhost',
},
})
const query = client.addTenantIdToFilter({ filter: {} }, 'tenantId')
expect(query.filter?.tenantId).toBe('tenantId')
})

test('Adds tenant id if no filter', () => {
const client = new TestableOperateApiClient({
config: {
CAMUNDA_OAUTH_DISABLED: true,
CAMUNDA_OPERATE_BASE_URL: 'http://localhost',
},
})
const query = client.addTenantIdToFilter({}, 'tenantId2')
expect(query.filter?.tenantId).toBe('tenantId2')
})

test('Does not add a tenantId if none given', async () => {
const client = new TestableOperateApiClient({
config: {
CAMUNDA_OAUTH_DISABLED: true,
CAMUNDA_OPERATE_BASE_URL: 'http://localhost',
CAMUNDA_TENANT_ID: '', // best we can do to erase the tenant id from env
},
})
const query = client.addTenantIdToFilter({ filter: { id: 3 } })
expect(query.filter?.tenantId).toBe('')
})

test('Adds tenant id from environment', () => {
const client = new TestableOperateApiClient({
config: {
CAMUNDA_OAUTH_DISABLED: true,
CAMUNDA_OPERATE_BASE_URL: 'http://localhost',
CAMUNDA_TENANT_ID: 'red5', // best we can do to erase the tenant id from env
},
})
const query = client.addTenantIdToFilter({ filter: { id: 3 } })
expect(query.filter?.tenantId).toBe('red5')
expect(query.filter?.id).toBe(3)
})
50 changes: 50 additions & 0 deletions src/__tests__/testdata/OperateMultitenancy.bpmn
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:zeebe="http://camunda.org/schema/zeebe/1.0" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_0gtgqqw" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="5.21.0" modeler:executionPlatform="Camunda Cloud" modeler:executionPlatformVersion="8.4.0">
<bpmn:process id="operate-mt" name="OperateMultitenancy" isExecutable="true">
<bpmn:startEvent id="StartEvent_1" name="Start">
<bpmn:outgoing>Flow_0fgdzij</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:sequenceFlow id="Flow_0fgdzij" sourceRef="StartEvent_1" targetRef="Activity_0ui3en7" />
<bpmn:endEvent id="Event_1wzv4ji" name="End">
<bpmn:incoming>Flow_0l1o4z0</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_0l1o4z0" name="End" sourceRef="Activity_0ui3en7" targetRef="Event_1wzv4ji" />
<bpmn:serviceTask id="Activity_0ui3en7" name="Never Executed">
<bpmn:extensionElements>
<zeebe:taskDefinition type="never-executed" />
</bpmn:extensionElements>
<bpmn:incoming>Flow_0fgdzij</bpmn:incoming>
<bpmn:outgoing>Flow_0l1o4z0</bpmn:outgoing>
</bpmn:serviceTask>
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="operate-mt">
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
<dc:Bounds x="179" y="99" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="185" y="142" width="24" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_1wzv4ji_di" bpmnElement="Event_1wzv4ji">
<dc:Bounds x="432" y="99" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="440" y="142" width="20" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_05jxyb8_di" bpmnElement="Activity_0ui3en7">
<dc:Bounds x="270" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="Flow_0fgdzij_di" bpmnElement="Flow_0fgdzij">
<di:waypoint x="215" y="117" />
<di:waypoint x="270" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_0l1o4z0_di" bpmnElement="Flow_0l1o4z0">
<di:waypoint x="370" y="117" />
<di:waypoint x="432" y="117" />
<bpmndi:BPMNLabel>
<dc:Bounds x="391" y="99" width="20" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ beforeAll(async () => {
const res = await zbc.deployResource({
processFilename: './src/__tests__/testdata/Signal.bpmn',
})
pid = res.deployments[0].process.bpmnProcessId
pid = res.deployments[0].process.processDefinitionKey
await cancelProcesses(pid)
})

Expand Down
Loading
Loading