This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathche-api-service.ts
347 lines (295 loc) · 12 KB
/
che-api-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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*********************************************************************
* Copyright (c) 2018 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
import { CheApiService, Preferences, WorkspaceSettings } from '../common/che-protocol';
import { che as cheApi } from '@eclipse-che/api';
import WorkspaceClient, { IRestAPIConfig, IRemoteAPI } from '@eclipse-che/workspace-client';
import { injectable } from 'inversify';
import { SS_CRT_PATH } from './che-https';
import { TelemetryClient, Event, EventProperty } from '@eclipse-che/workspace-telemetry-client';
const ENV_WORKSPACE_ID_IS_NOT_SET = 'Environment variable CHE_WORKSPACE_ID is not set';
@injectable()
export class CheApiServiceImpl implements CheApiService {
private workspaceRestAPI: IRemoteAPI | undefined;
private telemetryClient: TelemetryClient | undefined = this.getWorkspaceTelemetryClient();
async getCurrentWorkspaceId(): Promise<string> {
return this.getWorkspaceIdFromEnv();
}
async getCheApiURI(): Promise<string | undefined> {
return process.env.CHE_API_INTERNAL;
}
async getUserId(): Promise<string> {
const cheApiClient = await this.getCheApiClient();
const user = await cheApiClient.getCurrentUser();
return user.id;
}
async getUserPreferences(filter?: string): Promise<Preferences> {
const cheApiClient = await this.getCheApiClient();
return cheApiClient.getUserPreferences(filter);
}
async updateUserPreferences(update: Preferences): Promise<Preferences> {
const cheApiClient = await this.getCheApiClient();
return cheApiClient.updateUserPreferences(update);
}
async replaceUserPreferences(preferences: Preferences): Promise<Preferences> {
const cheApiClient = await this.getCheApiClient();
return cheApiClient.replaceUserPreferences(preferences);
}
async deleteUserPreferences(list?: string[]): Promise<void> {
const cheApiClient = await this.getCheApiClient();
return cheApiClient.deleteUserPreferences(list);
}
async getWorkspaceSettings(): Promise<WorkspaceSettings> {
const cheApiClient = await this.getCheApiClient();
return cheApiClient.getSettings();
}
async currentWorkspace(): Promise<cheApi.workspace.Workspace> {
try {
const workspaceId = process.env.CHE_WORKSPACE_ID;
if (!workspaceId) {
return Promise.reject(ENV_WORKSPACE_ID_IS_NOT_SET);
}
const cheApiClient = await this.getCheApiClient();
if (cheApiClient) {
return await cheApiClient.getById<cheApi.workspace.Workspace>(workspaceId);
}
return Promise.reject('Cannot create Che API REST Client');
} catch (e) {
console.log(e);
return Promise.reject('Cannot create Che API REST Client');
}
}
async getWorkspaceById(workspaceId: string): Promise<cheApi.workspace.Workspace> {
try {
if (!workspaceId) {
return Promise.reject('Che Workspace id is not set');
}
const cheApiClient = await this.getCheApiClient();
if (cheApiClient) {
return await cheApiClient.getById<cheApi.workspace.Workspace>(workspaceId);
}
return Promise.reject('Cannot create Che API REST Client');
} catch (e) {
console.log(e);
return Promise.reject('Cannot create Che API REST Client');
}
}
async updateWorkspace(workspaceId: string, workspace: cheApi.workspace.Workspace): Promise<cheApi.workspace.Workspace> {
try {
if (!workspaceId) {
return Promise.reject('Che Workspace id is not set');
}
const cheApiClient = await this.getCheApiClient();
if (cheApiClient) {
return await cheApiClient.update(workspaceId, workspace);
}
return Promise.reject('Cannot create Che API REST Client');
} catch (e) {
console.log(e);
return Promise.reject('Cannot create Che API REST Client');
}
}
async stop(): Promise<void> {
const workspaceId = process.env.CHE_WORKSPACE_ID;
if (!workspaceId) {
return Promise.reject(ENV_WORKSPACE_ID_IS_NOT_SET);
}
const cheApiClient = await this.getCheApiClient();
return await cheApiClient.stop(workspaceId);
}
async getCurrentWorkspacesContainers(): Promise<{ [key: string]: cheApi.workspace.Machine }> {
const result: { [key: string]: cheApi.workspace.Machine } = {};
try {
const workspace = await this.currentWorkspace();
const containers = workspace.runtime!.machines;
if (containers) {
for (const containerName of Object.keys(containers)) {
const container = containers[containerName];
if (container) {
result[containerName] = container;
}
}
}
} catch (e) {
throw new Error(`Unable to get workspace containers. Cause: ${e}`);
}
return result;
}
async findUniqueServerByAttribute(attributeName: string, attributeValue: string): Promise<cheApi.workspace.Server> {
const containers = await this.getCurrentWorkspacesContainers();
try {
if (containers) {
for (const containerName of Object.keys(containers)) {
const servers = containers[containerName].servers;
if (servers) {
for (const serverName of Object.keys(servers)) {
const server = servers[serverName];
if (server && server.attributes && server.attributes[attributeName] === attributeValue) {
return server;
}
}
}
}
}
return Promise.reject(`Server by attributes '${attributeName}'='${attributeValue}' was not found.`);
} catch (e) {
return Promise.reject(`Unable to get workspace servers. Cause: ${e}`);
}
}
async getFactoryById(factoryId: string): Promise<cheApi.factory.Factory> {
try {
const client = await this.getCheApiClient();
if (client) {
return await client.getFactory<cheApi.factory.Factory>(factoryId);
}
return Promise.reject(`Unable to get factory with ID ${factoryId}`);
} catch (e) {
return Promise.reject('Unable to create Che API REST Client');
}
}
async generateSshKey(service: string, name: string): Promise<cheApi.ssh.SshPair> {
try {
const client = await this.getCheApiClient();
if (client) {
return client.generateSshKey(service, name);
}
throw new Error(`Unable to generate SSH Key for ${service}:${name}`);
} catch (e) {
console.error(e);
throw new Error(e);
}
}
async createSshKey(sshKeyPair: cheApi.ssh.SshPair): Promise<void> {
try {
const client = await this.getCheApiClient();
if (client) {
return client.createSshKey(sshKeyPair);
}
throw new Error('Unable to create SSH Key');
} catch (e) {
console.error(e);
throw new Error(e);
}
}
async getSshKey(service: string, name: string): Promise<cheApi.ssh.SshPair> {
try {
const client = await this.getCheApiClient();
if (client) {
return await client.getSshKey(service, name);
}
throw new Error(`Unable to get SSH Key for ${service}:${name}`);
} catch (e) {
console.error(e);
throw new Error(e);
}
}
async getAllSshKey(service: string): Promise<cheApi.ssh.SshPair[]> {
try {
const client = await this.getCheApiClient();
if (client) {
return client.getAllSshKey(service);
}
throw new Error(`Unable to get SSH Keys for ${service}`);
} catch (e) {
console.error(e);
throw new Error(e);
}
}
async deleteSshKey(service: string, name: string): Promise<void> {
try {
const client = await this.getCheApiClient();
if (client) {
return client.deleteSshKey(service, name);
}
throw new Error(`Unable to delete SSH Key for ${service}:${name}`);
} catch (e) {
console.error(e);
throw new Error(e);
}
}
async submitTelemetryEvent(id: string, ownerId: string, ip: string, agent: string, resolution: string, properties: [string, string][]): Promise<void> {
try {
const event: Event = {
id: id,
ip: ip,
ownerId: ownerId,
agent: agent,
resolution: resolution,
properties: properties.map((prop: [string, string]) => {
const eventProp: EventProperty = {
id: prop[0],
value: prop[1]
};
return eventProp;
})
};
if (this.telemetryClient) {
await this.telemetryClient.event(event);
}
} catch (e) {
console.error(e);
throw new Error(e);
}
}
async submitTelemetryActivity(): Promise<void> {
try {
if (this.telemetryClient) {
await this.telemetryClient.activity();
}
} catch (e) {
console.error(e);
throw new Error(e);
}
}
async getOAuthToken(oAuthProvider: string): Promise<string | undefined> {
const cheApiClient = await this.getCheApiClient();
try {
return await cheApiClient.getOAuthToken(oAuthProvider);
} catch (e) {
return undefined;
}
}
private getWorkspaceTelemetryClient(): TelemetryClient | undefined {
if (!this.telemetryClient) {
const cheWorkspaceTelemetryBackendPortVar = process.env.CHE_WORKSPACE_TELEMETRY_BACKEND_PORT;
if (!cheWorkspaceTelemetryBackendPortVar) {
console.error('Unable to create Che API REST Client: "CHE_WORKSPACE_TELEMETRY_BACKEND_PORT" is not set.');
return undefined;
}
this.telemetryClient = new TelemetryClient(undefined, 'http://localhost:' + cheWorkspaceTelemetryBackendPortVar);
}
return this.telemetryClient;
}
private async getCheApiClient(): Promise<IRemoteAPI> {
const cheApiInternalVar = process.env.CHE_API_INTERNAL;
const cheMachineToken = process.env.CHE_MACHINE_TOKEN;
if (!cheApiInternalVar) {
return Promise.reject('Unable to create Che API REST Client: "CHE_API_INTERNAL" is not set.');
}
if (!this.workspaceRestAPI) {
const restAPIConfig: IRestAPIConfig = {
baseUrl: cheApiInternalVar,
headers: {}
};
if (cheMachineToken) {
restAPIConfig.headers['Authorization'] = 'Bearer ' + cheMachineToken;
}
restAPIConfig.ssCrtPath = SS_CRT_PATH;
this.workspaceRestAPI = await WorkspaceClient.getRestApi(restAPIConfig);
}
return this.workspaceRestAPI;
}
private getWorkspaceIdFromEnv(): string {
const workspaceId = process.env.CHE_WORKSPACE_ID;
if (!workspaceId) {
throw new Error(ENV_WORKSPACE_ID_IS_NOT_SET);
}
return workspaceId;
}
}