-
Notifications
You must be signed in to change notification settings - Fork 52
/
index.ts
278 lines (246 loc) · 9.04 KB
/
index.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
/*
* Copyright (c) 2018-2021 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
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
import {
devfileToDevWorkspace,
devWorkspaceToDevfile,
IDevWorkspace,
IDevWorkspaceDevfile,
} from '@eclipse-che/devworkspace-client';
import { attributesToType, typeToAttributes } from '../storageTypes';
import { DevWorkspaceStatus, WorkspaceStatus } from '../helpers/types';
import { DEVWORKSPACE_NEXT_START_ANNOTATION } from '../workspace-client/devWorkspaceClient';
const ROUTING_CLASS = 'che';
export interface Workspace {
readonly ref: che.Workspace | IDevWorkspace;
readonly id: string;
name: string;
readonly namespace: string;
readonly infrastructureNamespace: string;
readonly created: number;
readonly updated: number;
status: WorkspaceStatus | DevWorkspaceStatus;
readonly ideUrl?: string;
devfile: che.WorkspaceDevfile | IDevWorkspaceDevfile;
storageType: che.WorkspaceStorageType;
readonly projects: string[];
readonly isStarting: boolean;
readonly isStopped: boolean;
readonly isStopping: boolean;
readonly isRunning: boolean;
readonly hasError: boolean;
}
export class WorkspaceAdapter<T extends che.Workspace | IDevWorkspace> implements Workspace {
private readonly workspace: T;
constructor(workspace: T) {
if (isWorkspaceV1(workspace) || isWorkspaceV2(workspace)) {
this.workspace = workspace;
} else {
console.error('Unexpected workspace object shape:', workspace);
throw new Error('Unexpected workspace object shape.');
}
}
get ref(): T {
return this.workspace;
}
get id(): string {
if (isWorkspaceV1(this.workspace)) {
return this.workspace.id;
} else {
return (this.workspace as IDevWorkspace).status.devworkspaceId;
}
}
get name(): string {
if (isWorkspaceV1(this.workspace)) {
return this.workspace.devfile.metadata.name || '';
} else {
return (this.workspace as IDevWorkspace).metadata.name;
}
}
set name(name: string) {
if (isWorkspaceV1(this.workspace)) {
this.workspace.devfile.metadata.name = name;
} else {
console.error('Not implemented: set name of the devworkspace.');
}
}
get namespace(): string {
if (isWorkspaceV1(this.workspace)) {
return this.workspace.namespace || '';
} else {
return (this.workspace as IDevWorkspace).metadata.namespace;
}
}
get infrastructureNamespace(): string {
if (isWorkspaceV1(this.workspace)) {
return this.workspace.attributes?.infrastructureNamespace || '';
} else {
return (this.workspace as IDevWorkspace).metadata.namespace;
}
}
get created(): number {
if (isWorkspaceV1(this.workspace)) {
return parseInt(this.workspace.attributes?.created || '', 10) || 0;
} else {
const reference = this.workspace as IDevWorkspace;
const timestamp = parseInt(reference.metadata.creationTimestamp || '0', 10);
if (!timestamp) {
return 0;
}
return new Date(timestamp).valueOf();
}
}
get updated(): number {
if (isWorkspaceV1(this.workspace)) {
return parseInt(this.workspace.attributes?.updated || '', 10) || 0;
} else {
return this.created;
}
}
get status(): WorkspaceStatus | DevWorkspaceStatus {
if (isWorkspaceV1(this.workspace)) {
return this.workspace.status as WorkspaceStatus;
} else {
return (this.workspace as IDevWorkspace).status?.phase as DevWorkspaceStatus;
}
}
get isStarting(): boolean {
if (isWorkspaceV1(this.workspace)) {
return this.workspace.status as WorkspaceStatus === WorkspaceStatus.STARTING;
} else {
return (this.workspace as IDevWorkspace).status?.phase as DevWorkspaceStatus === DevWorkspaceStatus.STARTING;
}
}
get isStopped(): boolean {
if (isWorkspaceV1(this.workspace)) {
return this.workspace.status as WorkspaceStatus === WorkspaceStatus.STOPPED;
} else {
return (this.workspace as IDevWorkspace).status?.phase as DevWorkspaceStatus === DevWorkspaceStatus.STOPPED;
}
}
get isStopping(): boolean {
if (isWorkspaceV1(this.workspace)) {
return this.workspace.status as WorkspaceStatus === WorkspaceStatus.STOPPING;
} else {
return (this.workspace as IDevWorkspace).status?.phase as DevWorkspaceStatus === DevWorkspaceStatus.STOPPING;
}
}
get isRunning(): boolean {
if (isWorkspaceV1(this.workspace)) {
return this.workspace.status as WorkspaceStatus === WorkspaceStatus.RUNNING;
} else {
return (this.workspace as IDevWorkspace).status?.phase as DevWorkspaceStatus === DevWorkspaceStatus.RUNNING;
}
}
get hasError(): boolean {
if (isWorkspaceV1(this.workspace)) {
return this.workspace.status as WorkspaceStatus === WorkspaceStatus.ERROR;
} else {
return (this.workspace as IDevWorkspace).status?.phase as DevWorkspaceStatus === DevWorkspaceStatus.FAILED;
}
}
get ideUrl(): string | undefined {
if (isWorkspaceV1(this.workspace)) {
const runtime = this.workspace.runtime;
if (!runtime || !runtime.machines) {
return;
}
for (const machineName of Object.keys(runtime.machines)) {
const servers = runtime.machines[machineName].servers || {};
for (const serverId of Object.keys(servers)) {
const attributes = (servers[serverId] as any).attributes;
if (attributes && attributes['type'] === 'ide') {
return servers[serverId].url;
}
}
}
} else {
return (this.workspace as IDevWorkspace).status.mainUrl;
}
}
get storageType(): che.WorkspaceStorageType {
if (isWorkspaceV1(this.workspace)) {
return attributesToType(this.workspace.devfile.attributes);
} else {
console.error('Not implemented: get storage type of the devworkspace.');
return attributesToType(undefined);
}
}
set storageType(type: che.WorkspaceStorageType) {
if (isWorkspaceV1(this.workspace)) {
const attributes = typeToAttributes(type);
if (!this.workspace.devfile.attributes) {
this.workspace.devfile.attributes = {};
} else {
delete this.workspace.devfile.attributes.asyncPersist;
delete this.workspace.devfile.attributes.persistVolumes;
}
if (attributes) {
Object.assign(this.workspace.devfile.attributes, attributes);
}
if (Object.keys(this.workspace.devfile.attributes).length === 0) {
delete this.workspace.devfile.attributes;
}
} else {
console.error('Not implemented: set storage type of the devworkspace.');
}
}
get devfile(): che.WorkspaceDevfile | IDevWorkspaceDevfile {
if (isWorkspaceV1(this.workspace)) {
return this.workspace.devfile as T extends che.Workspace ? che.WorkspaceDevfile : IDevWorkspaceDevfile;
} else {
const currentWorkspace = this.workspace as IDevWorkspace;
if (currentWorkspace.metadata.annotations && currentWorkspace.metadata.annotations[DEVWORKSPACE_NEXT_START_ANNOTATION]) {
return devWorkspaceToDevfile(JSON.parse(currentWorkspace.metadata.annotations[DEVWORKSPACE_NEXT_START_ANNOTATION]));
} else {
return devWorkspaceToDevfile(currentWorkspace) as T extends che.Workspace ? che.WorkspaceDevfile : IDevWorkspaceDevfile;
}
}
}
set devfile(devfile: che.WorkspaceDevfile | IDevWorkspaceDevfile) {
if (isWorkspaceV1(this.workspace)) {
this.workspace.devfile = devfile as che.WorkspaceDevfile;
} else {
(this.workspace as IDevWorkspace) = devfileToDevWorkspace(
devfile as IDevWorkspaceDevfile,
ROUTING_CLASS,
this.status === DevWorkspaceStatus.RUNNING
);
}
}
get projects(): string[] {
if (isWorkspaceV1(this.workspace)) {
return (this.workspace.devfile.projects || [])
.map(project => project.name);
} else {
const devfile = devWorkspaceToDevfile(this.workspace as IDevWorkspace);
return (devfile.projects || [])
.map(project => project.name);
}
}
}
export function convertWorkspace<T extends che.Workspace | IDevWorkspace>(workspace: T): Workspace {
return new WorkspaceAdapter(workspace);
}
export function isWorkspaceV1(workspace: che.Workspace | IDevWorkspace): workspace is che.Workspace {
return (workspace as che.Workspace).id !== undefined
&& (workspace as che.Workspace).devfile !== undefined
&& (workspace as che.Workspace).status !== undefined;
}
export function isWorkspaceV2(workspace: che.Workspace | IDevWorkspace): workspace is IDevWorkspace {
return (workspace as IDevWorkspace).kind === 'DevWorkspace';
}
export function isDevfileV1(devfile: che.WorkspaceDevfile | IDevWorkspaceDevfile): devfile is che.WorkspaceDevfile {
return !isDevfileV2(devfile);
}
export function isDevfileV2(devfile: che.WorkspaceDevfile | IDevWorkspaceDevfile): devfile is IDevWorkspaceDevfile {
return (devfile as IDevWorkspaceDevfile).schemaVersion !== undefined;
}