|
10 | 10 | * Red Hat, Inc. - initial API and implementation
|
11 | 11 | */
|
12 | 12 |
|
13 |
| -import { che as chetypes } from '@eclipse-che/api' |
14 | 13 | import axios, { AxiosInstance } from 'axios'
|
15 |
| -import { cli } from 'cli-ux' |
16 | 14 | import * as https from 'https'
|
17 | 15 |
|
18 | 16 | import { newError, sleep } from '../util'
|
@@ -106,237 +104,6 @@ export class CheApiClient {
|
106 | 104 | return response.data.status
|
107 | 105 | }
|
108 | 106 |
|
109 |
| - async startCheServerShutdown(accessToken = '', responseTimeoutMs = this.defaultCheResponseTimeoutMs): Promise<void> { |
110 |
| - const endpoint = `${this.cheApiEndpoint}/system/stop?shutdown=true` |
111 |
| - const headers = accessToken ? { Authorization: accessToken } : null |
112 |
| - let response = null |
113 |
| - try { |
114 |
| - response = await this.axios.post(endpoint, null, { headers, timeout: responseTimeoutMs }) |
115 |
| - } catch (error) { |
116 |
| - if (error.response && error.response.status === 409) { |
117 |
| - return |
118 |
| - } else { |
119 |
| - throw this.getCheApiError(error, endpoint) |
120 |
| - } |
121 |
| - } |
122 |
| - if (!response || response.status !== 204) { |
123 |
| - throw new Error('E_BAD_RESP_CHE_API') |
124 |
| - } |
125 |
| - } |
126 |
| - |
127 |
| - async waitUntilCheServerReadyToShutdown(intervalMs = 500, timeoutMs = 60000): Promise<void> { |
128 |
| - const iterations = timeoutMs / intervalMs |
129 |
| - for (let index = 0; index < iterations; index++) { |
130 |
| - const status = await this.getCheServerStatus() |
131 |
| - if (status === 'READY_TO_SHUTDOWN') { |
132 |
| - return |
133 |
| - } |
134 |
| - await cli.wait(intervalMs) |
135 |
| - } |
136 |
| - throw new Error('ERR_TIMEOUT') |
137 |
| - } |
138 |
| - |
139 |
| - /** |
140 |
| - * Returns list of all workspaces of the user. |
141 |
| - */ |
142 |
| - async getAllWorkspaces(accessToken?: string): Promise<chetypes.workspace.Workspace[]> { |
143 |
| - const all: chetypes.workspace.Workspace[] = [] |
144 |
| - const itemsPerPage = 30 |
145 |
| - |
146 |
| - let skipCount = 0 |
147 |
| - let workspaces: chetypes.workspace.Workspace[] |
148 |
| - do { |
149 |
| - workspaces = await this.getWorkspaces(skipCount, itemsPerPage, accessToken) |
150 |
| - all.push(...workspaces) |
151 |
| - skipCount += workspaces.length |
152 |
| - } while (workspaces.length === itemsPerPage) |
153 |
| - |
154 |
| - return all |
155 |
| - } |
156 |
| - |
157 |
| - /** |
158 |
| - * Returns list of workspaces in given range. |
159 |
| - * If lst of all workspaces is needed, getAllWorkspaces should be used insted. |
160 |
| - */ |
161 |
| - async getWorkspaces(skipCount = 0, maxItems = 30, accessToken?: string): Promise<chetypes.workspace.Workspace[]> { |
162 |
| - const endpoint = `${this.cheApiEndpoint}/workspace?skipCount=${skipCount}&maxItems=${maxItems}` |
163 |
| - const headers: any = { 'Content-Type': 'text/yaml' } |
164 |
| - if (accessToken && accessToken.length > 0) { |
165 |
| - headers.Authorization = accessToken |
166 |
| - } |
167 |
| - |
168 |
| - try { |
169 |
| - const response = await this.axios.get(endpoint, { headers }) |
170 |
| - if (response && response.data) { |
171 |
| - return response.data |
172 |
| - } else { |
173 |
| - throw new Error('E_BAD_RESP_CHE_SERVER') |
174 |
| - } |
175 |
| - } catch (error) { |
176 |
| - throw this.getCheApiError(error, endpoint) |
177 |
| - } |
178 |
| - } |
179 |
| - |
180 |
| - async getWorkspaceById(workspaceId: string, accessToken?: string): Promise<chetypes.workspace.Workspace> { |
181 |
| - const endpoint = `${this.cheApiEndpoint}/workspace/${workspaceId}` |
182 |
| - const headers: any = { 'Content-Type': 'text/yaml' } |
183 |
| - if (accessToken) { |
184 |
| - headers.Authorization = accessToken |
185 |
| - } |
186 |
| - |
187 |
| - try { |
188 |
| - const response = await this.axios.get(endpoint, { headers }) |
189 |
| - return response.data |
190 |
| - } catch (error) { |
191 |
| - if (error.response.status === 404) { |
192 |
| - throw new Error(`Workspace ${workspaceId} not found. Please use the command workspace:list to get list of the existed workspaces.`) |
193 |
| - } |
194 |
| - throw this.getCheApiError(error, endpoint) |
195 |
| - } |
196 |
| - } |
197 |
| - |
198 |
| - async deleteWorkspaceById(workspaceId: string, accessToken?: string): Promise<void> { |
199 |
| - const endpoint = `${this.cheApiEndpoint}/workspace/${workspaceId}` |
200 |
| - const headers: any = {} |
201 |
| - if (accessToken) { |
202 |
| - headers.Authorization = accessToken |
203 |
| - } |
204 |
| - |
205 |
| - try { |
206 |
| - await this.axios.delete(endpoint, { headers }) |
207 |
| - } catch (error) { |
208 |
| - if (error.response.status === 404) { |
209 |
| - throw new Error(`Workspace ${workspaceId} not found. Please use the command workspace:list to get list of the existed workspaces.`) |
210 |
| - } else if (error.response.status === 409) { |
211 |
| - throw new Error('Cannot delete a running workspace. Please stop it using the command workspace:stop and try again') |
212 |
| - } |
213 |
| - throw this.getCheApiError(error, endpoint) |
214 |
| - } |
215 |
| - } |
216 |
| - |
217 |
| - async startWorkspace(workspaceId: string, debug: boolean, accessToken?: string): Promise<void> { |
218 |
| - let endpoint = `${this.cheApiEndpoint}/workspace/${workspaceId}/runtime` |
219 |
| - if (debug) { |
220 |
| - endpoint += '?debug-workspace-start=true' |
221 |
| - } |
222 |
| - let response |
223 |
| - |
224 |
| - const headers: { [key: string]: string } = {} |
225 |
| - if (accessToken) { |
226 |
| - headers.Authorization = accessToken |
227 |
| - } |
228 |
| - try { |
229 |
| - response = await this.axios.post(endpoint, undefined, { headers }) |
230 |
| - } catch (error) { |
231 |
| - if (error.response && error.response.status === 404) { |
232 |
| - throw new Error(`E_WORKSPACE_NOT_EXIST - workspace with "${workspaceId}" id doesn't exist`) |
233 |
| - } else { |
234 |
| - throw this.getCheApiError(error, endpoint) |
235 |
| - } |
236 |
| - } |
237 |
| - |
238 |
| - this.checkResponse(response, endpoint) |
239 |
| - } |
240 |
| - |
241 |
| - async stopWorkspace(workspaceId: string, accessToken?: string): Promise<void> { |
242 |
| - const endpoint = `${this.cheApiEndpoint}/workspace/${workspaceId}/runtime` |
243 |
| - let response |
244 |
| - |
245 |
| - const headers: { [key: string]: string } = {} |
246 |
| - if (accessToken) { |
247 |
| - headers.Authorization = accessToken |
248 |
| - } |
249 |
| - try { |
250 |
| - response = await this.axios.delete(endpoint, { headers }) |
251 |
| - } catch (error) { |
252 |
| - if (error.response && error.response.status === 404) { |
253 |
| - throw new Error(`E_WORKSPACE_NOT_EXIST - workspace with "${workspaceId}" id doesn't exist`) |
254 |
| - } else { |
255 |
| - throw this.getCheApiError(error, endpoint) |
256 |
| - } |
257 |
| - } |
258 |
| - |
259 |
| - if (!response || response.status !== 204) { |
260 |
| - throw new Error('E_BAD_RESP_CHE_API') |
261 |
| - } |
262 |
| - } |
263 |
| - |
264 |
| - async createWorkspaceFromDevfile(devfileContent: string, accessToken?: string): Promise<chetypes.workspace.Workspace> { |
265 |
| - const endpoint = `${this.cheApiEndpoint}/workspace/devfile` |
266 |
| - const headers: any = { 'Content-Type': 'text/yaml' } |
267 |
| - if (accessToken) { |
268 |
| - headers.Authorization = accessToken |
269 |
| - } |
270 |
| - |
271 |
| - let response: any |
272 |
| - try { |
273 |
| - response = await this.axios.post(endpoint, devfileContent, { headers }) |
274 |
| - } catch (error) { |
275 |
| - if (error.response) { |
276 |
| - if (error.response.status === 400) { |
277 |
| - throw new Error(`E_BAD_DEVFILE_FORMAT - Message: ${error.response.data.message}`) |
278 |
| - } |
279 |
| - if (error.response.status === 409) { |
280 |
| - let message = '' |
281 |
| - if (error.response.data) { |
282 |
| - message = error.response.data.message |
283 |
| - } |
284 |
| - throw new Error(`E_CONFLICT - Message: ${message}`) |
285 |
| - } |
286 |
| - } |
287 |
| - |
288 |
| - throw this.getCheApiError(error, endpoint) |
289 |
| - } |
290 |
| - |
291 |
| - if (response && response.data) { |
292 |
| - return response.data as chetypes.workspace.Workspace |
293 |
| - } else { |
294 |
| - throw new Error('E_BAD_RESP_CHE_SERVER') |
295 |
| - } |
296 |
| - } |
297 |
| - |
298 |
| - /** |
299 |
| - * Returns Keycloak settings or undefined for single user mode. |
300 |
| - */ |
301 |
| - async getKeycloakSettings(responseTimeoutMs = this.defaultCheResponseTimeoutMs): Promise<any | undefined> { |
302 |
| - const endpoint = `${this.cheApiEndpoint}/keycloak/settings` |
303 |
| - let response |
304 |
| - try { |
305 |
| - response = await this.axios.get(endpoint, { timeout: responseTimeoutMs }) |
306 |
| - } catch (error) { |
307 |
| - if (error.response && error.response.status === 404) { |
308 |
| - return |
309 |
| - } |
310 |
| - throw this.getCheApiError(error, endpoint) |
311 |
| - } |
312 |
| - this.checkResponse(response, endpoint) |
313 |
| - if (!response.data['che.keycloak.token.endpoint']) { |
314 |
| - // The response is not keycloak response, but a default fallback |
315 |
| - throw new Error('E_BAD_CHE_API_URL') |
316 |
| - } |
317 |
| - return response.data |
318 |
| - } |
319 |
| - |
320 |
| - async isAuthenticationEnabled(responseTimeoutMs = this.defaultCheResponseTimeoutMs): Promise<boolean> { |
321 |
| - const endpoint = `${this.cheApiEndpoint}/keycloak/settings` |
322 |
| - let response |
323 |
| - try { |
324 |
| - response = await this.axios.get(endpoint, { timeout: responseTimeoutMs }) |
325 |
| - } catch (error) { |
326 |
| - if (error.response && (error.response.status === 404 || error.response.status === 503)) { |
327 |
| - return false |
328 |
| - } else { |
329 |
| - throw this.getCheApiError(error, endpoint) |
330 |
| - } |
331 |
| - } |
332 |
| - this.checkResponse(response, endpoint) |
333 |
| - if (!response.data['che.keycloak.token.endpoint']) { |
334 |
| - // The response is not keycloak response, but a default fallback |
335 |
| - return false |
336 |
| - } |
337 |
| - return true |
338 |
| - } |
339 |
| - |
340 | 107 | private checkResponse(response: any, endpoint?: string): void {
|
341 | 108 | if (!response || response.status !== 200 || !response.data) {
|
342 | 109 | throw new Error(`E_BAD_RESP_CHE_API - Response code: ${response.status}` + endpoint ? `, endpoint: ${endpoint}` : '')
|
|
0 commit comments