Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 1 | /**
|
| 2 | * Created by arpit on 7/7/2016.
|
| 3 | */
|
| 4 | 'use strict';
|
| 5 |
|
| 6 | angular.module('xos.tenant')
|
| 7 | .directive('createSlice', function(){
|
| 8 | return {
|
| 9 | //sites : {},
|
| 10 | restrict: 'E',
|
| 11 | scope: {},
|
| 12 | bindToController: true,
|
| 13 | controllerAs: 'cs',
|
| 14 | templateUrl: 'templates/createslice.html',
|
| 15 | controller: function(Slices, SlicesPlus, Sites, Images, $stateParams, $http, $state, $q, XosUserPrefs){
|
| 16 | this.config = {
|
| 17 | 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'],
|
| 18 | formName: 'SliceDetails',
|
| 19 | feedback: {
|
| 20 | show: false,
|
| 21 | message: 'Form submitted successfully !!!',
|
| 22 | type: 'success'
|
| 23 | },
|
| 24 | actions: [
|
| 25 | {
|
| 26 | label: 'Save',
|
| 27 | icon: 'ok', // refers to bootstraps glyphicon
|
| 28 | cb: (model, form) => { // receive the model
|
| 29 | saveform(model, form).then(()=> {
|
| 30 | $state.go('site', {id: this.model.site});
|
| 31 | });
|
| 32 | },
|
| 33 | class: 'success'
|
| 34 | }, {
|
| 35 | label: 'Save and continue editing',
|
| 36 | icon: 'ok', // refers to bootstraps glyphicon
|
| 37 | cb: (model, form) => { // receive the model
|
Arpit Agarwal | 16cf8df | 2016-07-13 13:18:44 -0700 | [diff] [blame] | 38 | saveform(model, form);
|
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 39 | },
|
| 40 | class: 'primary'
|
| 41 | },
|
| 42 | {
|
| 43 | label: 'Save and add another',
|
| 44 | icon: 'ok', // refers to bootstraps glyphicon
|
| 45 | cb: (model, form) => {
|
Arpit Agarwal | 16cf8df | 2016-07-13 13:18:44 -0700 | [diff] [blame] | 46 | saveform(model, form).then(()=> {
|
| 47 | $state.go('createslice', { site: this.model.site, id: ''});
|
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 48 | });
|
| 49 | },
|
| 50 | class: 'primary'
|
| 51 | }
|
| 52 | ],
|
| 53 | fields:
|
| 54 | {
|
| 55 | site: {
|
| 56 | label: 'Site',
|
| 57 | type: 'select',
|
| 58 | validators: { required: true},
|
| 59 | hint: 'The Site this Slice belongs to',
|
| 60 | options: []
|
| 61 |
|
| 62 | },
|
| 63 | name: {
|
| 64 | label: 'Name',
|
| 65 | type: 'string',
|
| 66 | hint: 'The Name of the Slice',
|
| 67 | validators: {
|
Arpit Agarwal | 16cf8df | 2016-07-13 13:18:44 -0700 | [diff] [blame] | 68 | required: true,
|
| 69 | custom: (value)=>{
|
| 70 | if(this.model.site){
|
| 71 | var options_list = this.config.fields.site.options;
|
| 72 | var i;
|
| 73 | for (i=0;i<options_list.length;i++){
|
| 74 | if(options_list[i].id === this.model.site)
|
| 75 | {
|
| 76 | if(this.model.name) {
|
| 77 | return (this.model.name.toLowerCase().indexOf(options_list[i].label.toLowerCase()) === 0) ;
|
| 78 | }
|
| 79 | else{
|
| 80 | return false;
|
| 81 | }
|
| 82 | }
|
| 83 | }
|
| 84 | }
|
| 85 | else{
|
| 86 | return false;
|
| 87 | }
|
| 88 | }
|
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 89 | }
|
| 90 | },
|
| 91 | serviceClass: {
|
| 92 | label: 'ServiceClass',
|
| 93 | type: 'select',
|
| 94 | validators: {required: true},
|
| 95 | hint: 'The Site this Slice belongs to',
|
| 96 | options: [
|
| 97 | {
|
| 98 | id: 1,
|
| 99 | label: 'Best effort'
|
| 100 | }
|
| 101 | ]
|
| 102 | },
|
| 103 | enabled: {
|
| 104 | label: 'Enabled',
|
| 105 | type: 'boolean',
|
| 106 | hint: 'Status for this Slice'
|
| 107 | },
|
| 108 | description: {
|
| 109 | label: 'Description',
|
| 110 | type: 'string',
|
| 111 | hint: 'High level description of the slice and expected activities',
|
| 112 | validators: {
|
| 113 | required: false,
|
| 114 | minlength: 10
|
| 115 | }
|
| 116 | },
|
| 117 | service: {
|
| 118 | label: 'Service',
|
| 119 | type: 'select',
|
| 120 | validators: { required: false},
|
| 121 | options: [
|
| 122 | {
|
| 123 | id: 0,
|
| 124 | label: '--------'
|
| 125 | }
|
| 126 | ]
|
| 127 | },
|
| 128 | slice_url: {
|
| 129 | label: 'Slice url',
|
| 130 | type: 'string',
|
| 131 | validators: {
|
| 132 | required: false,
|
| 133 | minlength: 10
|
| 134 | }
|
| 135 | },
|
| 136 | max_instances: {
|
| 137 | label: 'Max Instances',
|
| 138 | type: 'number',
|
| 139 | validators: {
|
| 140 | required: false,
|
| 141 | min: 0
|
| 142 | }
|
| 143 | },
|
| 144 | default_isolation: {
|
| 145 | label: 'Default Isolation',
|
| 146 | type: 'select',
|
| 147 | validators: { required: false},
|
| 148 | options: [
|
| 149 | {
|
| 150 | id: 'vm',
|
| 151 | label: 'Virtual Machine'
|
| 152 | },
|
| 153 | {
|
| 154 | id: 'container',
|
| 155 | label: 'Container'
|
| 156 | },
|
| 157 | {
|
| 158 | id: 'container_vm',
|
| 159 | label: 'Container in VM'
|
| 160 | }
|
| 161 | ]
|
| 162 | },
|
| 163 | default_image: {
|
| 164 | label: 'Default image',
|
| 165 | type: 'select',
|
| 166 | validators: { required: false},
|
| 167 | options: []
|
| 168 | },
|
| 169 | network: {
|
| 170 | label: 'Network',
|
| 171 | type: 'select',
|
| 172 | validators: { required: false},
|
| 173 | options: [
|
| 174 | {
|
| 175 | id: 'default',
|
| 176 | label: 'Default'
|
| 177 | },
|
| 178 | {
|
| 179 | id: 'host',
|
| 180 | label: 'Host'
|
| 181 | },
|
| 182 | {
|
| 183 | id: 'bridged',
|
| 184 | label: 'Bridged'
|
| 185 | },
|
| 186 | {
|
| 187 | id: 'noauto',
|
| 188 | label: 'No Automatic Networks'
|
| 189 | }
|
| 190 | ]
|
| 191 | }
|
| 192 |
|
| 193 | }
|
| 194 | };
|
| 195 | var data;
|
| 196 | Images.query().$promise
|
| 197 | .then((users) => {
|
| 198 | this.users = users;
|
| 199 | data = this.users;
|
| 200 | this.optionValImg = this.setData(data, {field1: 'id', field2: 'name'});
|
| 201 | this.config.fields['default_image'].options = this.optionValImg;
|
| 202 | })
|
| 203 | .catch((e) => {
|
| 204 | throw new Error(e);
|
| 205 | });
|
| 206 |
|
| 207 | // Use this method for select by seting object in fields variable of format { field1 : "val1", field2 : "val2"}
|
| 208 | this.setData = (data, fields) => {
|
| 209 | var i;
|
| 210 | var retObj=[];
|
| 211 | for(i = 0; i<data.length; i++){
|
| 212 | var optVal = {id: data[i][fields.field1], label: data[i][fields.field2]};
|
| 213 | retObj.push(optVal);
|
| 214 |
|
| 215 | }
|
| 216 | return retObj;
|
| 217 | };
|
| 218 |
|
| 219 | // retrieving user list
|
| 220 |
|
| 221 | if ($stateParams.id)
|
| 222 | {
|
| 223 | delete this.config.fields['site'];
|
| 224 | this.config.exclude.push('site');
|
| 225 |
|
| 226 | Slices.get({id: $stateParams.id}).$promise
|
| 227 | .then((users) => {
|
| 228 | this.users = users;
|
| 229 | data = users;
|
| 230 |
|
| 231 | this.model = data;
|
| 232 | })
|
| 233 | .catch((e) => {
|
| 234 | throw new Error(e);
|
| 235 | });
|
| 236 | }
|
| 237 | else
|
| 238 | {
|
| 239 |
|
| 240 |
|
| 241 | this.model = {};
|
| 242 | XosUserPrefs.getUserDetailsCookie().$promise
|
| 243 | .then((userdata)=>{
|
| 244 | this.model['creator'] =userdata.current_user_id;
|
| 245 | })
|
| 246 | .catch ((e) => {
|
| 247 | throw new Error(e);
|
| 248 | });
|
| 249 |
|
| 250 | Sites.query().$promise
|
| 251 | .then((users) => {
|
| 252 | this.users_site = users;
|
| 253 | this.optionVal = this.setData(this.users_site, {field1: 'id', field2: 'name'});
|
| 254 | this.config.fields['site'].options = this.optionVal;
|
| 255 | })
|
| 256 | .catch((e) => {
|
| 257 | throw new Error(e);
|
| 258 | });
|
| 259 | }
|
| 260 |
|
| 261 | var saveform = (model,form) =>
|
| 262 | { // receive the model
|
| 263 | var deferred = $q.defer();
|
| 264 | delete model.networks;
|
| 265 | if (form.$valid )
|
| 266 | {
|
| 267 | if(model.id){
|
| 268 | var pr = Slices.update(model).$promise;
|
| 269 | }
|
| 270 | else{
|
| 271 | var pr = Slices.save(model).$promise;
|
| 272 | }
|
| 273 | pr.then((users) => {
|
| 274 | this.model = users;
|
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 275 | this.config.feedback.show = true;
|
Arpit Agarwal | b8f83f7 | 2016-07-12 14:06:55 -0700 | [diff] [blame] | 276 | deferred.resolve(users);
|
Arpit Agarwal | 711b1ec | 2016-06-27 13:28:54 -0700 | [diff] [blame] | 277 | })
|
| 278 | .catch((e) => {
|
| 279 | this.config.feedback.show = true;
|
| 280 | this.config.feedback.type='danger';
|
| 281 | if(e.data && e.data.detail )
|
| 282 | {
|
| 283 | this.config.feedback.message = e.data.detail;
|
| 284 | }
|
| 285 | else {
|
| 286 | this.config.feedback.message=e.statusText;
|
| 287 | }
|
| 288 | deferred.reject(e);
|
| 289 | });
|
| 290 | }
|
| 291 |
|
| 292 | return deferred.promise;
|
| 293 | }
|
| 294 | }
|
| 295 | };
|
| 296 | }); |