Merge "Add common openstack opertation scenarios: network"
[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                 $scope.envInfo=[];
112                 var tempKeyArray = Object.keys(object);
113
114                 for (var i = 0; i < tempKeyArray.length; i++) {
115                     var tempkey = tempKeyArray[i];
116                     var tempValue = object[tempKeyArray[i]];
117                     var temp = {
118                         name: tempkey,
119                         value: tempValue
120                     };
121                     $scope.envInfo.push(temp);
122
123                 }
124
125                 console.log($scope.envInfo);
126                 console.log($scope.openrcInfo);
127             }
128
129             function uploadFiles($file, $invalidFiles) {
130                 $scope.openrcInfo = {};
131                 $scope.loadingOPENrc = true;
132
133                 $scope.displayOpenrcFile = $file;
134                 timeConstruct($scope.displayOpenrcFile.lastModified);
135                 Upload.upload({
136                     url: Base_URL + '/api/v2/yardstick/openrcs',
137                     data: { file: $file, 'environment_id': $scope.uuid, 'action': 'upload_openrc' }
138                 }).then(function(response) {
139
140                     $scope.loadingOPENrc = false;
141                     if (response.data.status == 1) {
142                         toaster.pop({
143                             type: 'success',
144                             title: 'upload success',
145                             body: 'you can go next step',
146                             timeout: 3000
147                         });
148                         $scope.openrcInfo = response.data.result;
149                         getItemIdDetail();
150
151                     } else {
152                         toaster.pop({
153                             type: 'error',
154                             title: 'faile',
155                             body: response.error_msg,
156                             timeout: 3000
157                         });
158                     }
159
160                 }, function(error) {
161                     $scope.uploadfile = null;
162                     toaster.pop({
163                         type: 'error',
164                         title: 'fail',
165                         body: 'unknow error',
166                         timeout: 3000
167                     });
168                 })
169             }
170
171             function timeConstruct(array) {
172                 var date = new Date(1398250549490);
173                 var Y = date.getFullYear() + '-';
174                 var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
175                 var D = date.getDate() + ' ';
176                 var h = date.getHours() + ':';
177                 var m = date.getMinutes() + ':';
178                 var s = date.getSeconds();
179                 $scope.filelastModified = Y + M + D + h + m + s;
180
181             }
182
183             function addEnvironment() {
184                 mainFactory.addEnvName().post({
185                     'action': 'create_environment',
186                     args: {
187                         'name': $scope.baseElementInfo.name
188                     }
189                 }).$promise.then(function(response) {
190                     if (response.status == 1) {
191                         toaster.pop({
192                             type: 'success',
193                             title: 'create name success',
194                             body: 'you can go next step',
195                             timeout: 3000
196                         });
197                         $scope.uuid = response.result.uuid;
198                         var path = $location.path();
199                         path = path + $scope.uuid;
200                         $location.url(path);
201                         getItemIdDetail();
202                     }
203
204                 }, function(error) {
205                     toaster.pop({
206                         type: 'error',
207                         title: 'fail',
208                         body: 'unknow error',
209                         timeout: 3000
210                     });
211                 })
212             }
213
214             function getItemIdDetail() {
215
216                 mainFactory.ItemDetail().get({
217                     'envId': $scope.uuid
218                 }).$promise.then(function(response) {
219                     if (response.status == 1) {
220                         $scope.baseElementInfo = response.result.environment;
221
222
223                         if ($scope.ifNew != 'true') {
224                             $scope.baseElementInfo = response.result.environment;
225                             if ($scope.baseElementInfo.openrc_id != null) {
226                                 getOpenrcDetail($scope.baseElementInfo.openrc_id);
227                             }
228                         }
229
230                     } else {
231                         toaster.pop({
232                             type: 'error',
233                             title: 'fail',
234                             body: response.error_msg,
235                             timeout: 3000
236                         });
237
238                     }
239                 }, function(error) {
240                     toaster.pop({
241                         type: 'error',
242                         title: 'fail',
243                         body: 'unknow error',
244                         timeout: 3000
245                     });
246
247                 })
248             }
249             //getopenRcid
250             function getOpenrcDetail(openrcId) {
251                 mainFactory.getEnvironmentDetail().get({
252                     'openrc_id': openrcId
253                 }).$promise.then(function(response) {
254                     $scope.openrcInfo = response.result;
255                     buildToEnvInfo($scope.openrcInfo.openrc)
256                 }, function(response) {
257
258                 })
259             }
260
261
262             //getImgDetail
263             function getImageDetail() {
264                 mainFactory.ImageDetail().get({
265                     'image_id': $scope.baseElementInfo.image_id
266                 }).$promise.then(function(response) {
267                     if (response.status == 1) {
268                         $scope.imageDetail = response.result.image;
269                     }
270                 }, function(error) {
271                     toaster.pop({
272                         type: 'error',
273                         title: 'fail',
274                         body: 'unknow error',
275                         timeout: 3000
276                     });
277                 })
278             }
279
280             //getPodDetail
281             function getPodDetail() {
282                 mainFactory.podDeatil().get({
283                     'podId': $scope.baseElementInfo.pod_id
284                 }).$promise.then(function(response) {
285                     if (response.status == 1) {
286                         $scope.podDetail = response.result.pod;
287                     }
288                 }, function(error) {
289                     toaster.pop({
290                         type: 'error',
291                         title: 'fail',
292                         body: 'unknow error',
293                         timeout: 3000
294                     });
295                 })
296             }
297             //getContainerDetail
298             function getPodDetail(containerId) {
299                 mainFactory.containerDetail().get({
300                     'containerId': containerId
301                 }).$promise.then(function(response) {
302                     if (response.status == 1) {
303                         $scope.podDetail = response.result.pod;
304                     } else {
305                         toaster.pop({
306                             type: 'error',
307                             title: 'fail',
308                             body: response.error_msg,
309                             timeout: 3000
310                         });
311                     }
312                 }, function(error) {
313                     toaster.pop({
314                         type: 'error',
315                         title: 'fail',
316                         body: 'unknow error',
317                         timeout: 3000
318                     });
319                 })
320             }
321             $scope.goBack = function goBack() {
322                 window.history.back();
323             }
324
325             $scope.goNext = function goNext() {
326                 $scope.path = $location.path();
327                 $scope.uuid = $scope.path.split('/').pop();
328                 $state.go('app.uploadImage', { uuid: $scope.uuid });
329             }
330
331             $scope.openDeleteEnv = function openDeleteEnv(id, name) {
332                 $scope.deleteName = name;
333                 $scope.deleteId = id;
334                 ngDialog.open({
335                     template: 'views/modal/deleteConfirm.html',
336                     scope: $scope,
337                     className: 'ngdialog-theme-default',
338                     width: 500,
339                     showClose: true,
340                     closeByDocument: false
341                 })
342
343             }
344
345             $scope.deleteOpenRc = function deleteOpenRc() {
346                 mainFactory.deleteOpenrc().delete({ 'openrc': $scope.baseElementInfo.openrc_id }).$promise.then(function(response) {
347                     if (response.status == 1) {
348                         toaster.pop({
349                             type: 'success',
350                             title: 'delete openrc success',
351                             body: 'you can go next step',
352                             timeout: 3000
353                         });
354                         ngDialog.close();
355                         getItemIdDetail();
356                         $scope.openrcInfo = null;
357                         $scope.envInfo = [];
358                         $scope.displayOpenrcFile = null;
359                     } else {
360                         toaster.pop({
361                             type: 'error',
362                             title: 'Wrong',
363                             body: response.result,
364                             timeout: 3000
365                         });
366                     }
367
368                 }, function(error) {
369                     toaster.pop({
370                         type: 'error',
371                         title: 'fail',
372                         body: 'unknow error',
373                         timeout: 3000
374                     });
375                 })
376             }
377
378
379
380
381
382
383
384
385
386         }
387
388
389     ]);