Revert "RIFT-14989: Login fixes for Accounts and RO Config pages"
[osm/UI.git] / skyquake / plugins / accounts / src / account / accountSource.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
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;
24
25 if (DEV_MODE) {
26 HOST = window.location.protocol + '//' + window.location.hostname;
27 }
28
29
30 module.exports = function(Alt) {
31 return {
32 openAccountsSocket: {
33 remote: function(state) {
34 return new Promise(function(resolve, reject) {
35 //If socket connection already exists, eat the request.
36 if(state.socket) {
37 return resolve(false);
38 }
39 $.ajax({
40 url: '//' + window.location.hostname + ':' + NODE_PORT + '/socket-polling?api_server=' + API_SERVER ,
41 type: 'POST',
42 beforeSend: Utils.addAuthorizationStub,
43 data: {
44 url:window.location.protocol + '//' + window.location.host + '/accounts/all?api_server=' + API_SERVER
45 },
46 success: function(data, textStatus, jqXHR) {
47 Utils.checkAndResolveSocketRequest(data, resolve, reject);
48 }
49 }).fail(function(xhr){
50 //Authentication and the handling of fail states should be wrapped up into a connection class.
51 Utils.checkAuthentication(xhr.status);
52 });;
53 });
54 },
55 loading: Alt.actions.global.openAccountSocketLoading,
56 success: Alt.actions.global.openAccountSocketSuccess,
57 error: Alt.actions.global.openAccountSocketError
58 },
59 refreshAll: {
60 remote: function(state, accounts) {
61 return new Promise(function(resolve, reject) {
62 let Refreshing = syncRefresh();
63 Refreshing.next();
64 function* syncRefresh() {
65 for (let t of accounts) {
66 if (t.data.constructor.name == "Array") {
67 for (let u of t.data) {
68 console.log('Refreshing: ' + u.name);
69 yield refreshStatus(t.type, u.name);
70 }
71 }
72 }
73 resolve({})
74 }
75
76 function refreshStatus(type, name) {
77 let url = type + '/' + name + '/refresh?api_server=' + API_SERVER;
78 setTimeout(function(){
79 Refreshing.next();
80 },100);
81 $.ajax({
82 url: url,
83 type: 'POST',
84 beforeSend: Utils.addAuthorizationStub,
85 success: function() {
86
87 },
88 error: function() {
89 }
90 });
91
92 }
93 })
94 },
95 loading: Alt.actions.global.refreshAllAccountsLoading,
96 success: Alt.actions.global.refreshAllAccountsSuccess,
97 error: Alt.actions.global.refreshAllAccountsError
98 },
99 refreshAccount: {
100 remote: function(state, name, type) {
101 return new Promise(function(resolve, reject) {
102 $.ajax({
103 url: type + '/' + name + '/refresh?api_server=' + API_SERVER,
104 type: 'POST',
105 beforeSend: Utils.addAuthorizationStub,
106 success: function(account) {
107 resolve(account);
108 }
109 });
110 });
111 },
112 success: Alt.actions.global.refreshCloudAccountSuccess,
113 error: Alt.actions.global.refreshCloudAccountFail
114 },
115 create: {
116 remote: function(state, account, type, cb) {
117 delete account['connection-status'];
118 return new Promise(function(resolve, reject) {
119 $.ajax({
120 url: type + '?api_server=' + API_SERVER,
121 type:'POST',
122 beforeSend: Utils.addAuthorizationStub,
123 data: JSON.stringify(account),
124 contentType: "application/json",
125 success: function(data) {
126 resolve({data, cb});
127 },
128 error: function(error) {
129 console.log("There was an error creating the account: ", arguments);
130
131 }
132 }).fail(function(xhr){
133 //Authentication and the handling of fail states should be wrapped up into a connection class.
134 Utils.checkAuthentication(xhr.status);
135 reject();
136 });
137
138 });
139 },
140 success: Alt.actions.global.createAccountSuccess,
141 loading: Alt.actions.global.createAccountLoading,
142 error: Alt.actions.global.createAccountFail
143 },
144 update: {
145 remote: function(state, account, type, cb) {
146 var payload = Object.assign({}, account);
147 delete payload['connection-status'];
148 delete payload['params'];
149 delete payload['pools'];
150 (
151 (payload.nestedParams == null) &&
152 delete payload.nestedParams
153 ) ||
154 (
155 payload.nestedParams &&
156 payload.nestedParams['container-name'] &&
157 delete payload[payload.nestedParams['container-name']] &&
158 delete payload.nestedParams
159 );
160
161
162 return new Promise(function(resolve, reject) {
163 $.ajax({
164 url: type + '/' + account.name + '?api_server=' + API_SERVER,
165 type:'PUT',
166 beforeSend: Utils.addAuthorizationStub,
167 data: JSON.stringify(payload),
168 contentType: "application/json",
169 success: function(data) {
170 resolve({data, cb});
171 },
172 error: function(error) {
173 console.log("There was an error updating the account: ", arguments);
174
175 }
176 }).fail(function(xhr){
177 //Authentication and the handling of fail states should be wrapped up into a connection class.
178 Utils.checkAuthentication(xhr.status);
179 reject('error');
180 });
181
182 });
183 },
184 interceptResponse: interceptResponse({
185 'error': 'There was an error updating the account.'
186 }),
187 success: Alt.actions.global.createAccountSuccess,
188 loading: Alt.actions.global.createAccountLoading,
189 error: Alt.actions.global.showNotification
190 },
191 delete: {
192 remote: function(state, type, name, cb) {
193 return new Promise(function(resolve, reject) {
194 $.ajax({
195 url: type + '/' + name + '/?api_server=' + API_SERVER,
196 type:'DELETE',
197 dataType : 'html',
198 beforeSend: Utils.addAuthorizationStub,
199 success: function(data) {
200 console.log('Account deleted');
201 resolve(data);
202 }
203 }).fail(function(xhr){
204 //Authentication and the handling of fail states should be wrapped up into a connection class.
205 Utils.checkAuthentication(xhr.status);
206 reject('error');
207 });
208 })
209 },
210 interceptResponse: interceptResponse({
211 'error': 'Something went wrong while trying to delete the account. Check the error logs for more information' }),
212 success: Alt.actions.global.deleteAccountSuccess,
213 loading: Alt.actions.global.deleteAccountLoading,
214 error: Alt.actions.global.showNotification
215 },
216 getResourceOrchestrator: {
217 remote: function() {
218 return new Promise(function(resolve, reject) {
219 $.ajax({
220 url: 'passthrough/data/api/running/resource-orchestrator' + '?api_server=' + API_SERVER,
221 type: 'GET',
222 beforeSend: Utils.addAuthorizationStub,
223 contentType: "application/json",
224 success: function(data) {
225 let returnedData;
226 if (data.hasOwnProperty("rw-launchpad:resource-orchestrator")) {
227 returnedData = data;
228 } else {
229 returnedData = {};
230 }
231 resolve(returnedData);
232 },
233 error: function(error) {
234 console.log("There was an error updating the account: ", arguments);
235
236 }
237 }).fail(function(xhr){
238 //Authentication and the handling of fail states should be wrapped up into a connection class.
239 Utils.checkAuthentication(xhr.status);
240 return reject('error');
241 });
242 });
243 },
244 interceptResponse: interceptResponse({
245 'error': 'There was an error retrieving the resource orchestrator information.'
246 }),
247 success: Alt.actions.global.getResourceOrchestratorSuccess,
248 loading: Alt.actions.global.showScreenLoader,
249 error: Alt.actions.global.showNotification
250 },
251 }
252 }
253
254 function interceptResponse (responses) {
255 return function(data, action, args) {
256 if(responses.hasOwnProperty(data)) {
257 return {
258 type: data,
259 msg: responses[data]
260 }
261 } else {
262 return data;
263 }
264 }
265 }