@contrail/cli 1.0.41 → 1.0.43

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.
@@ -3,3 +3,4 @@ export declare function updateWorkflowTemplateDefinition(options: {
3
3
  id: string;
4
4
  updates: any;
5
5
  }): Promise<any>;
6
+ export declare function deleteWorkflowTemplateDefinition(templateId: string): Promise<any>;
@@ -12,3 +12,9 @@ export async function updateWorkflowTemplateDefinition(options) {
12
12
  object: options.updates,
13
13
  });
14
14
  }
15
+ export async function deleteWorkflowTemplateDefinition(templateId) {
16
+ return await new Entities().delete({
17
+ entityName: 'event-workflow-template-definition',
18
+ id: templateId,
19
+ });
20
+ }
@@ -0,0 +1,6 @@
1
+ export declare function getAppVersions(app: any): Promise<any>;
2
+ export declare function upgradeAppVersion(appOrg: any, appVersionId: any): Promise<any>;
3
+ export declare function deleteAppOrg(appOrgId: string): Promise<any>;
4
+ export declare function getInstalledApps(): Promise<any>;
5
+ export declare function installAppToOrg(app: any, configProperties: any): Promise<any>;
6
+ export declare function getAvailableApps(): Promise<any[]>;
@@ -0,0 +1,59 @@
1
+ import { Entities } from '@contrail/sdk';
2
+ import { Auth } from '../auth/auth';
3
+ export async function getAppVersions(app) {
4
+ return await new Entities().get({
5
+ entityName: 'app-version',
6
+ criteria: {
7
+ appId: app.id
8
+ }
9
+ });
10
+ }
11
+ export async function upgradeAppVersion(appOrg, appVersionId) {
12
+ return await new Entities().update({
13
+ entityName: 'app-org',
14
+ id: appOrg.id,
15
+ object: {
16
+ appVersionId: appVersionId
17
+ }
18
+ });
19
+ }
20
+ export async function deleteAppOrg(appOrgId) {
21
+ return await new Entities().delete({
22
+ entityName: 'app-org',
23
+ id: appOrgId
24
+ });
25
+ }
26
+ export async function getInstalledApps() {
27
+ return await new Entities().get({
28
+ entityName: 'app-org',
29
+ relations: ['app']
30
+ });
31
+ }
32
+ export async function installAppToOrg(app, configProperties) {
33
+ return await new Entities().create({
34
+ entityName: 'app-org',
35
+ object: {
36
+ appId: app.id,
37
+ appConfig: configProperties
38
+ }
39
+ });
40
+ }
41
+ export async function getAvailableApps() {
42
+ const orgId = Auth.config.orgId;
43
+ const apps = [];
44
+ apps.push(...await new Entities().get({
45
+ entityName: 'app',
46
+ criteria: {
47
+ orgId,
48
+ private: true,
49
+ },
50
+ }));
51
+ apps.push(...await new Entities().get({
52
+ entityName: 'app',
53
+ criteria: {
54
+ orgId,
55
+ private: false,
56
+ },
57
+ }));
58
+ return apps;
59
+ }
@@ -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, WorkflowDefinition } from '../../interfaces';
2
+ export declare class WorkflowDefinitionDeployer {
3
+ deployWorkflowDefinitions(appDeclaration: AppDeclaration, app: App): Promise<void>;
4
+ getWorkflowDefinitionsForApp(app: App): Promise<any>;
5
+ deployWorkflow(workflowKey: string, workflow: WorkflowDefinition, app: App, existingWorkflows: Array<any>): Promise<any>;
6
+ private createNewWorkflowDefinition;
7
+ private populateActionIds;
8
+ private updateWorkflowDefinition;
9
+ getWorkflowDeclarationIdentifier(actionKey: any, app: App): string;
10
+ }
@@ -0,0 +1,95 @@
1
+ import { Entities } from '@contrail/sdk';
2
+ import chalk from 'chalk';
3
+ import { getActionDeclarationIdentifier, getActionsForApp } from './actions-deployer';
4
+ export class WorkflowDefinitionDeployer {
5
+ async deployWorkflowDefinitions(appDeclaration, app) {
6
+ // FETCH ALL WORKFLOWS FROM THE API.
7
+ const existingWorkflows = await this.getWorkflowDefinitionsForApp(app);
8
+ // UPSERT / DELETE BASED ON WORKFLOW IDENTIFIER
9
+ console.log('%s \tDeploying workflow definitions.', chalk.yellow.bold('WORKFLOW DEFINITION'));
10
+ if (appDeclaration.workflows) {
11
+ const localWorkflows = Object.keys(appDeclaration.workflows);
12
+ if ((localWorkflows === null || localWorkflows === void 0 ? void 0 : localWorkflows.length) > 0) {
13
+ for (const workflowKey of localWorkflows) {
14
+ await this.deployWorkflow(workflowKey, appDeclaration.workflows[workflowKey], app, existingWorkflows);
15
+ }
16
+ }
17
+ }
18
+ }
19
+ async getWorkflowDefinitionsForApp(app) {
20
+ return await new Entities().get({
21
+ entityName: 'event-workflow-template-definition',
22
+ criteria: { ownerKey: `app:${app.id}` },
23
+ });
24
+ }
25
+ async deployWorkflow(workflowKey, workflow, app, existingWorkflows) {
26
+ console.log(`%s \tDeploying workflow definitions: ${workflowKey}`, chalk.yellow.bold('WORKFLOW DEFINITION'));
27
+ const identifier = this.getWorkflowDeclarationIdentifier(workflowKey, app);
28
+ const existingDefinition = existingWorkflows.find(ad => ad.identifier === identifier);
29
+ if (existingDefinition) {
30
+ return await this.updateWorkflowDefinition(existingDefinition, workflow, app);
31
+ }
32
+ return await this.createNewWorkflowDefinition(workflowKey, workflow, app);
33
+ }
34
+ async createNewWorkflowDefinition(workflowKey, workflowDeclaration, app) {
35
+ var _a;
36
+ await this.populateActionIds(workflowDeclaration, app);
37
+ const newObject = {
38
+ identifier: this.getWorkflowDeclarationIdentifier(workflowKey, app),
39
+ name: workflowDeclaration.name,
40
+ ownerKey: `app:${app.id}`,
41
+ triggerKey: workflowDeclaration.triggerKey,
42
+ workflowDefinition: workflowDeclaration.workflowDefinition,
43
+ public: (_a = workflowDeclaration.public) !== null && _a !== void 0 ? _a : false,
44
+ isActive: workflowDeclaration.isActive,
45
+ messageGroupId: workflowDeclaration.messageGroupId,
46
+ parallelWorkerCount: workflowDeclaration.parallelWorkerCount,
47
+ };
48
+ const workflowTemplate = await new Entities().create({
49
+ entityName: 'event-workflow-template-definition',
50
+ object: newObject,
51
+ });
52
+ console.log(`%s Created new workflow definition definitions: ${workflowKey}`, chalk.yellow.bold('WORKFLOW DEFINITION'));
53
+ return workflowTemplate;
54
+ }
55
+ async populateActionIds(workflowDefinition, app) {
56
+ var _a;
57
+ const availableAppActions = await getActionsForApp(app);
58
+ if (!((_a = workflowDefinition.workflowDefinition) === null || _a === void 0 ? void 0 : _a.paths)) {
59
+ return;
60
+ }
61
+ for (const path of workflowDefinition.workflowDefinition.paths) {
62
+ if (!(path === null || path === void 0 ? void 0 : path.actionDefinitionConfigs)) {
63
+ continue;
64
+ }
65
+ for (const actionConfiguration of path.actionDefinitionConfigs) {
66
+ const actionKey = actionConfiguration.action;
67
+ const action = availableAppActions.find(action => action.identifier === getActionDeclarationIdentifier(actionKey, app));
68
+ if (!action) {
69
+ throw new Error(`Could not find action ${actionKey} declared in workflow definition ${workflowDefinition.name}`);
70
+ }
71
+ actionConfiguration.id = action.id;
72
+ }
73
+ }
74
+ }
75
+ async updateWorkflowDefinition(existingWorkflowDefinition, workflowDefinition, app) {
76
+ await this.populateActionIds(workflowDefinition, app);
77
+ const updates = Object.assign(new Object(), {
78
+ name: workflowDefinition.name,
79
+ triggerKey: workflowDefinition.triggerKey,
80
+ workflowDefinition: workflowDefinition.workflowDefinition,
81
+ messageGroupId: workflowDefinition.messageGroupId,
82
+ parallelWorkerCount: workflowDefinition.parallelWorkerCount,
83
+ });
84
+ const updatedWorkflowDefinition = await new Entities().update({
85
+ entityName: 'event-workflow-template-definition',
86
+ id: existingWorkflowDefinition.id,
87
+ object: updates,
88
+ });
89
+ console.log(`%s Updated existing workflow definition: ${updatedWorkflowDefinition.identifier}`, chalk.yellow.bold('WORKFLOW DEFINITION'));
90
+ return updatedWorkflowDefinition;
91
+ }
92
+ getWorkflowDeclarationIdentifier(actionKey, app) {
93
+ return app.identifier + ':' + actionKey;
94
+ }
95
+ }
@@ -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
+ }
@@ -2,7 +2,7 @@ import inquirer from 'inquirer';
2
2
  import chalk from 'chalk';
3
3
  import { getInstalledApps, getWorkflowTemplateDefinitionsForApp } from '../api/apps';
4
4
  import { createWorkflowTemplate } from '../api/event-workflow-templates';
5
- import { createWorkflowTemplateDefinition, updateWorkflowTemplateDefinition as apiUpdateWorkflowTemplateDefinition } from '../api/event-workflow-template-definition';
5
+ import { createWorkflowTemplateDefinition, deleteWorkflowTemplateDefinition, updateWorkflowTemplateDefinition as apiUpdateWorkflowTemplateDefinition } from '../api/event-workflow-template-definition';
6
6
  import { getActionDeclarationIdentifier, getActionsForApp } from '../apps/deploy-app/actions-deployer';
7
7
  export class WorkflowTemplateDefinitionDeployer {
8
8
  async cliInstallWorkflowTemplateDefinitions() {
@@ -11,7 +11,7 @@ export class WorkflowTemplateDefinitionDeployer {
11
11
  console.log('%s \nNo apps are currently installed. You must install an app before installing its associated workflow template definitions.', chalk.red.bold('WORKFLOW DEFINITION'));
12
12
  return;
13
13
  }
14
- let answers = await inquirer.prompt([{
14
+ const answers = await inquirer.prompt([{
15
15
  type: 'list',
16
16
  name: 'appIdentifier',
17
17
  message: 'Which app\'s workflow would you like to install?',
@@ -22,27 +22,24 @@ export class WorkflowTemplateDefinitionDeployer {
22
22
  for (const definition of workflowTemplateDefinitions) {
23
23
  console.log(chalk.yellow.bold(`- ${definition.name}`));
24
24
  }
25
- answers = await inquirer.prompt([{
26
- type: 'confirm',
27
- name: 'shouldInstall',
28
- message: 'Confirmation: would you like to install these workflows? (yes)',
29
- default: true,
30
- }]);
31
- if (!answers.shouldInstall) {
32
- console.log('%s Canceling installing workflows template definitions.', chalk.yellow.bold('WORKFLOW DEFINITION TEMPLATE'));
33
- return;
34
- }
35
25
  await this.installWorkflowTemplateDefinitions(workflowTemplateDefinitions);
36
26
  }
37
27
  async installWorkflowTemplateDefinitions(workflowTemplateDefinitions) {
38
- const answers = await inquirer.prompt([{
28
+ let answers = await inquirer.prompt([{
29
+ type: 'checkbox',
30
+ name: 'workflowTemplateDefinitionsToInstall',
31
+ message: 'Which workflow templates would you like to install?',
32
+ choices: workflowTemplateDefinitions.map(x => { return { value: x.identifier, name: x.name }; }),
33
+ }]);
34
+ const workflowTemplateDefinitionsToInstall = workflowTemplateDefinitions.filter(x => answers.workflowTemplateDefinitionsToInstall.includes(x.identifier));
35
+ answers = await inquirer.prompt([{
39
36
  type: 'confirm',
40
37
  name: 'makeActive',
41
38
  message: 'Should the workflows be active ?(yes)',
42
39
  default: true,
43
40
  }]);
44
41
  const makeActive = answers.makeActive;
45
- for (const definition of workflowTemplateDefinitions) {
42
+ for (const definition of workflowTemplateDefinitionsToInstall) {
46
43
  const newObject = {
47
44
  name: definition.name,
48
45
  triggerKey: definition.triggerKey,
@@ -81,6 +78,12 @@ export class WorkflowTemplateDefinitionDeployer {
81
78
  await this.deployWorkflow(workflowKey, appDeclaration.workflows[workflowKey], app, existingWorkflows);
82
79
  }
83
80
  }
81
+ // remove old templates that were removed by the app publisher
82
+ const localIdentifiers = localWorkflows.map(x => this.getWorkflowDeclarationIdentifier(x, app));
83
+ const workflowTemplateDefinitionsToDelete = existingWorkflows.filter(x => !localIdentifiers.includes(x.identifier));
84
+ for (const templateToDelete of workflowTemplateDefinitionsToDelete) {
85
+ await deleteWorkflowTemplateDefinition(templateToDelete.id);
86
+ }
84
87
  }
85
88
  }
86
89
  async deployWorkflow(workflowKey, workflow, app, existingWorkflows) {
@@ -1,4 +1,4 @@
1
1
  export declare function deleteWorkflowTemplates(): Promise<void>;
2
2
  export declare function localDeleteWorkflowTemplate(): Promise<void>;
3
3
  export declare function localDeployWorkflowTemplate(): Promise<void>;
4
- export declare function listWorkflowTemplate(): Promise<any>;
4
+ export declare function listWorkflowTemplate(): Promise<void>;
@@ -1,4 +1,4 @@
1
1
  export declare class WorkflowTemplateDeployer {
2
2
  deleteWorkflowTemplates(): Promise<void>;
3
- listExistingWorkflowTemplates(): Promise<any>;
3
+ listExistingWorkflowTemplates(): Promise<void>;
4
4
  }
@@ -19,7 +19,8 @@ export class WorkflowTemplateDeployer {
19
19
  }
20
20
  async listExistingWorkflowTemplates() {
21
21
  const templates = await getExistingWorkflowTemplates();
22
- console.log(templates);
23
- return templates;
22
+ console.log('(Date Created) (active) workflow name');
23
+ console.log('------------------------');
24
+ console.log(chalk.green.bold(templates.map(x => `(${x.createdOn}) (${x.isActive ? 'active' : 'inactive'}) ${x.name} `).join('\n')));
24
25
  }
25
26
  }
@@ -0,0 +1,14 @@
1
+ import { WorkflowTemplate, WorkflowTemplateDeclaration } from '../interfaces';
2
+ export declare class WorkflowTemplateDeployer {
3
+ deployWorkflows(wtDeclaration: WorkflowTemplateDeclaration): Promise<void>;
4
+ getExistingWorkflowTemplates(): Promise<any>;
5
+ deployWorkflowTemplate(workflowKey: string, workflow: WorkflowTemplate, existingWorkflows: Array<any>): Promise<any>;
6
+ private createNewWorkflowTemplate;
7
+ private populateActionIds;
8
+ private updateWorkflowTemplate;
9
+ deleteWorkflowTemplates(wtDeclaration: WorkflowTemplateDeclaration): Promise<void>;
10
+ private deleteWorkflowTemplate;
11
+ listExistingWorkflowTemplates(): Promise<any>;
12
+ }
13
+ export declare function getWorkflowTemplateIdentifier(workflowKey: string): string;
14
+ export declare function getActionByIdentifier(identifier: string): Promise<any>;
@@ -0,0 +1,127 @@
1
+ import { Entities } from '@contrail/sdk';
2
+ import chalk from 'chalk';
3
+ export class WorkflowTemplateDeployer {
4
+ async deployWorkflows(wtDeclaration) {
5
+ // FETCH ALL WORKFLOWS FROM THE API.
6
+ const existingWorkflows = await this.getExistingWorkflowTemplates();
7
+ // UPSERT / DELETE BASED ON WORKFLOW TEMPLATES IDENTIFIER
8
+ console.log('%s \tDeploying workflow templates.', chalk.yellow.bold('WORKFLOW TEMPLATES'));
9
+ for (const workflowKey of Object.keys(wtDeclaration.workflows)) {
10
+ await this.deployWorkflowTemplate(workflowKey, wtDeclaration.workflows[workflowKey], existingWorkflows);
11
+ }
12
+ }
13
+ async getExistingWorkflowTemplates() {
14
+ return await new Entities().get({
15
+ entityName: 'event-workflow-template'
16
+ });
17
+ }
18
+ async deployWorkflowTemplate(workflowKey, workflow, existingWorkflows) {
19
+ console.log(`%s \tDeploying workflow: ${workflowKey}`, chalk.yellow.bold('WORKFLOW TEMPLATES'));
20
+ const identifier = getWorkflowTemplateIdentifier(workflowKey);
21
+ const existingTemplate = existingWorkflows.find(ad => ad.identifier === identifier);
22
+ if (existingTemplate) {
23
+ return await this.updateWorkflowTemplate(workflowKey, existingTemplate, workflow);
24
+ }
25
+ return await this.createNewWorkflowTemplate(workflowKey, workflow);
26
+ }
27
+ async createNewWorkflowTemplate(workflowKey, workflowTemplate) {
28
+ await this.populateActionIds(workflowTemplate, workflowKey);
29
+ const newObject = {
30
+ identifier: getWorkflowTemplateIdentifier(workflowKey),
31
+ name: workflowTemplate.name,
32
+ triggerKey: workflowTemplate.triggerKey,
33
+ workflowDefinition: workflowTemplate.workflowDefinition,
34
+ isActive: workflowTemplate.isActive,
35
+ supportingAppId: workflowTemplate.supportingAppId,
36
+ messageGroupId: workflowTemplate.messageGroupId,
37
+ parallelWorkerCount: workflowTemplate.parallelWorkerCount,
38
+ };
39
+ const newWorkflowTemplate = await new Entities().create({
40
+ entityName: 'event-workflow-template',
41
+ object: newObject,
42
+ });
43
+ console.log(`%s Created new workflow definition: ${workflowKey}`, chalk.yellow.bold('WORKFLOW TEMPLATES'));
44
+ return newWorkflowTemplate;
45
+ }
46
+ async populateActionIds(workflowTemplate, workflowKey) {
47
+ var _a;
48
+ if (!((_a = workflowTemplate === null || workflowTemplate === void 0 ? void 0 : workflowTemplate.workflowDefinition) === null || _a === void 0 ? void 0 : _a.paths)) {
49
+ return;
50
+ }
51
+ for (const path of workflowTemplate.workflowDefinition.paths) {
52
+ if (!(path === null || path === void 0 ? void 0 : path.actionDefinitionConfigs)) {
53
+ continue;
54
+ }
55
+ for (const actionConfiguration of path.actionDefinitionConfigs) {
56
+ const actionKey = actionConfiguration.action;
57
+ try {
58
+ const actionEntity = await getActionByIdentifier(actionKey);
59
+ actionConfiguration.id = actionEntity.id;
60
+ }
61
+ catch (e) {
62
+ console.log(e);
63
+ }
64
+ if (!actionConfiguration.id) {
65
+ throw new Error(`Could not find action ${actionKey} declared in ${workflowKey}`);
66
+ }
67
+ }
68
+ }
69
+ }
70
+ async updateWorkflowTemplate(workflowKey, existingWorkflowTemplate, workflowTemplate) {
71
+ await this.populateActionIds(workflowTemplate, workflowKey);
72
+ const updates = Object.assign(new Object(), {
73
+ name: workflowTemplate.name,
74
+ triggerKey: workflowTemplate.triggerKey,
75
+ workflowDefinition: workflowTemplate.workflowDefinition
76
+ });
77
+ const updatedWorkflowTemplate = await new Entities().update({
78
+ entityName: 'event-workflow-template',
79
+ id: existingWorkflowTemplate.id,
80
+ object: updates,
81
+ });
82
+ console.log(`%s Updated existing workflow template: ${updatedWorkflowTemplate.identifier}`, chalk.yellow.bold('WORKFLOW TEMPLATES'));
83
+ return updatedWorkflowTemplate;
84
+ }
85
+ async deleteWorkflowTemplates(wtDeclaration) {
86
+ // FETCH ALL WORKFLOWS FROM THE API.
87
+ const existingWorkflows = await this.getExistingWorkflowTemplates();
88
+ // UPSERT / DELETE BASED ON WORKFLOW TEMPLATES IDENTIFIER
89
+ console.log('%s \tDELETING workflow templates.', chalk.yellow.bold('WORKFLOW TEMPLATES'));
90
+ for (const workflowKey of Object.keys(wtDeclaration.workflows)) {
91
+ const identifier = getWorkflowTemplateIdentifier(workflowKey);
92
+ const existingTemplate = existingWorkflows.find(ad => ad.identifier === identifier);
93
+ if (existingTemplate) {
94
+ try {
95
+ await this.deleteWorkflowTemplate(identifier);
96
+ }
97
+ catch (e) {
98
+ console.log('%s \tFAILED to delete workflow template %s.', chalk.red.bold('WORKFLOW TEMPLATES'), identifier);
99
+ }
100
+ }
101
+ else {
102
+ console.log('%s \tUnable to locate workflow template %s.', chalk.yellow.bold('WORKFLOW TEMPLATES'), identifier);
103
+ }
104
+ }
105
+ }
106
+ async deleteWorkflowTemplate(identifier) {
107
+ return await new Entities().delete({
108
+ entityName: 'event-workflow-template',
109
+ id: identifier,
110
+ });
111
+ }
112
+ async listExistingWorkflowTemplates() {
113
+ const templates = await this.getExistingWorkflowTemplates();
114
+ console.log(templates);
115
+ return templates;
116
+ }
117
+ }
118
+ export function getWorkflowTemplateIdentifier(workflowKey) {
119
+ return workflowKey;
120
+ }
121
+ export async function getActionByIdentifier(identifier) {
122
+ const result = await new Entities().get({
123
+ entityName: 'event-workflow-action-definition',
124
+ criteria: { identifier },
125
+ });
126
+ return result.length > 0 ? result[0] : {};
127
+ }
@@ -0,0 +1,3 @@
1
+ export declare function deleteWorkflowTemplate(): Promise<void>;
2
+ export declare function deployWorkflowTemplate(): Promise<void>;
3
+ export declare function listWorkflowTemplate(): Promise<any>;
@@ -0,0 +1,46 @@
1
+ import chalk from 'chalk';
2
+ import fs from 'fs';
3
+ import { authUser } from '../auth/login';
4
+ import { getSeparatorByPlatform } from '../common';
5
+ const yaml = require('js-yaml');
6
+ import { WorkflowTemplateDeployer } from './workflow-template-deployer';
7
+ export async function deleteWorkflowTemplate() {
8
+ let wtDeclaration;
9
+ try {
10
+ wtDeclaration = loadTemplateConfig();
11
+ }
12
+ catch (e) {
13
+ console.log(`%s Workflow Deletion Failed. ${e}`, chalk.red.bold('FAIL'));
14
+ return;
15
+ }
16
+ await authUser();
17
+ await new WorkflowTemplateDeployer().deleteWorkflowTemplates(wtDeclaration);
18
+ }
19
+ export async function deployWorkflowTemplate() {
20
+ let wtDeclaration;
21
+ try {
22
+ wtDeclaration = loadTemplateConfig();
23
+ }
24
+ catch (e) {
25
+ console.log(`%s Workflow Deletion Failed. ${e}`, chalk.red.bold('FAIL'));
26
+ return;
27
+ }
28
+ await authUser();
29
+ await new WorkflowTemplateDeployer().deployWorkflows(wtDeclaration);
30
+ }
31
+ export async function listWorkflowTemplate() {
32
+ await authUser();
33
+ return await new WorkflowTemplateDeployer().listExistingWorkflowTemplates();
34
+ }
35
+ function loadTemplateConfig() {
36
+ const TARGET_DIR = process.cwd();
37
+ try {
38
+ const separator = getSeparatorByPlatform();
39
+ const doc = yaml.load(fs.readFileSync(TARGET_DIR + separator + 'workflow.yml', 'utf8'));
40
+ console.log(doc);
41
+ return doc;
42
+ }
43
+ catch (e) {
44
+ throw new Error(`ERROR PARSING YAML: ${e}`);
45
+ }
46
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/cli",
3
- "version": "1.0.41",
3
+ "version": "1.0.43",
4
4
  "description": "VibeIQ's Contrail Platform CLI.",
5
5
  "main": "lib/index.js",
6
6
  "bin": {
@@ -24,7 +24,7 @@
24
24
  "license": "ISC",
25
25
  "dependencies": {
26
26
  "@contrail/sdk": "^1.0.154",
27
- "@contrail/types": "^2.0.74",
27
+ "@contrail/types": "^2.0.75",
28
28
  "@contrail/util": "^1.0.17",
29
29
  "amazon-cognito-identity-js": "^5.2.3",
30
30
  "arg": "^4.0.1",
@@ -13,4 +13,11 @@ export async function updateWorkflowTemplateDefinition(options: {id: string, upd
13
13
  id: options.id,
14
14
  object: options.updates,
15
15
  });
16
+ }
17
+
18
+ export async function deleteWorkflowTemplateDefinition(templateId: string) {
19
+ return await new Entities().delete({
20
+ entityName: 'event-workflow-template-definition',
21
+ id: templateId,
22
+ });
16
23
  }
@@ -3,7 +3,7 @@ import chalk from 'chalk';
3
3
  import { App, AppDeclaration, WorkflowDefinition, WorkflowTemplate } from '../interfaces';
4
4
  import { getInstalledApps, getWorkflowTemplateDefinitionsForApp } from '../api/apps';
5
5
  import { createWorkflowTemplate } from '../api/event-workflow-templates';
6
- import { createWorkflowTemplateDefinition, updateWorkflowTemplateDefinition as apiUpdateWorkflowTemplateDefinition } from '../api/event-workflow-template-definition';
6
+ import { createWorkflowTemplateDefinition, deleteWorkflowTemplateDefinition, updateWorkflowTemplateDefinition as apiUpdateWorkflowTemplateDefinition } from '../api/event-workflow-template-definition';
7
7
  import { getActionDeclarationIdentifier, getActionsForApp } from '../apps/deploy-app/actions-deployer';
8
8
 
9
9
  export class WorkflowTemplateDefinitionDeployer {
@@ -15,7 +15,7 @@ export class WorkflowTemplateDefinitionDeployer {
15
15
  return;
16
16
  }
17
17
 
18
- let answers = await inquirer.prompt([{
18
+ const answers = await inquirer.prompt([{
19
19
  type: 'list',
20
20
  name: 'appIdentifier',
21
21
  message: 'Which app\'s workflow would you like to install?',
@@ -26,31 +26,26 @@ export class WorkflowTemplateDefinitionDeployer {
26
26
  for (const definition of workflowTemplateDefinitions) {
27
27
  console.log(chalk.yellow.bold(`- ${definition.name}`));
28
28
  }
29
- answers = await inquirer.prompt([{
30
- type: 'confirm',
31
- name: 'shouldInstall',
32
- message: 'Confirmation: would you like to install these workflows? (yes)',
33
- default: true,
34
- }]);
35
-
36
- if (!answers.shouldInstall) {
37
- console.log('%s Canceling installing workflows template definitions.', chalk.yellow.bold('WORKFLOW DEFINITION TEMPLATE'));
38
- return;
39
- }
40
-
41
29
  await this.installWorkflowTemplateDefinitions(workflowTemplateDefinitions);
42
30
  }
43
31
 
44
32
  async installWorkflowTemplateDefinitions(workflowTemplateDefinitions: WorkflowTemplate[]) {
33
+ let answers = await inquirer.prompt([{
34
+ type: 'checkbox',
35
+ name: 'workflowTemplateDefinitionsToInstall',
36
+ message: 'Which workflow templates would you like to install?',
37
+ choices: workflowTemplateDefinitions.map(x => {return {value: x.identifier, name: x.name};}),
38
+ }]);
45
39
 
46
- const answers = await inquirer.prompt([{
40
+ const workflowTemplateDefinitionsToInstall = workflowTemplateDefinitions.filter(x => answers.workflowTemplateDefinitionsToInstall.includes(x.identifier));
41
+ answers = await inquirer.prompt([{
47
42
  type: 'confirm',
48
43
  name: 'makeActive',
49
44
  message: 'Should the workflows be active ?(yes)',
50
45
  default: true,
51
46
  }]);
52
47
  const makeActive = answers.makeActive;
53
- for (const definition of workflowTemplateDefinitions) {
48
+ for (const definition of workflowTemplateDefinitionsToInstall) {
54
49
  const newObject: WorkflowTemplate = {
55
50
  name: definition.name,
56
51
  triggerKey: definition.triggerKey,
@@ -100,6 +95,13 @@ export class WorkflowTemplateDefinitionDeployer {
100
95
  );
101
96
  }
102
97
  }
98
+
99
+ // remove old templates that were removed by the app publisher
100
+ const localIdentifiers = localWorkflows.map(x=> this.getWorkflowDeclarationIdentifier(x, app));
101
+ const workflowTemplateDefinitionsToDelete = existingWorkflows.filter(x=> !localIdentifiers.includes(x.identifier));
102
+ for(const templateToDelete of workflowTemplateDefinitionsToDelete) {
103
+ await deleteWorkflowTemplateDefinition(templateToDelete.id);
104
+ }
103
105
  }
104
106
  }
105
107
 
@@ -23,7 +23,8 @@ export class WorkflowTemplateDeployer {
23
23
 
24
24
  public async listExistingWorkflowTemplates() {
25
25
  const templates = await getExistingWorkflowTemplates();
26
- console.log(templates);
27
- return templates;
26
+ console.log('(Date Created) (active) workflow name');
27
+ console.log('------------------------');
28
+ console.log(chalk.green.bold(templates.map(x => `(${x.createdOn}) (${x.isActive? 'active': 'inactive'}) ${x.name} `).join('\n')));
28
29
  }
29
30
  }