提交 | 用户 | 时间
|
e7c126
|
1 |
/* |
H |
2 |
* Copyright 1999-2018 Alibaba Group Holding Ltd. |
|
3 |
* |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
*/ |
|
16 |
|
|
17 |
package com.alibaba.nacos.console.controller; |
|
18 |
|
|
19 |
import com.alibaba.nacos.api.exception.NacosException; |
|
20 |
import com.alibaba.nacos.auth.annotation.Secured; |
|
21 |
import com.alibaba.nacos.common.model.RestResult; |
|
22 |
import com.alibaba.nacos.common.model.RestResultUtils; |
|
23 |
import com.alibaba.nacos.common.utils.StringUtils; |
|
24 |
import com.alibaba.nacos.console.paramcheck.ConsoleDefaultHttpParamExtractor; |
|
25 |
import com.alibaba.nacos.core.namespace.repository.NamespacePersistService; |
|
26 |
import com.alibaba.nacos.core.namespace.model.Namespace; |
|
27 |
import com.alibaba.nacos.core.paramcheck.ExtractorManager; |
|
28 |
import com.alibaba.nacos.core.service.NamespaceOperationService; |
|
29 |
import com.alibaba.nacos.plugin.auth.constant.ActionTypes; |
|
30 |
import com.alibaba.nacos.plugin.auth.impl.constant.AuthConstants; |
|
31 |
import org.springframework.beans.factory.annotation.Autowired; |
|
32 |
import org.springframework.web.bind.annotation.DeleteMapping; |
|
33 |
import org.springframework.web.bind.annotation.GetMapping; |
|
34 |
import org.springframework.web.bind.annotation.PostMapping; |
|
35 |
import org.springframework.web.bind.annotation.PutMapping; |
|
36 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
37 |
import org.springframework.web.bind.annotation.RequestParam; |
|
38 |
import org.springframework.web.bind.annotation.RestController; |
|
39 |
|
|
40 |
import java.util.List; |
|
41 |
import java.util.UUID; |
|
42 |
import java.util.regex.Pattern; |
|
43 |
|
|
44 |
/** |
|
45 |
* namespace service. |
|
46 |
* |
|
47 |
* @author Nacos |
|
48 |
*/ |
|
49 |
@RestController |
|
50 |
@RequestMapping("/v1/console/namespaces") |
|
51 |
@ExtractorManager.Extractor(httpExtractor = ConsoleDefaultHttpParamExtractor.class) |
|
52 |
public class NamespaceController { |
|
53 |
|
|
54 |
@Autowired |
|
55 |
private NamespacePersistService namespacePersistService; |
|
56 |
|
|
57 |
@Autowired |
|
58 |
private NamespaceOperationService namespaceOperationService; |
|
59 |
|
|
60 |
private final Pattern namespaceIdCheckPattern = Pattern.compile("^[\\w-]+"); |
|
61 |
|
|
62 |
private final Pattern namespaceNameCheckPattern = Pattern.compile("^[^@#$%^&*]+$"); |
|
63 |
|
|
64 |
private static final int NAMESPACE_ID_MAX_LENGTH = 128; |
|
65 |
|
|
66 |
/** |
|
67 |
* Get namespace list. |
|
68 |
* |
|
69 |
* @return namespace list |
|
70 |
*/ |
|
71 |
@GetMapping |
|
72 |
public RestResult<List<Namespace>> getNamespaces() { |
|
73 |
return RestResultUtils.success(namespaceOperationService.getNamespaceList()); |
|
74 |
} |
|
75 |
|
|
76 |
/** |
|
77 |
* get namespace all info by namespace id. |
|
78 |
* |
|
79 |
* @param namespaceId namespaceId |
|
80 |
* @return namespace all info |
|
81 |
*/ |
|
82 |
@GetMapping(params = "show=all") |
|
83 |
public Namespace getNamespace(@RequestParam("namespaceId") String namespaceId) throws NacosException { |
|
84 |
return namespaceOperationService.getNamespace(namespaceId); |
|
85 |
} |
|
86 |
|
|
87 |
/** |
|
88 |
* create namespace. |
|
89 |
* |
|
90 |
* @param namespaceName namespace Name |
|
91 |
* @param namespaceDesc namespace Desc |
|
92 |
* @return whether create ok |
|
93 |
*/ |
|
94 |
@PostMapping |
|
95 |
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE) |
|
96 |
public Boolean createNamespace(@RequestParam("customNamespaceId") String namespaceId, |
|
97 |
@RequestParam("namespaceName") String namespaceName, |
|
98 |
@RequestParam(value = "namespaceDesc", required = false) String namespaceDesc) { |
|
99 |
if (StringUtils.isBlank(namespaceId)) { |
|
100 |
namespaceId = UUID.randomUUID().toString(); |
|
101 |
} else { |
|
102 |
namespaceId = namespaceId.trim(); |
|
103 |
if (!namespaceIdCheckPattern.matcher(namespaceId).matches()) { |
|
104 |
return false; |
|
105 |
} |
|
106 |
if (namespaceId.length() > NAMESPACE_ID_MAX_LENGTH) { |
|
107 |
return false; |
|
108 |
} |
|
109 |
// check unique |
|
110 |
if (namespacePersistService.tenantInfoCountByTenantId(namespaceId) > 0) { |
|
111 |
return false; |
|
112 |
} |
|
113 |
} |
|
114 |
// contains illegal chars |
|
115 |
if (!namespaceNameCheckPattern.matcher(namespaceName).matches()) { |
|
116 |
return false; |
|
117 |
} |
|
118 |
try { |
|
119 |
return namespaceOperationService.createNamespace(namespaceId, namespaceName, namespaceDesc); |
|
120 |
} catch (NacosException e) { |
|
121 |
return false; |
|
122 |
} |
|
123 |
} |
|
124 |
|
|
125 |
/** |
|
126 |
* check namespaceId exist. |
|
127 |
* |
|
128 |
* @param namespaceId namespace id |
|
129 |
* @return true if exist, otherwise false |
|
130 |
*/ |
|
131 |
@GetMapping(params = "checkNamespaceIdExist=true") |
|
132 |
public Boolean checkNamespaceIdExist(@RequestParam("customNamespaceId") String namespaceId) { |
|
133 |
if (StringUtils.isBlank(namespaceId)) { |
|
134 |
return false; |
|
135 |
} |
|
136 |
return (namespacePersistService.tenantInfoCountByTenantId(namespaceId) > 0); |
|
137 |
} |
|
138 |
|
|
139 |
/** |
|
140 |
* edit namespace. |
|
141 |
* |
|
142 |
* @param namespace namespace |
|
143 |
* @param namespaceShowName namespace ShowName |
|
144 |
* @param namespaceDesc namespace Desc |
|
145 |
* @return whether edit ok |
|
146 |
*/ |
|
147 |
@PutMapping |
|
148 |
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE) |
|
149 |
public Boolean editNamespace(@RequestParam("namespace") String namespace, |
|
150 |
@RequestParam("namespaceShowName") String namespaceShowName, |
|
151 |
@RequestParam(value = "namespaceDesc", required = false) String namespaceDesc) { |
|
152 |
// contains illegal chars |
|
153 |
if (!namespaceNameCheckPattern.matcher(namespaceShowName).matches()) { |
|
154 |
return false; |
|
155 |
} |
|
156 |
return namespaceOperationService.editNamespace(namespace, namespaceShowName, namespaceDesc); |
|
157 |
} |
|
158 |
|
|
159 |
/** |
|
160 |
* del namespace by id. |
|
161 |
* |
|
162 |
* @param namespaceId namespace Id |
|
163 |
* @return whether del ok |
|
164 |
*/ |
|
165 |
@DeleteMapping |
|
166 |
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE) |
|
167 |
public Boolean deleteNamespace(@RequestParam("namespaceId") String namespaceId) { |
|
168 |
return namespaceOperationService.removeNamespace(namespaceId); |
|
169 |
} |
|
170 |
|
|
171 |
} |