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