blob: 16beca1d85d1e558e226117522fa4c0cfd3db112 [file] [log] [blame]
Matteo Scandoloecf088a2016-10-20 10:25:34 +02001'use strict';
2
3angular.module('xos.globalXos', [
4 'ngResource',
5 'ngCookies',
6 'ui.router',
7 'xos.helpers',
8 'ui.bootstrap.modal',
9 'ui.bootstrap.tpls'
10])
11.config(($stateProvider) => {
12 $stateProvider
13 .state('xos-list', {
14 url: '/',
15 template: '<xos-list></xos-list>'
16 });
17})
18.config(function($httpProvider){
19 $httpProvider.interceptors.push('NoHyperlinks');
20})
21.value('LXOS', [])
22.directive('xosList', function(){
23 return {
24 restrict: 'E',
25 scope: {},
26 bindToController: true,
27 controllerAs: 'vm',
28 templateUrl: 'templates/xos-list.tpl.html',
29 controller: function($window, $q, _, Controllers, LXOS, LocalAuth, LocalSlices, LocalUsers, $uibModal, Slices){
30 const self = this;
31 $q.all([
Matteo Scandolo12be8672016-11-10 15:03:11 -080032 Controllers.query({backend_type: 'CORD'}).$promise,
Matteo Scandoloa93248a2016-11-15 17:03:11 -080033 Slices.query().$promise
Matteo Scandoloecf088a2016-10-20 10:25:34 +020034 ])
35 .then(res => {
36 [this.xoss, this.gSlices] = res;
37 });
38
39 this.openLocally = (itemKind) => {
40 return (item) => {
41 $window.open(`${item.xos.auth_url}admin/core/${itemKind}/${item.id}`, '_blank');
42 }
43 };
44
Matteo Scandoloa93248a2016-11-15 17:03:11 -080045 const getGlobalInstances = (item) => {
46 $uibModal.open({
47 animation: true,
48 size: 'lg',
49 templateUrl: 'listInstances.html',
50 controllerAs: 'vm',
51 resolve: {
52 slice: function () {
53 return {
54 name: item.name,
55 xos: {
56 name: 'G-XOS'
57 }
58 };
59 }
60 },
61 controller: function($uibModalInstance, slice, LocalInstances, LocalSlices) {
62 this.slice = slice;
63
64 this.config = {
65 columns: [
66 {
67 label: 'Name',
68 prop: 'name',
69 }
70 ],
71 actions: [
72 {
73 label: 'Add Instance',
74 icon: 'remove',
75 color: 'red',
76 cb: (item) => {
77 console.log(item);
78 LocalInstances.deleteFromLocal(item)
79 .then(() => {
80 _.remove(this.instances, i => i.id === item.id);
81 });
82 }
83 }
84 ]
85 };
86
87 LocalSlices.queryFromAll(self.xoss).$promise
88 .then(slices => {
89 // keep only the slice that match the name
90 this.slicesId = slices
91 .filter(s => s.name.indexOf(this.slice.name) > -1)
92 .reduce((o, s) => {
93 o[s.xos.id] = s.id;
94 return o;
95 }, {});
96 return LocalInstances.queryFromAll(self.xoss).$promise;
97 })
98 .then(instances => {
99 this.instances = instances.filter(i => this.slicesId[i.xos.id] === i.slice);
100 })
101 .catch(e => {
102 this.instances = [];
103 });
104
105 this.close = () => {
106 $uibModalInstance.dismiss('cancel');
107 }
108 }
109 })
110 };
111
112 const createGlobalInstance = (item) => {
113 $uibModal.open({
114 animation: true,
115 size: 'lg',
116 templateUrl: 'addInstance.html',
117 controller: function($scope, $q, $uibModalInstance, slice, LocalInstances, LocalAuth){
118 this.slice = slice;
119
120 this.model = {
121 // isolation: 'vm'
122 };
123
124 let xos;
125
126 Controllers.query({backend_type: 'CORD'}).$promise
127 .then((xos) => {
128 this.xoss = xos;
129 this.config.fields['xos'].options = _.map(xos, item => {
130 return {id: item.id, label: item.name}
131 });
132 });
133
134 $scope.$watch(() => this.model.xos, () => {
135 if(!this.model.xos){
136 return;
137 }
138 xos = _.find(this.xoss, {id: this.model.xos});
139 LocalInstances.getLocalInfo(xos)
140 .then((res) => {
141 [
142 this.config.fields['deployment'].options,
143 this.config.fields['image'].options,
144 this.config.fields['flavor'].options,
145 this.config.fields['node'].options
146 ] = res;
147 return $q.all([
148 LocalSlices.getLocalByName(xos, this.slice.name),
149 LocalAuth.getUserByName(xos, xos.admin_user)
150 ]);
151 })
152 .then((res) => {
153 console.log('aaaa: ', res);
154 [this.localSlice, this.user] = res;
155 });
156 });
157
158
159 this.config = {
160 formName: 'instanceForm',
161 order: ['xos', 'name'],
162 excludedFields: ['xos', 'slice'],
163 actions: [
164 {
165 label: 'Save',
166 icon: 'ok',
167 cb: (instance) => {
168 instance.xos = xos;
169 instance.slice = this.localSlice.id;
170 instance.creator = this.user.id;
171 LocalInstances.createOnLocal(instance)
172 .then(res => {
173 slice.instance_total = slice.instance_total + 1;
174 $uibModalInstance.close();
175 });
176 },
177 class: 'success'
178 },
179 {
180 label: 'Cancel',
181 icon: 'remove',
182 cb: () => {
183 $uibModalInstance.dismiss('cancel');
184 },
185 class: 'warning'
186 }
187 ],
188 fields: {
189 xos: {
190 type: 'select',
191 validators: {
192 required: true
193 }
194 },
195 name: {
196 type: 'text',
197 validators: {
198 required: true
199 }
200 },
201 deployment: {
202 type: 'select',
203 validators: {
204 required: true
205 }
206 },
207 node: {
208 type: 'select',
209 validators: {
210 required: true
211 }
212 },
213 image: {
214 type: 'select',
215 validators: {
216 required: true,
217 }
218 },
219 flavor: {
220 type: 'select',
221 validators: {
222 required: true,
223 }
224 },
225 isolation: {
226 type: 'select',
227 options: [
228 {id: 'vm', label: 'VM'},
229 {id: 'container', label: 'Container'},
230 {id: 'container_vm', label: 'Container in VM'}
231 ],
232 validators: {
233 required: true,
234 }
235 },
236 }
237 };
238 },
239 controllerAs: 'vm',
240 resolve: {
241 slice: function () {
242 return item;
243 }
244 }
245 });
246 };
247
Matteo Scandoloecf088a2016-10-20 10:25:34 +0200248 const baseSliceCols = [
249 {
250 label: 'Name',
251 prop: 'name',
252 },
Matteo Scandoloa93248a2016-11-15 17:03:11 -0800253 // {
254 // label: 'Mount Data Sets',
255 // prop: 'mount_data_sets'
256 // }
Matteo Scandoloecf088a2016-10-20 10:25:34 +0200257 ];
258
259 const lXosSliceCols = [
260 {
261 label: 'Max Instances',
262 prop: 'max_instances'
263 },
264 {
265 label: 'Instances',
266 prop: 'instance_total'
267 },
268 {
269 label: 'L-XOS',
270 type: 'custom',
271 formatter: item => item.xos.name
272 }
273 ];
274
275 this.gSliceTableCgf = {
276 columns: baseSliceCols,
277 filter: 'field',
278 order: true,
279 actions: [
280 {
281 label: 'Get Instances',
282 icon: 'search',
Matteo Scandoloa93248a2016-11-15 17:03:11 -0800283 cb: getGlobalInstances
284 },
285 {
286 label: 'Add Instances',
287 icon: 'plus',
288 cb: createGlobalInstance
Matteo Scandoloecf088a2016-10-20 10:25:34 +0200289 },
290 ]
291 };
292
293 this.sliceTableCfg = {
294 columns: baseSliceCols.concat(lXosSliceCols),
295 actions: [
296 {
297 label: 'open locally',
298 icon: 'open',
299 cb: this.openLocally('slice')
300 },
301 {
302 label: 'Get Instances',
303 icon: 'search',
304 cb: (item) => {
305 $uibModal.open({
306 animation: true,
307 size: 'lg',
308 templateUrl: 'listInstances.html',
309 controllerAs: 'vm',
310 resolve: {
311 slice: function () {
312 return item;
313 }
314 },
315 controller: function($uibModalInstance, slice, LocalInstances) {
316 this.slice = slice;
317
318 this.config = {
319 columns: [
320 {
321 label: 'Name',
322 prop: 'name',
323 },
324 {
325 label: 'deployment',
326 prop: 'deployment',
327 },
328 ]
329 };
330
331 LocalInstances.getFromLocal(slice.xos)
332 .then(instances => {
333 this.instances = instances.filter(i => i.slice === slice.id);
334 });
335
336 this.close = () => {
337 $uibModalInstance.dismiss('cancel');
338 }
339 }
340 })
341 }
342 },
343 {
344 label: 'Add Instance',
345 icon: 'plus',
346 cb: (item) => {
347 $uibModal.open({
348 animation: true,
349 size: 'lg',
350 templateUrl: 'addInstance.html',
351 controller: function($uibModalInstance, slice, LocalInstances){
352 this.slice = slice;
353
354 this.model = {};
Matteo Scandoloa93248a2016-11-15 17:03:11 -0800355 console.log(slice);
Matteo Scandoloecf088a2016-10-20 10:25:34 +0200356 LocalInstances.getLocalInfo(slice.xos)
357 .then((res) => {
358 [
359 this.config.fields['deployment'].options,
360 this.config.fields['image'].options,
361 this.config.fields['flavor'].options,
362 this.config.fields['node'].options
363 ] = res;
364 });
365
366 this.config = {
367 formName: 'instanceForm',
368 excludedFields: ['xos', 'slice'],
369 actions: [
370 {
371 label: 'Save',
372 icon: 'ok',
373 cb: (instance) => {
374 instance.xos = slice.xos;
375 instance.slice = slice.id;
Matteo Scandoloa93248a2016-11-15 17:03:11 -0800376 instance.creator = this.user.id;
Matteo Scandoloecf088a2016-10-20 10:25:34 +0200377 LocalInstances.createOnLocal(instance)
378 .then(res => {
379 slice.instance_total = slice.instance_total + 1;
380 $uibModalInstance.close();
381 });
382 },
383 class: 'success'
384 },
385 {
386 label: 'Cancel',
387 icon: 'remove',
388 cb: () => {
389 $uibModalInstance.dismiss('cancel');
390 },
391 class: 'warning'
392 }
393 ],
394 fields: {
395 name: {
396 type: 'text',
397 validators: {
398 required: true
399 }
400 },
401 deployment: {
402 type: 'select',
403 validators: {
404 required: true
405 }
406 },
407 node: {
408 type: 'select',
409 validators: {
410 required: true
411 }
412 },
413 image: {
414 type: 'select',
415 validators: {
416 required: true,
417 }
418 },
419 flavor: {
420 type: 'select',
421 validators: {
422 required: true,
423 }
424 },
425 isolation: {
426 type: 'select',
427 options: [
428 {id: 'vm', label: 'VM'},
429 {id: 'container', label: 'Container'},
430 {id: 'container_vm', label: 'Container in VM'}
431 ],
432 validators: {
433 required: true,
434 }
435 },
436 }
437 };
438 },
439 controllerAs: 'vm',
440 resolve: {
441 slice: function () {
442 return item;
443 }
444 }
445 });
446 }
447 }
448 ],
449 filter: 'field',
450 order: true
451 };
452
453 this.usersTableCfg = {
454 columns: [
455 {
456 label: 'Name',
457 type: 'custom',
458 formatter: item => `${item.firstname} ${item.lastname}`
459 },
460 {
461 label: 'E-Mail',
462 prop: 'email'
463 },
464 {
465 label: 'User Name',
466 prop: 'username'
467 },
468 {
469 label: 'Time Zone',
470 prop: 'timezone'
471 },
472 {
473 label: 'L-XOS',
474 type: 'custom',
475 formatter: item => item.xos.name
476 }
477 ],
478 actions: [
479 {
480 label: 'open locally',
481 icon: 'open',
482 cb: this.openLocally('user')
483 }
484 ],
485 filter: 'field',
486 order: true
487 };
488
489 this.toggleXos = (xos) => {
490 if(_.findIndex(LXOS, {id: xos.id}) > -1){
491 xos.active = false;
492 _.remove(LXOS, {id: xos.id});
493 }
494 else{
495 xos.active = true;
496 LXOS.push(xos);
497 }
498
499 // authenticate on L-XOS
500 LocalAuth.login()
501 .then(() => {
502 // fetch slices
503 return $q.all([
504 LocalSlices.queryFromAll().$promise,
505 LocalUsers.queryFromAll().$promise,
506 ]);
507 })
508 .then(res => {
509 [this.localSlices, this.localUsers] = res;
510 })
511 .catch(e => {
512 console.log(e);
513 });
514 }
515 }
516 };
517});