Search in sources :

Example 6 with ConfigInfo

use of com.alibaba.nacos.config.server.model.ConfigInfo in project nacos by alibaba.

the class ExternalStoragePersistServiceImpl method updateConfigInfoCas.

@Override
public boolean updateConfigInfoCas(final ConfigInfo configInfo, final String srcIp, final String srcUser, final Timestamp time, final Map<String, Object> configAdvanceInfo, final boolean notify) {
    return tjt.execute(status -> {
        try {
            ConfigInfo oldConfigInfo = findConfigInfo(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant());
            String appNameTmp = oldConfigInfo.getAppName();
            /*
                 If the appName passed by the user is not empty, use the persistent user's appName,
                 otherwise use db; when emptying appName, you need to pass an empty string
                 */
            if (configInfo.getAppName() == null) {
                configInfo.setAppName(appNameTmp);
            }
            int rows = updateConfigInfoAtomicCas(configInfo, srcIp, srcUser, time, configAdvanceInfo);
            if (rows < 1) {
                return Boolean.FALSE;
            }
            String configTags = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("config_tags");
            if (configTags != null) {
                // delete all tags and then recreate
                removeTagByIdAtomic(oldConfigInfo.getId());
                addConfigTagsRelation(oldConfigInfo.getId(), configTags, configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant());
            }
            insertConfigHistoryAtomic(oldConfigInfo.getId(), oldConfigInfo, srcIp, srcUser, time, "U");
        } catch (CannotGetJdbcConnectionException e) {
            LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e);
            throw e;
        }
        return Boolean.TRUE;
    });
}
Also used : CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo)

Example 7 with ConfigInfo

use of com.alibaba.nacos.config.server.model.ConfigInfo in project nacos by alibaba.

the class ExternalStoragePersistServiceImpl method findConfigInfo4Page.

@Override
public Page<ConfigInfo> findConfigInfo4Page(final int pageNo, final int pageSize, final String dataId, final String group, final String tenant, final Map<String, Object> configAdvanceInfo) {
    String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
    PaginationHelper<ConfigInfo> helper = createPaginationHelper();
    final String appName = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("appName");
    final String configTags = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("config_tags");
    String sqlCount = "SELECT count(*) FROM config_info";
    String sql = "SELECT id,data_id,group_id,tenant_id,app_name,content,type,encrypted_data_key FROM config_info";
    StringBuilder where = new StringBuilder(" WHERE ");
    List<String> paramList = new ArrayList<>();
    paramList.add(tenantTmp);
    if (StringUtils.isNotBlank(configTags)) {
        sqlCount = "SELECT count(*) FROM config_info  a LEFT JOIN config_tags_relation b ON a.id=b.id";
        sql = "SELECT a.id,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content FROM config_info  a LEFT JOIN " + "config_tags_relation b ON a.id=b.id";
        where.append(" a.tenant_id=? ");
        if (StringUtils.isNotBlank(dataId)) {
            where.append(" AND a.data_id=? ");
            paramList.add(dataId);
        }
        if (StringUtils.isNotBlank(group)) {
            where.append(" AND a.group_id=? ");
            paramList.add(group);
        }
        if (StringUtils.isNotBlank(appName)) {
            where.append(" AND a.app_name=? ");
            paramList.add(appName);
        }
        where.append(" AND b.tag_name IN (");
        String[] tagArr = configTags.split(",");
        for (int i = 0; i < tagArr.length; i++) {
            if (i != 0) {
                where.append(", ");
            }
            where.append('?');
            paramList.add(tagArr[i]);
        }
        where.append(") ");
    } else {
        where.append(" tenant_id=? ");
        if (StringUtils.isNotBlank(dataId)) {
            where.append(" AND data_id=? ");
            paramList.add(dataId);
        }
        if (StringUtils.isNotBlank(group)) {
            where.append(" AND group_id=? ");
            paramList.add(group);
        }
        if (StringUtils.isNotBlank(appName)) {
            where.append(" AND app_name=? ");
            paramList.add(appName);
        }
    }
    try {
        Page<ConfigInfo> page = helper.fetchPage(sqlCount + where, sql + where, paramList.toArray(), pageNo, pageSize, CONFIG_INFO_ROW_MAPPER);
        for (ConfigInfo configInfo : page.getPageItems()) {
            Pair<String, String> pair = EncryptionHandler.decryptHandler(configInfo.getDataId(), configInfo.getEncryptedDataKey(), configInfo.getContent());
            configInfo.setContent(pair.getSecond());
        }
        return page;
    } catch (CannotGetJdbcConnectionException e) {
        LogUtil.FATAL_LOG.error("[db-error] ", e);
        throw e;
    }
}
Also used : CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) ArrayList(java.util.ArrayList) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo)

Example 8 with ConfigInfo

use of com.alibaba.nacos.config.server.model.ConfigInfo in project nacos by alibaba.

the class ExternalStoragePersistServiceImpl method removeConfigInfo4Beta.

@Override
public void removeConfigInfo4Beta(final String dataId, final String group, final String tenant) {
    final String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
    tjt.execute(status -> {
        try {
            ConfigInfo configInfo = findConfigInfo4Beta(dataId, group, tenant);
            if (configInfo != null) {
                jt.update("DELETE FROM config_info_beta WHERE data_id=? AND group_id=? AND tenant_id=?", dataId, group, tenantTmp);
            }
        } catch (CannotGetJdbcConnectionException e) {
            LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e);
            throw e;
        }
        return Boolean.TRUE;
    });
}
Also used : CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo)

Example 9 with ConfigInfo

use of com.alibaba.nacos.config.server.model.ConfigInfo in project nacos by alibaba.

the class ExternalStoragePersistServiceImpl method updateConfigInfo.

@Override
public void updateConfigInfo(final ConfigInfo configInfo, final String srcIp, final String srcUser, final Timestamp time, final Map<String, Object> configAdvanceInfo, final boolean notify) {
    boolean result = tjt.execute(status -> {
        try {
            ConfigInfo oldConfigInfo = findConfigInfo(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant());
            String appNameTmp = oldConfigInfo.getAppName();
            /*
                 If the appName passed by the user is not empty, use the persistent user's appName,
                 otherwise use db; when emptying appName, you need to pass an empty string
                 */
            if (configInfo.getAppName() == null) {
                configInfo.setAppName(appNameTmp);
            }
            updateConfigInfoAtomic(configInfo, srcIp, srcUser, time, configAdvanceInfo);
            String configTags = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("config_tags");
            if (configTags != null) {
                // delete all tags and then recreate
                removeTagByIdAtomic(oldConfigInfo.getId());
                addConfigTagsRelation(oldConfigInfo.getId(), configTags, configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant());
            }
            insertConfigHistoryAtomic(oldConfigInfo.getId(), oldConfigInfo, srcIp, srcUser, time, "U");
        } catch (CannotGetJdbcConnectionException e) {
            LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e);
            throw e;
        }
        return Boolean.TRUE;
    });
}
Also used : CannotGetJdbcConnectionException(org.springframework.jdbc.CannotGetJdbcConnectionException) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo)

Example 10 with ConfigInfo

use of com.alibaba.nacos.config.server.model.ConfigInfo in project nacos by alibaba.

the class ConfigController method cloneConfig.

/**
 * Execute clone config operation.
 *
 * @param request         http servlet request .
 * @param srcUser         src user string value.
 * @param namespace       namespace string value.
 * @param configBeansList config beans list.
 * @param policy          config policy model.
 * @return RestResult for map.
 * @throws NacosException NacosException.
 */
@PostMapping(params = "clone=true")
@Secured(action = ActionTypes.WRITE, signType = SignType.CONFIG)
public RestResult<Map<String, Object>> cloneConfig(HttpServletRequest request, @RequestParam(value = "src_user", required = false) String srcUser, @RequestParam(value = "tenant") String namespace, @RequestBody List<SameNamespaceCloneConfigBean> configBeansList, @RequestParam(value = "policy", defaultValue = "ABORT") SameConfigPolicy policy) throws NacosException {
    Map<String, Object> failedData = new HashMap<>(4);
    if (CollectionUtils.isEmpty(configBeansList)) {
        failedData.put("succCount", 0);
        return RestResultUtils.buildResult(ResultCodeEnum.NO_SELECTED_CONFIG, failedData);
    }
    configBeansList.removeAll(Collections.singleton(null));
    namespace = NamespaceUtil.processNamespaceParameter(namespace);
    if (StringUtils.isNotBlank(namespace) && persistService.tenantInfoCountByTenantId(namespace) <= 0) {
        failedData.put("succCount", 0);
        return RestResultUtils.buildResult(ResultCodeEnum.NAMESPACE_NOT_EXIST, failedData);
    }
    List<Long> idList = new ArrayList<>(configBeansList.size());
    Map<Long, SameNamespaceCloneConfigBean> configBeansMap = configBeansList.stream().collect(Collectors.toMap(SameNamespaceCloneConfigBean::getCfgId, cfg -> {
        idList.add(cfg.getCfgId());
        return cfg;
    }, (k1, k2) -> k1));
    List<ConfigAllInfo> queryedDataList = persistService.findAllConfigInfo4Export(null, null, null, null, idList);
    if (queryedDataList == null || queryedDataList.isEmpty()) {
        failedData.put("succCount", 0);
        return RestResultUtils.buildResult(ResultCodeEnum.DATA_EMPTY, failedData);
    }
    List<ConfigAllInfo> configInfoList4Clone = new ArrayList<>(queryedDataList.size());
    for (ConfigAllInfo ci : queryedDataList) {
        SameNamespaceCloneConfigBean paramBean = configBeansMap.get(ci.getId());
        ConfigAllInfo ci4save = new ConfigAllInfo();
        ci4save.setTenant(namespace);
        ci4save.setType(ci.getType());
        ci4save.setGroup((paramBean != null && StringUtils.isNotBlank(paramBean.getGroup())) ? paramBean.getGroup() : ci.getGroup());
        ci4save.setDataId((paramBean != null && StringUtils.isNotBlank(paramBean.getDataId())) ? paramBean.getDataId() : ci.getDataId());
        ci4save.setContent(ci.getContent());
        if (StringUtils.isNotBlank(ci.getAppName())) {
            ci4save.setAppName(ci.getAppName());
        }
        ci4save.setDesc(ci.getDesc());
        ci4save.setEncryptedDataKey(ci.getEncryptedDataKey() == null ? StringUtils.EMPTY : ci.getEncryptedDataKey());
        configInfoList4Clone.add(ci4save);
    }
    final String srcIp = RequestUtil.getRemoteIp(request);
    String requestIpApp = RequestUtil.getAppName(request);
    final Timestamp time = TimeUtils.getCurrentTime();
    Map<String, Object> saveResult = persistService.batchInsertOrUpdate(configInfoList4Clone, srcUser, srcIp, null, time, false, policy);
    for (ConfigInfo configInfo : configInfoList4Clone) {
        ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(false, configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), time.getTime()));
        ConfigTraceService.logPersistenceEvent(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), requestIpApp, time.getTime(), InetUtils.getSelfIP(), ConfigTraceService.PERSISTENCE_EVENT_PUB, configInfo.getContent());
    }
    return RestResultUtils.success("Clone Completed Successfully", saveResult);
}
Also used : SameNamespaceCloneConfigBean(com.alibaba.nacos.config.server.controller.parameters.SameNamespaceCloneConfigBean) ConfigType(com.alibaba.nacos.api.config.ConfigType) ConfigMetadata(com.alibaba.nacos.config.server.model.ConfigMetadata) RequestParam(org.springframework.web.bind.annotation.RequestParam) URLDecoder(java.net.URLDecoder) Pair(com.alibaba.nacos.common.utils.Pair) ServletException(javax.servlet.ServletException) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) Constants(com.alibaba.nacos.config.server.constant.Constants) MapUtil(com.alibaba.nacos.common.utils.MapUtil) ResultCodeEnum(com.alibaba.nacos.config.server.result.code.ResultCodeEnum) Map(java.util.Map) GroupkeyListenserStatus(com.alibaba.nacos.config.server.model.GroupkeyListenserStatus) YamlParserUtil(com.alibaba.nacos.config.server.utils.YamlParserUtil) SameNamespaceCloneConfigBean(com.alibaba.nacos.config.server.controller.parameters.SameNamespaceCloneConfigBean) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) RestResult(com.alibaba.nacos.common.model.RestResult) PostMapping(org.springframework.web.bind.annotation.PostMapping) TimeUtils(com.alibaba.nacos.config.server.utils.TimeUtils) HttpHeaders(org.springframework.http.HttpHeaders) Timestamp(java.sql.Timestamp) InetUtils(com.alibaba.nacos.sys.utils.InetUtils) Set(java.util.Set) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) StringUtils(com.alibaba.nacos.common.utils.StringUtils) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) CollectionUtils(org.springframework.util.CollectionUtils) RestResultUtils(com.alibaba.nacos.common.model.RestResultUtils) ActionTypes(com.alibaba.nacos.plugin.auth.constant.ActionTypes) Secured(com.alibaba.nacos.auth.annotation.Secured) DateFormatUtils(com.alibaba.nacos.common.utils.DateFormatUtils) ConfigDataChangeEvent(com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent) ConfigTraceService(com.alibaba.nacos.config.server.service.trace.ConfigTraceService) SignType(com.alibaba.nacos.plugin.auth.constant.SignType) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) HashMap(java.util.HashMap) NamespaceUtil(com.alibaba.nacos.common.utils.NamespaceUtil) RequestUtil(com.alibaba.nacos.config.server.utils.RequestUtil) ConfigAllInfo(com.alibaba.nacos.config.server.model.ConfigAllInfo) PersistService(com.alibaba.nacos.config.server.service.repository.PersistService) ArrayList(java.util.ArrayList) RequestBody(org.springframework.web.bind.annotation.RequestBody) HttpServletRequest(javax.servlet.http.HttpServletRequest) ZipUtils(com.alibaba.nacos.config.server.utils.ZipUtils) ConfigInfo4Beta(com.alibaba.nacos.config.server.model.ConfigInfo4Beta) EncryptionHandler(com.alibaba.nacos.plugin.encryption.handler.EncryptionHandler) NacosException(com.alibaba.nacos.api.exception.NacosException) ConfigAdvanceInfo(com.alibaba.nacos.config.server.model.ConfigAdvanceInfo) GetMapping(org.springframework.web.bind.annotation.GetMapping) Page(com.alibaba.nacos.config.server.model.Page) SameConfigPolicy(com.alibaba.nacos.config.server.model.SameConfigPolicy) ParamUtils(com.alibaba.nacos.config.server.utils.ParamUtils) Logger(org.slf4j.Logger) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) GroupKey(com.alibaba.nacos.config.server.utils.GroupKey) ConfigSubService(com.alibaba.nacos.config.server.service.ConfigSubService) HttpStatus(org.springframework.http.HttpStatus) AggrWhitelist(com.alibaba.nacos.config.server.service.AggrWhitelist) MD5Util(com.alibaba.nacos.config.server.utils.MD5Util) ConfigChangePublisher(com.alibaba.nacos.config.server.service.ConfigChangePublisher) SampleResult(com.alibaba.nacos.config.server.model.SampleResult) MultipartFile(org.springframework.web.multipart.MultipartFile) ResponseEntity(org.springframework.http.ResponseEntity) Collections(java.util.Collections) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) Timestamp(java.sql.Timestamp) ConfigAllInfo(com.alibaba.nacos.config.server.model.ConfigAllInfo) ConfigDataChangeEvent(com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent) PostMapping(org.springframework.web.bind.annotation.PostMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Aggregations

ConfigInfo (com.alibaba.nacos.config.server.model.ConfigInfo)36 ArrayList (java.util.ArrayList)17 Timestamp (java.sql.Timestamp)11 HashMap (java.util.HashMap)10 Secured (com.alibaba.nacos.auth.annotation.Secured)8 ConfigDataChangeEvent (com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent)8 NacosException (com.alibaba.nacos.api.exception.NacosException)6 Page (com.alibaba.nacos.config.server.model.Page)6 CannotGetJdbcConnectionException (org.springframework.jdbc.CannotGetJdbcConnectionException)6 ConfigAllInfo (com.alibaba.nacos.config.server.model.ConfigAllInfo)5 NacosConfigException (com.alibaba.nacos.config.server.exception.NacosConfigException)4 PersistService (com.alibaba.nacos.config.server.service.repository.PersistService)4 Map (java.util.Map)4 PostMapping (org.springframework.web.bind.annotation.PostMapping)4 ConfigInfoAggr (com.alibaba.nacos.config.server.model.ConfigInfoAggr)3 ConfigKey (com.alibaba.nacos.config.server.model.ConfigKey)3 List (java.util.List)3 Test (org.junit.Test)3 RestResult (com.alibaba.nacos.common.model.RestResult)2 Pair (com.alibaba.nacos.common.utils.Pair)2