arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | angular.module('xos.tenant', [ |
| 4 | 'ngResource', |
| 5 | 'ngCookies', |
| 6 | 'ui.router', |
| 7 | 'xos.helpers' |
| 8 | ]) |
| 9 | .config(($stateProvider) => { |
| 10 | $stateProvider |
| 11 | .state('user-list', { |
| 12 | url: '/', |
| 13 | template: '<users-list></users-list>' |
| 14 | }) |
| 15 | .state('site', { |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 16 | url: '/site/:id', |
| 17 | template: '<site-detail></site-detail>' |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 18 | |
| 19 | }) |
| 20 | .state('createslice', { |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 21 | url: '/site/:site/slice/:id?', |
| 22 | template: '<create-slice></create-slice>' |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 23 | |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 24 | }); |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 25 | }) |
| 26 | .config(function($httpProvider){ |
| 27 | $httpProvider.interceptors.push('NoHyperlinks'); |
| 28 | }) |
| 29 | .directive('usersList', function(){ |
| 30 | return { |
| 31 | //sites : {}, |
| 32 | restrict: 'E', |
| 33 | scope: {}, |
| 34 | bindToController: true, |
| 35 | controllerAs: 'vm', |
| 36 | templateUrl: 'templates/users-list.tpl.html', |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 37 | controller: function(Sites, SlicesPlus){ |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 38 | |
| 39 | |
| 40 | |
| 41 | this.tableConfig = { |
| 42 | columns: [ |
| 43 | { |
| 44 | label: 'Site1', |
| 45 | prop: 'name', |
Matteo Scandolo | 2919495 | 2016-06-17 11:57:05 -0700 | [diff] [blame] | 46 | link: item => `#/site/${item.id}` |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 47 | }, |
| 48 | { |
| 49 | label: 'Allocated', |
| 50 | prop: 'instance_total' |
| 51 | }, |
| 52 | { |
| 53 | label: 'Ready', |
| 54 | prop: 'instance_total_ready' |
| 55 | } |
| 56 | ] |
| 57 | }; |
| 58 | |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 59 | // retrieving user list |
| 60 | Sites.query().$promise |
| 61 | .then((users) => { |
| 62 | this.sites = users; |
| 63 | return SlicesPlus.query().$promise |
| 64 | }) |
| 65 | .then((users) => { |
| 66 | this.slices = users; |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 67 | this.site_list = this.returnData(this.sites, this.slices); |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 68 | }) |
| 69 | .catch((e) => { |
| 70 | throw new Error(e); |
| 71 | }); |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 72 | |
| 73 | |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 74 | this.returnData = (sites, slices) => { |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 75 | var i, j=0; |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 76 | var site_list=[]; |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 77 | |
| 78 | for(i = 0; i<sites.length; i++){ |
| 79 | var instance_t = 0; |
| 80 | var instance_t_r = 0; |
| 81 | for(j=0;j<slices.length;j++){ |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 82 | if (sites[i].id != null && slices[j].site !=null && sites[i].id === slices[j].site){ |
| 83 | instance_t = instance_t + slices[j].instance_total; |
| 84 | instance_t_r = instance_t_r + slices[j].instance_total_ready; |
| 85 | } |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 86 | } |
| 87 | var data_sites = { |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 88 | 'id': sites[i].id, |
| 89 | 'name': sites[i].name, |
| 90 | 'instance_total': instance_t, |
| 91 | 'instance_total_ready': instance_t_r |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 92 | }; |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 93 | site_list.push(data_sites); |
| 94 | } |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 95 | return site_list; |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | }; |
| 99 | }) |
| 100 | .directive('siteDetail', function(){ |
| 101 | return { |
| 102 | restrict: 'E', |
| 103 | scope: {}, |
| 104 | bindToController: true, |
| 105 | controllerAs: 'sl', |
| 106 | templateUrl: 'templates/slicelist.html', |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 107 | controller: function(SlicesPlus, $stateParams){ |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 108 | this.siteId = $stateParams.id; |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 109 | this.tableConfig = { |
| 110 | columns: [ |
| 111 | { |
| 112 | label: 'Slice List', |
| 113 | prop: 'name', |
Matteo Scandolo | 2919495 | 2016-06-17 11:57:05 -0700 | [diff] [blame] | 114 | link: item => `#/site/${item.site}/slice/${item.id}` |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 115 | }, |
| 116 | { |
| 117 | label: 'Allocated', |
| 118 | prop: 'instance_total' |
| 119 | }, |
| 120 | { |
| 121 | label: 'Ready', |
| 122 | prop: 'instance_total_ready' |
| 123 | } |
| 124 | ] |
| 125 | }; |
| 126 | |
| 127 | // retrieving user list |
| 128 | SlicesPlus.query({ |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 129 | site: $stateParams.id |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 130 | }).$promise |
| 131 | .then((users) => { |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 132 | this.sliceList = users; |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 133 | }) |
| 134 | .catch((e) => { |
| 135 | throw new Error(e); |
| 136 | }); |
| 137 | } |
| 138 | }; |
| 139 | }) |
| 140 | .directive('createSlice', function(){ |
| 141 | return { |
| 142 | //sites : {}, |
| 143 | restrict: 'E', |
| 144 | scope: {}, |
| 145 | bindToController: true, |
| 146 | controllerAs: 'cs', |
| 147 | templateUrl: 'templates/createslice.html', |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 148 | controller: function(Slices, SlicesPlus, Sites, Images, $stateParams, $http, $state, $q){ |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 149 | this.config = { |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 150 | exclude: ['site', 'password', 'last_login', 'mount_data_sets', 'default_flavor', 'creator', 'exposed_ports', 'networks', 'omf_friendly', 'omf_friendly', 'no_sync', 'no_policy', 'lazy_blocked', 'write_protect', 'deleted', 'backend_status', 'backend_register', 'policed', 'enacted', 'updated', 'created', 'validators', 'humanReadableName'], |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 151 | formName: 'SliceDetails', |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 152 | feedback: { |
| 153 | show: false, |
| 154 | message: 'Form submitted successfully !!!', |
| 155 | type: 'success' |
| 156 | }, |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 157 | actions: [ |
| 158 | { |
| 159 | label: 'Save', |
| 160 | icon: 'ok', // refers to bootstraps glyphicon |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 161 | cb: (model, form) => { // receive the model |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 162 | saveform(model, form).then(()=> { |
| 163 | $state.go('site', {id: this.model.site}); |
| 164 | }); |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 165 | }, |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 166 | class: 'success' |
| 167 | }, { |
| 168 | label: 'Save and continue editing', |
| 169 | icon: 'ok', // refers to bootstraps glyphicon |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 170 | cb: (model, form) => { // receive the model |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 171 | saveform(model,form); |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 172 | }, |
| 173 | class: 'primary' |
| 174 | }, |
| 175 | { |
| 176 | label: 'Save and add another', |
| 177 | icon: 'ok', // refers to bootstraps glyphicon |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 178 | cb: (model, form) => { |
arpiagariu | abfdab5 | 2016-06-15 10:55:41 -0700 | [diff] [blame] | 179 | saveform(model,form).then(()=> { |
| 180 | $state.go('createslice',{site : this.model.site,id : ''}); |
| 181 | }); |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 182 | }, |
| 183 | class: 'primary' |
| 184 | } |
| 185 | ], |
| 186 | fields: |
| 187 | { |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 188 | site: { |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 189 | label: 'Site', |
| 190 | type: 'select', |
| 191 | validators: { required: true}, |
| 192 | hint: 'The Site this Slice belongs to', |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 193 | options: [] |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 194 | |
| 195 | }, |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 196 | name: { |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 197 | label: 'Name', |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 198 | type: 'string', |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 199 | hint: 'The Name of the Slice', |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 200 | validators: { |
| 201 | required: true |
| 202 | } |
| 203 | }, |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 204 | serviceClass: { |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 205 | label: 'ServiceClass', |
| 206 | type: 'select', |
| 207 | validators: {required: true}, |
| 208 | hint: 'The Site this Slice belongs to', |
| 209 | options: [ |
| 210 | { |
| 211 | id: 1, |
| 212 | label: 'Best effort' |
| 213 | } |
| 214 | ] |
| 215 | }, |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 216 | enabled: { |
| 217 | label: 'Enabled', |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 218 | type: 'boolean', |
| 219 | hint: 'Status for this Slice' |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 220 | }, |
| 221 | description: { |
| 222 | label: 'Description', |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 223 | type: 'string', |
| 224 | hint: 'High level description of the slice and expected activities', |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 225 | validators: { |
| 226 | required: false, |
| 227 | minlength: 10 |
| 228 | } |
| 229 | }, |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 230 | service: { |
| 231 | label: 'Service', |
| 232 | type: 'select', |
| 233 | validators: { required: false}, |
| 234 | options: [ |
| 235 | { |
| 236 | id: 0, |
| 237 | label: '--------' |
| 238 | } |
| 239 | ] |
| 240 | }, |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 241 | slice_url: { |
| 242 | label: 'Slice url', |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 243 | type: 'string', |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 244 | validators: { |
| 245 | required: false, |
| 246 | minlength: 10 |
| 247 | } |
| 248 | }, |
| 249 | max_instances: { |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 250 | label: 'Max Instances', |
| 251 | type: 'number', |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 252 | validators: { |
| 253 | required: false, |
| 254 | min: 0 |
| 255 | } |
| 256 | }, |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 257 | default_isolation: { |
| 258 | label: 'Default Isolation', |
| 259 | type: 'select', |
| 260 | validators: { required: false}, |
| 261 | options: [ |
| 262 | { |
| 263 | id: 'vm', |
| 264 | label: 'Virtual Machine' |
| 265 | }, |
| 266 | { |
| 267 | id: 'container', |
| 268 | label: 'Container' |
| 269 | }, |
| 270 | { |
| 271 | id: 'container_vm', |
| 272 | label: 'Container in VM' |
| 273 | } |
| 274 | ] |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 275 | }, |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 276 | default_image: { |
| 277 | label: 'Default image', |
| 278 | type: 'select', |
| 279 | validators: { required: false}, |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 280 | options: [] |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 281 | }, |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 282 | network: { |
| 283 | label: 'Network', |
| 284 | type: 'select', |
| 285 | validators: { required: false}, |
| 286 | options: [ |
| 287 | { |
| 288 | id: 'default', |
| 289 | label: 'Default' |
| 290 | }, |
| 291 | { |
| 292 | id: 'host', |
| 293 | label: 'Host' |
| 294 | }, |
| 295 | { |
| 296 | id: 'bridged', |
| 297 | label: 'Bridged' |
| 298 | }, |
| 299 | { |
| 300 | id: 'noauto', |
| 301 | label: 'No Automatic Networks' |
| 302 | } |
| 303 | ] |
| 304 | } |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 305 | |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 306 | } |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 307 | }; |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 308 | var data; |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 309 | Images.query().$promise |
| 310 | .then((users) => { |
| 311 | this.users = users; |
| 312 | data = this.users; |
| 313 | this.optionValImg = this.setData(data, {field1: 'id', field2: 'name'}); |
| 314 | this.config.fields['default_image'].options = this.optionValImg; |
| 315 | }) |
| 316 | .catch((e) => { |
| 317 | throw new Error(e); |
| 318 | }); |
| 319 | |
| 320 | // Use this method for select by seting object in fields variable of format { field1 : "val1", field2 : "val2"} |
| 321 | this.setData = (data, fields) => { |
| 322 | var i; |
| 323 | var retObj=[]; |
| 324 | for(i = 0; i<data.length; i++){ |
| 325 | var optVal = {id: data[i][fields.field1], label: data[i][fields.field2]}; |
| 326 | retObj.push(optVal); |
| 327 | |
| 328 | } |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 329 | return retObj; |
| 330 | }; |
| 331 | |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 332 | // retrieving user list |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 333 | |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 334 | if ($stateParams.id) |
| 335 | { |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 336 | delete this.config.fields['site']; |
| 337 | this.config.exclude.push('site'); |
| 338 | |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 339 | Slices.get({id: $stateParams.id}).$promise |
| 340 | .then((users) => { |
| 341 | this.users = users; |
| 342 | data = users; |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 343 | |
| 344 | this.model = data; |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 345 | }) |
| 346 | .catch((e) => { |
| 347 | throw new Error(e); |
| 348 | }); |
| 349 | } |
| 350 | else |
| 351 | { |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 352 | |
| 353 | |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 354 | this.model = {}; |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 355 | $http.get('/xoslib/tenantview/'). |
| 356 | success((data) => { |
| 357 | this.userList = data; |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 358 | this.model['creator'] = this.userList.current_user_id; |
| 359 | |
| 360 | }); |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | Sites.query().$promise |
| 368 | .then((users) => { |
| 369 | this.users_site = users; |
arpiagariu | 4a872ad | 2016-06-10 13:13:36 -0700 | [diff] [blame] | 370 | this.optionVal = this.setData(this.users_site, {field1: 'id', field2: 'name'}); |
| 371 | this.config.fields['site'].options = this.optionVal; |
| 372 | //= this.optionVal; |
| 373 | |
| 374 | }) |
| 375 | .catch((e) => { |
| 376 | throw new Error(e); |
| 377 | }); |
| 378 | |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 379 | } |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 380 | |
| 381 | var saveform = (model,form) => |
| 382 | { // receive the model |
| 383 | var deferred = $q.defer(); |
| 384 | delete model.networks; |
arpiagariu | abfdab5 | 2016-06-15 10:55:41 -0700 | [diff] [blame] | 385 | if (form.$valid ) |
| 386 | { |
| 387 | if(model.id){ |
| 388 | var pr = Slices.update(model).$promise; |
| 389 | } |
| 390 | else{ |
| 391 | var pr = Slices.save(model).$promise; |
| 392 | } |
| 393 | pr.then((users) => { |
| 394 | this.model = users; |
| 395 | //data = users; |
| 396 | //this.model = this.users; |
| 397 | this.config.feedback.show = true; |
| 398 | deferred.resolve(this.model); |
| 399 | }) |
| 400 | .catch((e) => { |
| 401 | this.config.feedback.show = true; |
| 402 | this.config.feedback.type='danger'; |
| 403 | if(e.data && e.data.detail ) |
| 404 | { |
| 405 | this.config.feedback.message = e.data.detail; |
arpiagariu | 10bd9cb | 2016-06-10 13:28:34 -0700 | [diff] [blame] | 406 | } |
arpiagariu | abfdab5 | 2016-06-15 10:55:41 -0700 | [diff] [blame] | 407 | else { |
| 408 | this.config.feedback.message=e.statusText; |
| 409 | } |
| 410 | deferred.reject(e); |
| 411 | }); |
| 412 | } |
| 413 | |
| 414 | return deferred.promise; |
| 415 | } |
arpiagariu | d4f6db1 | 2016-06-06 15:25:28 -0700 | [diff] [blame] | 416 | } |
| 417 | }; |
arpiagariu | 8808639 | 2016-06-07 15:50:10 -0700 | [diff] [blame] | 418 | }); |