ConfigNamespaceController.js 23.4 KB
Newer Older
L
lepdou 已提交
1
application_module.controller("ConfigNamespaceController",
2 3
                              ['$rootScope', '$scope', '$window', '$location', 'toastr', 'AppUtil', 'ConfigService',
                               'PermissionService',
L
lepdou 已提交
4
                               'CommitService', 'NamespaceLockService', 'UserService', 'ReleaseService',
L
lepdou 已提交
5
                               function ($rootScope, $scope, $window, $location, toastr, AppUtil, ConfigService,
6
                                         PermissionService,
L
lepdou 已提交
7
                                         CommitService, NamespaceLockService, UserService, ReleaseService) {
8

L
lepdou 已提交
9
                                   var namespace_view_type = {
L
lepdou 已提交
10
                                       TEXT: 'text',
L
lepdou 已提交
11
                                       TABLE: 'table',
Z
zhangle 已提交
12
                                       HISTORY: 'history'
L
lepdou 已提交
13 14
                                   };

L
lepdou 已提交
15 16 17 18 19 20
                                   var TABLE_VIEW_OPER_TYPE = {
                                       CREATE: 'create',
                                       UPDATE: 'update'
                                   };

                                   $rootScope.refreshNamespaces = refreshNamespaces;
L
lepdou 已提交
21

L
lepdou 已提交
22
                                   $scope.commitChange = commitChange;
L
lepdou 已提交
23

L
lepdou 已提交
24
                                   $scope.prepareReleaseNamespace = prepareReleaseNamespace;
L
lepdou 已提交
25

L
lepdou 已提交
26
                                   $scope.release = release;
27 28
                                   
                                   $scope.switchReleaseChangeViewType = switchReleaseChangeViewType;
L
lepdou 已提交
29

L
lepdou 已提交
30
                                   $scope.showRollbackAlertDialog = showRollbackAlertDialog;
L
lepdou 已提交
31 32 33 34 35

                                   $scope.preRollback = preRollback;

                                   $scope.rollback = rollback;

L
lepdou 已提交
36
                                   $scope.preDeleteItem = preDeleteItem;
L
lepdou 已提交
37

L
lepdou 已提交
38
                                   $scope.deleteItem = deleteItem;
L
lepdou 已提交
39

L
lepdou 已提交
40
                                   $scope.editItem = editItem;
41
                                   
L
lepdou 已提交
42
                                   $scope.createItem = createItem;
43

L
lepdou 已提交
44
                                   $scope.doItem = doItem;
Z
zhangle 已提交
45 46
                                   
                                   $scope.closeTip = closeTip;
Z
zhangle 已提交
47 48
                                   
                                   $scope.showText = showText;
49 50

                                   $scope.releaseBtnDisabled = false;
L
lepdou 已提交
51
                                   $scope.rollbackBtnDisabled = false;
52 53
                                   $scope.addItemBtnDisabled = false;
                                   $scope.commitChangeBtnDisabled = false;
Z
zhangle 已提交
54 55 56 57 58 59 60 61 62 63 64 65
                                   
                                   init();
                                   
                                   function init() {
                                       PermissionService.get_app_role_users($rootScope.pageContext.appId)
                                           .then(function (result) {
                                               var masterUsers = '';
                                               result.masterUsers.forEach(function (user) {
                                                   masterUsers += user.userId + ',';
                                               });
                                               $scope.masterUsers = masterUsers.substring(0, masterUsers.length - 1);
                                           }, function (result) {
66

L
lepdou 已提交
67 68
                                           });

Z
zhangle 已提交
69 70
                                       UserService.load_user().then(function (result) {
                                           $scope.currentUser = result.userId;
L
lepdou 已提交
71
                                       });
Z
zhangle 已提交
72 73
                                       
                                   }
L
lepdou 已提交
74

Z
zhangle 已提交
75
                                   
L
lepdou 已提交
76 77

                                   function refreshNamespaces(viewType) {
L
lepdou 已提交
78
                                       if ($rootScope.pageContext.env == '') {
79 80
                                           return;
                                       }
L
lepdou 已提交
81 82 83 84
                                       ConfigService.load_all_namespaces($rootScope.pageContext.appId,
                                                                         $rootScope.pageContext.env,
                                                                         $rootScope.pageContext.clusterName,
                                                                         viewType).then(
L
lepdou 已提交
85
                                           function (result) {
86

L
lepdou 已提交
87
                                               $scope.namespaces = result;
L
lepdou 已提交
88
                                               
L
lepdou 已提交
89
                                           }, function (result) {
L
lepdou 已提交
90
                                               toastr.error(AppUtil.errorMsg(result), "加载配置信息出错");
L
lepdou 已提交
91
                                           });
L
lepdou 已提交
92
                                   }
L
lepdou 已提交
93

L
lepdou 已提交
94 95 96
                                   function commitChange(namespace) {
                                       var model = {
                                           configText: namespace.editText,
L
lepdou 已提交
97
                                           namespaceId: namespace.baseInfo.id,
L
lepdou 已提交
98 99
                                           format: namespace.format
                                       };
100 101

                                       //prevent repeat submit
L
lepdou 已提交
102
                                       if ($scope.commitChangeBtnDisabled) {
103 104 105
                                           return;
                                       }
                                       $scope.commitChangeBtnDisabled = true;
L
lepdou 已提交
106 107
                                       ConfigService.modify_items($rootScope.pageContext.appId,
                                                                  $rootScope.pageContext.env,
L
lepdou 已提交
108
                                                                  $rootScope.pageContext.clusterName,
L
lepdou 已提交
109
                                                                  namespace.baseInfo.namespaceName,
L
lepdou 已提交
110
                                                                  model).then(
L
lepdou 已提交
111
                                           function (result) {
L
lepdou 已提交
112
                                               toastr.success("更新成功, 如需生效请发布");
L
lepdou 已提交
113
                                               //refresh all namespace items
L
lepdou 已提交
114
                                               $rootScope.refreshNamespaces();
115
                                               $scope.commitChangeBtnDisabled = false;
L
lepdou 已提交
116
                                               return true;
L
lepdou 已提交
117

L
lepdou 已提交
118
                                           }, function (result) {
L
lepdou 已提交
119
                                               toastr.error(AppUtil.errorMsg(result), "更新失败");
120
                                               $scope.commitChangeBtnDisabled = false;
L
lepdou 已提交
121
                                               return false;
L
lepdou 已提交
122 123
                                           }
                                       );
L
lepdou 已提交
124
                                   }
125

L
lepdou 已提交
126
                                   var releaseModal = $('#releaseModal');
127
                                   $scope.toReleaseNamespace = {};
L
lepdou 已提交
128
                                   function prepareReleaseNamespace(namespace) {
129
                                       if (!namespace.hasReleasePermission) {
L
lepdou 已提交
130 131
                                           $('#releaseNoPermissionDialog').modal('show');
                                           return;
L
lepdou 已提交
132
                                       } else if (namespace.lockOwner && $scope.currentUser == namespace.lockOwner) {
133 134 135
                                           //自己修改不能自己发布
                                           $('#releaseDenyDialog').modal('show');
                                       } else {
L
lepdou 已提交
136 137
                                           $('#releaseModal').modal('show');
                                       }
L
lepdou 已提交
138
                                       $scope.releaseTitle = new Date().Format("yyyyMMddhhmmss") + "-release";
139
                                       $scope.toReleaseNamespace = namespace;
L
lepdou 已提交
140
                                   }
141

L
lepdou 已提交
142
                                   $scope.releaseComment = '';
L
lepdou 已提交
143
                                   function release() {
L
lepdou 已提交
144

145
                                       $scope.releaseBtnDisabled = true;
L
lepdou 已提交
146
                                       ReleaseService.release($rootScope.pageContext.appId, $rootScope.pageContext.env,
L
lepdou 已提交
147 148 149 150
                                                              $rootScope.pageContext.clusterName,
                                                              $scope.toReleaseNamespace.baseInfo.namespaceName,
                                                              $scope.releaseTitle,
                                                              $scope.releaseComment).then(
L
lepdou 已提交
151
                                           function (result) {
L
lepdou 已提交
152
                                               releaseModal.modal('hide');
L
lepdou 已提交
153 154
                                               toastr.success("发布成功");
                                               //refresh all namespace items
155
                                               $scope.releaseBtnDisabled = false;
L
lepdou 已提交
156
                                               $rootScope.refreshNamespaces();
L
lepdou 已提交
157

L
lepdou 已提交
158
                                           }, function (result) {
159
                                               $scope.releaseBtnDisabled = false;
L
lepdou 已提交
160
                                               toastr.error(AppUtil.errorMsg(result), "发布失败");
L
lepdou 已提交
161

L
lepdou 已提交
162
                                           }
L
lepdou 已提交
163
                                       );
L
lepdou 已提交
164
                                   }
165 166 167 168 169
                                   
                                   $scope.releaseChangeViewType = 'change';
                                   function switchReleaseChangeViewType(type) {
                                       $scope.releaseChangeViewType = type;
                                   }
L
lepdou 已提交
170

L
lepdou 已提交
171 172 173
                                   function showRollbackAlertDialog() {
                                       $("#rollbackModal").modal('hide');
                                       $("#rollbackAlertDialog").modal('show');
L
lepdou 已提交
174
                                   }
L
lepdou 已提交
175

L
lepdou 已提交
176
                                   
L
lepdou 已提交
177 178 179
                                   $scope.toRollbackNamespace = {};
                                   function preRollback(namespace) {
                                       $scope.toRollbackNamespace = namespace;
L
lepdou 已提交
180
                                       //load latest two active releases
Z
zhangle 已提交
181 182 183 184
                                       ReleaseService.findActiveReleases($rootScope.pageContext.appId,
                                                                         $rootScope.pageContext.env,
                                                                         $rootScope.pageContext.clusterName,
                                                                         $scope.toRollbackNamespace.baseInfo.namespaceName, 0, 2)
L
lepdou 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197
                                           .then(function (result) {
                                               if (result.length <= 1) {
                                                   toastr.error("没有可以回滚的发布历史");
                                                   return;
                                               }
                                               $scope.firstRelease = result[0];
                                               $scope.secondRelease = result[1];

                                               ReleaseService.compare($rootScope.pageContext.env,
                                                                      $scope.firstRelease.id,
                                                                      $scope.secondRelease.id)
                                                   .then(function (result) {
                                                       $scope.releaseCompareResult = result.changes;
L
lepdou 已提交
198

L
lepdou 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
                                                       $("#rollbackModal").modal('show');
                                                   }, function (result) {
                                                       toastr.error(AppUtil.errorMsg(result), "对比失败");
                                                   })
                                           }, function (result) {
                                               toastr.error(AppUtil.errorMsg(result), "加载最近两次发布失败");
                                           });
                                   }

                                   function rollback() {
                                       $scope.rollbackBtnDisabled = true;
                                       ReleaseService.rollback(
                                                               $rootScope.pageContext.env,
                                                               $scope.firstRelease.id)
                                           .then(function (result) {
                                               toastr.success("回滚成功");
                                               $scope.rollbackBtnDisabled = false;
                                               $("#rollbackModal").modal("hide");
                                               $rootScope.refreshNamespaces();
                                           }, function (result) {
                                               $scope.rollbackBtnDisabled = false;
                                               toastr.error(AppUtil.errorMsg(result), "回滚失败");
                                           })
                                   }
L
lepdou 已提交
223 224

                                   $scope.tableViewOperType = '', $scope.item = {};
L
bugfix  
lepdou 已提交
225
                                   var toOperationNamespace;
L
lepdou 已提交
226

L
bugfix  
lepdou 已提交
227
                                   var toDeleteItemId = 0;
L
lepdou 已提交
228

L
lepdou 已提交
229
                                   function preDeleteItem(namespace, itemId) {
L
lepdou 已提交
230
                                       if (!lockCheck(namespace)) {
231 232 233
                                           return;
                                       }

L
bugfix  
lepdou 已提交
234
                                       toOperationNamespace = namespace;
235
                                       toDeleteItemId = itemId;
236 237

                                       $("#deleteConfirmDialog").modal("show");
L
lepdou 已提交
238
                                   }
239

L
lepdou 已提交
240
                                   function deleteItem() {
L
lepdou 已提交
241 242 243
                                       ConfigService.delete_item($rootScope.pageContext.appId,
                                                                 $rootScope.pageContext.env,
                                                                 $rootScope.pageContext.clusterName,
L
lepdou 已提交
244
                                                                 toOperationNamespace.baseInfo.namespaceName,
L
lepdou 已提交
245
                                                                 toDeleteItemId).then(
246 247 248 249 250 251
                                           function (result) {
                                               toastr.success("删除成功!");
                                               $rootScope.refreshNamespaces();
                                           }, function (result) {
                                               toastr.error(AppUtil.errorMsg(result), "删除失败");
                                           });
L
lepdou 已提交
252
                                   }
253

L
lepdou 已提交
254
                                   //修改配置
L
lepdou 已提交
255
                                   function editItem(namespace, item) {
L
lepdou 已提交
256
                                       if (!lockCheck(namespace)) {
257 258
                                           return;
                                       }
L
lepdou 已提交
259
                                       switchTableViewOperType(TABLE_VIEW_OPER_TYPE.UPDATE);
Z
zhangle 已提交
260
                                       $scope.item = _.clone(item);
L
bugfix  
lepdou 已提交
261
                                       toOperationNamespace = namespace;
262 263

                                       $("#itemModal").modal("show");
L
lepdou 已提交
264
                                   }
265
                                   
L
lepdou 已提交
266
                                   //新增配置
L
lepdou 已提交
267
                                   function createItem(namespace) {
L
lepdou 已提交
268
                                       if (!lockCheck(namespace)) {
269 270 271
                                           return;
                                       }

L
lepdou 已提交
272 273
                                       switchTableViewOperType(TABLE_VIEW_OPER_TYPE.CREATE);
                                       $scope.item = {};
L
bugfix  
lepdou 已提交
274
                                       toOperationNamespace = namespace;
275
                                       $('#itemModal').modal('show');
L
lepdou 已提交
276
                                   }
L
lepdou 已提交
277 278 279 280 281 282 283 284 285 286 287

                                   var selectedClusters = [];
                                   $scope.collectSelectedClusters = function (data) {
                                       selectedClusters = data;
                                   };

                                   function switchTableViewOperType(type) {
                                       $scope.tableViewOperType = type;
                                   }

                                   var itemModal = $("#itemModal");
L
lepdou 已提交
288

L
lepdou 已提交
289
                                   function doItem() {
L
lepdou 已提交
290 291 292 293

                                       if (selectedClusters.length == 0) {
                                           toastr.error("请选择集群");
                                       } else {
L
lepdou 已提交
294 295 296
                                           if (!$scope.item.value) {
                                               $scope.item.value = "";
                                           }
L
lepdou 已提交
297 298
                                           selectedClusters.forEach(function (cluster) {
                                               if ($scope.tableViewOperType == TABLE_VIEW_OPER_TYPE.CREATE) {
L
lepdou 已提交
299

L
bugfix  
lepdou 已提交
300 301 302
                                                   //check key unique
                                                   var hasRepeatKey = false;
                                                   toOperationNamespace.items.forEach(function (item) {
L
lepdou 已提交
303 304 305 306 307
                                                       if (!item.isDeleted && $scope.item.key == item.item.key) {
                                                           toastr.error("key=" + $scope.item.key + " 已存在");
                                                           hasRepeatKey = true;
                                                           return;
                                                       }
L
bugfix  
lepdou 已提交
308
                                                   });
L
lepdou 已提交
309
                                                   if (hasRepeatKey) {
L
bugfix  
lepdou 已提交
310 311
                                                       return;
                                                   }
L
lepdou 已提交
312

313
                                                   $scope.addItemBtnDisabled = true;
L
lepdou 已提交
314 315 316
                                                   ConfigService.create_item($rootScope.pageContext.appId,
                                                                             cluster.env,
                                                                             cluster.name,
L
lepdou 已提交
317
                                                                             toOperationNamespace.baseInfo.namespaceName,
L
lepdou 已提交
318 319
                                                                             $scope.item).then(
                                                       function (result) {
L
lepdou 已提交
320 321
                                                           toastr.success(cluster.env + " , " + $scope.item.key,
                                                                          "添加成功");
L
lepdou 已提交
322
                                                           itemModal.modal('hide');
323
                                                           $scope.addItemBtnDisabled = false;
L
lepdou 已提交
324 325
                                                           $rootScope.refreshNamespaces(namespace_view_type.TABLE);
                                                       }, function (result) {
326
                                                           $scope.addItemBtnDisabled = false;
L
lepdou 已提交
327
                                                           toastr.error(AppUtil.errorMsg(result), "添加失败");
L
lepdou 已提交
328 329 330
                                                       });

                                               } else if ($scope.tableViewOperType == TABLE_VIEW_OPER_TYPE.UPDATE) {
L
lepdou 已提交
331

332
                                                   if (!$scope.item.comment) {
L
lepdou 已提交
333 334
                                                       $scope.item.comment = "";
                                                   }
L
lepdou 已提交
335

L
lepdou 已提交
336 337 338
                                                   ConfigService.update_item($rootScope.pageContext.appId,
                                                                             cluster.env,
                                                                             cluster.name,
L
lepdou 已提交
339
                                                                             toOperationNamespace.baseInfo.namespaceName,
L
lepdou 已提交
340 341
                                                                             $scope.item).then(
                                                       function (result) {
L
lepdou 已提交
342
                                                           toastr.success("更新成功, 如需生效请发布");
L
lepdou 已提交
343 344 345
                                                           itemModal.modal('hide');
                                                           $rootScope.refreshNamespaces(namespace_view_type.TABLE);
                                                       }, function (result) {
L
lepdou 已提交
346
                                                           toastr.error(AppUtil.errorMsg(result), "更新失败");
L
lepdou 已提交
347 348 349 350 351
                                                       });
                                               }
                                           });
                                       }

L
lepdou 已提交
352
                                   }
L
lepdou 已提交
353

L
lepdou 已提交
354
                                   function lockCheck(namespace) {
L
bugfix  
lepdou 已提交
355 356
                                       if (namespace.lockOwner && $scope.currentUser != namespace.lockOwner) {
                                           $scope.lockOwner = namespace.lockOwner;
L
lepdou 已提交
357
                                           $('#namespaceLockedDialog').modal('show');
358 359
                                           return false;
                                       }
L
lepdou 已提交
360 361
                                       return true;
                                   }
362

Z
zhangle 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
                                   function closeTip(clusterName) {
                                       var hideTip = JSON.parse(localStorage.getItem("hideTip"));
                                       if (!hideTip){
                                           hideTip = {};
                                           hideTip[$rootScope.pageContext.appId] = {};
                                       }
                                       
                                       if (!hideTip[$rootScope.pageContext.appId]){
                                           hideTip[$rootScope.pageContext.appId] = {};
                                       }

                                       hideTip[$rootScope.pageContext.appId][clusterName] = true;
                                       
                                       $rootScope.hideTip = hideTip;
                                       
                                       localStorage.setItem("hideTip", JSON.stringify(hideTip));
                                       
                                   }
Z
zhangle 已提交
381 382 383 384 385 386
                                   
                                   function showText(text) {
                                       $scope.text = text;
                                       $('#showText').modal('show');
                                   }
                                   
L
lepdou 已提交
387
                                   $('.config-item-container').removeClass('hide');
Z
zhangle 已提交
388 389

                                   new Clipboard('.clipboard');
L
lepdou 已提交
390 391
                               }]);