Merge "Sample ACL VNF"
[yardstick.git] / gui / app / scripts / controllers / detail.controller.js
1 'use strict';
2
3 angular.module('yardStickGui2App')
4     .controller('DetailController', ['$scope', '$state', '$stateParams', 'mainFactory', 'Upload', 'toaster', '$location', 'ngDialog',
5         function($scope, $state, $stateParams, mainFactory, Upload, toaster, $location, ngDialog) {
6
7
8
9
10             init();
11             $scope.showEnvironment = false;
12             $scope.envInfo = [];
13
14             function init() {
15                 $scope.showEnvironments = showEnvironments;
16                 // $scope.openrcID = $stateParams.uuid;
17                 $scope.deleteEnvItem = deleteEnvItem;
18                 $scope.addInfo = addInfo;
19                 $scope.submitOpenRcFile = submitOpenRcFile;
20                 $scope.uploadFiles = uploadFiles;
21                 $scope.addEnvironment = addEnvironment;
22
23                 $scope.uuid = $stateParams.uuid;
24                 $scope.openrcID = $stateParams.opercId;
25                 $scope.imageID = $stateParams.imageId;
26                 $scope.podID = $stateParams.podId;
27                 $scope.containerId = $stateParams.containerId;
28                 $scope.ifNew = $stateParams.ifNew;
29
30
31                 getItemIdDetail();
32             }
33
34
35
36             function showEnvironments() {
37                 $scope.showEnvironment = true;
38             }
39
40
41             function deleteEnvItem(index) {
42                 $scope.envInfo.splice(index, 1);
43             }
44
45             function addInfo() {
46                 var tempKey = null;
47                 var tempValue = null;
48                 var temp = {
49                     name: tempKey,
50                     value: tempValue
51                 }
52                 $scope.envInfo.push(temp);
53
54             }
55
56             function submitOpenRcFile() {
57                 $scope.showloading = true;
58
59                 var postData = {};
60                 postData['action'] = 'update_openrc';
61                 rebuildEnvInfo();
62                 postData['args'] = {};
63                 postData['args']['openrc'] = $scope.postEnvInfo;
64                 postData['args']['environment_id'] = $scope.uuid;
65
66
67                 mainFactory.postEnvironmentVariable().post(postData).$promise.then(function(response) {
68                     $scope.showloading = false;
69
70                     if (response.status == 1) {
71
72                         $scope.openrcInfo = response.result;
73                         toaster.pop({
74                             type: 'success',
75                             title: 'create success',
76                             body: 'you can go next step',
77                             timeout: 3000
78                         });
79                         $scope.showEnvrionment = true;
80                         getItemIdDetail();
81                     } else {
82                         toaster.pop({
83                             type: 'error',
84                             title: 'faile',
85                             body: response.error_msg,
86                             timeout: 3000
87                         });
88                     }
89
90                 }, function(error) {
91                     toaster.pop({
92                         type: 'error',
93                         title: 'fail',
94                         body: 'unknow error',
95                         timeout: 3000
96                     });
97                 })
98             }
99
100             //reconstruc EnvInfo
101             function rebuildEnvInfo() {
102                 $scope.postEnvInfo = {};
103                 for (var i = 0; i < $scope.envInfo.length; i++) {
104                     $scope.postEnvInfo[$scope.envInfo[i].name] = $scope.envInfo[i].value;
105                 }
106
107             }
108
109             //buildtoEnvInfo
110             function buildToEnvInfo(object) {
111                 var tempKeyArray = Object.keys(object);
112
113                 for (var i = 0; i < tempKeyArray.length; i++) {
114                     var tempkey = tempKeyArray[i];
115                     var tempValue = object[tempKeyArray[i]];
116                     var temp = {
117                         name: tempkey,
118                         value: tempValue
119                     };
120                     $scope.envInfo.push(temp);
121                 }
122             }
123
124             function uploadFiles($file, $invalidFiles) {
125                 $scope.openrcInfo = {};
126                 $scope.loadingOPENrc = true;
127
128                 $scope.displayOpenrcFile = $file;
129                 timeConstruct($scope.displayOpenrcFile.lastModified);
130                 Upload.upload({
131                     url: Base_URL + '/api/v2/yardstick/openrcs',
132                     data: { file: $file, 'environment_id': $scope.uuid, 'action': 'upload_openrc' }
133                 }).then(function(response) {
134
135                     $scope.loadingOPENrc = false;
136                     if (response.data.status == 1) {
137                         toaster.pop({
138                             type: 'success',
139                             title: 'upload success',
140                             body: 'you can go next step',
141                             timeout: 3000
142                         });
143                         $scope.openrcInfo = response.data.result;
144                         getItemIdDetail();
145
146                     } else {
147                         toaster.pop({
148                             type: 'error',
149                             title: 'faile',
150                             body: response.error_msg,
151                             timeout: 3000
152                         });
153                     }
154
155                 }, function(error) {
156                     $scope.uploadfile = null;
157                     toaster.pop({
158                         type: 'error',
159                         title: 'fail',
160                         body: 'unknow error',
161                         timeout: 3000
162                     });
163                 })
164             }
165
166             function timeConstruct(array) {
167                 var date = new Date(1398250549490);
168                 var Y = date.getFullYear() + '-';
169                 var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
170                 var D = date.getDate() + ' ';
171                 var h = date.getHours() + ':';
172                 var m = date.getMinutes() + ':';
173                 var s = date.getSeconds();
174                 $scope.filelastModified = Y + M + D + h + m + s;
175
176             }
177
178             function addEnvironment() {
179                 mainFactory.addEnvName().post({
180                     'action': 'create_environment',
181                     args: {
182                         'name': $scope.baseElementInfo.name
183                     }
184                 }).$promise.then(function(response) {
185                     if (response.status == 1) {
186                         toaster.pop({
187                             type: 'success',
188                             title: 'create name success',
189                             body: 'you can go next step',
190                             timeout: 3000
191                         });
192                         $scope.uuid = response.result.uuid;
193                         var path = $location.path();
194                         path = path + $scope.uuid;
195                         $location.url(path);
196                         getItemIdDetail();
197                     }
198
199                 }, function(error) {
200                     toaster.pop({
201                         type: 'error',
202                         title: 'fail',
203                         body: 'unknow error',
204                         timeout: 3000
205                     });
206                 })
207             }
208
209             function getItemIdDetail() {
210
211                 mainFactory.ItemDetail().get({
212                     'envId': $scope.uuid
213                 }).$promise.then(function(response) {
214                     if (response.status == 1) {
215                         $scope.baseElementInfo = response.result.environment;
216
217
218                         if ($scope.ifNew != 'true') {
219                             $scope.baseElementInfo = response.result.environment;
220                             if ($scope.baseElementInfo.openrc_id != null) {
221                                 getOpenrcDetail($scope.baseElementInfo.openrc_id);
222                             }
223                         }
224
225                     } else {
226                         toaster.pop({
227                             type: 'error',
228                             title: 'fail',
229                             body: response.error_msg,
230                             timeout: 3000
231                         });
232
233                     }
234                 }, function(error) {
235                     toaster.pop({
236                         type: 'error',
237                         title: 'fail',
238                         body: 'unknow error',
239                         timeout: 3000
240                     });
241
242                 })
243             }
244             //getopenRcid
245             function getOpenrcDetail(openrcId) {
246                 mainFactory.getEnvironmentDetail().get({
247                     'openrc_id': openrcId
248                 }).$promise.then(function(response) {
249                     $scope.openrcInfo = response.result;
250                     buildToEnvInfo($scope.openrcInfo.openrc)
251                 }, function(response) {
252
253                 })
254             }
255
256
257             //getImgDetail
258             function getImageDetail() {
259                 mainFactory.ImageDetail().get({
260                     'image_id': $scope.baseElementInfo.image_id
261                 }).$promise.then(function(response) {
262                     if (response.status == 1) {
263                         $scope.imageDetail = response.result.image;
264                     }
265                 }, function(error) {
266                     toaster.pop({
267                         type: 'error',
268                         title: 'fail',
269                         body: 'unknow error',
270                         timeout: 3000
271                     });
272                 })
273             }
274
275             //getPodDetail
276             function getPodDetail() {
277                 mainFactory.podDeatil().get({
278                     'podId': $scope.baseElementInfo.pod_id
279                 }).$promise.then(function(response) {
280                     if (response.status == 1) {
281                         $scope.podDetail = response.result.pod;
282                     }
283                 }, function(error) {
284                     toaster.pop({
285                         type: 'error',
286                         title: 'fail',
287                         body: 'unknow error',
288                         timeout: 3000
289                     });
290                 })
291             }
292             //getContainerDetail
293             function getPodDetail(containerId) {
294                 mainFactory.containerDetail().get({
295                     'containerId': containerId
296                 }).$promise.then(function(response) {
297                     if (response.status == 1) {
298                         $scope.podDetail = response.result.pod;
299                     } else {
300                         toaster.pop({
301                             type: 'error',
302                             title: 'fail',
303                             body: response.error_msg,
304                             timeout: 3000
305                         });
306                     }
307                 }, function(error) {
308                     toaster.pop({
309                         type: 'error',
310                         title: 'fail',
311                         body: 'unknow error',
312                         timeout: 3000
313                     });
314                 })
315             }
316             $scope.goBack = function goBack() {
317                 window.history.back();
318             }
319
320             $scope.goNext = function goNext() {
321                 $scope.path = $location.path();
322                 $scope.uuid = $scope.path.split('/').pop();
323                 $state.go('app.uploadImage', { uuid: $scope.uuid });
324             }
325
326             $scope.openDeleteEnv = function openDeleteEnv(id, name) {
327                 $scope.deleteName = name;
328                 $scope.deleteId = id;
329                 ngDialog.open({
330                     template: 'views/modal/deleteConfirm.html',
331                     scope: $scope,
332                     className: 'ngdialog-theme-default',
333                     width: 500,
334                     showClose: true,
335                     closeByDocument: false
336                 })
337
338             }
339
340             $scope.deleteOpenRc = function deleteOpenRc() {
341                 mainFactory.deleteOpenrc().delete({ 'openrc': $scope.baseElementInfo.openrc_id }).$promise.then(function(response) {
342                     if (response.status == 1) {
343                         toaster.pop({
344                             type: 'success',
345                             title: 'delete openrc success',
346                             body: 'you can go next step',
347                             timeout: 3000
348                         });
349                         ngDialog.close();
350                         getItemIdDetail();
351                         $scope.openrcInfo = null;
352                         $scope.envInfo = [];
353                         $scope.displayOpenrcFile = null;
354                     } else {
355                         toaster.pop({
356                             type: 'error',
357                             title: 'Wrong',
358                             body: response.result,
359                             timeout: 3000
360                         });
361                     }
362
363                 }, function(error) {
364                     toaster.pop({
365                         type: 'error',
366                         title: 'fail',
367                         body: 'unknow error',
368                         timeout: 3000
369                     });
370                 })
371             }
372
373
374
375
376
377
378
379
380
381         }
382
383
384     ]);