This is an automated email from the ASF dual-hosted git repository. marat pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-karavan.git
The following commit(s) were added to refs/heads/main by this push: new cea8342 List of placeholders (#354) cea8342 is described below commit cea83427bb370a12551d8f297f4e8b4433cc7847 Author: Marat Gubaidullin <marat.gubaidul...@gmail.com> AuthorDate: Tue May 24 16:36:56 2022 -0400 List of placeholders (#354) --- karavan-core/src/core/api/CamelUtil.ts | 49 +++++++++++- karavan-core/test/placeholder.spec.ts | 32 ++++++++ karavan-core/test/placeholder.yaml | 133 +++++++++++++++++++++++++++++++++ 3 files changed, 213 insertions(+), 1 deletion(-) diff --git a/karavan-core/src/core/api/CamelUtil.ts b/karavan-core/src/core/api/CamelUtil.ts index bf948c1..84ffdcd 100644 --- a/karavan-core/src/core/api/CamelUtil.ts +++ b/karavan-core/src/core/api/CamelUtil.ts @@ -16,7 +16,7 @@ */ import { Integration, - CamelElement, Beans, Dependency, + CamelElement, Beans, Dependency, CamelElementMeta, } from "../model/IntegrationDefinition"; import {CamelDefinitionApi} from "./CamelDefinitionApi"; import {KameletDefinition, NamedBeanDefinition, ToDefinition} from "../model/CamelDefinition"; @@ -213,4 +213,51 @@ export class CamelUtil { } return result; } + + static findPlaceholdersInObject = (item: any, result: Set<string> = new Set<string>()): Set<string> => { + if (typeof item === 'object'){ + Object.keys(item).forEach(key => { + const value = (item as any)[key]; + if (Array.isArray(value)){ + this.findPlaceholdersInArray(value, result); + } else if (typeof value === 'object'){ + this.findPlaceholdersInObject(value, result); + } else { + const r = this.findPlaceholder(value.toString()); + if (r[0] && r[1]) result.add(r[1]); + } + }) + } else { + const r = this.findPlaceholder(item.toString()); + if (r[0] && r[1]) result.add(r[1]); + } + return result; + } + + static findPlaceholdersInArray = (items: any[] | undefined, result: Set<string> = new Set<string>()): Set<string> => { + if (items !== undefined) { + items.forEach(item => { + if (typeof item === 'object'){ + this.findPlaceholdersInObject(item, result); + } else { + const r = this.findPlaceholder(item.toString()); + if (r[0] && r[1]) result.add(r[1]); + } + }) + } + return result; + } + + static findPlaceholder = (value: string): [boolean, string?] => { + let result = false; + let placeholder = undefined; + if (value !== undefined) { + const val = value.trim(); + result = val.includes("{{") && val.includes("}}"); + const start = val.search("{{") + 2; + const end = val.search("}}"); + placeholder = val.substring(start, end).trim(); + } + return [result, placeholder]; + } } diff --git a/karavan-core/test/placeholder.spec.ts b/karavan-core/test/placeholder.spec.ts new file mode 100644 index 0000000..1232fb7 --- /dev/null +++ b/karavan-core/test/placeholder.spec.ts @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import {expect} from 'chai'; +import * as fs from 'fs'; +import 'mocha'; +import {CamelDefinitionYaml} from "../src/core/api/CamelDefinitionYaml"; +import {CamelUtil} from "../src/core/api/CamelUtil"; + +describe('Get Placeholders', () => { + + it('Get Placeholders', () => { + const yaml = fs.readFileSync('test/placeholder.yaml',{encoding:'utf8', flag:'r'}); + const i = CamelDefinitionYaml.yamlToIntegration("test1.yaml", yaml); + const placeholders = CamelUtil.findPlaceholdersInObject(i); + expect(placeholders.size).to.equal(4); + }); + +}); diff --git a/karavan-core/test/placeholder.yaml b/karavan-core/test/placeholder.yaml new file mode 100644 index 0000000..acbe3ad --- /dev/null +++ b/karavan-core/test/placeholder.yaml @@ -0,0 +1,133 @@ +apiVersion: camel.apache.org/v1 +kind: Integration +metadata: + name: Postman Demo +spec: + flows: + - rest: + post: + - to: direct:post + path: /parcels + consumes: application/json + produces: application/json + - route: + from: + uri: direct:post + steps: + - log: + message: 'Received: ${body}' + - multicast: + steps: + - to: + uri: kamelet:kafka-not-secured-sink + parameters: + topic: parcels + bootstrapServers: '{{kafka-brokers}}' + - to: + uri: kamelet:postgresql-sink + parameters: + serverName: '{{postgres-server}}' + serverPort: '5432' + username: postgres + password: postgres + databaseName: demo + query: >- + INSERT INTO parcels (id,address) VALUES + (:#id,:#address) ON CONFLICT (id) DO NOTHING + aggregationStrategy: >- + #class:org.apache.camel.processor.aggregate.UseOriginalAggregationStrategy + parallelProcessing: true + streaming: true + id: post + - route: + from: + uri: kamelet:jms-apache-artemis-source + steps: + - to: + uri: xj:identity + parameters: + transformDirection: XML2JSON + - to: + uri: kamelet:kafka-not-secured-sink + parameters: + topic: payments + bootstrapServers: '{{kafka-brokers}}' + parameters: + destinationType: queue + destinationName: payments + brokerURL: '{{jms-broker}}' + id: payment + - route: + from: + uri: kamelet:kafka-not-secured-source + steps: + - log: + message: 'Aggegating: ${body}' + - unmarshal: + json: + library: jackson + - aggregate: + steps: + - choice: + when: + - expression: + groovy: + expression: >- + body.find { it.containsKey('status') }.status == + 'confirmed' + steps: + - marshal: + json: + library: jackson + - log: + message: 'Send to MQTT : ${body}' + - to: + uri: kamelet:mqtt-sink + parameters: + topic: deliveries + brokerUrl: '{{mqtt-broker}}' + otherwise: + steps: + - setBody: + expression: + groovy: + expression: 'body.find { it.containsKey(''status'') } ' + - marshal: + json: + library: jackson + - log: + message: 'Send to database: ${body}' + - to: + uri: kamelet:postgresql-sink + parameters: + serverName: '{{postgres-server}}' + serverPort: '5432' + username: postgres + password: postgres + databaseName: demo + query: >- + UPDATE parcels set status = 'CANCELED' WHERE + id = :#id + aggregationStrategy: aggregator + completionSize: 2 + correlationExpression: + groovy: + expression: body.get('id') + parameters: + topic: parcels,payments + bootstrapServers: '{{kafka-brokers}}' + autoCommitEnable: true + consumerGroup: postman + id: aggregator + - route: + from: + uri: kamelet:mqtt-source + steps: + - log: + message: 'Delivery: ${body}' + parameters: + topic: deliveries + brokerUrl: '{{mqtt-broker}}' + - beans: + - name: aggregator + type: org.apache.camel.processor.aggregate.GroupedBodyAggregationStrategy