add yardstick iruya 9.0.0 release notes
[yardstick.git] / gui / app / scripts / controllers / image.controller.js
1 'use strict';
2
3 angular.module('yardStickGui2App')
4     .controller('ImageController', ['$scope', '$state', '$stateParams', 'mainFactory', 'Upload', 'toaster', '$location', '$interval', 'ngDialog',
5         function($scope, $state, $stateParams, mainFactory, Upload, toaster, $location, $interval, ngDialog) {
6
7
8             init();
9
10             function init() {
11                 $scope.showloading = false;
12                 $scope.ifshowStatus = 0;
13
14                 $scope.yardstickImage = [
15                     {
16                         'name': 'yardstick-image',
17                         'description': '',
18                         'size': 'N/A',
19                         'status': 'N/A',
20                         'time': 'N/A'
21                     },
22                     {
23                         'name': 'Ubuntu-16.04',
24                         'description': '',
25                         'size': 'N/A',
26                         'status': 'N/A',
27                         'time': 'N/A'
28                     },
29                     {
30                         'name': 'cirros-0.3.5',
31                         'description': '',
32                         'size': 'N/A',
33                         'status': 'N/A',
34                         'time': 'N/A'
35                     }
36                 ];
37                 $scope.customImage = [];
38
39
40                 $scope.uuid = $stateParams.uuid;
41                 $scope.showloading = false;
42                 $scope.url = null;
43                 $scope.environmentInfo = null;
44
45                 getYardstickImageList();
46                 getCustomImageList(function(image, image_id){});
47             }
48
49             function getYardstickImageList(){
50                 mainFactory.ImageList().get({}).$promise.then(function(response){
51                     if(response.status == 1){
52                         angular.forEach($scope.yardstickImage, function(ele, index){
53                             if(typeof(response.result.images[ele.name]) != 'undefined'){
54                                 $scope.yardstickImage[index] = response.result.images[ele.name];
55                             }
56                         });
57                     }else{
58                         mainFactory.errorHandler1(response);
59                     }
60                 }, function(response){
61                     mainFactory.errorHandler2(response);
62                 });
63             }
64
65             function getCustomImageList(func){
66                 mainFactory.ItemDetail().get({
67                     'envId': $stateParams.uuid
68                 }).$promise.then(function(response) {
69                     if(response.status == 1){
70                         $scope.environmentInfo = response.result.environment;
71                         $scope.customImage = [];
72                         angular.forEach(response.result.environment.image_id, function(ele){
73                             mainFactory.getImage().get({'imageId': ele}).$promise.then(function(responseData){
74                                 if(responseData.status == 1){
75                                     $scope.customImage.push(responseData.result.image);
76                                     func(responseData.result.image, ele);
77                                 }else{
78                                     mainFactory.errorHandler1(responseData);
79                                 }
80                             }, function(errorData){
81                                 mainFactory.errorHandler2(errorData);
82                             });
83                         });
84                     }else{
85                         mainFactory.errorHandler1(response);
86                     }
87                 }, function(response){
88                     mainFactory.errorHandler2(response);
89                 });
90             }
91
92             $scope.loadYardstickImage = function(image_name){
93
94                 var updateImageTask = $interval(updateYardstickImage, 10000);
95
96                 function updateYardstickImage(){
97                     mainFactory.ImageList().get({}).$promise.then(function(responseData){
98                         if(responseData.status == 1){
99                             if(typeof(responseData.result.images[image_name]) != 'undefined' && responseData.result.images[image_name].status == 'ACTIVE'){
100                                 angular.forEach($scope.yardstickImage, function(ele, index){
101                                     if(ele.name == image_name){
102                                         $scope.yardstickImage[index] = responseData.result.images[ele.name];
103                                     }
104                                 });
105                                 $interval.cancel(updateImageTask);
106                             }
107                         }else{
108                             mainFactory.errorHandler1(responseData);
109                         }
110                     },function(errorData){
111                         mainFactory.errorHandler2(errorData);
112                     });
113                 }
114
115                 mainFactory.uploadImage().post({'action': 'load_image', 'args': {'name': image_name}}).$promise.then(function(response){
116                 },function(response){
117                     mainFactory.errorHandler2(response);
118                 });
119             }
120
121             $scope.deleteYardstickImage = function(image_name){
122
123                 var updateImageTask = $interval(updateYardstickImage, 10000);
124
125                 function updateYardstickImage(){
126                     mainFactory.ImageList().get({}).$promise.then(function(response){
127                         if(response.status == 1){
128                             if(typeof(response.result.images[image_name]) == 'undefined'){
129                                 angular.forEach($scope.yardstickImage, function(ele, index){
130                                     if(ele.name == image_name){
131                                         $scope.yardstickImage[index].size = 'N/A';
132                                         $scope.yardstickImage[index].status = 'N/A';
133                                         $scope.yardstickImage[index].time = 'N/A';
134                                     }
135                                 });
136                                 $interval.cancel(updateImageTask);
137                             }
138                         }else{
139                             mainFactory.errorHandler1(response);
140                         }
141                     },function(response){
142                         mainFactory.errorHandler2(response);
143                     });
144                 }
145
146                 mainFactory.uploadImage().post({'action': 'delete_image', 'args': {'name': image_name}}).$promise.then(function(response){
147                 },function(response){
148                     mainFactory.errorHandler2(response);
149                 });
150             }
151
152             $scope.uploadCustomImageByUrl = function(url){
153                 mainFactory.uploadImageByUrl().post({
154                     'action': 'upload_image_by_url',
155                     'args': {
156                         'environment_id': $stateParams.uuid,
157                         'url': url
158                     }
159                 }).$promise.then(function(response){
160                     if(response.status == 1){
161                         var updateImageTask = $interval(getCustomImageList, 30000, 10, true, function(image, image_id){
162                             if(image_id == response.result.uuid && image.status == 'ACTIVE'){
163                                 $interval.cancel(updateImageTask);
164                             }
165                         });
166                         ngDialog.close();
167                     }else{
168                         mainFactory.errorHandler1(response);
169                     }
170                 }, function(response){
171                     mainFactory.errorHandler2(response);
172                 });
173             }
174
175             $scope.uploadCustomImage = function($file, $invalidFiles) {
176                 $scope.showloading = true;
177
178                 $scope.displayImageFile = $file;
179                 Upload.upload({
180                     url: Base_URL + '/api/v2/yardstick/images',
181                     data: { file: $file, 'environment_id': $scope.uuid, 'action': 'upload_image' }
182                 }).then(function(response) {
183
184                     $scope.showloading = false;
185                     if (response.data.status == 1) {
186
187                         toaster.pop({
188                             type: 'success',
189                             title: 'upload success',
190                             body: 'you can go next step',
191                             timeout: 3000
192                         });
193
194                         var updateImageTask = $interval(getCustomImageList, 10000, 10, true, function(image, image_id){
195                             if(image_id == response.data.result.uuid && image.status == 'ACTIVE'){
196                                 $interval.cancel(updateImageTask);
197                             }
198                         });
199                     }else{
200                         mainFactory.errorHandler1(response);
201                     }
202
203                 }, function(response) {
204                     $scope.uploadfile = null;
205                     mainFactory.errorHandler2(response);
206                 })
207             }
208
209             $scope.deleteCustomImage = function(image_id){
210                 mainFactory.deleteImage().delete({'imageId': image_id}).$promise.then(function(response){
211                     if(response.status == 1){
212                         $interval(getCustomImageList, 10000, 5, true, function(image, image_id){
213                         });
214                     }else{
215                         mainFactory.errorHandler2(response);
216                     }
217                 }, function(response){
218                     mainFactory.errorHandler2(response);
219                 });
220             }
221
222             $scope.openImageDialog = function(){
223                 $scope.url = null;
224                 ngDialog.open({
225                     preCloseCallback: function(value) {
226                     },
227                     template: 'views/modal/imageDialog.html',
228                     scope: $scope,
229                     className: 'ngdialog-theme-default',
230                     width: 950,
231                     showClose: true,
232                     closeByDocument: false
233                 })
234             }
235
236             $scope.goBack = function goBack() {
237                 $state.go('app.projectList');
238             }
239
240             $scope.goNext = function goNext() {
241                 $scope.path = $location.path();
242                 $scope.uuid = $scope.path.split('/').pop();
243                 $state.go('app.podUpload', { uuid: $scope.uuid });
244             }
245
246         }
247     ]);