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