-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathflow_framework_routes_service.ts
196 lines (180 loc) · 5.55 KB
/
flow_framework_routes_service.ts
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { schema } from '@osd/config-schema';
import {
IRouter,
IOpenSearchDashboardsResponse,
RequestHandlerContext,
OpenSearchDashboardsRequest,
OpenSearchDashboardsResponseFactory,
} from '../../../../src/core/server';
import {
CREATE_WORKFLOW_NODE_API_PATH,
DELETE_WORKFLOW_NODE_API_PATH,
GET_WORKFLOW_NODE_API_PATH,
GET_WORKFLOW_STATE_NODE_API_PATH,
SEARCH_WORKFLOWS_NODE_API_PATH,
} from '../../common';
import { generateCustomError, getWorkflowsFromResponses } from './helpers';
/**
* Server-side routes to process flow-framework-related node API calls and execute the
* corresponding API calls against the OpenSearch cluster.
*/
export function registerFlowFrameworkRoutes(
router: IRouter,
flowFrameworkRoutesService: FlowFrameworkRoutesService
): void {
router.get(
{
path: `${GET_WORKFLOW_NODE_API_PATH}/{workflow_id}`,
validate: {
params: schema.object({
workflow_id: schema.string(),
}),
},
},
flowFrameworkRoutesService.getWorkflow
);
router.post(
{
path: SEARCH_WORKFLOWS_NODE_API_PATH,
validate: {
body: schema.any(),
},
},
flowFrameworkRoutesService.searchWorkflows
);
router.get(
{
path: `${GET_WORKFLOW_STATE_NODE_API_PATH}/{workflow_id}`,
validate: {
params: schema.object({
workflow_id: schema.string(),
}),
},
},
flowFrameworkRoutesService.getWorkflowState
);
router.post(
{
path: CREATE_WORKFLOW_NODE_API_PATH,
validate: {
body: schema.any(),
},
},
flowFrameworkRoutesService.createWorkflow
);
router.delete(
{
path: `${DELETE_WORKFLOW_NODE_API_PATH}/{workflow_id}`,
validate: {
params: schema.object({
workflow_id: schema.string(),
}),
},
},
flowFrameworkRoutesService.deleteWorkflow
);
}
export class FlowFrameworkRoutesService {
private client: any;
constructor(client: any) {
this.client = client;
}
// TODO: test e2e
getWorkflow = async (
context: RequestHandlerContext,
req: OpenSearchDashboardsRequest,
res: OpenSearchDashboardsResponseFactory
): Promise<IOpenSearchDashboardsResponse<any>> => {
const { workflow_id } = req.params as { workflow_id: string };
try {
const response = await this.client
.asScoped(req)
.callAsCurrentUser('flowFramework.getWorkflow', { workflow_id });
console.log('response from get workflow: ', response);
// TODO: format response
return res.ok({ body: response });
} catch (err: any) {
return generateCustomError(res, err);
}
};
// TODO: can remove or simplify if we can fetch all data from a single API call. Tracking issue:
// https://github.com/opensearch-project/flow-framework/issues/171
// Current implementation is making two calls and combining results via helper fn
searchWorkflows = async (
context: RequestHandlerContext,
req: OpenSearchDashboardsRequest,
res: OpenSearchDashboardsResponseFactory
): Promise<IOpenSearchDashboardsResponse<any>> => {
const body = req.body;
try {
const workflowsResponse = await this.client
.asScoped(req)
.callAsCurrentUser('flowFramework.searchWorkflows', { body });
const workflowHits = workflowsResponse.hits.hits as any[];
const workflowStatesResponse = await this.client
.asScoped(req)
.callAsCurrentUser('flowFramework.searchWorkflowState', { body });
const workflowStateHits = workflowStatesResponse.hits.hits as any[];
const workflowDict = getWorkflowsFromResponses(
workflowHits,
workflowStateHits
);
return res.ok({ body: { workflows: workflowDict } });
} catch (err: any) {
return generateCustomError(res, err);
}
};
// TODO: test e2e
getWorkflowState = async (
context: RequestHandlerContext,
req: OpenSearchDashboardsRequest,
res: OpenSearchDashboardsResponseFactory
): Promise<IOpenSearchDashboardsResponse<any>> => {
const { workflow_id } = req.params as { workflow_id: string };
try {
const response = await this.client
.asScoped(req)
.callAsCurrentUser('flowFramework.getWorkflowState', { workflow_id });
console.log('response from get workflow state: ', response);
// TODO: format response
return res.ok({ body: response });
} catch (err: any) {
return generateCustomError(res, err);
}
};
// TODO: test e2e
createWorkflow = async (
context: RequestHandlerContext,
req: OpenSearchDashboardsRequest,
res: OpenSearchDashboardsResponseFactory
): Promise<IOpenSearchDashboardsResponse<any>> => {
const body = req.body;
try {
const response = await this.client
.asScoped(req)
.callAsCurrentUser('flowFramework.createWorkflow', { body });
return res.ok({ body: { id: response._id } });
} catch (err: any) {
return generateCustomError(res, err);
}
};
deleteWorkflow = async (
context: RequestHandlerContext,
req: OpenSearchDashboardsRequest,
res: OpenSearchDashboardsResponseFactory
): Promise<IOpenSearchDashboardsResponse<any>> => {
const { workflow_id } = req.params as { workflow_id: string };
try {
const response = await this.client
.asScoped(req)
.callAsCurrentUser('flowFramework.deleteWorkflow', { workflow_id });
return res.ok({ body: { id: response._id } });
} catch (err: any) {
return generateCustomError(res, err);
}
};
}