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
);
53 reject(xhr
.responseText
|| 'An error occurred. Check your logs for more information');
57 loading
: Alt
.actions
.global
.openAccountSocketLoading
,
58 success
: Alt
.actions
.global
.openAccountSocketSuccess
,
59 error
: Alt
.actions
.global
.openAccountSocketError
62 remote: function(state
, accounts
) {
63 return new Promise(function(resolve
, reject
) {
64 let Refreshing
= syncRefresh();
66 function* syncRefresh() {
67 for (let t
of accounts
) {
68 if (t
.data
.constructor.name
== "Array") {
69 for (let u
of t
.data
) {
70 console
.log('Refreshing: ' + u
.name
);
71 yield refreshStatus(t
.type
, u
.name
);
78 function refreshStatus(type
, name
) {
79 let url
= type
+ '/' + name
+ '/refresh?api_server=' + API_SERVER
;
80 setTimeout(function(){
86 beforeSend
: Utils
.addAuthorizationStub
,
97 loading
: Alt
.actions
.global
.refreshAllAccountsLoading
,
98 success
: Alt
.actions
.global
.refreshAllAccountsSuccess
,
99 error
: Alt
.actions
.global
.refreshAllAccountsError
102 remote: function(state
, name
, type
) {
103 return new Promise(function(resolve
, reject
) {
105 url
: type
+ '/' + name
+ '/refresh?api_server=' + API_SERVER
,
107 beforeSend
: Utils
.addAuthorizationStub
,
108 success: function(account
) {
114 success
: Alt
.actions
.global
.refreshCloudAccountSuccess
,
115 error
: Alt
.actions
.global
.refreshCloudAccountFail
118 remote: function(state
, account
, type
, cb
) {
119 delete account
['connection-status'];
120 return new Promise(function(resolve
, reject
) {
122 url
: type
+ '?api_server=' + API_SERVER
,
124 beforeSend
: Utils
.addAuthorizationStub
,
125 data
: JSON
.stringify(account
),
126 contentType
: "application/json",
127 success: function(data
) {
130 error: function(error
) {
131 console
.log("There was an error creating the account: ", arguments
);
134 }).fail(function(xhr
){
135 //Authentication and the handling of fail states should be wrapped up into a connection class.
136 Utils
.checkAuthentication(xhr
.status
);
137 reject(xhr
.responseText
|| 'An error occurred. Check your logs for more information');
142 success
: Alt
.actions
.global
.createAccountSuccess
,
143 loading
: Alt
.actions
.global
.createAccountLoading
,
144 error
: Alt
.actions
.global
.createAccountFail
147 remote: function(state
, account
, type
, cb
) {
148 var payload
= Object
.assign({}, account
);
149 delete payload
['connection-status'];
150 delete payload
['params'];
151 delete payload
['pools'];
153 (payload
.nestedParams
== null) &&
154 delete payload
.nestedParams
157 payload
.nestedParams
&&
158 payload
.nestedParams
['container-name'] &&
159 delete payload
[payload
.nestedParams
['container-name']] &&
160 delete payload
.nestedParams
164 return new Promise(function(resolve
, reject
) {
166 url
: type
+ '/' + account
.name
+ '?api_server=' + API_SERVER
,
168 beforeSend
: Utils
.addAuthorizationStub
,
169 data
: JSON
.stringify(payload
),
170 contentType
: "application/json",
171 success: function(data
) {
174 error: function(error
) {
175 console
.log("There was an error updating the account: ", arguments
);
178 }).fail(function(xhr
){
179 //Authentication and the handling of fail states should be wrapped up into a connection class.
180 Utils
.checkAuthentication(xhr
.status
);
181 reject(xhr
.responseText
|| 'An error occurred. Check your logs for more information');
186 interceptResponse
: interceptResponse({
187 'error': 'There was an error updating the account.'
189 success
: Alt
.actions
.global
.createAccountSuccess
,
190 loading
: Alt
.actions
.global
.createAccountLoading
,
191 error
: Alt
.actions
.global
.showNotification
194 remote: function(state
, type
, name
, cb
) {
195 return new Promise(function(resolve
, reject
) {
197 url
: type
+ '/' + name
+ '/?api_server=' + API_SERVER
,
200 beforeSend
: Utils
.addAuthorizationStub
,
201 success: function(data
) {
202 console
.log('Account deleted');
205 }).fail(function(xhr
){
206 //Authentication and the handling of fail states should be wrapped up into a connection class.
207 Utils
.checkAuthentication(xhr
.status
);
208 reject(xhr
.responseText
|| 'An error occurred. Check your logs for more information');
212 interceptResponse
: interceptResponse({
213 'error': 'Something went wrong while trying to delete the account. Check the error logs for more information' }),
214 success
: Alt
.actions
.global
.deleteAccountSuccess
,
215 loading
: Alt
.actions
.global
.deleteAccountLoading
,
216 error
: Alt
.actions
.global
.showNotification
218 getResourceOrchestrator
: {
220 return new Promise(function(resolve
, reject
) {
222 url
: 'passthrough/data/api/running/resource-orchestrator' + '?api_server=' + API_SERVER
,
224 beforeSend
: Utils
.addAuthorizationStub
,
225 contentType
: "application/json",
226 success: function(data
) {
228 if (data
.hasOwnProperty("rw-launchpad:resource-orchestrator")) {
233 resolve(returnedData
);
235 error: function(error
) {
236 console
.log("There was an error updating the account: ", arguments
);
239 }).fail(function(xhr
){
240 //Authentication and the handling of fail states should be wrapped up into a connection class.
241 Utils
.checkAuthentication(xhr
.status
);
242 return reject('error');
246 interceptResponse
: interceptResponse({
247 'error': 'There was an error retrieving the resource orchestrator information.'
249 success
: Alt
.actions
.global
.getResourceOrchestratorSuccess
,
250 loading
: Alt
.actions
.global
.showScreenLoader
,
251 error
: Alt
.actions
.global
.showNotification
256 function interceptResponse (responses
) {
257 return function(data
, action
, args
) {
258 if(responses
.hasOwnProperty(data
)) {