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;
}
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);
}
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;
}
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;
}
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);
}
Aggregations