8c1dd6d37f9ce0ed7013b73a4c361723c28a825d
[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 console.log('connection already exists')
38 return resolve(false);
39 }
40 $.ajax({
41 url: '/socket-polling',
42 type: 'POST',
43 beforeSend: Utils.addAuthorizationStub,
44 data: {
45 url: 'accounts/all?api_server=' + API_SERVER
46 },
47 success: function(data, textStatus, jqXHR) {
48 Utils.checkAndResolveSocketRequest(data, resolve, reject);
49 }
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');
54 });;
55 });
56 },
57 loading: Alt.actions.global.openAccountSocketLoading,
58 success: Alt.actions.global.openAccountSocketSuccess,
59 error: Alt.actions.global.openAccountSocketError
60 },
61 refreshAll: {
62 remote: function(state, accounts) {
63 return new Promise(function(resolve, reject) {
64 let Refreshing = syncRefresh();
65 Refreshing.next();
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);
72 }
73 }
74 }
75 resolve({})
76 }
77
78 function refreshStatus(type, name) {
79 let url = type + '/' + name + '/refresh?api_server=' + API_SERVER;
80 setTimeout(function(){
81 Refreshing.next();
82 },100);
83 $.ajax({
84 url: url,
85 type: 'POST',
86 beforeSend: Utils.addAuthorizationStub,
87 success: function() {
88
89 },
90 error: function() {
91 }
92 });
93
94 }
95 })
96 },
97 loading: Alt.actions.global.refreshAllAccountsLoading,
98 success: Alt.actions.global.refreshAllAccountsSuccess,
99 error: Alt.actions.global.refreshAllAccountsError
100 },
101 refreshAccount: {
102 remote: function(state, name, type) {
103 return new Promise(function(resolve, reject) {
104 $.ajax({
105 url: type + '/' + name + '/refresh?api_server=' + API_SERVER,
106 type: 'POST',
107 beforeSend: Utils.addAuthorizationStub,
108 success: function(account) {
109 resolve(account);
110 }
111 });
112 });
113 },
114 success: Alt.actions.global.refreshCloudAccountSuccess,
115 error: Alt.actions.global.refreshCloudAccountFail
116 },
117 create: {
118 remote: function(state, account, type, cb) {
119 delete account['connection-status'];
120 return new Promise(function(resolve, reject) {
121 $.ajax({
122 url: type + '?api_server=' + API_SERVER,
123 type:'POST',
124 beforeSend: Utils.addAuthorizationStub,
125 data: JSON.stringify(account),
126 contentType: "application/json",
127 success: function(data) {
128 resolve({data, cb});
129 },
130 error: function(error) {
131 console.log("There was an error creating the account: ", arguments);
132
133 }
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');
138 });
139
140 });
141 },
142 success: Alt.actions.global.createAccountSuccess,
143 loading: Alt.actions.global.createAccountLoading,
144 error: Alt.actions.global.createAccountFail
145 },
146 update: {
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'];
152 (
153 (payload.nestedParams == null) &&
154 delete payload.nestedParams
155 ) ||
156 (
157 payload.nestedParams &&
158 payload.nestedParams['container-name'] &&
159 delete payload[payload.nestedParams['container-name']] &&
160 delete payload.nestedParams
161 );
162
163
164 return new Promise(function(resolve, reject) {
165 $.ajax({
166 url: type + '/' + account.name + '?api_server=' + API_SERVER,
167 type:'PUT',
168 beforeSend: Utils.addAuthorizationStub,
169 data: JSON.stringify(payload),
170 contentType: "application/json",
171 success: function(data) {
172 resolve({data, cb});
173 },
174 error: function(error) {
175 console.log("There was an error updating the account: ", arguments);
176
177 }
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');
182 });
183
184 });
185 },
186 interceptResponse: interceptResponse({
187 'error': 'There was an error updating the account.'
188 }),
189 success: Alt.actions.global.createAccountSuccess,
190 loading: Alt.actions.global.createAccountLoading,
191 error: Alt.actions.global.showNotification
192 },
193 delete: {
194 remote: function(state, type, name, cb) {
195 return new Promise(function(resolve, reject) {
196 $.ajax({
197 url: type + '/' + name + '/?api_server=' + API_SERVER,
198 type:'DELETE',
199 dataType : 'html',
200 beforeSend: Utils.addAuthorizationStub,
201 success: function(data) {
202 console.log('Account deleted');
203 resolve(data);
204 }
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');
209 });
210 })
211 },
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
217 },
218 getResourceOrchestrator: {
219 remote: function() {
220 return new Promise(function(resolve, reject) {
221 $.ajax({
222 url: 'passthrough/data/api/running/resource-orchestrator' + '?api_server=' + API_SERVER,
223 type: 'GET',
224 beforeSend: Utils.addAuthorizationStub,
225 contentType: "application/json",
226 success: function(data) {
227 let returnedData;
228 if (data.hasOwnProperty("rw-launchpad:resource-orchestrator")) {
229 returnedData = data;
230 } else {
231 returnedData = {};
232 }
233 resolve(returnedData);
234 },
235 error: function(error) {
236 console.log("There was an error updating the account: ", arguments);
237
238 }
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');
243 });
244 });
245 },
246 interceptResponse: interceptResponse({
247 'error': 'There was an error retrieving the resource orchestrator information.'
248 }),
249 success: Alt.actions.global.getResourceOrchestratorSuccess,
250 loading: Alt.actions.global.showScreenLoader,
251 error: Alt.actions.global.showNotification
252 },
253 }
254 }
255
256 function interceptResponse (responses) {
257 return function(data, action, args) {
258 if(responses.hasOwnProperty(data)) {
259 return {
260 type: data,
261 msg: responses[data]
262 }
263 } else {
264 return data;
265 }
266 }
267 }