Search in sources :

Example 1 with ConfigAllInfo

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

the class ConfigController method parseImportData.

/**
 * old import config.
 *
 * @param unziped          export file.
 * @param configInfoList   parse file result.
 * @param unrecognizedList unrecognized file.
 * @param namespace        import namespace.
 * @return error result.
 */
private RestResult<Map<String, Object>> parseImportData(ZipUtils.UnZipResult unziped, List<ConfigAllInfo> configInfoList, List<Map<String, String>> unrecognizedList, String namespace) {
    ZipUtils.ZipItem metaDataZipItem = unziped.getMetaDataItem();
    Map<String, String> metaDataMap = new HashMap<>(16);
    if (metaDataZipItem != null) {
        // compatible all file separator
        String metaDataStr = metaDataZipItem.getItemData().replaceAll("[\r\n]+", "|");
        String[] metaDataArr = metaDataStr.split("\\|");
        Map<String, Object> failedData = new HashMap<>(4);
        for (String metaDataItem : metaDataArr) {
            String[] metaDataItemArr = metaDataItem.split("=");
            if (metaDataItemArr.length != 2) {
                failedData.put("succCount", 0);
                return RestResultUtils.buildResult(ResultCodeEnum.METADATA_ILLEGAL, failedData);
            }
            metaDataMap.put(metaDataItemArr[0], metaDataItemArr[1]);
        }
    }
    List<ZipUtils.ZipItem> itemList = unziped.getZipItemList();
    if (itemList != null && !itemList.isEmpty()) {
        for (ZipUtils.ZipItem item : itemList) {
            String[] groupAdnDataId = item.getItemName().split(Constants.CONFIG_EXPORT_ITEM_FILE_SEPARATOR);
            if (groupAdnDataId.length != 2) {
                Map<String, String> unrecognizedItem = new HashMap<>(2);
                unrecognizedItem.put("itemName", item.getItemName());
                unrecognizedList.add(unrecognizedItem);
                continue;
            }
            String group = groupAdnDataId[0];
            String dataId = groupAdnDataId[1];
            String tempDataId = dataId;
            if (tempDataId.contains(".")) {
                tempDataId = tempDataId.substring(0, tempDataId.lastIndexOf(".")) + "~" + tempDataId.substring(tempDataId.lastIndexOf(".") + 1);
            }
            final String metaDataId = group + "." + tempDataId + ".app";
            // encrypted
            String content = item.getItemData();
            Pair<String, String> pair = EncryptionHandler.encryptHandler(dataId, content);
            content = pair.getSecond();
            ConfigAllInfo ci = new ConfigAllInfo();
            ci.setGroup(group);
            ci.setDataId(dataId);
            ci.setContent(content);
            if (metaDataMap.get(metaDataId) != null) {
                ci.setAppName(metaDataMap.get(metaDataId));
            }
            ci.setTenant(namespace);
            ci.setEncryptedDataKey(pair.getFirst());
            configInfoList.add(ci);
        }
    }
    return null;
}
Also used : ConfigAllInfo(com.alibaba.nacos.config.server.model.ConfigAllInfo) HashMap(java.util.HashMap) ZipUtils(com.alibaba.nacos.config.server.utils.ZipUtils)

Example 2 with ConfigAllInfo

use of com.alibaba.nacos.config.server.model.ConfigAllInfo 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)

Example 3 with ConfigAllInfo

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

the class ConfigController method detailConfigInfo.

/**
 * Get the specific configuration information that the console USES.
 *
 * @throws NacosException NacosException.
 */
@GetMapping(params = "show=all")
@Secured(action = ActionTypes.READ, signType = SignType.CONFIG)
public ConfigAllInfo detailConfigInfo(@RequestParam("dataId") String dataId, @RequestParam("group") String group, @RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant) throws NacosException {
    // check tenant
    ParamUtils.checkTenant(tenant);
    // check params
    ParamUtils.checkParam(dataId, group, "datumId", "content");
    ConfigAllInfo configAllInfo = persistService.findConfigAllInfo(dataId, group, tenant);
    // decrypted
    if (Objects.nonNull(configAllInfo)) {
        String encryptedDataKey = configAllInfo.getEncryptedDataKey();
        Pair<String, String> pair = EncryptionHandler.decryptHandler(dataId, encryptedDataKey, configAllInfo.getContent());
        configAllInfo.setContent(pair.getSecond());
    }
    return configAllInfo;
}
Also used : ConfigAllInfo(com.alibaba.nacos.config.server.model.ConfigAllInfo) GetMapping(org.springframework.web.bind.annotation.GetMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 4 with ConfigAllInfo

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

the class ConfigController method parseImportDataV2.

/**
 * new version import config add .metadata.yml file.
 *
 * @param unziped          export file.
 * @param configInfoList   parse file result.
 * @param unrecognizedList unrecognized file.
 * @param namespace        import namespace.
 * @return error result.
 */
private RestResult<Map<String, Object>> parseImportDataV2(ZipUtils.UnZipResult unziped, List<ConfigAllInfo> configInfoList, List<Map<String, String>> unrecognizedList, String namespace) {
    ZipUtils.ZipItem metaDataItem = unziped.getMetaDataItem();
    String metaData = metaDataItem.getItemData();
    Map<String, Object> failedData = new HashMap<>(4);
    ConfigMetadata configMetadata = YamlParserUtil.loadObject(metaData, ConfigMetadata.class);
    if (configMetadata == null || CollectionUtils.isEmpty(configMetadata.getMetadata())) {
        failedData.put("succCount", 0);
        return RestResultUtils.buildResult(ResultCodeEnum.METADATA_ILLEGAL, failedData);
    }
    List<ConfigMetadata.ConfigExportItem> configExportItems = configMetadata.getMetadata();
    // check config metadata
    for (ConfigMetadata.ConfigExportItem configExportItem : configExportItems) {
        if (StringUtils.isBlank(configExportItem.getDataId()) || StringUtils.isBlank(configExportItem.getGroup()) || StringUtils.isBlank(configExportItem.getType())) {
            failedData.put("succCount", 0);
            return RestResultUtils.buildResult(ResultCodeEnum.METADATA_ILLEGAL, failedData);
        }
    }
    List<ZipUtils.ZipItem> zipItemList = unziped.getZipItemList();
    Set<String> metaDataKeys = configExportItems.stream().map(metaItem -> GroupKey.getKey(metaItem.getDataId(), metaItem.getGroup())).collect(Collectors.toSet());
    Map<String, String> configContentMap = new HashMap<>(zipItemList.size());
    int itemNameLength = 2;
    zipItemList.forEach(item -> {
        String itemName = item.getItemName();
        String[] groupAdnDataId = itemName.split(Constants.CONFIG_EXPORT_ITEM_FILE_SEPARATOR);
        if (groupAdnDataId.length != itemNameLength) {
            Map<String, String> unrecognizedItem = new HashMap<>(2);
            unrecognizedItem.put("itemName", item.getItemName());
            unrecognizedList.add(unrecognizedItem);
            return;
        }
        String group = groupAdnDataId[0];
        String dataId = groupAdnDataId[1];
        String key = GroupKey.getKey(dataId, group);
        // metadata does not contain config file
        if (!metaDataKeys.contains(key)) {
            Map<String, String> unrecognizedItem = new HashMap<>(2);
            unrecognizedItem.put("itemName", "未在元数据中找到: " + item.getItemName());
            unrecognizedList.add(unrecognizedItem);
            return;
        }
        String itemData = item.getItemData();
        configContentMap.put(key, itemData);
    });
    for (ConfigMetadata.ConfigExportItem configExportItem : configExportItems) {
        String dataId = configExportItem.getDataId();
        String group = configExportItem.getGroup();
        String content = configContentMap.get(GroupKey.getKey(dataId, group));
        // config file not in metadata
        if (content == null) {
            Map<String, String> unrecognizedItem = new HashMap<>(2);
            unrecognizedItem.put("itemName", "未在文件中找到: " + group + "/" + dataId);
            unrecognizedList.add(unrecognizedItem);
            continue;
        }
        // encrypted
        Pair<String, String> pair = EncryptionHandler.encryptHandler(dataId, content);
        content = pair.getSecond();
        ConfigAllInfo ci = new ConfigAllInfo();
        ci.setGroup(group);
        ci.setDataId(dataId);
        ci.setContent(content);
        ci.setType(configExportItem.getType());
        ci.setDesc(configExportItem.getDesc());
        ci.setAppName(configExportItem.getAppName());
        ci.setTenant(namespace);
        ci.setEncryptedDataKey(pair.getFirst());
        configInfoList.add(ci);
    }
    return null;
}
Also used : 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) ZipUtils(com.alibaba.nacos.config.server.utils.ZipUtils) ConfigMetadata(com.alibaba.nacos.config.server.model.ConfigMetadata) ConfigAllInfo(com.alibaba.nacos.config.server.model.ConfigAllInfo)

Example 5 with ConfigAllInfo

use of com.alibaba.nacos.config.server.model.ConfigAllInfo 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)

Aggregations

ConfigAllInfo (com.alibaba.nacos.config.server.model.ConfigAllInfo)13 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)7 Secured (com.alibaba.nacos.auth.annotation.Secured)6 ConfigInfo (com.alibaba.nacos.config.server.model.ConfigInfo)6 ZipUtils (com.alibaba.nacos.config.server.utils.ZipUtils)6 Map (java.util.Map)5 GetMapping (org.springframework.web.bind.annotation.GetMapping)5 NacosException (com.alibaba.nacos.api.exception.NacosException)4 Pair (com.alibaba.nacos.common.utils.Pair)3 StringUtils (com.alibaba.nacos.common.utils.StringUtils)3 Constants (com.alibaba.nacos.config.server.constant.Constants)3 SameNamespaceCloneConfigBean (com.alibaba.nacos.config.server.controller.parameters.SameNamespaceCloneConfigBean)3 ConfigAdvanceInfo (com.alibaba.nacos.config.server.model.ConfigAdvanceInfo)3 ConfigMetadata (com.alibaba.nacos.config.server.model.ConfigMetadata)3 Page (com.alibaba.nacos.config.server.model.Page)3 SameConfigPolicy (com.alibaba.nacos.config.server.model.SameConfigPolicy)3 ConfigDataChangeEvent (com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent)3 IOException (java.io.IOException)3 Timestamp (java.sql.Timestamp)3