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',
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
+ '/' + encodeURIComponent(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
+ '/' + encodeURIComponent(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 var payload
= account
;
121 var accountKey
= payload
.hasOwnProperty('account-type') ? 'account-type' : 'ro-account-type';
122 var payloadKeys
= Object
.keys(payload
[payload
[accountKey
]]);
123 var accountData
= payload
[payload
[accountKey
]];
124 payloadKeys
.map(function(k
) {
125 if (!accountData
[k
] || accountData
[k
].trim() == '') {
126 delete payload
[payload
[accountKey
]][k
];
129 return new Promise(function(resolve
, reject
) {
131 url
: type
+ '?api_server=' + API_SERVER
,
133 beforeSend
: Utils
.addAuthorizationStub
,
134 data
: JSON
.stringify(payload
),
135 contentType
: "application/json",
136 success: function(data
) {
139 error: function(error
) {
140 console
.log("There was an error creating the account: ", arguments
);
143 }).fail(function(xhr
){
144 //Authentication and the handling of fail states should be wrapped up into a connection class.
145 Utils
.checkAuthentication(xhr
.status
);
146 reject(xhr
.responseText
|| 'An error occurred. Check your logs for more information');
151 success
: Alt
.actions
.global
.createAccountSuccess
,
152 loading
: Alt
.actions
.global
.createAccountLoading
,
153 error
: Alt
.actions
.global
.createAccountFail
156 remote: function(state
, account
, type
, cb
) {
157 var payload
= Object
.assign({}, account
);
158 delete payload
["instance-ref-count"]
159 delete payload
['connection-status'];
160 delete payload
['params'];
161 delete payload
['pools'];
162 delete payload
['datacenters'];
163 delete payload
['config-data'];
165 (payload
.nestedParams
== null) &&
166 delete payload
.nestedParams
169 payload
.nestedParams
&&
170 payload
.nestedParams
['container-name'] &&
171 delete payload
[payload
.nestedParams
['container-name']] &&
172 delete payload
.nestedParams
175 var payloadKeys
= Object
.keys(payload
[payload
['account-type']]);
176 var accountData
= payload
[payload
['account-type']];
177 payloadKeys
.map(function(k
) {
178 if (!accountData
[k
] || (accountData
[k
].trim
&& accountData
[k
].trim() == '')) {
179 delete payload
[payload
['account-type']][k
];
183 return new Promise(function(resolve
, reject
) {
185 url
: type
+ '/' + encodeURIComponent(account
.name
) + '?api_server=' + API_SERVER
,
187 beforeSend
: Utils
.addAuthorizationStub
,
188 data
: JSON
.stringify(payload
),
189 contentType
: "application/json",
190 success: function(data
) {
193 error: function(error
) {
194 console
.log("There was an error updating the account: ", arguments
);
196 }).fail(function(xhr
){
197 //Authentication and the handling of fail states should be wrapped up into a connection class.
198 Utils
.checkAuthentication(xhr
.status
);
199 reject(xhr
.responseText
|| 'An error occurred. Check your logs for more information');
204 interceptResponse
: interceptResponse({
205 'error': 'There was an error updating the account.'
207 success
: Alt
.actions
.global
.createAccountSuccess
,
208 loading
: Alt
.actions
.global
.createAccountLoading
,
209 error
: Alt
.actions
.global
.createAccountFail
212 remote: function(state
, type
, name
, cb
) {
213 return new Promise(function(resolve
, reject
) {
215 url
: type
+ '/' + encodeURIComponent(name
) + '/?api_server=' + API_SERVER
,
218 beforeSend
: Utils
.addAuthorizationStub
,
219 success: function(data
) {
220 console
.log('Account deleted');
223 }).fail(function(xhr
){
224 //Authentication and the handling of fail states should be wrapped up into a connection class.
225 Utils
.checkAuthentication(xhr
.status
);
226 reject(xhr
.responseText
|| 'An error occurred. Check your logs for more information');
230 interceptResponse
: interceptResponse({
231 'error': 'Something went wrong while trying to delete the account. Check the error logs for more information' }),
232 success
: Alt
.actions
.global
.deleteAccountSuccess
,
233 loading
: Alt
.actions
.global
.deleteAccountLoading
,
234 error
: Alt
.actions
.global
.deleteAccountFail
239 function interceptResponse (responses
) {
240 return function(data
, action
, args
) {
241 if(responses
.hasOwnProperty(data
)) {