|
|
@@ -16,27 +16,20 @@
|
|
|
|
|
|
package com.alibaba.nacos.console.controller;
|
|
|
|
|
|
+import com.alibaba.nacos.api.exception.NacosException;
|
|
|
import com.alibaba.nacos.auth.annotation.Secured;
|
|
|
import com.alibaba.nacos.common.model.RestResult;
|
|
|
import com.alibaba.nacos.common.model.RestResultUtils;
|
|
|
import com.alibaba.nacos.common.utils.StringUtils;
|
|
|
-import com.alibaba.nacos.config.server.model.TenantInfo;
|
|
|
-import com.alibaba.nacos.config.server.service.repository.PersistService;
|
|
|
-import com.alibaba.nacos.console.enums.NamespaceTypeEnum;
|
|
|
+import com.alibaba.nacos.config.server.service.repository.CommonPersistService;
|
|
|
import com.alibaba.nacos.console.model.Namespace;
|
|
|
import com.alibaba.nacos.console.model.NamespaceAllInfo;
|
|
|
+import com.alibaba.nacos.console.service.NamespaceOperationService;
|
|
|
import com.alibaba.nacos.plugin.auth.constant.ActionTypes;
|
|
|
import com.alibaba.nacos.plugin.auth.impl.constant.AuthConstants;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.web.bind.annotation.DeleteMapping;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.PutMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestParam;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.UUID;
|
|
|
import java.util.regex.Pattern;
|
|
|
@@ -49,28 +42,17 @@ import java.util.regex.Pattern;
|
|
|
@RestController
|
|
|
@RequestMapping("/v1/console/namespaces")
|
|
|
public class NamespaceController {
|
|
|
-
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CommonPersistService commonPersistService;
|
|
|
+
|
|
|
@Autowired
|
|
|
- private PersistService persistService;
|
|
|
-
|
|
|
+ private NamespaceOperationService namespaceOperationService;
|
|
|
+
|
|
|
private final Pattern namespaceIdCheckPattern = Pattern.compile("^[\\w-]+");
|
|
|
-
|
|
|
+
|
|
|
private static final int NAMESPACE_ID_MAX_LENGTH = 128;
|
|
|
-
|
|
|
- private static final String DEFAULT_NAMESPACE = "public";
|
|
|
-
|
|
|
- private static final int DEFAULT_QUOTA = 200;
|
|
|
-
|
|
|
- private static final String DEFAULT_CREATE_SOURCE = "nacos";
|
|
|
-
|
|
|
- private static final String DEFAULT_NAMESPACE_SHOW_NAME = "Public";
|
|
|
-
|
|
|
- private static final String DEFAULT_NAMESPACE_DESCRIPTION = "Public Namespace";
|
|
|
-
|
|
|
- private static final String DEFAULT_TENANT = "";
|
|
|
-
|
|
|
- private static final String DEFAULT_KP = "1";
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Get namespace list.
|
|
|
*
|
|
|
@@ -78,21 +60,9 @@ public class NamespaceController {
|
|
|
*/
|
|
|
@GetMapping
|
|
|
public RestResult<List<Namespace>> getNamespaces() {
|
|
|
- // TODO 获取用kp
|
|
|
- List<TenantInfo> tenantInfos = persistService.findTenantByKp(DEFAULT_KP);
|
|
|
- Namespace namespace0 = new Namespace("", DEFAULT_NAMESPACE, DEFAULT_QUOTA,
|
|
|
- persistService.configInfoCount(DEFAULT_TENANT), NamespaceTypeEnum.GLOBAL.getType());
|
|
|
- List<Namespace> namespaces = new ArrayList<>();
|
|
|
- namespaces.add(namespace0);
|
|
|
- for (TenantInfo tenantInfo : tenantInfos) {
|
|
|
- int configCount = persistService.configInfoCount(tenantInfo.getTenantId());
|
|
|
- Namespace namespaceTmp = new Namespace(tenantInfo.getTenantId(), tenantInfo.getTenantName(),
|
|
|
- tenantInfo.getTenantDesc(), DEFAULT_QUOTA, configCount, NamespaceTypeEnum.CUSTOM.getType());
|
|
|
- namespaces.add(namespaceTmp);
|
|
|
- }
|
|
|
- return RestResultUtils.success(namespaces);
|
|
|
+ return RestResultUtils.success(namespaceOperationService.getNamespaceList());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* get namespace all info by namespace id.
|
|
|
*
|
|
|
@@ -100,20 +70,10 @@ public class NamespaceController {
|
|
|
* @return namespace all info
|
|
|
*/
|
|
|
@GetMapping(params = "show=all")
|
|
|
- public NamespaceAllInfo getNamespace(@RequestParam("namespaceId") String namespaceId) {
|
|
|
- // TODO 获取用kp
|
|
|
- if (StringUtils.isBlank(namespaceId)) {
|
|
|
- return new NamespaceAllInfo(namespaceId, DEFAULT_NAMESPACE_SHOW_NAME, DEFAULT_QUOTA,
|
|
|
- persistService.configInfoCount(DEFAULT_TENANT), NamespaceTypeEnum.GLOBAL.getType(),
|
|
|
- DEFAULT_NAMESPACE_DESCRIPTION);
|
|
|
- } else {
|
|
|
- TenantInfo tenantInfo = persistService.findTenantByKp(DEFAULT_KP, namespaceId);
|
|
|
- int configCount = persistService.configInfoCount(namespaceId);
|
|
|
- return new NamespaceAllInfo(namespaceId, tenantInfo.getTenantName(), DEFAULT_QUOTA, configCount,
|
|
|
- NamespaceTypeEnum.CUSTOM.getType(), tenantInfo.getTenantDesc());
|
|
|
- }
|
|
|
+ public NamespaceAllInfo getNamespace(@RequestParam("namespaceId") String namespaceId) throws NacosException {
|
|
|
+ return namespaceOperationService.getNamespace(namespaceId);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* create namespace.
|
|
|
*
|
|
|
@@ -126,7 +86,6 @@ public class NamespaceController {
|
|
|
public Boolean createNamespace(@RequestParam("customNamespaceId") String namespaceId,
|
|
|
@RequestParam("namespaceName") String namespaceName,
|
|
|
@RequestParam(value = "namespaceDesc", required = false) String namespaceDesc) {
|
|
|
- // TODO 获取用kp
|
|
|
if (StringUtils.isBlank(namespaceId)) {
|
|
|
namespaceId = UUID.randomUUID().toString();
|
|
|
} else {
|
|
|
@@ -137,15 +96,14 @@ public class NamespaceController {
|
|
|
if (namespaceId.length() > NAMESPACE_ID_MAX_LENGTH) {
|
|
|
return false;
|
|
|
}
|
|
|
- if (persistService.tenantInfoCountByTenantId(namespaceId) > 0) {
|
|
|
- return false;
|
|
|
- }
|
|
|
}
|
|
|
- persistService.insertTenantInfoAtomic(DEFAULT_KP, namespaceId, namespaceName, namespaceDesc,
|
|
|
- DEFAULT_CREATE_SOURCE, System.currentTimeMillis());
|
|
|
- return true;
|
|
|
+ try {
|
|
|
+ return namespaceOperationService.createNamespace(namespaceId, namespaceName, namespaceDesc);
|
|
|
+ } catch (NacosException e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* check namespaceId exist.
|
|
|
*
|
|
|
@@ -157,9 +115,9 @@ public class NamespaceController {
|
|
|
if (StringUtils.isBlank(namespaceId)) {
|
|
|
return false;
|
|
|
}
|
|
|
- return (persistService.tenantInfoCountByTenantId(namespaceId) > 0);
|
|
|
+ return (commonPersistService.tenantInfoCountByTenantId(namespaceId) > 0);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* edit namespace.
|
|
|
*
|
|
|
@@ -173,11 +131,9 @@ public class NamespaceController {
|
|
|
public Boolean editNamespace(@RequestParam("namespace") String namespace,
|
|
|
@RequestParam("namespaceShowName") String namespaceShowName,
|
|
|
@RequestParam(value = "namespaceDesc", required = false) String namespaceDesc) {
|
|
|
- // TODO 获取用kp
|
|
|
- persistService.updateTenantNameAtomic(DEFAULT_KP, namespace, namespaceShowName, namespaceDesc);
|
|
|
- return true;
|
|
|
+ return namespaceOperationService.editNamespace(namespace, namespaceShowName, namespaceDesc);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* del namespace by id.
|
|
|
*
|
|
|
@@ -186,9 +142,8 @@ public class NamespaceController {
|
|
|
*/
|
|
|
@DeleteMapping
|
|
|
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE)
|
|
|
- public Boolean deleteConfig(@RequestParam("namespaceId") String namespaceId) {
|
|
|
- persistService.removeTenantInfoAtomic(DEFAULT_KP, namespaceId);
|
|
|
- return true;
|
|
|
+ public Boolean deleteNamespace(@RequestParam("namespaceId") String namespaceId) {
|
|
|
+ return namespaceOperationService.removeNamespace(namespaceId);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|