@contrail/cli 1.0.27 → 1.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -35,7 +35,7 @@ export class ActionsDeployer {
35
35
  await this.createNewActionDefinition(actionKey, actionDeclaration, app);
36
36
  }
37
37
  else {
38
- await this.updateActionDefinition(existingDefinition, actionDeclaration);
38
+ await this.updateActionDefinition(existingDefinition, actionDeclaration, app);
39
39
  }
40
40
  }
41
41
  async createNewActionDefinition(actionKey, actionDeclaration, app) {
@@ -47,7 +47,7 @@ export class ActionsDeployer {
47
47
  identifier: getActionDeclarationIdentifier(actionKey, app),
48
48
  name: actionDeclaration.name,
49
49
  ownerKey: `app:${app.id}`,
50
- public: app.public,
50
+ public: 'public' in actionDeclaration ? actionDeclaration.public : app.public,
51
51
  configProperties,
52
52
  };
53
53
  const functionString = await this.getFunction(actionDeclaration);
@@ -61,7 +61,7 @@ export class ActionsDeployer {
61
61
  console.log(`%s Created new action definition: ${actionKey}`, chalk.yellow.bold('APP'));
62
62
  return actionDefinition;
63
63
  }
64
- async updateActionDefinition(existingActionDefinition, actionDeclaration) {
64
+ async updateActionDefinition(existingActionDefinition, actionDeclaration, app) {
65
65
  let configProperties;
66
66
  if (actionDeclaration === null || actionDeclaration === void 0 ? void 0 : actionDeclaration.actionConfiguration) {
67
67
  configProperties = ConfigPropertiesParser.getConfigProperties(actionDeclaration.actionConfiguration);
@@ -71,7 +71,7 @@ export class ActionsDeployer {
71
71
  }
72
72
  const updates = {
73
73
  name: actionDeclaration.name,
74
- // public: actionDeclaration.public, // TODO - an actions visibility is allowed to change
74
+ public: 'public' in actionDeclaration ? actionDeclaration.public : app.public,
75
75
  configProperties,
76
76
  };
77
77
  const functionString = await this.getFunction(actionDeclaration);
@@ -0,0 +1,10 @@
1
+ import { App, AppDeclaration, AppActionDeclaration } from '../../interfaces';
2
+ export declare class ActionsDeployer {
3
+ deployActions(appDeclaration: AppDeclaration, app: App): Promise<void>;
4
+ getActionsForApp(app: App): Promise<any>;
5
+ deployAction(actionKey: string, actionDeclaration: AppActionDeclaration, app: App, existingActions: Array<any>): Promise<void>;
6
+ private createNewActionDefinition;
7
+ private updateActionDefinition;
8
+ getActionDeclarationIdentifier(actionKey: any, app: App): string;
9
+ getFunction(actionDeclaration: AppActionDeclaration): Promise<string>;
10
+ }
@@ -0,0 +1,97 @@
1
+ import { Entities } from '@contrail/sdk';
2
+ import chalk from 'chalk';
3
+ import { ConfigPropertiesParser } from './app-template-parsers';
4
+ import { getSeparatorByPlatform } from '../../common';
5
+ import fs from 'fs';
6
+ export class ActionsDeployer {
7
+ async deployActions(appDeclaration, app) {
8
+ // FETCH ALL ACTIONS FROM THE API.
9
+ const existingActions = await this.getActionsForApp(app);
10
+ // UPSERT / DELETE BASED ON ACTION IDENTIFIER
11
+ console.log('%s \tDeploying actions.', chalk.yellow.bold('ACTION'));
12
+ for (const actionKey of Object.keys(appDeclaration.actions)) {
13
+ await this.deployAction(actionKey, appDeclaration.actions[actionKey], app, existingActions);
14
+ }
15
+ }
16
+ async getActionsForApp(app) {
17
+ return await new Entities().get({
18
+ entityName: 'event-workflow-action-definition',
19
+ criteria: { ownerKey: `app:${app.id}` },
20
+ });
21
+ }
22
+ async deployAction(actionKey, actionDeclaration, app, existingActions) {
23
+ console.log(`%s \tDeploying action: ${actionKey}`, chalk.yellow.bold('ACTION'));
24
+ const identifier = this.getActionDeclarationIdentifier(actionKey, app);
25
+ const existingDefinition = existingActions.find(ad => ad.identifier === identifier);
26
+ if (!existingDefinition) {
27
+ await this.createNewActionDefinition(actionKey, actionDeclaration, app);
28
+ }
29
+ else {
30
+ await this.updateActionDefinition(existingDefinition, actionDeclaration);
31
+ }
32
+ }
33
+ async createNewActionDefinition(actionKey, actionDeclaration, app) {
34
+ let configProperties;
35
+ if (actionDeclaration === null || actionDeclaration === void 0 ? void 0 : actionDeclaration.actionConfiguration) {
36
+ configProperties = ConfigPropertiesParser.getConfigProperties(actionDeclaration.actionConfiguration);
37
+ }
38
+ const newObject = {
39
+ identifier: this.getActionDeclarationIdentifier(actionKey, app),
40
+ name: actionDeclaration.name,
41
+ ownerKey: `app:${app.id}`,
42
+ public: app.public,
43
+ configProperties,
44
+ };
45
+ const functionString = await this.getFunction(actionDeclaration);
46
+ if (functionString) {
47
+ newObject.executeFunctionString = functionString;
48
+ }
49
+ const actionDefinition = await new Entities().create({
50
+ entityName: 'event-workflow-action-definition',
51
+ object: newObject,
52
+ });
53
+ console.log(`%s Created new action definition: ${actionKey}`, chalk.yellow.bold('APP'));
54
+ return actionDefinition;
55
+ }
56
+ async updateActionDefinition(existingActionDefinition, actionDeclaration) {
57
+ let configProperties;
58
+ if (actionDeclaration === null || actionDeclaration === void 0 ? void 0 : actionDeclaration.actionConfiguration) {
59
+ configProperties = ConfigPropertiesParser.getConfigProperties(actionDeclaration.actionConfiguration);
60
+ }
61
+ else if (!(actionDeclaration === null || actionDeclaration === void 0 ? void 0 : actionDeclaration.actionConfiguration) && existingActionDefinition.configProperties) {
62
+ configProperties = null;
63
+ }
64
+ const updates = {
65
+ name: actionDeclaration.name,
66
+ // public: actionDeclaration.public, // TODO - an actions visibility is allowed to change
67
+ configProperties,
68
+ };
69
+ const functionString = await this.getFunction(actionDeclaration);
70
+ if (functionString) {
71
+ updates.executeFunctionString = functionString;
72
+ }
73
+ const actionDefinition = await new Entities().update({
74
+ entityName: 'event-workflow-action-definition',
75
+ id: existingActionDefinition.id,
76
+ object: updates,
77
+ });
78
+ console.log(`%s Updated existing action definition: ${actionDefinition.identifier}`, chalk.yellow.bold('APP'));
79
+ return actionDefinition;
80
+ }
81
+ getActionDeclarationIdentifier(actionKey, app) {
82
+ return app.identifier + ':' + actionKey;
83
+ }
84
+ async getFunction(actionDeclaration) {
85
+ if (!actionDeclaration.handler) {
86
+ return;
87
+ }
88
+ if (!actionDeclaration.handler.endsWith('.js')) {
89
+ throw new Error('Action handler must be a .js file.');
90
+ }
91
+ const CURRENT_DIR = process.cwd();
92
+ const separator = getSeparatorByPlatform();
93
+ const functionLocation = CURRENT_DIR + separator + actionDeclaration.handler;
94
+ const content = fs.readFileSync(functionLocation);
95
+ return content.toString();
96
+ }
97
+ }
@@ -0,0 +1,76 @@
1
+ /** Describes the derivative JSON / object represented
2
+ * in app.yml file.
3
+ */
4
+ export interface AppDeclaration {
5
+ appIdentifier: string;
6
+ appName: string;
7
+ appDescription?: string;
8
+ visibility: 'private' | 'public';
9
+ publisher: string;
10
+ version: string;
11
+ eventWorkflowTriggerKeyMapping: string;
12
+ thumbnail?: string;
13
+ keywords?: Array<string>;
14
+ actions?: {
15
+ [key: string]: AppActionDeclaration;
16
+ };
17
+ appConfiguration: ConfigPropertyElements;
18
+ triggers: {
19
+ [key: string]: AppTriggerDeclaration;
20
+ };
21
+ isDockerBased?: boolean;
22
+ }
23
+ export interface ConfigPropertyElements {
24
+ [key: string]: ConfigPropertyElement;
25
+ }
26
+ export interface ConfigPropertyElement {
27
+ propertyType: string;
28
+ options?: any[];
29
+ label: string;
30
+ }
31
+ export interface AppActionDeclaration {
32
+ handler: string;
33
+ name: string;
34
+ description?: string;
35
+ actionConfiguration?: ConfigPropertyElements;
36
+ }
37
+ export interface AppTriggerDeclaration {
38
+ key: string;
39
+ name: string;
40
+ external?: boolean;
41
+ description?: string;
42
+ }
43
+ export interface App {
44
+ id: string;
45
+ identifier: string;
46
+ name: string;
47
+ public: boolean;
48
+ description?: string;
49
+ publisher: string;
50
+ thumbnail: string;
51
+ eventWorkflowTriggerKeyMapping: string;
52
+ configProperties?: ConfigProperty[];
53
+ triggerKeyDefinitions?: TriggerKeyDefinition[];
54
+ }
55
+ export declare class ConfigProperty {
56
+ slug: string;
57
+ propertyType: string;
58
+ options?: any[];
59
+ label: string;
60
+ }
61
+ export interface EventWorkflowActionDefinition {
62
+ id?: string;
63
+ identifier?: string;
64
+ name?: string;
65
+ executeFunctionString?: string;
66
+ ownerKey?: string;
67
+ public?: boolean;
68
+ configProperties?: ConfigProperty[];
69
+ isDockerBased?: boolean;
70
+ }
71
+ export declare class TriggerKeyDefinition {
72
+ name: string;
73
+ description?: string;
74
+ triggerKey: string;
75
+ external?: boolean;
76
+ }
@@ -0,0 +1,4 @@
1
+ export class ConfigProperty {
2
+ }
3
+ export class TriggerKeyDefinition {
4
+ }
@@ -0,0 +1,10 @@
1
+ import { App, AppDeclaration, WorkflowTemplateDeclaration } from './interfaces';
2
+ export declare class WorkflowDeployer {
3
+ deployWorkflows(appDeclaration: AppDeclaration, app: App): Promise<void>;
4
+ getWorkflowsForApp(app: App): Promise<any>;
5
+ deployWorkflow(workflowKey: string, workflow: WorkflowTemplateDeclaration, app: App, existingWorkflows: Array<any>, appDeclaration: AppDeclaration): Promise<any>;
6
+ private createNewWorkflowDefinition;
7
+ private populateActionIds;
8
+ private updateWorkflowDefinition;
9
+ getWorkflowDeclarationIdentifier(actionKey: any, app: App): string;
10
+ }
@@ -0,0 +1,87 @@
1
+ import { Entities } from '@contrail/sdk';
2
+ import chalk from 'chalk';
3
+ import { getActionDeclarationIdentifier, getActionsForApp } from './actions-deployer';
4
+ export class WorkflowDeployer {
5
+ async deployWorkflows(appDeclaration, app) {
6
+ // FETCH ALL WORKFLOWS FROM THE API.
7
+ const existingWorkflows = await this.getWorkflowsForApp(app);
8
+ // UPSERT / DELETE BASED ON WORKFLOW IDENTIFIER
9
+ console.log('%s \tDeploying workflows.', chalk.yellow.bold('WORKFLOW'));
10
+ for (const workflowKey of Object.keys(appDeclaration.workflows)) {
11
+ await this.deployWorkflow(workflowKey, appDeclaration.workflows[workflowKey], app, existingWorkflows, appDeclaration);
12
+ }
13
+ }
14
+ async getWorkflowsForApp(app) {
15
+ return await new Entities().get({
16
+ entityName: 'event-workflow-template',
17
+ criteria: { ownerKey: `app:${app.id}` },
18
+ });
19
+ }
20
+ async deployWorkflow(workflowKey, workflow, app, existingWorkflows, appDeclaration) {
21
+ console.log(`%s \tDeploying workflow: ${workflowKey}`, chalk.yellow.bold('WORKFLOW'));
22
+ const identifier = this.getWorkflowDeclarationIdentifier(workflowKey, app);
23
+ const existingDefinition = existingWorkflows.find(ad => ad.identifier === identifier);
24
+ if (existingDefinition) {
25
+ return await this.updateWorkflowDefinition(existingDefinition, workflow, app, appDeclaration);
26
+ }
27
+ return await this.createNewWorkflowDefinition(workflowKey, workflow, app, appDeclaration);
28
+ }
29
+ async createNewWorkflowDefinition(workflowKey, workflowDeclaration, app, appDeclaration) {
30
+ await this.populateActionIds(workflowDeclaration, appDeclaration, app);
31
+ const newObject = {
32
+ identifier: this.getWorkflowDeclarationIdentifier(workflowKey, app),
33
+ name: workflowDeclaration.name,
34
+ ownerKey: `app:${app.id}`,
35
+ triggerKey: workflowDeclaration.triggerKey,
36
+ workflowDefinition: workflowDeclaration.workflowDefinition,
37
+ isActive: workflowDeclaration.isActive,
38
+ supportingAppId: workflowDeclaration.supportingAppId,
39
+ messageGroupId: workflowDeclaration.messageGroupId,
40
+ parallelWorkerCount: workflowDeclaration.parallelWorkerCount,
41
+ };
42
+ const workflowTemplate = await new Entities().create({
43
+ entityName: 'event-workflow-template',
44
+ object: newObject,
45
+ });
46
+ console.log(`%s Created new workflow definition: ${workflowKey}`, chalk.yellow.bold('WORKFLOW'));
47
+ return workflowTemplate;
48
+ }
49
+ async populateActionIds(workflowDeclaration, appDeclaration, app) {
50
+ var _a;
51
+ let availableAppActions = await getActionsForApp(app);
52
+ if (!((_a = workflowDeclaration.workflowDefinition) === null || _a === void 0 ? void 0 : _a.paths)) {
53
+ return;
54
+ }
55
+ for (const path of workflowDeclaration.workflowDefinition.paths) {
56
+ if (!(path === null || path === void 0 ? void 0 : path.actionDefinitionConfigs)) {
57
+ continue;
58
+ }
59
+ for (const actionConfiguration of path.actionDefinitionConfigs) {
60
+ const actionKey = actionConfiguration.action;
61
+ const action = availableAppActions.find(action => action.identifier === getActionDeclarationIdentifier(actionKey, app));
62
+ if (!action) {
63
+ throw new Error(`Could not find action ${actionKey} declared in ${workflowDeclaration.name}`);
64
+ }
65
+ actionConfiguration.id = action.id;
66
+ }
67
+ }
68
+ }
69
+ async updateWorkflowDefinition(existingWorkflowDefinition, workflowDeclaration, app, appDeclaration) {
70
+ await this.populateActionIds(workflowDeclaration, appDeclaration, app);
71
+ const updates = Object.assign(new Object(), {
72
+ name: workflowDeclaration.name,
73
+ triggerKey: workflowDeclaration.triggerKey,
74
+ workflowDefinition: workflowDeclaration.workflowDefinition
75
+ });
76
+ const updatedWorkflowDefinition = await new Entities().update({
77
+ entityName: 'event-workflow-template',
78
+ id: existingWorkflowDefinition.id,
79
+ object: updates,
80
+ });
81
+ console.log(`%s Updated existing workflow definition: ${updatedWorkflowDefinition.identifier}`, chalk.yellow.bold('WORKFLOW'));
82
+ return updatedWorkflowDefinition;
83
+ }
84
+ getWorkflowDeclarationIdentifier(actionKey, app) {
85
+ return app.identifier + ':' + actionKey;
86
+ }
87
+ }
@@ -34,6 +34,7 @@ export interface ConfigPropertyElement {
34
34
  }
35
35
  export interface AppActionDeclaration {
36
36
  handler: string;
37
+ public?: boolean;
37
38
  name: string;
38
39
  description?: string;
39
40
  actionConfiguration?: ConfigPropertyElements;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/cli",
3
- "version": "1.0.27",
3
+ "version": "1.0.29",
4
4
  "description": "VibeIQ's Contrail Platform CLI.",
5
5
  "main": "lib/index.js",
6
6
  "bin": {
@@ -45,9 +45,9 @@
45
45
  "@types/jest": "^23.3.14",
46
46
  "@types/lodash": "^4.14.178",
47
47
  "@types/node": "^14.6.4",
48
- "jest": "^23.6.0",
48
+ "jest": "^29.3.1",
49
49
  "prettier": "^1.19.1",
50
- "ts-jest": "^23.10.5",
50
+ "ts-jest": "^29.0.3",
51
51
  "tslint": "^5.11.0",
52
52
  "tslint-config-prettier": "^1.18.0",
53
53
  "typescript": "^3.0.1"
@@ -46,7 +46,7 @@ export class ActionsDeployer {
46
46
  if (!existingDefinition) {
47
47
  await this.createNewActionDefinition(actionKey, actionDeclaration, app);
48
48
  } else {
49
- await this.updateActionDefinition(existingDefinition, actionDeclaration);
49
+ await this.updateActionDefinition(existingDefinition, actionDeclaration, app);
50
50
  }
51
51
  }
52
52
 
@@ -59,7 +59,7 @@ export class ActionsDeployer {
59
59
  identifier: getActionDeclarationIdentifier(actionKey, app),
60
60
  name: actionDeclaration.name,
61
61
  ownerKey: `app:${app.id}`,
62
- public: app.public, // TODO - an actions visibility is not tied to the visibility of the app
62
+ public: 'public' in actionDeclaration? actionDeclaration.public: app.public,
63
63
  configProperties,
64
64
  };
65
65
 
@@ -78,7 +78,7 @@ export class ActionsDeployer {
78
78
  return actionDefinition;
79
79
  }
80
80
 
81
- private async updateActionDefinition(existingActionDefinition: EventWorkflowActionDefinition, actionDeclaration: AppActionDeclaration) {
81
+ private async updateActionDefinition(existingActionDefinition: EventWorkflowActionDefinition, actionDeclaration: AppActionDeclaration, app: App) {
82
82
 
83
83
  let configProperties;
84
84
  if (actionDeclaration?.actionConfiguration) {
@@ -90,7 +90,7 @@ export class ActionsDeployer {
90
90
 
91
91
  const updates: EventWorkflowActionDefinition = {
92
92
  name: actionDeclaration.name,
93
- // public: actionDeclaration.public, // TODO - an actions visibility is allowed to change
93
+ public: 'public' in actionDeclaration? actionDeclaration.public: app.public,
94
94
  configProperties,
95
95
  };
96
96
 
package/src/interfaces.ts CHANGED
@@ -31,6 +31,7 @@ export interface ConfigPropertyElement {
31
31
 
32
32
  export interface AppActionDeclaration {
33
33
  handler: string;
34
+ public?: boolean;
34
35
  name: string;
35
36
  description?: string;
36
37
  actionConfiguration?: ConfigPropertyElements;