提交 | 用户 | 时间
|
e7c126
|
1 |
/* |
H |
2 |
* Copyright 1999-2022 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.v2; |
|
18 |
|
|
19 |
import com.alibaba.nacos.api.annotation.NacosApi; |
|
20 |
import com.alibaba.nacos.api.exception.NacosException; |
|
21 |
import com.alibaba.nacos.api.exception.api.NacosApiException; |
|
22 |
import com.alibaba.nacos.api.model.v2.ErrorCode; |
|
23 |
import com.alibaba.nacos.api.model.v2.Result; |
|
24 |
import com.alibaba.nacos.auth.annotation.Secured; |
|
25 |
import com.alibaba.nacos.common.utils.StringUtils; |
|
26 |
import com.alibaba.nacos.console.paramcheck.ConsoleDefaultHttpParamExtractor; |
|
27 |
import com.alibaba.nacos.core.namespace.model.Namespace; |
|
28 |
import com.alibaba.nacos.core.namespace.model.form.NamespaceForm; |
|
29 |
import com.alibaba.nacos.core.namespace.repository.NamespacePersistService; |
|
30 |
import com.alibaba.nacos.core.paramcheck.ExtractorManager; |
|
31 |
import com.alibaba.nacos.core.service.NamespaceOperationService; |
|
32 |
import com.alibaba.nacos.plugin.auth.constant.ActionTypes; |
|
33 |
import com.alibaba.nacos.plugin.auth.constant.SignType; |
|
34 |
import com.alibaba.nacos.plugin.auth.impl.constant.AuthConstants; |
|
35 |
import org.springframework.http.HttpStatus; |
|
36 |
import org.springframework.web.bind.annotation.DeleteMapping; |
|
37 |
import org.springframework.web.bind.annotation.GetMapping; |
|
38 |
import org.springframework.web.bind.annotation.PostMapping; |
|
39 |
import org.springframework.web.bind.annotation.PutMapping; |
|
40 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
41 |
import org.springframework.web.bind.annotation.RequestParam; |
|
42 |
import org.springframework.web.bind.annotation.RestController; |
|
43 |
|
|
44 |
import java.util.List; |
|
45 |
import java.util.UUID; |
|
46 |
import java.util.regex.Pattern; |
|
47 |
|
|
48 |
/** |
|
49 |
* NamespaceControllerV2. |
|
50 |
* |
|
51 |
* @author dongyafei |
|
52 |
* @date 2022/8/16 |
|
53 |
*/ |
|
54 |
@NacosApi |
|
55 |
@RestController |
|
56 |
@RequestMapping("/v2/console/namespace") |
|
57 |
@ExtractorManager.Extractor(httpExtractor = ConsoleDefaultHttpParamExtractor.class) |
|
58 |
public class NamespaceControllerV2 { |
|
59 |
|
|
60 |
private final NamespaceOperationService namespaceOperationService; |
|
61 |
|
|
62 |
private NamespacePersistService namespacePersistService; |
|
63 |
|
|
64 |
public NamespaceControllerV2(NamespaceOperationService namespaceOperationService, NamespacePersistService namespacePersistService) { |
|
65 |
this.namespaceOperationService = namespaceOperationService; |
|
66 |
this.namespacePersistService = namespacePersistService; |
|
67 |
} |
|
68 |
|
|
69 |
private final Pattern namespaceIdCheckPattern = Pattern.compile("^[\\w-]+"); |
|
70 |
|
|
71 |
private final Pattern namespaceNameCheckPattern = Pattern.compile("^[^@#$%^&*]+$"); |
|
72 |
|
|
73 |
private static final int NAMESPACE_ID_MAX_LENGTH = 128; |
|
74 |
|
|
75 |
/** |
|
76 |
* Get namespace list. |
|
77 |
* |
|
78 |
* @return namespace list |
|
79 |
*/ |
|
80 |
@GetMapping("/list") |
|
81 |
public Result<List<Namespace>> getNamespaceList() { |
|
82 |
return Result.success(namespaceOperationService.getNamespaceList()); |
|
83 |
} |
|
84 |
|
|
85 |
/** |
|
86 |
* get namespace all info by namespace id. |
|
87 |
* |
|
88 |
* @param namespaceId namespaceId |
|
89 |
* @return namespace all info |
|
90 |
*/ |
|
91 |
@GetMapping() |
|
92 |
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX |
|
93 |
+ "namespaces", action = ActionTypes.READ, signType = SignType.CONSOLE) |
|
94 |
public Result<Namespace> getNamespace(@RequestParam("namespaceId") String namespaceId) throws NacosException { |
|
95 |
return Result.success(namespaceOperationService.getNamespace(namespaceId)); |
|
96 |
} |
|
97 |
|
|
98 |
/** |
|
99 |
* create namespace. |
|
100 |
* |
|
101 |
* @param namespaceForm namespaceForm. |
|
102 |
* @return whether create ok |
|
103 |
*/ |
|
104 |
@PostMapping |
|
105 |
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX |
|
106 |
+ "namespaces", action = ActionTypes.WRITE, signType = SignType.CONSOLE) |
|
107 |
public Result<Boolean> createNamespace(NamespaceForm namespaceForm) throws NacosException { |
|
108 |
|
|
109 |
namespaceForm.validate(); |
|
110 |
|
|
111 |
String namespaceId = namespaceForm.getNamespaceId(); |
|
112 |
String namespaceName = namespaceForm.getNamespaceName(); |
|
113 |
String namespaceDesc = namespaceForm.getNamespaceDesc(); |
|
114 |
|
|
115 |
if (StringUtils.isBlank(namespaceId)) { |
|
116 |
namespaceId = UUID.randomUUID().toString(); |
|
117 |
} else { |
|
118 |
namespaceId = namespaceId.trim(); |
|
119 |
if (!namespaceIdCheckPattern.matcher(namespaceId).matches()) { |
|
120 |
throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE, |
|
121 |
"namespaceId [" + namespaceId + "] mismatch the pattern"); |
|
122 |
} |
|
123 |
if (namespaceId.length() > NAMESPACE_ID_MAX_LENGTH) { |
|
124 |
throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE, |
|
125 |
"too long namespaceId, over " + NAMESPACE_ID_MAX_LENGTH); |
|
126 |
} |
|
127 |
// check unique |
|
128 |
if (namespacePersistService.tenantInfoCountByTenantId(namespaceId) > 0) { |
|
129 |
throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE, |
|
130 |
"the namespaceId is existed, namespaceId: " + namespaceForm.getNamespaceId()); |
|
131 |
} |
|
132 |
} |
|
133 |
// contains illegal chars |
|
134 |
if (!namespaceNameCheckPattern.matcher(namespaceName).matches()) { |
|
135 |
throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE, |
|
136 |
"namespaceName [" + namespaceName + "] contains illegal char"); |
|
137 |
} |
|
138 |
return Result.success(namespaceOperationService.createNamespace(namespaceId, namespaceName, namespaceDesc)); |
|
139 |
} |
|
140 |
|
|
141 |
/** |
|
142 |
* edit namespace. |
|
143 |
* |
|
144 |
* @param namespaceForm namespace params |
|
145 |
* @return whether edit ok |
|
146 |
*/ |
|
147 |
@PutMapping |
|
148 |
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX |
|
149 |
+ "namespaces", action = ActionTypes.WRITE, signType = SignType.CONSOLE) |
|
150 |
public Result<Boolean> editNamespace(NamespaceForm namespaceForm) throws NacosException { |
|
151 |
namespaceForm.validate(); |
|
152 |
// contains illegal chars |
|
153 |
if (!namespaceNameCheckPattern.matcher(namespaceForm.getNamespaceName()).matches()) { |
|
154 |
throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE, |
|
155 |
"namespaceName [" + namespaceForm.getNamespaceName() + "] contains illegal char"); |
|
156 |
} |
|
157 |
return Result.success(namespaceOperationService |
|
158 |
.editNamespace(namespaceForm.getNamespaceId(), namespaceForm.getNamespaceName(), |
|
159 |
namespaceForm.getNamespaceDesc())); |
|
160 |
} |
|
161 |
|
|
162 |
/** |
|
163 |
* delete namespace by id. |
|
164 |
* |
|
165 |
* @param namespaceId namespace ID |
|
166 |
* @return whether delete ok |
|
167 |
*/ |
|
168 |
@DeleteMapping |
|
169 |
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX |
|
170 |
+ "namespaces", action = ActionTypes.WRITE, signType = SignType.CONSOLE) |
|
171 |
public Result<Boolean> deleteNamespace(@RequestParam("namespaceId") String namespaceId) { |
|
172 |
return Result.success(namespaceOperationService.removeNamespace(namespaceId)); |
|
173 |
} |
|
174 |
} |