1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
namespace_module.controller("LinkNamespaceController",
['$scope', '$location', '$window', 'toastr', 'AppService', 'AppUtil', 'NamespaceService',
'PermissionService',
function ($scope, $location, $window, toastr, AppService, AppUtil, NamespaceService,
PermissionService) {
var params = AppUtil.parseParams($location.$$url);
$scope.appId = params.appid;
$scope.type = 'link';
$scope.step = 1;
$scope.submitBtnDisabled = false;
PermissionService.has_root_permission().then(function (result) {
$scope.hasRootPermission = result.hasPermission;
});
NamespaceService.find_public_namespaces().then(function (result) {
var publicNamespaces = [];
result.forEach(function (item) {
var namespace = {};
namespace.id = item.name;
namespace.text = item.name;
publicNamespaces.push(namespace);
});
$('#namespaces').select2({
placeholder: '请选择Namespace',
width: '100%',
data: publicNamespaces
});
$(".apollo-container").removeClass("hidden");
}, function (result) {
toastr.error(AppUtil.errorMsg(result), "load public namespace error");
});
AppService.load($scope.appId).then(function (result) {
$scope.appBaseInfo = result;
$scope.appBaseInfo.namespacePrefix = result.orgId + '.';
}, function (result) {
toastr.error(AppUtil.errorMsg(result), "加载App信息出错");
});
$scope.appNamespace = {
appId: $scope.appId,
name: '',
comment: '',
isPublic: true,
format: 'properties'
};
$scope.switchNSType = function (type) {
$scope.appNamespace.isPublic = type;
};
$scope.concatNamespace = function () {
if (!$scope.appBaseInfo) {
return '';
}
return $scope.appBaseInfo.namespacePrefix +
($scope.appNamespace.name ? $scope.appNamespace.name : '');
};
var selectedClusters = [];
$scope.collectSelectedClusters = function (data) {
selectedClusters = data;
};
$scope.createNamespace = function () {
if ($scope.type == 'link') {
if (selectedClusters.length == 0) {
toastr.warning("请选择集群");
return;
}
if ($scope.namespaceType == 1) {
var selectedNamespaceName = $('#namespaces').select2('data')[0].id;
if (!selectedNamespaceName) {
toastr.warning("请选择Namespace");
return;
}
$scope.namespaceName = selectedNamespaceName;
}
var namespaceCreationModels = [];
selectedClusters.forEach(function (cluster) {
namespaceCreationModels.push({
env: cluster.env,
namespace: {
appId: $scope.appId,
clusterName: cluster.clusterName,
namespaceName: $scope.namespaceName
}
});
});
$scope.submitBtnDisabled = true;
NamespaceService.createNamespace($scope.appId, namespaceCreationModels)
.then(function (result) {
toastr.success("创建成功");
$scope.step = 2;
setInterval(function () {
$scope.submitBtnDisabled = false;
$window.location.href =
'/namespace/role.html?#appid=' + $scope.appId
+ "&namespaceName=" + $scope.namespaceName;
}, 1000);
}, function (result) {
$scope.submitBtnDisabled = false;
toastr.error(AppUtil.errorMsg(result));
});
} else {
var namespaceNameLength = $scope.concatNamespace().length;
if (namespaceNameLength > 32){
toastr.error("namespace名称不能大于32个字符. 部门前缀"
+ (namespaceNameLength - $scope.appNamespace.name.length)
+ "个字符, 名称" + $scope.appNamespace.name.length + "个字符"
);
return;
}
$scope.submitBtnDisabled = true;
NamespaceService.createAppNamespace($scope.appId, $scope.appNamespace).then(
function (result) {
$scope.step = 2;
setTimeout(function () {
$scope.submitBtnDisabled = false;
if ($scope.appNamespace.isPublic) {
$window.location.reload();
} else {//private的直接link并且跳转到授权页面
$window.location.href =
"/namespace/role.html?#/appid=" + $scope.appId
+ "&namespaceName=" + result.name;
}
}, 1000);
}, function (result) {
$scope.submitBtnDisabled = false;
toastr.error(AppUtil.errorMsg(result), "创建失败");
});
}
};
$scope.namespaceType = 1;
$scope.selectNamespaceType = function (type) {
$scope.namespaceType = type;
};
$scope.back = function () {
$window.location.href = '/config.html?#appid=' + $scope.appId;
};
$scope.switchType = function (type) {
$scope.type = type;
};
}]);