update from RIFT as of 696b75d2fe9fb046261b08c616f1bcf6c0b54a9b third try
[osm/UI.git] / skyquake / plugins / redundancy / src / dashboard / redundancySource.js
1 /*
2 * STANDARD_RIFT_IO_COPYRIGHT
3 */
4 import $ from 'jquery';
5 var Utils = require('utils/utils.js');
6 let API_SERVER = require('utils/rw.js').getSearchParams(window.location).api_server;
7 let HOST = API_SERVER;
8 let NODE_PORT = require('utils/rw.js').getSearchParams(window.location).api_port || ((window.location.protocol == 'https:') ? 8443 : 8000);
9 let DEV_MODE = require('utils/rw.js').getSearchParams(window.location).dev_mode || false;
10
11 if (DEV_MODE) {
12 HOST = window.location.protocol + '//' + window.location.hostname;
13 }
14
15
16
17 module.exports = function(Alt) {
18 return {
19
20 getRedundancy: {
21 remote: function() {
22 return new Promise(function(resolve, reject) {
23 $.ajax({
24 url: `config?api_server=${API_SERVER}`,
25 type: 'GET',
26 beforeSend: Utils.addAuthorizationStub,
27 success: function(data, textStatus, jqXHR) {
28 resolve(data);
29 }
30 }).fail(function(xhr){
31 //Authentication and the handling of fail states should be wrapped up into a connection class.
32 Utils.checkAuthentication(xhr.status);
33 let msg = xhr.responseText;
34 if(xhr.errorMessage) {
35 msg = xhr.errorMessage
36 }
37 reject(msg);
38 });
39 });
40 },
41 interceptResponse: interceptResponse({
42 'error': 'There was an error retrieving the redundancy config.'
43 }),
44 success: Alt.actions.global.getRedundancySuccess,
45 loading: Alt.actions.global.showScreenLoader,
46 error: Alt.actions.global.handleServerReportedError
47 },
48 openRedundancyStateSocket: {
49 remote: function(state) {
50 return new Promise(function(resolve, reject) {
51 //If socket connection already exists, eat the request.
52 if(state.socket) {
53 console.log('connection already exists')
54 return resolve(false);
55 }
56 $.ajax({
57 url: '/socket-polling',
58 type: 'POST',
59 beforeSend: Utils.addAuthorizationStub,
60 data: {
61 url: 'redundancy/state?api_server=' + API_SERVER
62 },
63 success: function(data, textStatus, jqXHR) {
64 Utils.checkAndResolveSocketRequest(data, resolve, reject);
65 }
66 }).fail(function(xhr){
67 //Authentication and the handling of fail states should be wrapped up into a connection class.
68 Utils.checkAuthentication(xhr.status);
69 reject(xhr.responseText || 'An error occurred. Check your logs for more information');
70 });;
71 });
72 },
73 loading: Alt.actions.global.showScreenLoader,
74 success: Alt.actions.global.openRedundancyStateSocketSuccess
75 },
76 getSites: {
77 remote: function() {
78 return new Promise(function(resolve, reject) {
79 $.ajax({
80 url: `site?api_server=${API_SERVER}`,
81 type: 'GET',
82 beforeSend: Utils.addAuthorizationStub,
83 success: function(data, textStatus, jqXHR) {
84 resolve(data.site);
85 }
86 }).fail(function(xhr){
87 //Authentication and the handling of fail states should be wrapped up into a connection class.
88 Utils.checkAuthentication(xhr.status);
89 let msg = xhr.responseText;
90 if(xhr.errorMessage) {
91 msg = xhr.errorMessage
92 }
93 reject(msg);
94 });
95 });
96 },
97 interceptResponse: interceptResponse({
98 'error': 'There was an error retrieving the redundancy sites.'
99 }),
100 success: Alt.actions.global.getSitesSuccess,
101 loading: Alt.actions.global.showScreenLoader,
102 error: Alt.actions.global.handleServerReportedError
103 },
104 updateConfig: {
105 remote: function(state, site) {
106 return new Promise(function(resolve, reject) {
107 $.ajax({
108 url: `config?api_server=${API_SERVER}`,
109 type: 'PUT',
110 data: site,
111 beforeSend: Utils.addAuthorizationStub,
112 success: function(data, textStatus, jqXHR) {
113 resolve(data);
114 }
115 }).fail(function(xhr){
116 //Authentication and the handling of fail states should be wrapped up into a connection class.
117 Utils.checkAuthentication(xhr.status);
118 let msg = xhr.responseText;
119 if(xhr.errorMessage) {
120 msg = xhr.errorMessage
121 }
122 reject(msg);
123 });
124 });
125 },
126 interceptResponse: interceptResponse({
127 'error': 'There was an error updating the redundancy config.'
128 }),
129 success: Alt.actions.global.updateConfigSuccess,
130 loading: Alt.actions.global.showScreenLoader,
131 error: Alt.actions.global.handleServerReportedError
132 },
133 updateSite: {
134 remote: function(state, site) {
135 let data = site;
136 data['rw-instances'].map(function(s) {
137 if(s.hasOwnProperty('isNew')) {
138 delete s.isNew;
139 }
140 })
141 return new Promise(function(resolve, reject) {
142 $.ajax({
143 url: `site/${encodeURIComponent(site['site-name'])}?api_server=${API_SERVER}`,
144 type: 'PUT',
145 data: data,
146 beforeSend: Utils.addAuthorizationStub,
147 success: function(data, textStatus, jqXHR) {
148 resolve(data);
149 }
150 }).fail(function(xhr){
151 //Authentication and the handling of fail states should be wrapped up into a connection class.
152 Utils.checkAuthentication(xhr.status);
153 let msg = xhr.responseText;
154 if(xhr.errorMessage) {
155 msg = xhr.errorMessage
156 }
157 reject(msg);
158 });
159 });
160 },
161 interceptResponse: interceptResponse({
162 'error': 'There was an error updating the site.'
163 }),
164 success: Alt.actions.global.updateSiteSuccess,
165 loading: Alt.actions.global.showScreenLoader,
166 error: Alt.actions.global.handleServerReportedError
167 },
168 deleteSite: {
169 remote: function(state, site) {
170 return new Promise(function(resolve, reject) {
171 $.ajax({
172 url: `site/${encodeURIComponent(site['site-name'])}?api_server=${API_SERVER}`,
173 type: 'DELETE',
174 beforeSend: Utils.addAuthorizationStub,
175 success: function(data, textStatus, jqXHR) {
176 resolve(data);
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 let msg = xhr.responseText;
182 if(xhr.errorMessage) {
183 msg = xhr.errorMessage
184 }
185 reject(msg);
186 });
187 });
188 },
189 interceptResponse: interceptResponse({
190 'error': 'There was an error deleting the site.'
191 }),
192 success: Alt.actions.global.deleteSiteSuccess,
193 loading: Alt.actions.global.showScreenLoader,
194 error: Alt.actions.global.handleServerReportedError
195 },
196 createSite: {
197 remote: function(state, site) {
198 let data = site;
199 data['rw-instances'].map(function(s) {
200 if(s.hasOwnProperty('isNew')) {
201 delete s.isNew;
202 }
203 })
204 return new Promise(function(resolve, reject) {
205 $.ajax({
206 url: `site?api_server=${API_SERVER}`,
207 type: 'POST',
208 data: data,
209 beforeSend: Utils.addAuthorizationStub,
210 success: function(data, textStatus, jqXHR) {
211 resolve(data);
212 }
213 }).fail(function(xhr){
214 //Authentication and the handling of fail states should be wrapped up into a connection class.
215 Utils.checkAuthentication(xhr.status);
216 let msg = xhr.responseText;
217 if(xhr.errorMessage) {
218 msg = xhr.errorMessage
219 }
220 reject(msg);
221 });
222 });
223 },
224 interceptResponse: interceptResponse({
225 'error': 'There was an error creating the site.'
226 }),
227 success: Alt.actions.global.createSiteSuccess,
228 loading: Alt.actions.global.showScreenLoader,
229 error: Alt.actions.global.handleServerReportedError
230 }
231 }
232 }
233
234 function interceptResponse (responses) {
235 return function(data, action, args) {
236 if(responses.hasOwnProperty(data)) {
237 return {
238 type: data,
239 msg: responses[data]
240 }
241 } else {
242 return data;
243 }
244 }
245 }
246