Matteo Scandolo | ecf088a | 2016-10-20 10:25:34 +0200 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | angular.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 Scandolo | 12be867 | 2016-11-10 15:03:11 -0800 | [diff] [blame^] | 32 | Controllers.query({backend_type: 'CORD'}).$promise, |
Matteo Scandolo | ecf088a | 2016-10-20 10:25:34 +0200 | [diff] [blame] | 33 | Slices.query().$promise // NOTE why this is queryFromAll?? |
| 34 | ]) |
| 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 | |
| 45 | const baseSliceCols = [ |
| 46 | { |
| 47 | label: 'Name', |
| 48 | prop: 'name', |
| 49 | }, |
| 50 | { |
| 51 | label: 'Mount Data Sets', |
| 52 | prop: 'mount_data_sets' |
| 53 | } |
| 54 | ]; |
| 55 | |
| 56 | const lXosSliceCols = [ |
| 57 | { |
| 58 | label: 'Max Instances', |
| 59 | prop: 'max_instances' |
| 60 | }, |
| 61 | { |
| 62 | label: 'Instances', |
| 63 | prop: 'instance_total' |
| 64 | }, |
| 65 | { |
| 66 | label: 'L-XOS', |
| 67 | type: 'custom', |
| 68 | formatter: item => item.xos.name |
| 69 | } |
| 70 | ]; |
| 71 | |
| 72 | this.gSliceTableCgf = { |
| 73 | columns: baseSliceCols, |
| 74 | filter: 'field', |
| 75 | order: true, |
| 76 | actions: [ |
| 77 | { |
| 78 | label: 'Get Instances', |
| 79 | icon: 'search', |
| 80 | cb: (item) => { |
| 81 | $uibModal.open({ |
| 82 | animation: true, |
| 83 | size: 'lg', |
| 84 | templateUrl: 'listInstances.html', |
| 85 | controllerAs: 'vm', |
| 86 | resolve: { |
| 87 | slice: function () { |
| 88 | return { |
| 89 | name: item.name, |
| 90 | xos: { |
| 91 | name: 'G-XOS' |
| 92 | } |
| 93 | }; |
| 94 | } |
| 95 | }, |
| 96 | controller: function($uibModalInstance, slice, LocalInstances, LocalSlices) { |
| 97 | this.slice = slice; |
| 98 | |
| 99 | this.config = { |
| 100 | columns: [ |
| 101 | { |
| 102 | label: 'Name', |
| 103 | prop: 'name', |
| 104 | } |
| 105 | ] |
| 106 | }; |
| 107 | |
| 108 | LocalSlices.queryFromAll(self.xoss).$promise |
| 109 | .then(slices => { |
| 110 | // keep only the slice that match the name |
| 111 | this.slicesId = slices |
| 112 | .filter(s => s.name.indexOf(this.slice.name) > -1) |
| 113 | .reduce((o, s) => { |
| 114 | o[s.xos.id] = s.id; |
| 115 | return o; |
| 116 | }, {}); |
| 117 | return LocalInstances.queryFromAll(self.xoss).$promise; |
| 118 | }) |
| 119 | .then(instances => { |
| 120 | this.instances = instances.filter(i => this.slicesId[i.xos.id] === i.slice); |
| 121 | }) |
| 122 | .catch(e => { |
| 123 | this.instances = []; |
| 124 | }); |
| 125 | |
| 126 | this.close = () => { |
| 127 | $uibModalInstance.dismiss('cancel'); |
| 128 | } |
| 129 | } |
| 130 | }) |
| 131 | } |
| 132 | }, |
| 133 | ] |
| 134 | }; |
| 135 | |
| 136 | this.sliceTableCfg = { |
| 137 | columns: baseSliceCols.concat(lXosSliceCols), |
| 138 | actions: [ |
| 139 | { |
| 140 | label: 'open locally', |
| 141 | icon: 'open', |
| 142 | cb: this.openLocally('slice') |
| 143 | }, |
| 144 | { |
| 145 | label: 'Get Instances', |
| 146 | icon: 'search', |
| 147 | cb: (item) => { |
| 148 | $uibModal.open({ |
| 149 | animation: true, |
| 150 | size: 'lg', |
| 151 | templateUrl: 'listInstances.html', |
| 152 | controllerAs: 'vm', |
| 153 | resolve: { |
| 154 | slice: function () { |
| 155 | return item; |
| 156 | } |
| 157 | }, |
| 158 | controller: function($uibModalInstance, slice, LocalInstances) { |
| 159 | this.slice = slice; |
| 160 | |
| 161 | this.config = { |
| 162 | columns: [ |
| 163 | { |
| 164 | label: 'Name', |
| 165 | prop: 'name', |
| 166 | }, |
| 167 | { |
| 168 | label: 'deployment', |
| 169 | prop: 'deployment', |
| 170 | }, |
| 171 | ] |
| 172 | }; |
| 173 | |
| 174 | LocalInstances.getFromLocal(slice.xos) |
| 175 | .then(instances => { |
| 176 | this.instances = instances.filter(i => i.slice === slice.id); |
| 177 | }); |
| 178 | |
| 179 | this.close = () => { |
| 180 | $uibModalInstance.dismiss('cancel'); |
| 181 | } |
| 182 | } |
| 183 | }) |
| 184 | } |
| 185 | }, |
| 186 | { |
| 187 | label: 'Add Instance', |
| 188 | icon: 'plus', |
| 189 | cb: (item) => { |
| 190 | $uibModal.open({ |
| 191 | animation: true, |
| 192 | size: 'lg', |
| 193 | templateUrl: 'addInstance.html', |
| 194 | controller: function($uibModalInstance, slice, LocalInstances){ |
| 195 | this.slice = slice; |
| 196 | |
| 197 | this.model = {}; |
| 198 | |
| 199 | LocalInstances.getLocalInfo(slice.xos) |
| 200 | .then((res) => { |
| 201 | [ |
| 202 | this.config.fields['deployment'].options, |
| 203 | this.config.fields['image'].options, |
| 204 | this.config.fields['flavor'].options, |
| 205 | this.config.fields['node'].options |
| 206 | ] = res; |
| 207 | }); |
| 208 | |
| 209 | this.config = { |
| 210 | formName: 'instanceForm', |
| 211 | excludedFields: ['xos', 'slice'], |
| 212 | actions: [ |
| 213 | { |
| 214 | label: 'Save', |
| 215 | icon: 'ok', |
| 216 | cb: (instance) => { |
| 217 | instance.xos = slice.xos; |
| 218 | instance.slice = slice.id; |
| 219 | instance.creator = instance.xos.user.id; |
| 220 | LocalInstances.createOnLocal(instance) |
| 221 | .then(res => { |
| 222 | slice.instance_total = slice.instance_total + 1; |
| 223 | $uibModalInstance.close(); |
| 224 | }); |
| 225 | }, |
| 226 | class: 'success' |
| 227 | }, |
| 228 | { |
| 229 | label: 'Cancel', |
| 230 | icon: 'remove', |
| 231 | cb: () => { |
| 232 | $uibModalInstance.dismiss('cancel'); |
| 233 | }, |
| 234 | class: 'warning' |
| 235 | } |
| 236 | ], |
| 237 | fields: { |
| 238 | name: { |
| 239 | type: 'text', |
| 240 | validators: { |
| 241 | required: true |
| 242 | } |
| 243 | }, |
| 244 | deployment: { |
| 245 | type: 'select', |
| 246 | validators: { |
| 247 | required: true |
| 248 | } |
| 249 | }, |
| 250 | node: { |
| 251 | type: 'select', |
| 252 | validators: { |
| 253 | required: true |
| 254 | } |
| 255 | }, |
| 256 | image: { |
| 257 | type: 'select', |
| 258 | validators: { |
| 259 | required: true, |
| 260 | } |
| 261 | }, |
| 262 | flavor: { |
| 263 | type: 'select', |
| 264 | validators: { |
| 265 | required: true, |
| 266 | } |
| 267 | }, |
| 268 | isolation: { |
| 269 | type: 'select', |
| 270 | options: [ |
| 271 | {id: 'vm', label: 'VM'}, |
| 272 | {id: 'container', label: 'Container'}, |
| 273 | {id: 'container_vm', label: 'Container in VM'} |
| 274 | ], |
| 275 | validators: { |
| 276 | required: true, |
| 277 | } |
| 278 | }, |
| 279 | } |
| 280 | }; |
| 281 | }, |
| 282 | controllerAs: 'vm', |
| 283 | resolve: { |
| 284 | slice: function () { |
| 285 | return item; |
| 286 | } |
| 287 | } |
| 288 | }); |
| 289 | } |
| 290 | } |
| 291 | ], |
| 292 | filter: 'field', |
| 293 | order: true |
| 294 | }; |
| 295 | |
| 296 | this.usersTableCfg = { |
| 297 | columns: [ |
| 298 | { |
| 299 | label: 'Name', |
| 300 | type: 'custom', |
| 301 | formatter: item => `${item.firstname} ${item.lastname}` |
| 302 | }, |
| 303 | { |
| 304 | label: 'E-Mail', |
| 305 | prop: 'email' |
| 306 | }, |
| 307 | { |
| 308 | label: 'User Name', |
| 309 | prop: 'username' |
| 310 | }, |
| 311 | { |
| 312 | label: 'Time Zone', |
| 313 | prop: 'timezone' |
| 314 | }, |
| 315 | { |
| 316 | label: 'L-XOS', |
| 317 | type: 'custom', |
| 318 | formatter: item => item.xos.name |
| 319 | } |
| 320 | ], |
| 321 | actions: [ |
| 322 | { |
| 323 | label: 'open locally', |
| 324 | icon: 'open', |
| 325 | cb: this.openLocally('user') |
| 326 | } |
| 327 | ], |
| 328 | filter: 'field', |
| 329 | order: true |
| 330 | }; |
| 331 | |
| 332 | this.toggleXos = (xos) => { |
| 333 | if(_.findIndex(LXOS, {id: xos.id}) > -1){ |
| 334 | xos.active = false; |
| 335 | _.remove(LXOS, {id: xos.id}); |
| 336 | } |
| 337 | else{ |
| 338 | xos.active = true; |
| 339 | LXOS.push(xos); |
| 340 | } |
| 341 | |
| 342 | // authenticate on L-XOS |
| 343 | LocalAuth.login() |
| 344 | .then(() => { |
| 345 | // fetch slices |
| 346 | return $q.all([ |
| 347 | LocalSlices.queryFromAll().$promise, |
| 348 | LocalUsers.queryFromAll().$promise, |
| 349 | ]); |
| 350 | }) |
| 351 | .then(res => { |
| 352 | [this.localSlices, this.localUsers] = res; |
| 353 | }) |
| 354 | .catch(e => { |
| 355 | console.log(e); |
| 356 | }); |
| 357 | } |
| 358 | } |
| 359 | }; |
| 360 | }); |