blob: 012daa85a915500ce9c480a7973f825c07f518cd [file] [log] [blame]
Matteo Scandoloecf088a2016-10-20 10:25:34 +02001/**
2 * © OpenCORD
3 *
4 * Visit http://guide.xosproject.org/devguide/addview/ for more information
5 *
6 * Created by teone on 10/20/16.
7 */
8
9(function () {
10 'use strict';
11 angular.module('xos.globalXos')
12 .service('Controllers', function($resource){
13 return $resource('/api/core/controllers/:id', {id: '@id'});
14 })
15 .service('GXOS', function($q, $resource, _, LXOS){
16 this.attachXosToItem = (items, target = LXOS) => {
17 return _.map(items, (lxos, i) => {
18 let currentXos = target[i];
19 return _.map(lxos, (item) => {
20 item.xos = currentXos;
21 return item;
22 });
23 })
24 };
25
26 this.buildQueryEndpoint = (baseUrl, target = LXOS) => {
27 return () => {
28 const d = $q.defer();
29
30 // store generated $resource for each L-XOS
31 let r = [];
32
33 let p = [];
34 _.forEach(target, (xos, i) => {
35 let resource = $resource(`${xos.auth_url}${baseUrl}`, {id: '@id'}, {
36 query: {
37 isArray: true,
38 headers: {
39 Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}`
40 }
41 }
42 });
43 r.push(resource);
44 p.push(r[i].query().$promise);
45 });
46
47 $q.all(p)
48 .then(res => {
49 res = this.attachXosToItem(res, target);
50 d.resolve(_.flatten(res));
51 })
52 .catch(d.reject);
53
54 return {$promise: d.promise};
55 };
56 };
57
58 // TODO evaluate
59 this.buildLocalResource = (baseUrl, xos) => {
60 const headers = {
61 Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}`
62 };
63 const resource = $resource(`${xos.auth_url}${baseUrl}`, {id: '@id'}, {
64 query: {
65 isArray: true,
66 headers: headers
67 }
68 });
69 return resource;
70 }
71 })
72 .service('LocalAuth', function ($q, $http, _, LXOS) {
73 const baseUrl = `api/utility/login/`;
74 this.login = () => {
75 const d = $q.defer();
76
77 let p = [];
78 _.forEach(LXOS, (xos, i) => {
79 let loginRequest = $http.post(`${xos.auth_url}${baseUrl}`, {username: xos.admin_user, password: xos.admin_password});
80 p.push(loginRequest);
81 });
82
83 $q.all(p)
84 .then(auths => {
85 _.forEach(auths, (auth, i) => {
86 LXOS[i].xoscsrftoken = auth.data.xoscsrftoken;
87 LXOS[i].xossessionid = auth.data.xossessionid;
88 LXOS[i].user = JSON.parse(auth.data.user);
89 });
90 d.resolve();
91 })
92 .catch(e => {
93 d.reject(e);
94 });
95
96 return d.promise;
97 }
98 })
99 .service('LocalSlices', function(GXOS){
100 const baseUrl = `api/utility/slicesplus/`;
101
102 // TODO build a global resource
103 this.queryFromAll = (targets) => {
104 return GXOS.buildQueryEndpoint(baseUrl, targets)();
105 };
106 })
107 .service('LocalUsers', function(GXOS){
108 const baseUrl = `api/core/users/`;
109
110 // TODO build a global resource
111 this.queryFromAll = GXOS.buildQueryEndpoint(baseUrl);
112 })
113 .service('LocalInstances', function($q, $http, GXOS, LocalDeployments, LocalImages, LocalFlavor, LocalNode){
114 const baseUrl = `api/core/instances/`;
115
116 // NOTE Evaluate to dinamically create a resource targeted to a L-XOS
117
118 this.queryFromAll = (targets) => {
119 return GXOS.buildQueryEndpoint(baseUrl, targets)();
120 };
121 this.createOnLocal = (instance) => {
122 const d = $q.defer();
123 const xos = instance.xos;
124 delete instance.xos;
125 $http.post(`${xos.auth_url}${baseUrl}`, instance, {
126 headers: {
127 Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}`
128 }
129 })
130 .then((inst) => {
131 d.resolve(inst);
132 })
133 .catch(e => {
134 d.reject(e);
135 });
136
137 return d.promise;
138 };
139
140 this.getFromLocal = (xos, params) => {
141 const d = $q.defer();
142 $http.get(`${xos.auth_url}${baseUrl}`, {
143 params: params,
144 headers: {
145 Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}`
146 }
147 })
148 .then((inst) => {
149 d.resolve(inst.data);
150 })
151 .catch(e => {
152 d.reject(e);
153 });
154
155 return d.promise;
156 };
157
158 this.getLocalInfo = (xos) => {
159 const d = $q.defer();
160 $q.all([
161 LocalDeployments.queryFromLocal(xos),
162 LocalImages.queryFromLocal(xos),
163 LocalFlavor.queryFromLocal(xos),
164 LocalNode.queryFromLocal(xos)
165 ])
166 .then((res) => {
167 res = _.map(res, collection => {
168 return _.map(collection, item => {
169 return {id: item.id, label: item.name}
170 });
171 });
172 d.resolve(res);
173 })
174 .catch(d.reject);
175 return d.promise;
176 };
177
178 })
179 .service('LocalDeployments', function($q, $http){
180
181 const baseUrl = `api/core/deployments/`;
182
183 this.queryFromLocal = (xos) => {
184 const d = $q.defer();
185
186 $http.get(`${xos.auth_url}${baseUrl}`, {
187 headers: {
188 Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}`
189 }
190 })
191 .then((res) => {
192 d.resolve(res.data);
193 })
194 .catch(e => {
195 d.reject(e);
196 });
197
198 return d.promise;
199 }
200 })
201 .service('LocalImages', function($q, $http){
202
203 const baseUrl = `api/core/images/`;
204
205 this.queryFromLocal = (xos) => {
206 const d = $q.defer();
207
208 $http.get(`${xos.auth_url}${baseUrl}`, {
209 headers: {
210 Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}`
211 }
212 })
213 .then((res) => {
214 d.resolve(res.data);
215 })
216 .catch(e => {
217 d.reject(e);
218 });
219
220 return d.promise;
221 }
222 })
223 .service('LocalFlavor', function($q, $http){
224
225 const baseUrl = `api/core/flavors/`;
226
227 this.queryFromLocal = (xos) => {
228 const d = $q.defer();
229
230 $http.get(`${xos.auth_url}${baseUrl}`, {
231 headers: {
232 Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}`
233 }
234 })
235 .then((res) => {
236 d.resolve(res.data);
237 })
238 .catch(e => {
239 d.reject(e);
240 });
241
242 return d.promise;
243 }
244 })
245 .service('LocalNode', function($q, $http){
246
247 const baseUrl = `api/core/nodes/`;
248
249 this.queryFromLocal = (xos) => {
250 const d = $q.defer();
251
252 $http.get(`${xos.auth_url}${baseUrl}`, {
253 headers: {
254 Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}`
255 }
256 })
257 .then((res) => {
258 d.resolve(res.data);
259 })
260 .catch(e => {
261 d.reject(e);
262 });
263
264 return d.promise;
265 }
266 });
267})();
268