Skip to content

Commit

Permalink
Kamelets Definition UI #315
Browse files Browse the repository at this point in the history
  • Loading branch information
mgubaidullin committed Oct 1, 2023
1 parent 9808b99 commit df8c086
Show file tree
Hide file tree
Showing 18 changed files with 1,129 additions and 74 deletions.
21 changes: 10 additions & 11 deletions karavan-core/src/core/api/CamelDefinitionYaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,25 +237,24 @@ export class CamelDefinitionYaml {
return integration;
};

static yamlIsIntegration = (text: string): boolean => {
static yamlIsIntegration = (text: string): 'crd' | 'plain' | 'kamelet' | 'none' => {
try {
const fromYaml: any = yaml.load(text);
const camelized: any = CamelUtil.camelizeObject(fromYaml);
if (
camelized?.apiVersion &&
camelized.apiVersion.startsWith('camel.apache.org') &&
camelized.kind &&
camelized.kind === 'Integration'
) {
return true;
if (camelized?.apiVersion && camelized.apiVersion.startsWith('camel.apache.org') && camelized.kind) {
if (camelized.kind === 'Integration') {
return 'crd';
} else if (camelized.kind === 'Kamelet') {
return 'kamelet';
}
} else if (Array.isArray(camelized)) {
return true;
return 'plain';
} else {
return false;
return 'none';
}
} catch (e) {
return false;
}
return 'none';
};
static flowsToCamelElements = (flows: any[]): any[] => {
const rules: { [key: string]: (flow: any) => any } = {
Expand Down
46 changes: 27 additions & 19 deletions karavan-core/src/core/model/IntegrationDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,34 @@
import { v4 as uuidv4 } from 'uuid';
import { RegistryBeanDefinition } from './CamelDefinition';

export class KameletDefinitionProperty {
export class DefinitionProperty {
title: string = '';
description: string = '';
type: 'string' | 'integer' | 'boolean' = 'string';
default?: any;
example?: any;
format?: string;
"x-descriptors"?: string[];
properties: any = {};

public constructor(init?: Partial<KameletDefinition>) {
public constructor(init?: Partial<DefinitionProperty>) {
Object.assign(this, init);
}
}

export class KameletDefinition {
export class Definition {
title: string = '';
description: string = '';
required: string[] = [];
type: string = 'object';
properties: any = {};

public constructor(init?: Partial<KameletDefinition>) {
public constructor(init?: Partial<Definition>) {
Object.assign(this, init);
}
}

export class Spec {
definition?: KameletDefinition;
definition?: Definition;
types?: any;
flows?: any[] = [];
template?: any;
Expand All @@ -56,30 +55,31 @@ export class Spec {
}
}

export class MetadataLabel {
"camel.apache.org/kamelet.type": "sink" | "source" | "action"
export class MetadataLabels {
"camel.apache.org/kamelet.type": "sink" | "source" | "action" = 'source'

public constructor(init?: Partial<MetadataLabel>) {
public constructor(init?: Partial<MetadataLabels>) {
Object.assign(this, init);
}
}

export class MetadataAnnotation {
"camel.apache.org/catalog.version"?: string;
"camel.apache.org/kamelet.icon"?: string;
"camel.apache.org/provider"?: string;
"camel.apache.org/kamelet.group"?: string;
"camel.apache.org/kamelet.namespace"?: string;
export class MetadataAnnotations {
"camel.apache.org/kamelet.support.level:": string = 'Preview';
"camel.apache.org/catalog.version": string = '';
"camel.apache.org/kamelet.icon": string = '';
"camel.apache.org/provider": string = '';
"camel.apache.org/kamelet.group": string = '';
"camel.apache.org/kamelet.namespace": string = '';

public constructor(init?: Partial<MetadataAnnotation>) {
public constructor(init?: Partial<MetadataAnnotations>) {
Object.assign(this, init);
}
}

export class Metadata {
name: string = '';
annotations?: MetadataAnnotation;
labels?: MetadataLabel[];
annotations?: MetadataAnnotations;
labels?: MetadataLabels;

public constructor(init?: Partial<Metadata>) {
Object.assign(this, init);
Expand All @@ -98,10 +98,18 @@ export class Integration {
}

static createNew(name?: string, type: 'crd' | 'plain' | 'kamelet' = 'plain'): Integration {
return new Integration({ type: type,
const i = new Integration({ type: type,
metadata: new Metadata({ name: name }),
kind : type === 'kamelet' ? 'Kamelet' : 'Integration',
spec: new Spec({ flows: [] }) });

if (type === 'kamelet') {
i.metadata.annotations = new MetadataAnnotations({})
i.spec.definition = new Definition({})
i.spec.types = {}
}

return i;
}
}

Expand Down
12 changes: 9 additions & 3 deletions karavan-core/test/isIntegration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ describe('Is Integration', () => {
it('Is not integration', () => {
const yaml = fs.readFileSync('test/is-not-integration.yaml',{encoding:'utf8', flag:'r'});
const i = CamelDefinitionYaml.yamlIsIntegration(yaml);
expect(i).to.equal(false);
expect(i).to.equal('none');
});

it('Is integration', () => {
it('Is integration CRD', () => {
const yaml = fs.readFileSync('test/integration1.yaml',{encoding:'utf8', flag:'r'});
const i = CamelDefinitionYaml.yamlIsIntegration(yaml);
expect(i).to.equal(true);
expect(i).to.equal('crd');
});

it('Is integration plain', () => {
const yaml = fs.readFileSync('test/plain1.yaml',{encoding:'utf8', flag:'r'});
const i = CamelDefinitionYaml.yamlIsIntegration(yaml);
expect(i).to.equal('plain');
});

});
22 changes: 8 additions & 14 deletions karavan-core/test/kamelet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,21 @@
* 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 {
ChoiceDefinition,
ExpressionDefinition,
FilterDefinition, FromDefinition, LogDefinition,
ToDefinition,
WhenDefinition,
} from '../src/core/model/CamelDefinition';
import { FromDefinition, LogDefinition, } from '../src/core/model/CamelDefinition';
import { RouteDefinition} from "../src/core/model/CamelDefinition";
import { Beans, Integration, MetadataAnnotation } from '../src/core/model/IntegrationDefinition';
import { KameletMetadata } from '../lib/model/KameletModels';
import { RegistryBeanDefinition } from '../lib/model/CamelDefinition';
import { Beans, Definition, Integration } from '../src/core/model/IntegrationDefinition';
import { RegistryBeanDefinition } from '../src/core/model/CamelDefinition';
import { MetadataAnnotations } from '../src/core/model/IntegrationDefinition';

describe('Kamelet <=> YAML', () => {

it('Yaml to Kamelet', () => {
const yaml = fs.readFileSync('test/postgresql-source.kamelet.yaml',{encoding:'utf8', flag:'r'});
const i = CamelDefinitionYaml.yamlToIntegration("postgresql-source.kamelet.yaml", yaml);
console.log(i)
console.log(CamelDefinitionYaml.integrationToYaml(i))
// console.log(i)
});

it('Kamelet to YAML with beans', () => {
Expand All @@ -50,7 +42,7 @@ describe('Kamelet <=> YAML', () => {
b.beans.push(new RegistryBeanDefinition({name: "beanDS1", type: "String.class"}));
b.beans.push(new RegistryBeanDefinition({name: "beanDS2", type: "String.class"}));
i.spec.flows?.push(b);
const a = new MetadataAnnotation({"camel.apache.org/kamelet.group" : "hello world"})
const a = new MetadataAnnotations({"camel.apache.org/kamelet.group" : "hello world"})
i.metadata.annotations = a
});

Expand All @@ -60,6 +52,8 @@ describe('Kamelet <=> YAML', () => {
const flow1 = new FromDefinition({uri: "direct1"});
flow1.steps?.push(new LogDefinition({logName: 'log11', message: "hello11"}));
i.spec.flows?.push(new RouteDefinition({from:flow1}));

console.log(CamelDefinitionYaml.integrationToYaml(i))
});


Expand Down
1 change: 0 additions & 1 deletion karavan-core/test/topology.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ describe('Topology functions', () => {
const tin = TopologyUtils.findTopologyIncomingNodes([i1, i2]);
const trn = TopologyUtils.findTopologyRestNodes([i1, i2]);
const ton = TopologyUtils.findTopologyOutgoingNodes([i1, i2]);
console.log(tin)
});

});
Loading

0 comments on commit df8c086

Please sign in to comment.