724312e7fa409a520f236a56bd2f77f505ac6bc7
[osm/UI.git] / skyquake / plugins / user_management / src / platformRoleManagement / platformRoleManagementSource.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 getUsers: {
21 remote: function() {
22 return new Promise(function(resolve, reject) {
23 $.ajax({
24 url: `/user?api_server=${API_SERVER}`,
25 type: 'GET',
26 beforeSend: Utils.addAuthorizationStub,
27 success: function(data, textStatus, jqXHR) {
28 resolve(data.user);
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 resource orchestrator information.'
43 }),
44 success: Alt.actions.global.getPlatformRoleUsersSuccess,
45 loading: Alt.actions.global.showScreenLoader,
46 error: Alt.actions.global.showNotification
47 },
48 getPlatform: {
49 remote: function() {
50 return new Promise(function(resolve, reject) {
51 $.ajax({
52 url: `/platform?api_server=${API_SERVER}`,
53 type: 'GET',
54 beforeSend: Utils.addAuthorizationStub,
55 success: function(data, textStatus, jqXHR) {
56 resolve(data.platform);
57 }
58 }).fail(function(xhr){
59 //Authentication and the handling of fail states should be wrapped up into a connection class.
60 Utils.checkAuthentication(xhr.status);
61 let msg = xhr.responseText;
62 if(xhr.errorMessage) {
63 msg = xhr.errorMessage
64 }
65 reject(msg);
66 });
67 });
68 },
69 interceptResponse: interceptResponse({
70 'error': 'There was an error retrieving the resource orchestrator information.'
71 }),
72 success: Alt.actions.global.getPlatformSuccess,
73 loading: Alt.actions.global.showScreenLoader,
74 error: Alt.actions.global.showNotification
75 },
76 updatePlatform: {
77 remote: function(state, project) {
78 return new Promise(function(resolve, reject) {
79 $.ajax({
80 url: `/platform?api_server=${API_SERVER}`,
81 type: 'PUT',
82 data: project,
83 dataType: 'json',
84 beforeSend: Utils.addAuthorizationStub,
85 success: function(data, textStatus, jqXHR) {
86 resolve(data);
87 }
88 }).fail(function(xhr){
89 //Authentication and the handling of fail states should be wrapped up into a connection class.
90 Utils.checkAuthentication(xhr.status);
91 let msg = xhr.responseText;
92 if(xhr.errorMessage) {
93 msg = xhr.errorMessage
94 }
95 reject(msg);
96 });
97 });
98 },
99 interceptResponse: interceptResponse({
100 'error': 'There was an error updating the project.'
101 }),
102 success: Alt.actions.global.updateProjectSuccess,
103 loading: Alt.actions.global.showScreenLoader,
104 error: Alt.actions.global.showNotification
105 },
106 deleteProject: {
107 remote: function(state, project) {
108 return new Promise(function(resolve, reject) {
109 $.ajax({
110 url: `/project/${project['name']}?api_server=${API_SERVER}`,
111 type: 'DELETE',
112 beforeSend: Utils.addAuthorizationStub,
113 success: function(data, textStatus, jqXHR) {
114 resolve(data);
115 }
116 }).fail(function(xhr){
117 //Authentication and the handling of fail states should be wrapped up into a connection class.
118 Utils.checkAuthentication(xhr.status);
119 let msg = xhr.responseText;
120 if(xhr.errorMessage) {
121 msg = xhr.errorMessage
122 }
123 reject(msg);
124 });
125 });
126 },
127 interceptResponse: interceptResponse({
128 'error': 'There was an error deleting the user.'
129 }),
130 success: Alt.actions.global.deleteProjectSuccess,
131 loading: Alt.actions.global.showScreenLoader,
132 error: Alt.actions.global.showNotification
133 },
134 createProject: {
135 remote: function(state, project) {
136
137 return new Promise(function(resolve, reject) {
138 $.ajax({
139 url: `/project?api_server=${API_SERVER}`,
140 type: 'POST',
141 data: project,
142 beforeSend: Utils.addAuthorizationStub,
143 success: function(data, textStatus, jqXHR) {
144 resolve(data);
145 }
146 }).fail(function(xhr){
147 //Authentication and the handling of fail states should be wrapped up into a connection class.
148 Utils.checkAuthentication(xhr.status);
149 let msg = xhr.responseText;
150 if(xhr.errorMessage) {
151 msg = xhr.errorMessage
152 }
153 reject(msg);
154 });
155 });
156 },
157 interceptResponse: interceptResponse({
158 'error': 'There was an error updating the account.'
159 }),
160 success: Alt.actions.global.createProjectSuccess,
161 loading: Alt.actions.global.showScreenLoader,
162 error: Alt.actions.global.showNotification
163 }
164 }
165 }
166
167 function interceptResponse (responses) {
168 return function(data, action, args) {
169 if(responses.hasOwnProperty(data)) {
170 return {
171 type: data,
172 msg: responses[data]
173 }
174 } else {
175 return data;
176 }
177 }
178 }
179