Search in sources :

Example 11 with ConfigInfo

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

the class ConfigController method importAndPublishConfig.

/**
 * Execute import and publish config operation.
 *
 * @param request   http servlet request .
 * @param srcUser   src user string value.
 * @param namespace namespace string value.
 * @param policy    policy model.
 * @param file      MultipartFile.
 * @return RestResult Map.
 * @throws NacosException NacosException.
 */
@PostMapping(params = "import=true")
@Secured(action = ActionTypes.WRITE, signType = SignType.CONFIG)
public RestResult<Map<String, Object>> importAndPublishConfig(HttpServletRequest request, @RequestParam(value = "src_user", required = false) String srcUser, @RequestParam(value = "namespace", required = false) String namespace, @RequestParam(value = "policy", defaultValue = "ABORT") SameConfigPolicy policy, MultipartFile file) throws NacosException {
    Map<String, Object> failedData = new HashMap<>(4);
    if (Objects.isNull(file)) {
        return RestResultUtils.buildResult(ResultCodeEnum.DATA_EMPTY, failedData);
    }
    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<ConfigAllInfo> configInfoList = new ArrayList<>();
    List<Map<String, String>> unrecognizedList = new ArrayList<>();
    try {
        ZipUtils.UnZipResult unziped = ZipUtils.unzip(file.getBytes());
        ZipUtils.ZipItem metaDataZipItem = unziped.getMetaDataItem();
        RestResult<Map<String, Object>> errorResult;
        if (metaDataZipItem != null && Constants.CONFIG_EXPORT_METADATA_NEW.equals(metaDataZipItem.getItemName())) {
            // new export
            errorResult = parseImportDataV2(unziped, configInfoList, unrecognizedList, namespace);
        } else {
            errorResult = parseImportData(unziped, configInfoList, unrecognizedList, namespace);
        }
        if (errorResult != null) {
            return errorResult;
        }
    } catch (IOException e) {
        failedData.put("succCount", 0);
        LOGGER.error("parsing data failed", e);
        return RestResultUtils.buildResult(ResultCodeEnum.PARSING_DATA_FAILED, failedData);
    }
    if (CollectionUtils.isEmpty(configInfoList)) {
        failedData.put("succCount", 0);
        return RestResultUtils.buildResult(ResultCodeEnum.DATA_EMPTY, failedData);
    }
    final String srcIp = RequestUtil.getRemoteIp(request);
    String requestIpApp = RequestUtil.getAppName(request);
    final Timestamp time = TimeUtils.getCurrentTime();
    Map<String, Object> saveResult = persistService.batchInsertOrUpdate(configInfoList, srcUser, srcIp, null, time, false, policy);
    for (ConfigInfo configInfo : configInfoList) {
        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());
    }
    // unrecognizedCount
    if (!unrecognizedList.isEmpty()) {
        saveResult.put("unrecognizedCount", unrecognizedList.size());
        saveResult.put("unrecognizedData", unrecognizedList);
    }
    return RestResultUtils.success("导入成功", saveResult);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ZipUtils(com.alibaba.nacos.config.server.utils.ZipUtils) IOException(java.io.IOException) 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) Map(java.util.Map) HashMap(java.util.HashMap) PostMapping(org.springframework.web.bind.annotation.PostMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 12 with ConfigInfo

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

the class ConfigController method publishConfig.

/**
 * Adds or updates non-aggregated data.
 *
 * @throws NacosException NacosException.
 */
@PostMapping
@Secured(action = ActionTypes.WRITE, signType = SignType.CONFIG)
public Boolean publishConfig(HttpServletRequest request, @RequestParam(value = "dataId") String dataId, @RequestParam(value = "group") String group, @RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant, @RequestParam(value = "content") String content, @RequestParam(value = "tag", required = false) String tag, @RequestParam(value = "appName", required = false) String appName, @RequestParam(value = "src_user", required = false) String srcUser, @RequestParam(value = "config_tags", required = false) String configTags, @RequestParam(value = "desc", required = false) String desc, @RequestParam(value = "use", required = false) String use, @RequestParam(value = "effect", required = false) String effect, @RequestParam(value = "type", required = false) String type, @RequestParam(value = "schema", required = false) String schema) throws NacosException {
    final String srcIp = RequestUtil.getRemoteIp(request);
    final String requestIpApp = RequestUtil.getAppName(request);
    if (StringUtils.isBlank(srcUser)) {
        srcUser = RequestUtil.getSrcUserName(request);
    }
    // check type
    if (!ConfigType.isValidType(type)) {
        type = ConfigType.getDefaultType().getType();
    }
    // encrypted
    Pair<String, String> pair = EncryptionHandler.encryptHandler(dataId, content);
    content = pair.getSecond();
    // check tenant
    ParamUtils.checkTenant(tenant);
    ParamUtils.checkParam(dataId, group, "datumId", content);
    ParamUtils.checkParam(tag);
    Map<String, Object> configAdvanceInfo = new HashMap<>(10);
    MapUtil.putIfValNoNull(configAdvanceInfo, "config_tags", configTags);
    MapUtil.putIfValNoNull(configAdvanceInfo, "desc", desc);
    MapUtil.putIfValNoNull(configAdvanceInfo, "use", use);
    MapUtil.putIfValNoNull(configAdvanceInfo, "effect", effect);
    MapUtil.putIfValNoNull(configAdvanceInfo, "type", type);
    MapUtil.putIfValNoNull(configAdvanceInfo, "schema", schema);
    ParamUtils.checkParam(configAdvanceInfo);
    if (AggrWhitelist.isAggrDataId(dataId)) {
        LOGGER.warn("[aggr-conflict] {} attempt to publish single data, {}, {}", RequestUtil.getRemoteIp(request), dataId, group);
        throw new NacosException(NacosException.NO_RIGHT, "dataId:" + dataId + " is aggr");
    }
    final Timestamp time = TimeUtils.getCurrentTime();
    String betaIps = request.getHeader("betaIps");
    ConfigInfo configInfo = new ConfigInfo(dataId, group, tenant, appName, content);
    configInfo.setType(type);
    String encryptedDataKey = pair.getFirst();
    configInfo.setEncryptedDataKey(encryptedDataKey);
    if (StringUtils.isBlank(betaIps)) {
        if (StringUtils.isBlank(tag)) {
            persistService.insertOrUpdate(srcIp, srcUser, configInfo, time, configAdvanceInfo, false);
            ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(false, dataId, group, tenant, time.getTime()));
        } else {
            persistService.insertOrUpdateTag(configInfo, tag, srcIp, srcUser, time, false);
            ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(false, dataId, group, tenant, tag, time.getTime()));
        }
    } else {
        // beta publish
        configInfo.setEncryptedDataKey(encryptedDataKey);
        persistService.insertOrUpdateBeta(configInfo, betaIps, srcIp, srcUser, time, false);
        ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(true, dataId, group, tenant, time.getTime()));
    }
    ConfigTraceService.logPersistenceEvent(dataId, group, tenant, requestIpApp, time.getTime(), InetUtils.getSelfIP(), ConfigTraceService.PERSISTENCE_EVENT_PUB, content);
    return true;
}
Also used : HashMap(java.util.HashMap) ConfigDataChangeEvent(com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) Timestamp(java.sql.Timestamp) NacosException(com.alibaba.nacos.api.exception.NacosException) PostMapping(org.springframework.web.bind.annotation.PostMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 13 with ConfigInfo

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

the class ConfigController method deleteConfigs.

/**
 * Execute delete config operation.
 *
 * @return java.lang.Boolean
 * @author klw
 * @Description: delete configuration based on multiple config ids
 * @Date 2019/7/5 10:26
 * @Param [request, response, dataId, group, tenant, tag]
 */
@DeleteMapping(params = "delType=ids")
@Secured(action = ActionTypes.WRITE, signType = SignType.CONFIG)
public RestResult<Boolean> deleteConfigs(HttpServletRequest request, @RequestParam(value = "ids") List<Long> ids) {
    String clientIp = RequestUtil.getRemoteIp(request);
    final Timestamp time = TimeUtils.getCurrentTime();
    List<ConfigInfo> configInfoList = persistService.removeConfigInfoByIds(ids, clientIp, null);
    if (CollectionUtils.isEmpty(configInfoList)) {
        return RestResultUtils.success(true);
    }
    for (ConfigInfo configInfo : configInfoList) {
        ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(false, configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), time.getTime()));
        ConfigTraceService.logPersistenceEvent(configInfo.getDataId(), configInfo.getGroup(), configInfo.getTenant(), null, time.getTime(), clientIp, ConfigTraceService.PERSISTENCE_EVENT_REMOVE, null);
    }
    return RestResultUtils.success(true);
}
Also used : ConfigDataChangeEvent(com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) Timestamp(java.sql.Timestamp) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 14 with ConfigInfo

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

the class ConfigPublishRequestHandler method handle.

@Override
@TpsControl(pointName = "ConfigPublish", parsers = { ConfigPublishGroupKeyParser.class, ConfigPublishGroupParser.class })
@Secured(action = ActionTypes.WRITE, signType = SignType.CONFIG)
public ConfigPublishResponse handle(ConfigPublishRequest request, RequestMeta meta) throws NacosException {
    try {
        String dataId = request.getDataId();
        String group = request.getGroup();
        String content = request.getContent();
        final String tenant = request.getTenant();
        final String srcIp = meta.getClientIp();
        final String requestIpApp = request.getAdditionParam("requestIpApp");
        final String tag = request.getAdditionParam("tag");
        final String appName = request.getAdditionParam("appName");
        final String type = request.getAdditionParam("type");
        final String srcUser = request.getAdditionParam("src_user");
        final String encryptedDataKey = request.getAdditionParam("encryptedDataKey");
        // check tenant
        ParamUtils.checkParam(dataId, group, "datumId", content);
        ParamUtils.checkParam(tag);
        Map<String, Object> configAdvanceInfo = new HashMap<String, Object>(10);
        MapUtil.putIfValNoNull(configAdvanceInfo, "config_tags", request.getAdditionParam("config_tags"));
        MapUtil.putIfValNoNull(configAdvanceInfo, "desc", request.getAdditionParam("desc"));
        MapUtil.putIfValNoNull(configAdvanceInfo, "use", request.getAdditionParam("use"));
        MapUtil.putIfValNoNull(configAdvanceInfo, "effect", request.getAdditionParam("effect"));
        MapUtil.putIfValNoNull(configAdvanceInfo, "type", type);
        MapUtil.putIfValNoNull(configAdvanceInfo, "schema", request.getAdditionParam("schema"));
        ParamUtils.checkParam(configAdvanceInfo);
        if (AggrWhitelist.isAggrDataId(dataId)) {
            Loggers.REMOTE_DIGEST.warn("[aggr-conflict] {} attempt to publish single data, {}, {}", srcIp, dataId, group);
            throw new NacosException(NacosException.NO_RIGHT, "dataId:" + dataId + " is aggr");
        }
        final Timestamp time = TimeUtils.getCurrentTime();
        ConfigInfo configInfo = new ConfigInfo(dataId, group, tenant, appName, content);
        configInfo.setMd5(request.getCasMd5());
        configInfo.setType(type);
        configInfo.setEncryptedDataKey(encryptedDataKey);
        String betaIps = request.getAdditionParam("betaIps");
        if (StringUtils.isBlank(betaIps)) {
            if (StringUtils.isBlank(tag)) {
                if (StringUtils.isNotBlank(request.getCasMd5())) {
                    boolean casSuccess = persistService.insertOrUpdateCas(srcIp, srcUser, configInfo, time, configAdvanceInfo, false);
                    if (!casSuccess) {
                        return ConfigPublishResponse.buildFailResponse(ResponseCode.FAIL.getCode(), "Cas publish fail,server md5 may have changed.");
                    }
                } else {
                    persistService.insertOrUpdate(srcIp, srcUser, configInfo, time, configAdvanceInfo, false);
                }
                ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(false, dataId, group, tenant, time.getTime()));
            } else {
                if (StringUtils.isNotBlank(request.getCasMd5())) {
                    boolean casSuccess = persistService.insertOrUpdateTagCas(configInfo, tag, srcIp, srcUser, time, false);
                    if (!casSuccess) {
                        return ConfigPublishResponse.buildFailResponse(ResponseCode.FAIL.getCode(), "Cas publish tag config fail,server md5 may have changed.");
                    }
                } else {
                    persistService.insertOrUpdateTag(configInfo, tag, srcIp, srcUser, time, false);
                }
                ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(false, dataId, group, tenant, tag, time.getTime()));
            }
        } else {
            // beta publish
            if (StringUtils.isNotBlank(request.getCasMd5())) {
                boolean casSuccess = persistService.insertOrUpdateBetaCas(configInfo, betaIps, srcIp, srcUser, time, false);
                if (!casSuccess) {
                    return ConfigPublishResponse.buildFailResponse(ResponseCode.FAIL.getCode(), "Cas publish beta config fail,server md5 may have changed.");
                }
            } else {
                persistService.insertOrUpdateBeta(configInfo, betaIps, srcIp, srcUser, time, false);
            }
            ConfigChangePublisher.notifyConfigChange(new ConfigDataChangeEvent(true, dataId, group, tenant, time.getTime()));
        }
        ConfigTraceService.logPersistenceEvent(dataId, group, tenant, requestIpApp, time.getTime(), InetUtils.getSelfIP(), ConfigTraceService.PERSISTENCE_EVENT_PUB, content);
        return ConfigPublishResponse.buildSuccessResponse();
    } catch (Exception e) {
        Loggers.REMOTE_DIGEST.error("[ConfigPublishRequestHandler] publish config error ,request ={}", request, e);
        return ConfigPublishResponse.buildFailResponse((e instanceof NacosException) ? ((NacosException) e).getErrCode() : ResponseCode.FAIL.getCode(), e.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) ConfigDataChangeEvent(com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) Timestamp(java.sql.Timestamp) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosException(com.alibaba.nacos.api.exception.NacosException) Secured(com.alibaba.nacos.auth.annotation.Secured) TpsControl(com.alibaba.nacos.core.remote.control.TpsControl)

Example 15 with ConfigInfo

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

the class ConfigDerbyImport_CITCase method testDerbyImport.

@Test()
public void testDerbyImport() throws Throwable {
    DatabaseOperate operate = context.getBean(DatabaseOperate.class);
    File file = DiskUtils.createTmpFile("derby_import" + System.currentTimeMillis(), ".tmp");
    DiskUtils.writeFile(file, ByteUtils.toBytes(SQL_SCRIPT_CONTEXT), false);
    try {
        List<Integer> ids = operate.queryMany("SELECT id FROM config_info", new Object[] {}, Integer.class);
        for (Integer each : ids) {
            System.out.println("current id in table config_info contain: " + each);
        }
        CompletableFuture<RestResult<String>> future = operate.dataImport(file);
        RestResult<String> result = future.join();
        System.out.println(result);
        Assert.assertTrue(result.ok());
        final String queryDataId = "people";
        final String queryGroup = "DEFAULT_GROUP";
        final String expectContent = "people.enable=true";
        PersistService persistService = context.getBean(PersistService.class);
        ConfigInfo configInfo = persistService.findConfigInfo(queryDataId, queryGroup, "");
        System.out.println(configInfo);
        Assert.assertNotNull(configInfo);
        Assert.assertEquals(queryDataId, configInfo.getDataId());
        Assert.assertEquals(queryGroup, configInfo.getGroup());
        Assert.assertEquals("", configInfo.getTenant());
        Assert.assertEquals(expectContent, configInfo.getContent());
    } finally {
        DiskUtils.deleteQuietly(file);
    }
}
Also used : RestResult(com.alibaba.nacos.common.model.RestResult) PersistService(com.alibaba.nacos.config.server.service.repository.PersistService) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) DatabaseOperate(com.alibaba.nacos.config.server.service.repository.embedded.DatabaseOperate) File(java.io.File) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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