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