#2145 自定义namespaceId 服务端部分

This commit is contained in:
qq213539 2019-12-10 21:18:56 +08:00
parent 866e6e386d
commit 6703b2abbb
2 changed files with 37 additions and 1 deletions

View File

@ -3173,6 +3173,17 @@ public class PersistService {
}
}
/**
* @author klw(213539@qq.com)
* @Description: count tenant_info by tenant_id
* @Date 2019/12/10 17:36
* @param: tenantId
* @return int
*/
public int countByTenantId(String tenantId){
return jt.queryForObject("select count(1) from tenant_info where tenant_id = '" + tenantId + "'", Integer.class);
}
/**
* Update tenantInfo showname
*

View File

@ -29,6 +29,7 @@ import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;
/**
* namespace service
@ -42,6 +43,8 @@ public class NamespaceController {
@Autowired
private PersistService persistService;
private Pattern namespaceIdCheckPattern = Pattern.compile("^[\\w-]+");
/**
* Get namespace list
*
@ -102,15 +105,37 @@ public class NamespaceController {
*/
@PostMapping
public Boolean createNamespace(HttpServletRequest request, HttpServletResponse response,
@RequestParam("customNamespaceId") String namespaceId,
@RequestParam("namespaceName") String namespaceName,
@RequestParam(value = "namespaceDesc", required = false) String namespaceDesc) {
// TODO 获取用kp
String namespaceId = UUID.randomUUID().toString();
if(StringUtils.isBlank(namespaceId)){
namespaceId = UUID.randomUUID().toString();
} else {
namespaceId = namespaceId.trim();
if (!namespaceIdCheckPattern.matcher(namespaceId).matches()) {
return false;
}
if (namespaceId.length() > 128) {
return false;
}
if(persistService.countByTenantId(namespaceId) > 0){
return false;
}
}
persistService.insertTenantInfoAtomic("1", namespaceId, namespaceName, namespaceDesc, "nacos",
System.currentTimeMillis());
return true;
}
@GetMapping(params = "checkNamespaceIdExist=true")
public Boolean checkNamespaceIdExist(@RequestParam("customNamespaceId") String namespaceId){
if(StringUtils.isBlank(namespaceId)){
return true;
}
return (persistService.countByTenantId(namespaceId) > 0);
}
/**
* edit namespace
*