Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConfigV2Controller and HistoryV2Controller #9011

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ public enum ErrorCode {
*/
NAMESPACE_NOT_EXIST(22001, "namespace not exist"),

/**
* namespace already exist.
*/
NAMESPACE_ALREADY_EXIST(22002, "namespace already exist"),

/**
* illegal state.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

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;
Expand All @@ -25,6 +26,7 @@
import com.alibaba.nacos.console.enums.NamespaceTypeEnum;
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;
Expand All @@ -36,7 +38,6 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;
Expand All @@ -53,16 +54,15 @@ public class NamespaceController {
@Autowired
private PersistService persistService;

@Autowired
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";
Expand All @@ -78,19 +78,7 @@ 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());
}

/**
Expand Down Expand Up @@ -125,8 +113,7 @@ public NamespaceAllInfo getNamespace(@RequestParam("namespaceId") String namespa
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE)
public Boolean createNamespace(@RequestParam("customNamespaceId") String namespaceId,
@RequestParam("namespaceName") String namespaceName,
@RequestParam(value = "namespaceDesc", required = false) String namespaceDesc) {
// TODO 获取用kp
@RequestParam(value = "namespaceDesc", required = false) String namespaceDesc) throws NacosException {
if (StringUtils.isBlank(namespaceId)) {
namespaceId = UUID.randomUUID().toString();
} else {
Expand All @@ -137,13 +124,8 @@ public Boolean createNamespace(@RequestParam("customNamespaceId") String namespa
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;
return namespaceOperationService.createNamespace(namespaceId, namespaceName, namespaceDesc, false);
}

/**
Expand Down Expand Up @@ -173,9 +155,7 @@ public Boolean checkNamespaceIdExist(@RequestParam("customNamespaceId") String n
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);
}

/**
Expand All @@ -187,8 +167,7 @@ public Boolean editNamespace(@RequestParam("namespace") String namespace,
@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;
return namespaceOperationService.removeNamespace(namespaceId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright 1999-2022 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.console.controller.v2;

import com.alibaba.nacos.api.annotation.NacosApi;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.exception.api.NacosApiException;
import com.alibaba.nacos.api.model.v2.ErrorCode;
import com.alibaba.nacos.api.model.v2.Result;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.console.model.Namespace;
import com.alibaba.nacos.console.model.vo.NamespaceVo;
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.http.HttpStatus;
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 java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

/**
* NamespaceControllerV2.
* @author dongyafei
* @date 2022/8/16
*/
@NacosApi
@RestController
@RequestMapping(path = "/v2/console/namespace")
public class NamespaceControllerV2 {

private final NamespaceOperationService namespaceOperationService;

public NamespaceControllerV2(NamespaceOperationService namespaceOperationService) {
this.namespaceOperationService = namespaceOperationService;
}

private final Pattern namespaceIdCheckPattern = Pattern.compile("^[\\w-]+");

private static final int NAMESPACE_ID_MAX_LENGTH = 128;

/**
* Get namespace list.
*
* @return namespace list
*/
@GetMapping("/list")
public Result<List<Namespace>> getNamespaceList() {
return Result.success(namespaceOperationService.getNamespaceList());
}

/**
* create namespace.
*
* @param namespaceVo namespaceVo.
* @return whether create ok
*/
@PostMapping
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE)
public Result<Boolean> createNamespace(NamespaceVo namespaceVo) throws NacosException {

namespaceVo.validate();

String namespaceId = namespaceVo.getNamespaceId();
String namespaceName = namespaceVo.getNamespaceName();
String namespaceDesc = namespaceVo.getNamespaceDesc();

if (StringUtils.isBlank(namespaceId)) {
namespaceId = UUID.randomUUID().toString();
} else {
namespaceId = namespaceId.trim();
if (!namespaceIdCheckPattern.matcher(namespaceId).matches()) {
throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE,
"namespaceId [" + namespaceId + "] mismatch the pattern");
}
if (namespaceId.length() > NAMESPACE_ID_MAX_LENGTH) {
throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.ILLEGAL_NAMESPACE,
"too long namespaceId, over " + NAMESPACE_ID_MAX_LENGTH);
}
}
return Result.success(namespaceOperationService.createNamespace(namespaceId, namespaceName, namespaceDesc, true));
}

/**
* edit namespace.
*
* @param namespaceVo namespace params
* @return whether edit ok
*/
@PutMapping
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE)
public Result<Boolean> editNamespace(NamespaceVo namespaceVo) throws NacosException {
namespaceVo.validate();
return Result.success(namespaceOperationService.editNamespace(namespaceVo.getNamespaceId(),
namespaceVo.getNamespaceName(), namespaceVo.getNamespaceDesc()));
}

/**
* delete namespace by id.
*
* @param namespaceId namespace ID
* @return whether delete ok
*/
@DeleteMapping
@Secured(resource = AuthConstants.CONSOLE_RESOURCE_NAME_PREFIX + "namespaces", action = ActionTypes.WRITE)
public Result<Boolean> deleteNamespace(@RequestParam("namespaceId") String namespaceId) {
return Result.success(namespaceOperationService.removeNamespace(namespaceId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 1999-2022 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.console.model.vo;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.exception.api.NacosApiException;
import com.alibaba.nacos.api.model.v2.ErrorCode;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.Objects;

/**
* NamespaceVo.
* @author dongyafei
* @date 2022/8/16
*/
public class NamespaceVo implements Serializable {

private static final long serialVersionUID = -1078976569495343487L;

private String namespaceId;

private String namespaceName;

private String namespaceDesc;

public NamespaceVo() {
}

public NamespaceVo(String namespaceId, String namespaceName, String namespaceDesc) {
this.namespaceId = namespaceId;
this.namespaceName = namespaceName;
this.namespaceDesc = namespaceDesc;
}

public String getNamespaceId() {
return namespaceId;
}

public void setNamespaceId(String namespaceId) {
this.namespaceId = namespaceId;
}

public String getNamespaceName() {
return namespaceName;
}

public void setNamespaceName(String namespaceName) {
this.namespaceName = namespaceName;
}

public String getNamespaceDesc() {
return namespaceDesc;
}

public void setNamespaceDesc(String namespaceDesc) {
this.namespaceDesc = namespaceDesc;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
NamespaceVo that = (NamespaceVo) o;
return Objects.equals(namespaceId, that.namespaceId) && Objects.equals(namespaceName, that.namespaceName)
&& Objects.equals(namespaceDesc, that.namespaceDesc);
}

@Override
public int hashCode() {
return Objects.hash(namespaceId, namespaceName, namespaceDesc);
}

@Override
public String toString() {
return "NamespaceVo{" + "namespaceId='" + namespaceId + '\'' + ", namespaceName='" + namespaceName + '\''
+ ", namespaceDesc='" + namespaceDesc + '\'' + '}';
}

/**
* check required param.
* @throws NacosException NacosException
*/
public void validate() throws NacosException {
if (null == namespaceId) {
throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.PARAMETER_MISSING, "required parameter 'namespaceId' is missing");
}
if (null == namespaceName) {
throw new NacosApiException(HttpStatus.BAD_REQUEST.value(), ErrorCode.PARAMETER_MISSING, "required parameter 'namespaceName' is missing");
}
}
}
Loading