update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[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 + '/' + encodeURIComponent(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 + '/' + encodeURIComponent(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 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];
127 }
128 });
129 return new Promise(function(resolve, reject) {
130 $.ajax({
131 url: type + '?api_server=' + API_SERVER,
132 type:'POST',
133 beforeSend: Utils.addAuthorizationStub,
134 data: JSON.stringify(payload),
135 contentType: "application/json",
136 success: function(data) {
137 resolve({data, cb});
138 },
139 error: function(error) {
140 console.log("There was an error creating the account: ", arguments);
141
142 }
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');
147 });
148
149 });
150 },
151 success: Alt.actions.global.createAccountSuccess,
152 loading: Alt.actions.global.createAccountLoading,
153 error: Alt.actions.global.createAccountFail
154 },
155 update: {
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'];
164 (
165 (payload.nestedParams == null) &&
166 delete payload.nestedParams
167 ) ||
168 (
169 payload.nestedParams &&
170 payload.nestedParams['container-name'] &&
171 delete payload[payload.nestedParams['container-name']] &&
172 delete payload.nestedParams
173 );
174
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() == '') {
179 delete payload[payload['account-type']][k];
180 }
181 });
182
183 return new Promise(function(resolve, reject) {
184 $.ajax({
185 url: type + '/' + encodeURIComponent(account.name) + '?api_server=' + API_SERVER,
186 type:'PUT',
187 beforeSend: Utils.addAuthorizationStub,
188 data: JSON.stringify(payload),
189 contentType: "application/json",
190 success: function(data) {
191 resolve({data, cb});
192 },
193 error: function(error) {
194 console.log("There was an error updating the account: ", arguments);
195 }
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');
200 });
201
202 });
203 },
204 interceptResponse: interceptResponse({
205 'error': 'There was an error updating the account.'
206 }),
207 success: Alt.actions.global.createAccountSuccess,
208 loading: Alt.actions.global.createAccountLoading,
209 error: Alt.actions.global.createAccountFail
210 },
211 delete: {
212 remote: function(state, type, name, cb) {
213 return new Promise(function(resolve, reject) {
214 $.ajax({
215 url: type + '/' + encodeURIComponent(name) + '/?api_server=' + API_SERVER,
216 type:'DELETE',
217 dataType : 'html',
218 beforeSend: Utils.addAuthorizationStub,
219 success: function(data) {
220 console.log('Account deleted');
221 resolve(data);
222 }
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');
227 });
228 })
229 },
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
235 }
236 }
237 }
238
239 function interceptResponse (responses) {
240 return function(data, action, args) {
241 if(responses.hasOwnProperty(data)) {
242 return {
243 type: data,
244 msg: responses[data]
245 }
246 } else {
247 return data;
248 }
249 }
250 }