[ISSUE #3907] when publishConfig, if type is absent, set default value 'text' (#3915)

* when publishConfig, if type is absent, set default value 'text'

* 1.add publishConfig api(add param type)
2.check type is valid in nacos server
This commit is contained in:
赵延 2020-11-09 11:36:50 +08:00 committed by GitHub
parent 48d0c68527
commit 39a2f20d56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 3 deletions

View File

@ -79,6 +79,18 @@ public interface ConfigService {
*/
boolean publishConfig(String dataId, String group, String content) throws NacosException;
/**
* Publish config.
*
* @param dataId dataId
* @param group group
* @param content content
* @param type config type {@link ConfigType}
* @return Whether publish
* @throws NacosException NacosException
*/
boolean publishConfig(String dataId, String group, String content, String type) throws NacosException;
/**
* Remove config.
*

View File

@ -16,6 +16,8 @@
package com.alibaba.nacos.api.config;
import com.alibaba.nacos.api.utils.StringUtils;
/**
* Config data type.
*
@ -62,4 +64,26 @@ public enum ConfigType {
public String getType() {
return type;
}
public static ConfigType getDefaultType() {
return TEXT;
}
/**
* check input type is valid.
*
* @param type config type
* @return it the type valid
*/
public static Boolean isValidType(String type) {
if (StringUtils.isBlank(type)) {
return false;
}
for (ConfigType value : values()) {
if (value.type.equals(type)) {
return true;
}
}
return false;
}
}

View File

@ -19,6 +19,7 @@ package com.alibaba.nacos.client.config;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager;
@ -112,7 +113,12 @@ public class NacosConfigService implements ConfigService {
@Override
public boolean publishConfig(String dataId, String group, String content) throws NacosException {
return publishConfigInner(namespace, dataId, group, null, null, null, content);
return publishConfig(dataId, group, content, ConfigType.getDefaultType().getType());
}
@Override
public boolean publishConfig(String dataId, String group, String content, String type) throws NacosException {
return publishConfigInner(namespace, dataId, group, null, null, null, content, type);
}
@Override
@ -211,7 +217,7 @@ public class NacosConfigService implements ConfigService {
}
private boolean publishConfigInner(String tenant, String dataId, String group, String tag, String appName,
String betaIps, String content) throws NacosException {
String betaIps, String content, String type) throws NacosException {
group = null2defaultGroup(group);
ParamUtils.checkParam(dataId, group, content);
@ -228,6 +234,7 @@ public class NacosConfigService implements ConfigService {
params.put("dataId", dataId);
params.put("group", group);
params.put("content", content);
params.put("type", type);
if (StringUtils.isNotEmpty(tenant)) {
params.put("tenant", tenant);
}

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.config.server.controller;
import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.auth.common.ActionTypes;
@ -32,10 +33,10 @@ import com.alibaba.nacos.config.server.model.GroupkeyListenserStatus;
import com.alibaba.nacos.config.server.model.Page;
import com.alibaba.nacos.config.server.model.SameConfigPolicy;
import com.alibaba.nacos.config.server.model.SampleResult;
import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent;
import com.alibaba.nacos.config.server.result.ResultBuilder;
import com.alibaba.nacos.config.server.result.code.ResultCodeEnum;
import com.alibaba.nacos.config.server.service.AggrWhitelist;
import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent;
import com.alibaba.nacos.config.server.service.ConfigChangePublisher;
import com.alibaba.nacos.config.server.service.ConfigSubService;
import com.alibaba.nacos.config.server.service.repository.PersistService;
@ -134,6 +135,10 @@ public class ConfigController {
final String srcIp = RequestUtil.getRemoteIp(request);
final String requestIpApp = RequestUtil.getAppName(request);
srcUser = RequestUtil.getSrcUserName(request);
//check type
if (!ConfigType.isValidType(type)) {
type = ConfigType.getDefaultType().getType();
}
// check tenant
ParamUtils.checkTenant(tenant);
ParamUtils.checkParam(dataId, group, "datumId", content);