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