Search in sources :

Example 16 with ConfigInfo

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

the class ConfigControllerTest method testFuzzySearchConfig.

@Test
public void testFuzzySearchConfig() throws Exception {
    List<ConfigInfo> configInfoList = new ArrayList<>();
    ConfigInfo configInfo = new ConfigInfo("test", "test", "test");
    configInfoList.add(configInfo);
    Page<ConfigInfo> page = new Page<>();
    page.setTotalCount(15);
    page.setPageNumber(1);
    page.setPagesAvailable(2);
    page.setPageItems(configInfoList);
    Map<String, Object> configAdvanceInfo = new HashMap<>(8);
    when(persistService.findConfigInfoLike4Page(1, 10, "test", "test", "", configAdvanceInfo)).thenReturn(page);
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.CONFIG_CONTROLLER_PATH).param("search", "blur").param("dataId", "test").param("group", "test").param("appName", "").param("tenant", "").param("config_tags", "").param("pageNo", "1").param("pageSize", "10");
    String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
    List resultList = JacksonUtils.toObj(JacksonUtils.toObj(actualValue).get("pageItems").toString(), List.class);
    ConfigInfo resConfigInfo = JacksonUtils.toObj(JacksonUtils.toObj(actualValue).get("pageItems").get(0).toString(), ConfigInfo.class);
    Assert.assertEquals(configInfoList.size(), resultList.size());
    Assert.assertEquals(configInfo.getDataId(), resConfigInfo.getDataId());
    Assert.assertEquals(configInfo.getGroup(), resConfigInfo.getGroup());
    Assert.assertEquals(configInfo.getContent(), resConfigInfo.getContent());
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ArrayList(java.util.ArrayList) Page(com.alibaba.nacos.config.server.model.Page) ArrayList(java.util.ArrayList) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) List(java.util.List) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) Test(org.junit.Test)

Example 17 with ConfigInfo

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

the class ConfigControllerTest method testSearchConfig.

@Test
public void testSearchConfig() throws Exception {
    List<ConfigInfo> configInfoList = new ArrayList<>();
    ConfigInfo configInfo = new ConfigInfo("test", "test", "test");
    configInfoList.add(configInfo);
    Page<ConfigInfo> page = new Page<>();
    page.setTotalCount(15);
    page.setPageNumber(1);
    page.setPagesAvailable(2);
    page.setPageItems(configInfoList);
    Map<String, Object> configAdvanceInfo = new HashMap<>(8);
    when(persistService.findConfigInfo4Page(1, 10, "test", "test", "", configAdvanceInfo)).thenReturn(page);
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.CONFIG_CONTROLLER_PATH).param("search", "accurate").param("dataId", "test").param("group", "test").param("appName", "").param("tenant", "").param("config_tags", "").param("pageNo", "1").param("pageSize", "10");
    String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
    JsonNode pageItemsNode = JacksonUtils.toObj(actualValue).get("pageItems");
    List resultList = JacksonUtils.toObj(pageItemsNode.toString(), List.class);
    ConfigInfo resConfigInfo = JacksonUtils.toObj(pageItemsNode.get(0).toString(), ConfigInfo.class);
    Assert.assertEquals(configInfoList.size(), resultList.size());
    Assert.assertEquals(configInfo.getDataId(), resConfigInfo.getDataId());
    Assert.assertEquals(configInfo.getGroup(), resConfigInfo.getGroup());
    Assert.assertEquals(configInfo.getContent(), resConfigInfo.getContent());
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ArrayList(java.util.ArrayList) Page(com.alibaba.nacos.config.server.model.Page) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayList(java.util.ArrayList) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) List(java.util.List) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) Test(org.junit.Test)

Example 18 with ConfigInfo

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

the class EmbeddedStoragePersistServiceImpl 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) {
    try {
        ConfigInfo oldConfigInfo = findConfigInfo(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant());
        final String tenantTmp = StringUtils.isBlank(configInfo.getTenant()) ? StringUtils.EMPTY : configInfo.getTenant();
        oldConfigInfo.setTenant(tenantTmp);
        String appNameTmp = oldConfigInfo.getAppName();
        // otherwise, the appName of db is used. Empty string is required to clear appName
        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 recreate them
            removeTagByIdAtomic(oldConfigInfo.getId());
            addConfigTagsRelation(oldConfigInfo.getId(), configTags, configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant());
        }
        insertConfigHistoryAtomic(oldConfigInfo.getId(), oldConfigInfo, srcIp, srcUser, time, "U");
        EmbeddedStorageContextUtils.onModifyConfigInfo(configInfo, srcIp, time);
        databaseOperate.blockUpdate();
    } finally {
        EmbeddedStorageContextUtils.cleanAllContext();
    }
}
Also used : ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo)

Example 19 with ConfigInfo

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

the class EmbeddedStoragePersistServiceImpl method batchInsertOrUpdate.

@Override
public Map<String, Object> batchInsertOrUpdate(List<ConfigAllInfo> configInfoList, String srcUser, String srcIp, Map<String, Object> configAdvanceInfo, Timestamp time, boolean notify, SameConfigPolicy policy) throws NacosException {
    int succCount = 0;
    int skipCount = 0;
    List<Map<String, String>> failData = null;
    List<Map<String, String>> skipData = null;
    final BiConsumer<Boolean, Throwable> callFinally = (result, t) -> {
        if (t != null) {
            throw new NacosRuntimeException(0, t);
        }
    };
    for (int i = 0; i < configInfoList.size(); i++) {
        ConfigAllInfo configInfo = configInfoList.get(i);
        try {
            ParamUtils.checkParam(configInfo.getDataId(), configInfo.getGroup(), "datumId", configInfo.getContent());
        } catch (Throwable e) {
            DEFAULT_LOG.error("data verification failed", e);
            throw e;
        }
        ConfigInfo configInfo2Save = new ConfigInfo(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), configInfo.getAppName(), configInfo.getContent());
        configInfo2Save.setEncryptedDataKey(configInfo.getEncryptedDataKey() == null ? "" : configInfo.getEncryptedDataKey());
        String type = configInfo.getType();
        if (StringUtils.isBlank(type)) {
            // simple judgment of file type based on suffix
            if (configInfo.getDataId().contains(SPOT)) {
                String extName = configInfo.getDataId().substring(configInfo.getDataId().lastIndexOf(SPOT) + 1);
                FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(extName);
                type = fileTypeEnum.getFileType();
            } else {
                type = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(null).getFileType();
            }
        }
        if (configAdvanceInfo == null) {
            configAdvanceInfo = new HashMap<>(16);
        }
        configAdvanceInfo.put("type", type);
        configAdvanceInfo.put("desc", configInfo.getDesc());
        try {
            ConfigInfo foundCfg = findConfigInfo(configInfo2Save.getDataId(), configInfo2Save.getGroup(), configInfo2Save.getTenant());
            if (foundCfg != null) {
                throw new Throwable("DuplicateKeyException: config already exists, should be overridden");
            }
            addConfigInfo(srcIp, srcUser, configInfo2Save, time, configAdvanceInfo, notify, callFinally);
            succCount++;
        } catch (Throwable e) {
            if (!StringUtils.contains(e.toString(), "DuplicateKeyException")) {
                throw new NacosException(NacosException.SERVER_ERROR, e);
            }
            // uniqueness constraint conflict
            if (SameConfigPolicy.ABORT.equals(policy)) {
                failData = new ArrayList<>();
                skipData = new ArrayList<>();
                Map<String, String> faileditem = new HashMap<>(2);
                faileditem.put("dataId", configInfo2Save.getDataId());
                faileditem.put("group", configInfo2Save.getGroup());
                failData.add(faileditem);
                for (int j = (i + 1); j < configInfoList.size(); j++) {
                    ConfigInfo skipConfigInfo = configInfoList.get(j);
                    Map<String, String> skipitem = new HashMap<>(2);
                    skipitem.put("dataId", skipConfigInfo.getDataId());
                    skipitem.put("group", skipConfigInfo.getGroup());
                    skipData.add(skipitem);
                }
                break;
            } else if (SameConfigPolicy.SKIP.equals(policy)) {
                skipCount++;
                if (skipData == null) {
                    skipData = new ArrayList<>();
                }
                Map<String, String> skipitem = new HashMap<>(2);
                skipitem.put("dataId", configInfo2Save.getDataId());
                skipitem.put("group", configInfo2Save.getGroup());
                skipData.add(skipitem);
            } else if (SameConfigPolicy.OVERWRITE.equals(policy)) {
                succCount++;
                updateConfigInfo(configInfo2Save, srcIp, srcUser, time, configAdvanceInfo, notify);
            }
        }
    }
    Map<String, Object> result = new HashMap<>(4);
    result.put("succCount", succCount);
    result.put("skipCount", skipCount);
    if (failData != null && !failData.isEmpty()) {
        result.put("failData", failData);
    }
    if (skipData != null && !skipData.isEmpty()) {
        result.put("skipData", skipData);
    }
    return result;
}
Also used : NotifyCenter(com.alibaba.nacos.common.notify.NotifyCenter) Pair(com.alibaba.nacos.common.utils.Pair) CONFIG_KEY_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_KEY_ROW_MAPPER) Constants(com.alibaba.nacos.config.server.constant.Constants) HISTORY_DETAIL_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.HISTORY_DETAIL_ROW_MAPPER) ConfigInfoBase(com.alibaba.nacos.config.server.model.ConfigInfoBase) MD5Utils(com.alibaba.nacos.common.utils.MD5Utils) CONFIG_INFO_BASE_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_INFO_BASE_ROW_MAPPER) HISTORY_LIST_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.HISTORY_LIST_ROW_MAPPER) TENANT_INFO_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.TENANT_INFO_ROW_MAPPER) Map(java.util.Map) CONFIG_INFO_WRAPPER_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_INFO_WRAPPER_ROW_MAPPER) CONFIG_INFO_CHANGED_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_INFO_CHANGED_ROW_MAPPER) NacosConfigException(com.alibaba.nacos.config.server.exception.NacosConfigException) CONFIG_ADVANCE_INFO_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_ADVANCE_INFO_ROW_MAPPER) SubInfo(com.alibaba.nacos.config.server.model.SubInfo) IdGeneratorManager(com.alibaba.nacos.core.distributed.id.IdGeneratorManager) CONFIG_INFO_TAG_WRAPPER_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_INFO_TAG_WRAPPER_ROW_MAPPER) CONFIG_ALL_INFO_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_ALL_INFO_ROW_MAPPER) MAP_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.MAP_ROW_MAPPER) DynamicDataSource(com.alibaba.nacos.config.server.service.datasource.DynamicDataSource) Timestamp(java.sql.Timestamp) CONFIG_INFO_AGGR_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_INFO_AGGR_ROW_MAPPER) CONFIG_INFO_BETA_WRAPPER_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_INFO_BETA_WRAPPER_ROW_MAPPER) Collectors(java.util.stream.Collectors) CONFIG_INFO_ROW_MAPPER(com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_INFO_ROW_MAPPER) ConfigInfoTagWrapper(com.alibaba.nacos.config.server.model.ConfigInfoTagWrapper) Objects(java.util.Objects) List(java.util.List) DEFAULT_LOG(com.alibaba.nacos.config.server.utils.LogUtil.DEFAULT_LOG) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) StringUtils(com.alibaba.nacos.common.utils.StringUtils) PostConstruct(javax.annotation.PostConstruct) Entry(java.util.Map.Entry) Optional(java.util.Optional) Conditional(org.springframework.context.annotation.Conditional) TenantInfo(com.alibaba.nacos.config.server.model.TenantInfo) LogUtil(com.alibaba.nacos.config.server.utils.LogUtil) FileTypeEnum(com.alibaba.nacos.config.server.enums.FileTypeEnum) HashMap(java.util.HashMap) ConfigAllInfo(com.alibaba.nacos.config.server.model.ConfigAllInfo) ConfigInfoAggr(com.alibaba.nacos.config.server.model.ConfigInfoAggr) PersistService(com.alibaba.nacos.config.server.service.repository.PersistService) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) ArrayList(java.util.ArrayList) CollectionUtils(org.apache.commons.collections.CollectionUtils) EncryptionHandler(com.alibaba.nacos.plugin.encryption.handler.EncryptionHandler) NacosException(com.alibaba.nacos.api.exception.NacosException) EmbeddedStorageContextUtils(com.alibaba.nacos.config.server.service.sql.EmbeddedStorageContextUtils) BiConsumer(java.util.function.BiConsumer) ConfigAdvanceInfo(com.alibaba.nacos.config.server.model.ConfigAdvanceInfo) ConfigHistoryInfo(com.alibaba.nacos.config.server.model.ConfigHistoryInfo) ConfigKey(com.alibaba.nacos.config.server.model.ConfigKey) Page(com.alibaba.nacos.config.server.model.Page) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) ConditionOnEmbeddedStorage(com.alibaba.nacos.config.server.configuration.ConditionOnEmbeddedStorage) SameConfigPolicy(com.alibaba.nacos.config.server.model.SameConfigPolicy) ParamUtils(com.alibaba.nacos.config.server.utils.ParamUtils) ConfigInfoWrapper(com.alibaba.nacos.config.server.model.ConfigInfoWrapper) DerbyImportEvent(com.alibaba.nacos.config.server.model.event.DerbyImportEvent) IOException(java.io.IOException) ConfigInfoBetaWrapper(com.alibaba.nacos.config.server.model.ConfigInfoBetaWrapper) ConfigInfoChanged(com.alibaba.nacos.config.server.model.ConfigInfoChanged) PaginationHelper(com.alibaba.nacos.config.server.service.repository.PaginationHelper) Component(org.springframework.stereotype.Component) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Collections(java.util.Collections) DataSourceService(com.alibaba.nacos.config.server.service.datasource.DataSourceService) HashMap(java.util.HashMap) FileTypeEnum(com.alibaba.nacos.config.server.enums.FileTypeEnum) ArrayList(java.util.ArrayList) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) NacosException(com.alibaba.nacos.api.exception.NacosException) ConfigAllInfo(com.alibaba.nacos.config.server.model.ConfigAllInfo) Map(java.util.Map) HashMap(java.util.HashMap)

Example 20 with ConfigInfo

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

the class EmbeddedStoragePersistServiceImpl method findConfigInfoLike4Page.

@Override
public Page<ConfigInfo> findConfigInfoLike4Page(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;
    final String appName = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("appName");
    final String content = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("content");
    final String configTags = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("config_tags");
    String sqlCountRows = "SELECT count(*) FROM config_info";
    String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,app_name,content,encrypted_data_key FROM config_info";
    StringBuilder where = new StringBuilder(" WHERE ");
    List<String> params = new ArrayList<String>();
    params.add(generateLikeArgument(tenantTmp));
    if (StringUtils.isNotBlank(configTags)) {
        sqlCountRows = "SELECT count(*) FROM config_info  a LEFT JOIN config_tags_relation b ON a.id=b.id ";
        sqlFetchRows = "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 LIKE ? ");
        if (!StringUtils.isBlank(dataId)) {
            where.append(" AND a.data_id LIKE ? ");
            params.add(generateLikeArgument(dataId));
        }
        if (!StringUtils.isBlank(group)) {
            where.append(" AND a.group_id LIKE ? ");
            params.add(generateLikeArgument(group));
        }
        if (!StringUtils.isBlank(appName)) {
            where.append(" AND a.app_name = ? ");
            params.add(appName);
        }
        if (!StringUtils.isBlank(content)) {
            where.append(" AND a.content LIKE ? ");
            params.add(generateLikeArgument(content));
        }
        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('?');
            params.add(tagArr[i]);
        }
        where.append(") ");
    } else {
        where.append(" tenant_id LIKE ? ");
        if (!StringUtils.isBlank(dataId)) {
            where.append(" AND data_id LIKE ? ");
            params.add(generateLikeArgument(dataId));
        }
        if (!StringUtils.isBlank(group)) {
            where.append(" AND group_id LIKE ? ");
            params.add(generateLikeArgument(group));
        }
        if (!StringUtils.isBlank(appName)) {
            where.append(" AND app_name = ? ");
            params.add(appName);
        }
        if (!StringUtils.isBlank(content)) {
            where.append(" AND content LIKE ? ");
            params.add(generateLikeArgument(content));
        }
    }
    PaginationHelper<ConfigInfo> helper = createPaginationHelper();
    Page<ConfigInfo> page = helper.fetchPage(sqlCountRows + where, sqlFetchRows + where, params.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;
}
Also used : ArrayList(java.util.ArrayList) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo)

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