/
workflow-api-service.js
147 lines (125 loc) · 4.61 KB
/
workflow-api-service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright © 2016-2017 Dell Inc. or its subsidiaries. All Rights Reserved.
'use strict';
var di = require('di');
module.exports = workflowApiServiceFactory;
di.annotate(workflowApiServiceFactory, new di.Provide('Http.Services.Api.Workflows'));
di.annotate(workflowApiServiceFactory,
new di.Inject(
'Protocol.Events',
'Logger',
'Errors',
'Promise',
'Constants',
'_',
'Services.Lookup',
'Http.Services.Api.Taskgraph.Scheduler',
'Assert'
)
);
function workflowApiServiceFactory(
eventsProtocol,
Logger,
Errors,
Promise,
Constants,
_,
lookupService,
taskgraphService,
assert
) {
var logger = Logger.initialize(workflowApiServiceFactory);
function WorkflowApiService() {
}
WorkflowApiService.prototype.createAndRunGraph = function (configuration, nodeId) {
var self = this;
return Promise.try(function () {
if (!configuration.name || !_.isString(configuration.name)) {
throw new Errors.BadRequestError('Graph name is missing or in wrong format');
}
})
.then(function () {
if(nodeId !== null){
return taskgraphService.workflowsPost(configuration,nodeId);
} else {
return taskgraphService.workflowsPost(configuration);
}
});
};
WorkflowApiService.prototype.findGraphDefinitionByName = function (graphName) {
return taskgraphService.workflowsGetByInstanceId(graphName)
.then(function (graph) {
if (_.isEmpty(graph)) {
throw new Errors.NotFoundError('Graph definition not found for ' + graphName);
} else {
return graph[0];
}
});
};
WorkflowApiService.prototype.cancelTaskGraph = function (graphId) {
return taskgraphService.workflowsAction(graphId, 'cancel');
};
WorkflowApiService.prototype.deleteTaskGraph = function (graphId) {
return taskgraphService.workflowsDeleteByInstanceId(graphId);
};
WorkflowApiService.prototype.defineTaskGraph = function (definition) {
// Do validation before persisting a definition
return taskgraphService.workflowsPutGraphs(definition);
};
WorkflowApiService.prototype.defineTask = function (definition) {
return taskgraphService.workflowsPutTask(definition);
};
WorkflowApiService.prototype.getWorkflowsTasksByName = function (injectableName) {
return taskgraphService.workflowsGetTasksByName(injectableName)
.then(function (data) {
return data[0];
});
};
WorkflowApiService.prototype.deleteWorkflowsTasksByName = function (injectableName) {
return taskgraphService.workflowsDeleteTasksByName(injectableName);
};
WorkflowApiService.prototype.getGraphDefinitions = function (injectableName) {
if (injectableName) {
return taskgraphService.workflowsGetGraphsByName(injectableName);
}
return taskgraphService.workflowsGetGraphs();
};
WorkflowApiService.prototype.getTaskDefinitions = function () {
return taskgraphService.workflowsGetAllTasks();
};
WorkflowApiService.prototype.findActiveGraphForTarget = function (target) {
return taskgraphService.workflowsGet({
where: {
node: target,
_status: Constants.Task.ActiveStates
}
})
.then(function(graphs) {
assert.ok(graphs.length === 0 || graphs.length === 1);
return _.first(graphs);
});
};
WorkflowApiService.prototype.getWorkflowsByNodeId = function (id, query) {
var nodeId = ({ node: id });
var mergedQuery = _.merge({}, nodeId, query);
return taskgraphService.workflowsGet({where: mergedQuery});
};
WorkflowApiService.prototype.getAllWorkflows = function (where) {
var query = {
where: where
};
return taskgraphService.workflowsGet(query);
};
WorkflowApiService.prototype.getWorkflowByInstanceId = function (instanceId) {
return taskgraphService.workflowsGet({where: {instanceId: instanceId}})
.then(function(result) {
if(_.isEmpty(result)) {
throw new Errors.NotFoundError('Graph instance not found for ' + instanceId);
}
return result[0];
});
};
WorkflowApiService.prototype.destroyGraphDefinition = function (injectableName) {
return taskgraphService.workflowsDeleteGraphsByName(injectableName);
};
return new WorkflowApiService();
}