-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
che-remote-workspace.ts
93 lines (77 loc) · 2.95 KB
/
che-remote-workspace.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
/*
* Copyright (c) 2015-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*/
'use strict';
/**
* This class is handling the call to remote API
* @author Florent Benoit
*/
export class CheRemoteWorkspace {
/**
* Default constructor that is using resource
*/
constructor($resource, $q, cheWebsocket, authData) {
this.$resource = $resource;
this.$q = $q;
this.cheWebsocket = cheWebsocket;
this.authData = authData;
// remote call
this.remoteWorkspaceAPI = this.$resource('', {}, {
getDetails: {method: 'GET', url: authData.url + '/api/workspace/:workspaceId?token=' + authData.token},
getMachineToken: {method: 'GET', url: authData.url + '/api/machine/token/:workspaceId?token=' + authData.token},
create: {method: 'POST', url: authData.url + '/api/workspace?account=:accountId&token=' + authData.token},
startWorkspace: {method: 'POST', url : authData.url + '/api/workspace/:workspaceId/runtime?environment=:envName&token=' + authData.token}
}
);
}
createWorkspaceFromConfig(accountId, workspaceConfig) {
return this.remoteWorkspaceAPI.create({accountId : accountId}, workspaceConfig).$promise;
}
/**
* Provides machine token for given workspace
* @param workspaceId the ID of the workspace
* @returns {*}
*/
getMachineToken(workspaceId) {
let deferred = this.$q.defer();
let deferredPromise = deferred.promise;
let promise = this.remoteWorkspaceAPI.getMachineToken({workspaceId: workspaceId}, {}).$promise;
promise.then((workspace) => {
deferred.resolve(workspace);
}, (error) => {
deferred.reject(error);
});
return deferredPromise;
}
/**
* Starts the given workspace by specifying the ID and the environment name
* @param workspaceId the workspace ID
* @param envName the name of the environment
* @returns {*} promise
*/
startWorkspace(remoteWsURL, workspaceId, envName) {
let deferred = this.$q.defer();
let deferredPromise = deferred.promise;
let bus = this.cheWebsocket.getRemoteBus(remoteWsURL);
// subscribe to workspace events
bus.subscribe('workspace:' + workspaceId, (message) => {
if (message.status === 'RUNNING' && message.prevStatus === 'STARTING' && message.workspaceId === workspaceId) {
let promise = this.remoteWorkspaceAPI.getDetails({workspaceId: workspaceId}, {}).$promise;
promise.then((workspace) => {
deferred.resolve(workspace);
}, (error) => {
deferred.reject(error);
});
}
});
let promise = this.remoteWorkspaceAPI.startWorkspace({workspaceId: workspaceId, envName : envName}, {}).$promise;
return deferredPromise;
}
}