Merge "Sample ACL VNF"
[yardstick.git] / gui / app / scripts / controllers / main.js
1 'use strict';
2
3 angular.module('yardStickGui2App')
4     .controller('MainCtrl', ['$scope', '$state', '$stateParams', 'mainFactory', 'Upload', 'toaster', 'ngDialog', '$localStorage', '$loading', '$interval',
5         function($scope, $state, $stateParams, mainFactory, Upload, toaster, ngDialog, $localStorage, $loading, $interval) {
6
7
8             init();
9             $scope.project = 0;
10             $scope.showloading = false;
11             $scope.showEnvrionment = false;
12             $scope.loadingOPENrc = false;
13             $scope.uuidEnv = null;
14             $scope.showPod = null;
15             $scope.showImage = null;
16             $scope.showContainer = null;
17             $scope.showNextOpenRc = null;
18             $scope.showNextPod = null;
19             $scope.displayContainerInfo = [];
20             $scope.containerList = [{ value: 'create_influxdb', name: "InfluxDB" }, { value: 'create_grafana', name: "Grafana" }]
21             $scope.items = [
22                 'The first choice!',
23                 'And another choice for you.',
24                 'but wait! A third!'
25             ];
26             $scope.$on('$destroy', function() {
27                 $interval.cancel($scope.intervalImgae)
28             });
29             $scope.showImageStatus = 0;
30
31
32
33
34
35
36             function init() {
37
38
39                 $scope.gotoProject = gotoProject;
40                 $scope.gotoEnvironment = gotoEnvironment;
41                 $scope.gotoTask = gotoTask;
42                 $scope.gotoExcute = gotoExcute;
43                 $scope.gotoReport = gotoReport;
44                 $scope.deleteEnvItem = deleteEnvItem;
45                 $scope.addInfo = addInfo;
46                 $scope.submitOpenRcFile = submitOpenRcFile;
47                 $scope.uploadFilesPod = uploadFilesPod;
48                 $scope.uploadFiles = uploadFiles;
49                 $scope.showEnvriomentStatus = showEnvriomentStatus;
50                 $scope.openEnvironmentDialog = openEnvironmentDialog;
51                 $scope.getEnvironmentList = getEnvironmentList;
52                 $scope.gotoDetail = gotoDetail;
53                 $scope.addEnvironment = addEnvironment;
54                 $scope.createContainer = createContainer;
55                 $scope.chooseResult = chooseResult;
56
57                 getEnvironmentList();
58                 // getImageList();
59
60             }
61
62             function gotoProject() {
63                 $scope.project = 1;
64             }
65
66             function gotoEnvironment() {
67                 $scope.project = 0;
68             }
69
70             function gotoTask() {
71                 $scope.project = 2;
72             }
73
74             function gotoExcute() {
75                 $scope.project = 3;
76
77             }
78
79             function gotoReport() {
80                 $scope.project = 4;
81             }
82             $scope.skipPod = function skipPod() {
83                 $scope.showContainer = 1;
84
85             }
86             $scope.skipContainer = function skipContainer() {
87                 getEnvironmentList();
88                 ngDialog.close();
89             }
90
91             $scope.goToImage = function goToImage() {
92                 getImageListSimple();
93                 $scope.showImage = 1;
94             }
95             $scope.goToPod = function goToPod() {
96                 $scope.showPod = 1;
97             }
98             $scope.goToPodPrev = function goToPodPrev() {
99                 $scope.showImage = null;
100
101             }
102             $scope.skipPodPrev = function skipPodPrev() {
103                 $scope.showImage = 1;
104                 $scope.showPod = null;
105
106             }
107             $scope.skipContainerPrev = function skipContainerPrev() {
108                 $scope.showPod = 1;
109                 $scope.showContainer = null;
110             }
111
112             $scope.envInfo = [
113                 { name: 'OS_USERNAME', value: '' },
114                 { name: 'OS_PASSWORD', value: '' },
115                 { name: 'OS_TENANT_NAME', value: '' },
116                 { name: 'EXTERNAL_NETWORK', value: '' }
117             ];
118
119
120             function deleteEnvItem(index) {
121                 $scope.envInfo.splice(index, 1);
122             }
123
124             function addInfo() {
125                 var tempKey = null;
126                 var tempValue = null;
127                 var temp = {
128                     name: tempKey,
129                     value: tempValue
130                 }
131                 $scope.envInfo.push(temp);
132
133             }
134
135             function submitOpenRcFile() {
136                 $scope.showloading = true;
137
138                 var postData = {};
139                 postData['action'] = 'update_openrc';
140                 rebuildEnvInfo();
141                 postData['args'] = {};
142                 postData.args["openrc"] = $scope.postEnvInfo;
143                 postData.args['environment_id'] = $scope.uuidEnv;
144                 mainFactory.postEnvironmentVariable().post(postData).$promise.then(function(response) {
145                     $scope.showloading = false;
146
147                     if (response.status == 1) {
148
149                         $scope.openrcInfo = response.result;
150                         toaster.pop({
151                             type: 'success',
152                             title: 'create success',
153                             body: 'you can go next step',
154                             timeout: 3000
155                         });
156                         $scope.showEnvrionment = true;
157                         // $scope.showImage = response.status;
158                         $scope.showNextOpenRc = 1;
159                     } else {
160                         toaster.pop({
161                             type: 'error',
162                             title: 'fail',
163                             body: response.error_msg,
164                             timeout: 3000
165                         });
166                     }
167
168                 }, function(error) {
169                     toaster.pop({
170                         type: 'error',
171                         title: 'fail',
172                         body: 'unknow error',
173                         timeout: 3000
174                     });
175
176                 })
177             }
178
179             function uploadFiles($file, $invalidFiles) {
180                 $scope.openrcInfo = {};
181                 $scope.loadingOPENrc = true;
182                 $scope.displayOpenrcFile = $file;
183                 timeConstruct($scope.displayOpenrcFile.lastModified);
184                 Upload.upload({
185                     url: Base_URL + '/api/v2/yardstick/openrcs',
186                     data: { file: $file, 'environment_id': $scope.uuidEnv, 'action': 'upload_openrc' }
187                 }).then(function(response) {
188
189                     $scope.loadingOPENrc = false;
190                     if (response.data.status == 1) {
191                         toaster.pop({
192                             type: 'success',
193                             title: 'upload success',
194                             body: 'you can go next step',
195                             timeout: 3000
196                         });
197                         $scope.openrcInfo = response.data.result;
198
199                         getItemIdDetailforOpenrc();
200                         $scope.showNextOpenRc = 1;
201                     } else {
202                         toaster.pop({
203                             type: 'error',
204                             title: 'fail',
205                             body: response.error_msg,
206                             timeout: 3000
207                         });
208                     }
209
210                 }, function(error) {
211                     $scope.uploadfile = null;
212                     toaster.pop({
213                         type: 'error',
214                         title: 'fail',
215                         body: 'unknow error',
216                         timeout: 3000
217                     });
218                 })
219             }
220
221             //reconstruc EnvInfo
222             function rebuildEnvInfo() {
223                 $scope.postEnvInfo = {};
224                 for (var i = 0; i < $scope.envInfo.length; i++) {
225                     $scope.postEnvInfo[$scope.envInfo[i].name] = $scope.envInfo[i].value;
226                 }
227
228             }
229             function uploadFilesPod($file, $invalidFiles) {
230                 $scope.loadingOPENrc = true;
231
232                 $scope.displayPodFile = $file;
233                 timeConstruct($scope.displayPodFile.lastModified);
234                 Upload.upload({
235                     url: Base_URL + '/api/v2/yardstick/pods',
236                     data: { file: $file, 'environment_id': $scope.uuidEnv, 'action': 'upload_pod_file' }
237                 }).then(function(response) {
238
239                     $scope.loadingOPENrc = false;
240                     if (response.data.status == 1) {
241                         toaster.pop({
242                             type: 'success',
243                             title: 'upload success',
244                             body: 'you can go next step',
245                             timeout: 3000
246                         });
247
248                         $scope.podData = response.data.result;
249
250
251                     } else {
252                         toaster.pop({
253                             type: 'error',
254                             title: 'fail',
255                             body: response.error_msg,
256                             timeout: 3000
257                         });
258                     }
259
260                 }, function(error) {
261                     $scope.uploadfile = null;
262                     toaster.pop({
263                         type: 'error',
264                         title: 'fail',
265                         body: 'unknow error',
266                         timeout: 3000
267                     });
268                 })
269             }
270
271             function timeConstruct(array) {
272                 var date = new Date(1398250549490);
273                 var Y = date.getFullYear() + '-';
274                 var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
275                 var D = date.getDate() + ' ';
276                 var h = date.getHours() + ':';
277                 var m = date.getMinutes() + ':';
278                 var s = date.getSeconds();
279                 $scope.filelastModified = Y + M + D + h + m + s;
280
281             }
282
283             //display environment
284             function showEnvriomentStatus() {
285                 $scope.showEnvironment = true;
286             }
287
288             //open Environment dialog
289             function openEnvironmentDialog() {
290                 $scope.showEnvrionment = false;
291                 $scope.loadingOPENrc = false;
292                 $scope.uuidEnv = null;
293                 $scope.showPod = null;
294                 $scope.showImage = null;
295                 $scope.showContainer = null;
296                 $scope.showNextOpenRc = null;
297                 $scope.showNextPod = null;
298                 $scope.displayContainerInfo = [];
299
300                 $scope.displayPodFile = null;
301                 $scope.name = null;
302                 $scope.openrcInfo = null;
303                 $scope.envInfo = [
304                     { name: 'OS_USERNAME', value: '' },
305                     { name: 'OS_PASSWORD', value: '' },
306                     { name: 'OS_TENANT_NAME', value: '' },
307                     { name: 'EXTERNAL_NETWORK', value: '' }
308                 ];
309                 $scope.displayOpenrcFile = null;
310                 $scope.podData = null;
311                 $scope.displayContainerInfo = null;
312                 ngDialog.open({
313                     preCloseCallback: function(value) {
314                         getEnvironmentList();
315                         // getImageList();
316                     },
317                     template: 'views/modal/environmentDialog.html',
318                     scope: $scope,
319                     className: 'ngdialog-theme-default',
320                     width: 950,
321                     showClose: true,
322                     closeByDocument: false
323                 })
324             }
325
326             function getEnvironmentList() {
327                 $loading.start('key');
328
329                 mainFactory.getEnvironmentList().get().$promise.then(function(response) {
330                     $scope.environmentList = response.result.environments;
331                     $loading.finish('key');
332
333                 }, function(error) {
334                     $loading.finish('key');
335                     toaster.pop({
336                         type: 'error',
337                         title: 'fail',
338                         body: 'unknow error',
339                         timeout: 3000
340                     });
341
342                 })
343             }
344
345             //go to detail page
346             function gotoDetail(ifNew, uuid) {
347
348                 $state.go('app.environmentDetail', { uuid: uuid, ifNew: ifNew });
349             }
350
351
352             function addEnvironment(name) {
353                 mainFactory.addEnvName().post({
354                     'action': 'create_environment',
355                     args: {
356                         'name': name
357                     }
358                 }).$promise.then(function(response) {
359                     if (response.status == 1) {
360                         toaster.pop({
361                             type: 'success',
362                             title: 'create name success',
363                             body: 'you can go next step',
364                             timeout: 3000
365                         });
366                         $scope.uuidEnv = response.result.uuid;
367                         $scope.name = name;
368
369                     }
370
371                 }, function(error) {
372                     toaster.pop({
373                         type: 'error',
374                         title: 'fail',
375                         body: 'unknow error',
376                         timeout: 3000
377                     });
378                 })
379             }
380
381
382
383             $scope.goBack = function goBack() {
384                     $state.go('app2.projectList');
385                 }
386             $scope.displayContainerInfo = [];
387
388             function createContainer(selectContainer) {
389
390                 $scope.showloading = true;
391                 mainFactory.runAcontainer().post({
392                     'action': selectContainer.value,
393                     'args': {
394                         'environment_id': $scope.uuidEnv,
395                     }
396                 }).$promise.then(function(response) {
397                     $scope.showloading = false;
398                     if (response.status == 1) {
399                         toaster.pop({
400                             type: 'success',
401                             title: 'create container success',
402                             body: 'you can go next step',
403                             timeout: 3000
404                         });
405
406                         setTimeout(function() {
407                             getItemIdDetail();
408                         }, 10000);
409                         $scope.ifskipOrClose = 1;
410                     } else {
411                         toaster.pop({
412                             type: 'error',
413                             title: 'Wrong',
414                             body: response.result,
415                             timeout: 3000
416                         });
417                     }
418
419                 }, function(error) {
420                     toaster.pop({
421                         type: 'error',
422                         title: 'fail',
423                         body: 'unknow error',
424                         timeout: 3000
425                     });
426                 })
427             }
428
429             function getConDetail(id) {
430                 mainFactory.containerDetail().get({
431                     'containerId': id
432                 }).$promise.then(function(response) {
433                     if (response.status == 1) {
434                         // $scope.podData = response.result;
435                         $scope.displayContainerInfo.push(response.result.container);
436
437                     }
438
439                 }, function(error) {
440                     toaster.pop({
441                         type: 'error',
442                         title: 'fail',
443                         body: 'unknow error',
444                         timeout: 3000
445                     });
446                 })
447
448             }
449
450             function chooseResult(name) {
451                 $scope.selectContainer = name;
452             }
453
454             function getItemIdDetail() {
455                 $scope.displayContainerInfo = [];
456                 mainFactory.ItemDetail().get({
457                     'envId': $scope.uuidEnv
458                 }).$promise.then(function(response) {
459                     if (response.status == 1) {
460                         $scope.envName = response.result.environment.name;
461                         $scope.containerId = response.result.environment.container_id;
462                         if ($scope.containerId != null) {
463
464                             var keysArray = Object.keys($scope.containerId);
465                             for (var k in $scope.containerId) {
466                                 getConDetail($scope.containerId[k]);
467
468                             }
469
470
471                         } else {
472                             $scope.podData = null;
473                         }
474
475                     }
476                 }, function(error) {
477                     toaster.pop({
478                         type: 'error',
479                         title: 'fail',
480                         body: 'unknow error',
481                         timeout: 3000
482                     });
483                 })
484             }
485
486             $scope.uploadImage = function uploadImage() {
487                 $scope.imageStatus = 0;
488                 $scope.showImageStatus = 1;
489                 $scope.showloading = true;
490                 mainFactory.uploadImage().post({
491                     'action': 'load_image',
492                     'args': {
493                         'environment_id': $scope.uuid
494
495                     }
496                 }).$promise.then(function(response) {
497                     $scope.showloading = false;
498                     if (response.status == 1) {
499                         toaster.pop({
500                             type: 'success',
501                             title: 'create success',
502                             body: 'you can go next step',
503                             timeout: 3000
504                         });
505                         setTimeout(function() {
506                             getImageList();
507                         }, 10000);
508                         $scope.showNextPod = 1;
509
510                     } else {
511                         toaster.pop({
512                             type: 'error',
513                             title: 'failed',
514                             body: 'something wrong',
515                             timeout: 3000
516                         });
517
518                     }
519                 }, function(error) {
520                     toaster.pop({
521                         type: 'error',
522                         title: 'failed',
523                         body: 'something wrong',
524                         timeout: 3000
525                     });
526                 })
527             }
528
529             function getImageList() {
530                 if ($scope.intervalImgae != undefined) {
531                     $interval.cancel($scope.intervalImgae);
532                 }
533                 mainFactory.ImageList().get({}).$promise.then(function(response) {
534                     if (response.status == 1) {
535                         $scope.imageListData = response.result.images;
536                         $scope.imageStatus = response.result.status;
537
538                         if ($scope.imageStatus == 0) {
539                             $scope.intervalImgae = $interval(function() {
540                                 getImageList();
541                             }, 5000);
542                         } else if ($scope.intervalImgae != undefined) {
543                             $interval.cancel($scope.intervalImgae);
544                         }
545
546                     } else {
547                         toaster.pop({
548                             type: 'error',
549                             title: 'get data failed',
550                             body: 'please retry',
551                             timeout: 3000
552                         });
553                     }
554                 }, function(error) {
555                     toaster.pop({
556                         type: 'error',
557                         title: 'get data failed',
558                         body: 'please retry',
559                         timeout: 3000
560                     });
561                 })
562             }
563
564             function getImageListSimple() {
565
566                 mainFactory.ImageList().get({}).$promise.then(function(response) {
567                     if (response.status == 1) {
568                         $scope.imageListData = response.result.images;
569                         $scope.imageStatus = response.result.status;
570
571                     } else {
572                         toaster.pop({
573                             type: 'error',
574                             title: 'get data failed',
575                             body: 'please retry',
576                             timeout: 3000
577                         });
578                     }
579                 }, function(error) {
580                     toaster.pop({
581                         type: 'error',
582                         title: 'get data failed',
583                         body: 'please retry',
584                         timeout: 3000
585                     });
586                 })
587             }
588
589             $scope.openDeleteEnv = function openDeleteEnv(id, name) {
590                 $scope.deleteName = name;
591                 $scope.deleteId = id;
592                 ngDialog.open({
593                     template: 'views/modal/deleteConfirm.html',
594                     scope: $scope,
595                     className: 'ngdialog-theme-default',
596                     width: 500,
597                     showClose: true,
598                     closeByDocument: false
599                 })
600
601             }
602
603             $scope.deleteEnv = function deleteEnv() {
604                 mainFactory.deleteEnv().delete({ 'env_id': $scope.deleteId }).$promise.then(function(response) {
605                     if (response.status == 1) {
606                         toaster.pop({
607                             type: 'success',
608                             title: 'delete environment success',
609                             body: 'you can go next step',
610                             timeout: 3000
611                         });
612                         ngDialog.close();
613                         getEnvironmentList();
614                     } else {
615                         toaster.pop({
616                             type: 'error',
617                             title: 'Wrong',
618                             body: response.result,
619                             timeout: 3000
620                         });
621                     }
622
623                 }, function(error) {
624                     toaster.pop({
625                         type: 'error',
626                         title: 'fail',
627                         body: 'unknow error',
628                         timeout: 3000
629                     });
630                 })
631             }
632
633
634
635
636
637
638             function getItemIdDetailforOpenrc() {
639
640                 mainFactory.ItemDetail().get({
641                     'envId': $scope.uuidEnv
642                 }).$promise.then(function(response) {
643                     if (response.status == 1) {
644                         $scope.baseElementInfo = response.result.environment;
645
646
647                         if ($scope.ifNew != 'true') {
648                             $scope.baseElementInfo = response.result.environment;
649                             if ($scope.baseElementInfo.openrc_id != null) {
650                                 getOpenrcDetailForOpenrc($scope.baseElementInfo.openrc_id);
651                             }
652                         }
653
654                     } else {
655                         toaster.pop({
656                             type: 'error',
657                             title: 'fail',
658                             body: response.error_msg,
659                             timeout: 3000
660                         });
661
662                     }
663                 }, function(error) {
664
665                     toaster.pop({
666                         type: 'error',
667                         title: 'fail',
668                         body: 'unknow error',
669                         timeout: 3000
670                     });
671
672                 })
673             }
674
675
676
677             //getopenRcid
678             function getOpenrcDetailForOpenrc(openrcId) {
679                 mainFactory.getEnvironmentDetail().get({
680                     'openrc_id': openrcId
681                 }).$promise.then(function(response) {
682                     $scope.openrcInfo = response.result;
683                     buildToEnvInfoOpenrc($scope.openrcInfo.openrc)
684                 }, function(response) {
685                     toaster.pop({
686                         type: 'error',
687                         title: 'error',
688                         body: 'unknow error',
689                         timeout: 3000
690                     });
691                 })
692             }
693
694             //buildtoEnvInfo
695             function buildToEnvInfoOpenrc(object) {
696                 var tempKeyArray = Object.keys(object);
697                 $scope.envInfo = [];
698
699
700                 for (var i = 0; i < tempKeyArray.length; i++) {
701                     var tempkey = tempKeyArray[i];
702                     var tempValue = object[tempKeyArray[i]];
703                     var temp = {
704                         name: tempkey,
705                         value: tempValue
706                     };
707                     $scope.envInfo.push(temp);
708                 }
709             }
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724         }
725     ]);