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

Custom namespace id #2163

Merged
merged 9 commits into from
Dec 19, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -3173,6 +3173,19 @@ public void insertTenantInfoAtomic(String kp, String tenantId, String tenantName
}
}

/**
* @author klw([email protected])
* @Description: count tenant_info by tenant_id
* @Date 2019/12/10 17:36
* @param: tenantId
* @return int
*/
public int countByTenantId(String tenantId){
String[] args = new String[1];
args[0] = tenantId;
return jt.queryForObject("select count(1) from tenant_info where tenant_id = ?", args, Integer.class);
KeRan213539 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Update tenantInfo showname
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

/**
* namespace service
Expand All @@ -42,6 +43,10 @@ public class NamespaceController {
@Autowired
private PersistService persistService;

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

private static final int NAMESPACE_ID_MAX_LENGTH = 128;

/**
* Get namespace list
*
Expand Down Expand Up @@ -102,15 +107,44 @@ public NamespaceAllInfo getNamespace(HttpServletRequest request, HttpServletResp
*/
@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() > NAMESPACE_ID_MAX_LENGTH) {
return false;
}
if(persistService.countByTenantId(namespaceId) > 0){
return false;
}
}
persistService.insertTenantInfoAtomic("1", namespaceId, namespaceName, namespaceDesc, "nacos",
System.currentTimeMillis());
return true;
}

/**
* @author klw([email protected])
* @Description: check namespaceId exist
* @Date 2019/12/10 21:41
* @param: namespaceId
* @return java.lang.Boolean
*/
@GetMapping(params = "checkNamespaceIdExist=true")
public Boolean checkNamespaceIdExist(@RequestParam("customNamespaceId") String namespaceId){
if(StringUtils.isBlank(namespaceId)){
return false;
}
return (persistService.countByTenantId(namespaceId) > 0);
}

/**
* edit namespace
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,56 @@ class NewNameSpace extends React.Component {
this.setState({
disabled: true,
});
let { customNamespaceId } = values;
if (!customNamespaceId) {
customNamespaceId = '';
}
request({
type: 'post',
url: 'v1/console/namespaces',
type: 'get',
url: 'v1/console/namespaces?checkNamespaceIdExist=true',
contentType: 'application/x-www-form-urlencoded',
beforeSend: () => this.openLoading(),
data: {
namespaceName: values.namespaceShowName,
namespaceDesc: values.namespaceDesc,
customNamespaceId,
},
success: res => {
this.disabled = false;
this.setState({
disabled: false,
});
if (res === true) {
this.closeDialog();
this.props.getNameSpaces();
this.refreshNameSpace(); // 刷新全局namespace
} else {
Dialog.alert({
title: locale.notice,
content: res.message,
content: locale.namespaceIdAlreadyExist,
});
} else {
request({
type: 'post',
url: 'v1/console/namespaces',
contentType: 'application/x-www-form-urlencoded',
beforeSend: () => this.openLoading(),
data: {
customNamespaceId,
namespaceName: values.namespaceShowName,
namespaceDesc: values.namespaceDesc,
},
success: res => {
this.disabled = false;
this.setState({
disabled: false,
});
if (res === true) {
this.closeDialog();
this.props.getNameSpaces();
this.refreshNameSpace(); // 刷新全局namespace
} else {
Dialog.alert({
title: locale.notice,
content: locale.newnamespceFailedMessage,
});
}
},
complete: () => this.closeLoading(),
});
}
},
Expand Down Expand Up @@ -164,6 +192,32 @@ class NewNameSpace extends React.Component {
}
}

validateNamespzecId(rule, value, callback) {
if (!value || value.trim() === '') {
callback();
} else {
const { locale = {} } = this.props;
if (value.length > 128) {
callback(locale.namespaceIdTooLong);
}
const chartReg = /^[\w-]+/g;
const matchResult = value.match(chartReg);
if (matchResult) {
if (matchResult.length > 1) {
callback(locale.input);
} else {
if (value.length !== matchResult[0].length) {
callback(locale.input);
return;
}
callback();
}
} else {
callback(locale.input);
}
}
}

render() {
const { locale = {} } = this.props;
const footer = (
Expand Down Expand Up @@ -193,6 +247,14 @@ class NewNameSpace extends React.Component {
style={{ width: '100%', position: 'relative' }}
visible={this.state.loading}
>
<FormItem label={locale.namespaceId} {...formItemLayout}>
<Input
{...this.field.init('customNamespaceId', {
rules: [{ validator: this.validateNamespzecId.bind(this) }],
})}
style={{ width: '100%' }}
/>
</FormItem>
<FormItem label={locale.name} required {...formItemLayout}>
<Input
{...this.field.init('namespaceShowName', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,9 +463,14 @@ const I18N_CONF = {
newnamespce: 'Create Namespace',
loading: 'Loading...',
name: 'Namespace:',
namespaceId: 'Namespace ID(automatically generated if not filled):',
namespaceIdTooLong: 'The namespace ID length cannot exceed 128',
namespacenotnull: 'Namespace cannot be empty',
namespacedescnotnull: 'Namespace description cannot be empty',
description: 'Description:',
namespaceIdAlreadyExist: 'namespaceId already exist',
newnamespceFailedMessage:
'namespaceId format is incorrect/namespaceId length greater than 128/namespaceId already exist',
},
NameSpaceList: {
notice: 'Notice',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,13 @@ const I18N_CONF = {
newnamespce: '新建命名空间',
loading: '加载中...',
name: '命名空间名:',
namespaceId: '命名空间ID(不填则自动生成):',
namespaceIdTooLong: '命名空间ID长度不能超过128',
namespacenotnull: '命名空间不能为空',
namespacedescnotnull: '命名空间描述不能为空',
description: '描述:',
namespaceIdAlreadyExist: 'namespaceId已存在',
newnamespceFailedMessage: 'namespaceId格式不正确/namespaceId长度大于128/namespaceId已存在',
},
NameSpaceList: {
notice: '提示',
Expand Down