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