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
|
| 38 | saveform(model,form);
|
| 39 | },
|
| 40 | class: 'primary'
|
| 41 | },
|
| 42 | {
|
| 43 | label: 'Save and add another',
|
| 44 | icon: 'ok', // refers to bootstraps glyphicon
|
| 45 | cb: (model, form) => {
|
| 46 | saveform(model,form).then(()=> {
|
| 47 | $state.go('createslice',{site : this.model.site,id : ''});
|
| 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: {
|
| 68 | required: true
|
| 69 | }
|
| 70 | },
|
| 71 | serviceClass: {
|
| 72 | label: 'ServiceClass',
|
| 73 | type: 'select',
|
| 74 | validators: {required: true},
|
| 75 | hint: 'The Site this Slice belongs to',
|
| 76 | options: [
|
| 77 | {
|
| 78 | id: 1,
|
| 79 | label: 'Best effort'
|
| 80 | }
|
| 81 | ]
|
| 82 | },
|
| 83 | enabled: {
|
| 84 | label: 'Enabled',
|
| 85 | type: 'boolean',
|
| 86 | hint: 'Status for this Slice'
|
| 87 | },
|
| 88 | description: {
|
| 89 | label: 'Description',
|
| 90 | type: 'string',
|
| 91 | hint: 'High level description of the slice and expected activities',
|
| 92 | validators: {
|
| 93 | required: false,
|
| 94 | minlength: 10
|
| 95 | }
|
| 96 | },
|
| 97 | service: {
|
| 98 | label: 'Service',
|
| 99 | type: 'select',
|
| 100 | validators: { required: false},
|
| 101 | options: [
|
| 102 | {
|
| 103 | id: 0,
|
| 104 | label: '--------'
|
| 105 | }
|
| 106 | ]
|
| 107 | },
|
| 108 | slice_url: {
|
| 109 | label: 'Slice url',
|
| 110 | type: 'string',
|
| 111 | validators: {
|
| 112 | required: false,
|
| 113 | minlength: 10
|
| 114 | }
|
| 115 | },
|
| 116 | max_instances: {
|
| 117 | label: 'Max Instances',
|
| 118 | type: 'number',
|
| 119 | validators: {
|
| 120 | required: false,
|
| 121 | min: 0
|
| 122 | }
|
| 123 | },
|
| 124 | default_isolation: {
|
| 125 | label: 'Default Isolation',
|
| 126 | type: 'select',
|
| 127 | validators: { required: false},
|
| 128 | options: [
|
| 129 | {
|
| 130 | id: 'vm',
|
| 131 | label: 'Virtual Machine'
|
| 132 | },
|
| 133 | {
|
| 134 | id: 'container',
|
| 135 | label: 'Container'
|
| 136 | },
|
| 137 | {
|
| 138 | id: 'container_vm',
|
| 139 | label: 'Container in VM'
|
| 140 | }
|
| 141 | ]
|
| 142 | },
|
| 143 | default_image: {
|
| 144 | label: 'Default image',
|
| 145 | type: 'select',
|
| 146 | validators: { required: false},
|
| 147 | options: []
|
| 148 | },
|
| 149 | network: {
|
| 150 | label: 'Network',
|
| 151 | type: 'select',
|
| 152 | validators: { required: false},
|
| 153 | options: [
|
| 154 | {
|
| 155 | id: 'default',
|
| 156 | label: 'Default'
|
| 157 | },
|
| 158 | {
|
| 159 | id: 'host',
|
| 160 | label: 'Host'
|
| 161 | },
|
| 162 | {
|
| 163 | id: 'bridged',
|
| 164 | label: 'Bridged'
|
| 165 | },
|
| 166 | {
|
| 167 | id: 'noauto',
|
| 168 | label: 'No Automatic Networks'
|
| 169 | }
|
| 170 | ]
|
| 171 | }
|
| 172 |
|
| 173 | }
|
| 174 | };
|
| 175 | var data;
|
| 176 | Images.query().$promise
|
| 177 | .then((users) => {
|
| 178 | this.users = users;
|
| 179 | data = this.users;
|
| 180 | this.optionValImg = this.setData(data, {field1: 'id', field2: 'name'});
|
| 181 | this.config.fields['default_image'].options = this.optionValImg;
|
| 182 | })
|
| 183 | .catch((e) => {
|
| 184 | throw new Error(e);
|
| 185 | });
|
| 186 |
|
| 187 | // Use this method for select by seting object in fields variable of format { field1 : "val1", field2 : "val2"}
|
| 188 | this.setData = (data, fields) => {
|
| 189 | var i;
|
| 190 | var retObj=[];
|
| 191 | for(i = 0; i<data.length; i++){
|
| 192 | var optVal = {id: data[i][fields.field1], label: data[i][fields.field2]};
|
| 193 | retObj.push(optVal);
|
| 194 |
|
| 195 | }
|
| 196 | return retObj;
|
| 197 | };
|
| 198 |
|
| 199 | // retrieving user list
|
| 200 |
|
| 201 | if ($stateParams.id)
|
| 202 | {
|
| 203 | delete this.config.fields['site'];
|
| 204 | this.config.exclude.push('site');
|
| 205 |
|
| 206 | Slices.get({id: $stateParams.id}).$promise
|
| 207 | .then((users) => {
|
| 208 | this.users = users;
|
| 209 | data = users;
|
| 210 |
|
| 211 | this.model = data;
|
| 212 | })
|
| 213 | .catch((e) => {
|
| 214 | throw new Error(e);
|
| 215 | });
|
| 216 | }
|
| 217 | else
|
| 218 | {
|
| 219 |
|
| 220 |
|
| 221 | this.model = {};
|
| 222 | XosUserPrefs.getUserDetailsCookie().$promise
|
| 223 | .then((userdata)=>{
|
| 224 | this.model['creator'] =userdata.current_user_id;
|
| 225 | })
|
| 226 | .catch ((e) => {
|
| 227 | throw new Error(e);
|
| 228 | });
|
| 229 |
|
| 230 | Sites.query().$promise
|
| 231 | .then((users) => {
|
| 232 | this.users_site = users;
|
| 233 | this.optionVal = this.setData(this.users_site, {field1: 'id', field2: 'name'});
|
| 234 | this.config.fields['site'].options = this.optionVal;
|
| 235 | })
|
| 236 | .catch((e) => {
|
| 237 | throw new Error(e);
|
| 238 | });
|
| 239 | }
|
| 240 |
|
| 241 | var saveform = (model,form) =>
|
| 242 | { // receive the model
|
| 243 | var deferred = $q.defer();
|
| 244 | delete model.networks;
|
| 245 | if (form.$valid )
|
| 246 | {
|
| 247 | if(model.id){
|
| 248 | var pr = Slices.update(model).$promise;
|
| 249 | }
|
| 250 | else{
|
| 251 | var pr = Slices.save(model).$promise;
|
| 252 | }
|
| 253 | pr.then((users) => {
|
| 254 | this.model = users;
|
| 255 | //data = users;
|
| 256 | //this.model = this.users;
|
| 257 | this.config.feedback.show = true;
|
| 258 | deferred.resolve(this.model);
|
| 259 | })
|
| 260 | .catch((e) => {
|
| 261 | this.config.feedback.show = true;
|
| 262 | this.config.feedback.type='danger';
|
| 263 | if(e.data && e.data.detail )
|
| 264 | {
|
| 265 | this.config.feedback.message = e.data.detail;
|
| 266 | }
|
| 267 | else {
|
| 268 | this.config.feedback.message=e.statusText;
|
| 269 | }
|
| 270 | deferred.reject(e);
|
| 271 | });
|
| 272 | }
|
| 273 |
|
| 274 | return deferred.promise;
|
| 275 | }
|
| 276 | }
|
| 277 | };
|
| 278 | }); |