3 * Copyright 2016 RIFT.IO Inc
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 import $ from 'jquery';
19 var Utils
= require('utils/utils.js');
20 let API_SERVER
= require('utils/rw.js').getSearchParams(window
.location
).api_server
;
21 let HOST
= API_SERVER
;
22 let NODE_PORT
= require('utils/rw.js').getSearchParams(window
.location
).api_port
|| ((window
.location
.protocol
== 'https:') ? 8443 : 8000);
23 let DEV_MODE
= require('utils/rw.js').getSearchParams(window
.location
).dev_mode
|| false;
26 HOST
= window
.location
.protocol
+ '//' + window
.location
.hostname
;
30 module
.exports = function(Alt
) {
33 remote: function(state
) {
34 return new Promise(function(resolve
, reject
) {
35 //If socket connection already exists, eat the request.
37 console
.log('connection already exists')
38 return resolve(false);
41 url
: '/socket-polling?api_server=' + API_SERVER
,
43 beforeSend
: Utils
.addAuthorizationStub
,
45 url
: 'accounts/all?api_server=' + API_SERVER
47 success: function(data
, textStatus
, jqXHR
) {
48 Utils
.checkAndResolveSocketRequest(data
, resolve
, reject
);
50 }).fail(function(xhr
){
51 //Authentication and the handling of fail states should be wrapped up into a connection class.
52 Utils
.checkAuthentication(xhr
.status
);
56 loading
: Alt
.actions
.global
.openAccountSocketLoading
,
57 success
: Alt
.actions
.global
.openAccountSocketSuccess
,
58 error
: Alt
.actions
.global
.openAccountSocketError
61 remote: function(state
, accounts
) {
62 return new Promise(function(resolve
, reject
) {
63 let Refreshing
= syncRefresh();
65 function* syncRefresh() {
66 for (let t
of accounts
) {
67 if (t
.data
.constructor.name
== "Array") {
68 for (let u
of t
.data
) {
69 console
.log('Refreshing: ' + u
.name
);
70 yield refreshStatus(t
.type
, u
.name
);
77 function refreshStatus(type
, name
) {
78 let url
= type
+ '/' + name
+ '/refresh?api_server=' + API_SERVER
;
79 setTimeout(function(){
85 beforeSend
: Utils
.addAuthorizationStub
,
96 loading
: Alt
.actions
.global
.refreshAllAccountsLoading
,
97 success
: Alt
.actions
.global
.refreshAllAccountsSuccess
,
98 error
: Alt
.actions
.global
.refreshAllAccountsError
101 remote: function(state
, name
, type
) {
102 return new Promise(function(resolve
, reject
) {
104 url
: type
+ '/' + name
+ '/refresh?api_server=' + API_SERVER
,
106 beforeSend
: Utils
.addAuthorizationStub
,
107 success: function(account
) {
113 success
: Alt
.actions
.global
.refreshCloudAccountSuccess
,
114 error
: Alt
.actions
.global
.refreshCloudAccountFail
117 remote: function(state
, account
, type
, cb
) {
118 delete account
['connection-status'];
119 return new Promise(function(resolve
, reject
) {
121 url
: type
+ '?api_server=' + API_SERVER
,
123 beforeSend
: Utils
.addAuthorizationStub
,
124 data
: JSON
.stringify(account
),
125 contentType
: "application/json",
126 success: function(data
) {
129 error: function(error
) {
130 console
.log("There was an error creating the account: ", arguments
);
133 }).fail(function(xhr
){
134 //Authentication and the handling of fail states should be wrapped up into a connection class.
135 Utils
.checkAuthentication(xhr
.status
);
141 success
: Alt
.actions
.global
.createAccountSuccess
,
142 loading
: Alt
.actions
.global
.createAccountLoading
,
143 error
: Alt
.actions
.global
.createAccountFail
146 remote: function(state
, account
, type
, cb
) {
147 var payload
= Object
.assign({}, account
);
148 delete payload
['connection-status'];
149 delete payload
['params'];
150 delete payload
['pools'];
152 (payload
.nestedParams
== null) &&
153 delete payload
.nestedParams
156 payload
.nestedParams
&&
157 payload
.nestedParams
['container-name'] &&
158 delete payload
[payload
.nestedParams
['container-name']] &&
159 delete payload
.nestedParams
163 return new Promise(function(resolve
, reject
) {
165 url
: type
+ '/' + account
.name
+ '?api_server=' + API_SERVER
,
167 beforeSend
: Utils
.addAuthorizationStub
,
168 data
: JSON
.stringify(payload
),
169 contentType
: "application/json",
170 success: function(data
) {
173 error: function(error
) {
174 console
.log("There was an error updating the account: ", arguments
);
177 }).fail(function(xhr
){
178 //Authentication and the handling of fail states should be wrapped up into a connection class.
179 Utils
.checkAuthentication(xhr
.status
);
185 interceptResponse
: interceptResponse({
186 'error': 'There was an error updating the account.'
188 success
: Alt
.actions
.global
.createAccountSuccess
,
189 loading
: Alt
.actions
.global
.createAccountLoading
,
190 error
: Alt
.actions
.global
.showNotification
193 remote: function(state
, type
, name
, cb
) {
194 return new Promise(function(resolve
, reject
) {
196 url
: type
+ '/' + name
+ '/?api_server=' + API_SERVER
,
199 beforeSend
: Utils
.addAuthorizationStub
,
200 success: function(data
) {
201 console
.log('Account deleted');
204 }).fail(function(xhr
){
205 //Authentication and the handling of fail states should be wrapped up into a connection class.
206 Utils
.checkAuthentication(xhr
.status
);
211 interceptResponse
: interceptResponse({
212 'error': 'Something went wrong while trying to delete the account. Check the error logs for more information' }),
213 success
: Alt
.actions
.global
.deleteAccountSuccess
,
214 loading
: Alt
.actions
.global
.deleteAccountLoading
,
215 error
: Alt
.actions
.global
.showNotification
217 getResourceOrchestrator
: {
219 return new Promise(function(resolve
, reject
) {
221 url
: 'passthrough/data/api/running/resource-orchestrator' + '?api_server=' + API_SERVER
,
223 beforeSend
: Utils
.addAuthorizationStub
,
224 contentType
: "application/json",
225 success: function(data
) {
227 if (data
.hasOwnProperty("rw-launchpad:resource-orchestrator")) {
232 resolve(returnedData
);
234 error: function(error
) {
235 console
.log("There was an error updating the account: ", arguments
);
238 }).fail(function(xhr
){
239 //Authentication and the handling of fail states should be wrapped up into a connection class.
240 Utils
.checkAuthentication(xhr
.status
);
241 return reject('error');
245 interceptResponse
: interceptResponse({
246 'error': 'There was an error retrieving the resource orchestrator information.'
248 success
: Alt
.actions
.global
.getResourceOrchestratorSuccess
,
249 loading
: Alt
.actions
.global
.showScreenLoader
,
250 error
: Alt
.actions
.global
.showNotification
255 function interceptResponse (responses
) {
256 return function(data
, action
, args
) {
257 if(responses
.hasOwnProperty(data
)) {