use of com.alibaba.nacos.config.server.model.ConfigMetadata 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.ConfigMetadata in project nacos by alibaba.
the class YamlParserUtilTest method testDumpObject.
@Test
public void testDumpObject() {
ConfigMetadata configMetadata = new ConfigMetadata();
List<ConfigMetadata.ConfigExportItem> configMetadataItems = new ArrayList<>();
configMetadataItems.add(item1);
configMetadataItems.add(item2);
configMetadata.setMetadata(configMetadataItems);
String parseString = YamlParserUtil.dumpObject(configMetadata);
Assert.assertEquals(CONFIG_METADATA_STRING, parseString);
}
use of com.alibaba.nacos.config.server.model.ConfigMetadata in project nacos by alibaba.
the class YamlParserUtilTest method testLoadObject.
@Test
public void testLoadObject() {
ConfigMetadata configMetadata = YamlParserUtil.loadObject(CONFIG_METADATA_STRING, ConfigMetadata.class);
Assert.assertNotNull(configMetadata);
List<ConfigMetadata.ConfigExportItem> metadataList = configMetadata.getMetadata();
Assert.assertNotNull(metadataList);
Assert.assertEquals(metadataList.size(), 2);
ConfigMetadata.ConfigExportItem configExportItem1 = metadataList.get(0);
ConfigMetadata.ConfigExportItem configExportItem2 = metadataList.get(1);
Assert.assertEquals(configExportItem1, item1);
Assert.assertEquals(configExportItem2, item2);
}
use of com.alibaba.nacos.config.server.model.ConfigMetadata in project nacos by alibaba.
the class ConfigController method exportConfigV2.
/**
* new version export config add metadata.yml file record config metadata.
*
* @param dataId dataId string value.
* @param group group string value.
* @param appName appName string value.
* @param tenant tenant string value.
* @param ids id list value.
* @return ResponseEntity.
*/
@GetMapping(params = "exportV2=true")
@Secured(action = ActionTypes.READ, signType = SignType.CONFIG)
public ResponseEntity<byte[]> exportConfigV2(@RequestParam(value = "dataId", required = false) String dataId, @RequestParam(value = "group", required = false) String group, @RequestParam(value = "appName", required = false) String appName, @RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant, @RequestParam(value = "ids", required = false) List<Long> ids) {
ids.removeAll(Collections.singleton(null));
tenant = NamespaceUtil.processNamespaceParameter(tenant);
List<ConfigAllInfo> dataList = persistService.findAllConfigInfo4Export(dataId, group, tenant, appName, ids);
List<ZipUtils.ZipItem> zipItemList = new ArrayList<>();
List<ConfigMetadata.ConfigExportItem> configMetadataItems = new ArrayList<>();
for (ConfigAllInfo ci : dataList) {
ConfigMetadata.ConfigExportItem configMetadataItem = new ConfigMetadata.ConfigExportItem();
configMetadataItem.setAppName(ci.getAppName());
configMetadataItem.setDataId(ci.getDataId());
configMetadataItem.setDesc(ci.getDesc());
configMetadataItem.setGroup(ci.getGroup());
configMetadataItem.setType(ci.getType());
configMetadataItems.add(configMetadataItem);
Pair<String, String> pair = EncryptionHandler.decryptHandler(ci.getDataId(), ci.getEncryptedDataKey(), ci.getContent());
String itemName = ci.getGroup() + Constants.CONFIG_EXPORT_ITEM_FILE_SEPARATOR + ci.getDataId();
zipItemList.add(new ZipUtils.ZipItem(itemName, pair.getSecond()));
}
ConfigMetadata configMetadata = new ConfigMetadata();
configMetadata.setMetadata(configMetadataItems);
zipItemList.add(new ZipUtils.ZipItem(Constants.CONFIG_EXPORT_METADATA_NEW, YamlParserUtil.dumpObject(configMetadata)));
HttpHeaders headers = new HttpHeaders();
String fileName = EXPORT_CONFIG_FILE_NAME + DateFormatUtils.format(new Date(), EXPORT_CONFIG_FILE_NAME_DATE_FORMAT) + EXPORT_CONFIG_FILE_NAME_EXT;
headers.add("Content-Disposition", "attachment;filename=" + fileName);
return new ResponseEntity<>(ZipUtils.zip(zipItemList), headers, HttpStatus.OK);
}
use of com.alibaba.nacos.config.server.model.ConfigMetadata in project nacos by alibaba.
the class ConfigExportAndImportAPI_CITCase method testExportV2.
@Test
public void testExportV2() {
String dataId = "testNoAppname2.txt";
String getDataUrl = "?search=accurate&group=TEST1_GROUP&pageNo=1&pageSize=10&tenant=&namespaceId=&dataId=" + dataId;
String queryResult = httpClient.get(SERVER_ADDR + CONFIG_CONTROLLER_PATH + getDataUrl, null);
JsonNode resultObj = JacksonUtils.toObj(queryResult);
JsonNode resultConfigs = resultObj.get("pageItems");
JsonNode config1 = resultConfigs.get(0);
String configId = config1.get("id").asText();
String exportByIdsUrl = "?exportV2=true&tenant=&group=&appName=&ids=" + configId;
byte[] zipData = httpClient.download(SERVER_ADDR + CONFIG_CONTROLLER_PATH + exportByIdsUrl, null);
ZipUtils.UnZipResult unZiped = ZipUtils.unzip(zipData);
List<ZipUtils.ZipItem> zipItemList = unZiped.getZipItemList();
Assert.assertEquals(1, zipItemList.size());
String config1Name = config1.get("group").textValue() + "/" + config1.get("dataId").textValue();
for (ZipUtils.ZipItem zipItem : zipItemList) {
if (!(config1Name.equals(zipItem.getItemName()))) {
Assert.fail();
}
}
Assert.assertEquals(dataId, config1.get("dataId").asText());
String group = config1.get("group").asText();
String queryConfigDetailResult = httpClient.get(SERVER_ADDR + CONFIG_CONTROLLER_PATH + "?show=all&dataId=" + dataId + "&group=" + group, null);
JsonNode configDetailResult = JacksonUtils.toObj(queryConfigDetailResult);
Assert.assertNotNull(configDetailResult);
// verification metadata
ZipUtils.ZipItem metaDataItem = unZiped.getMetaDataItem();
Assert.assertNotNull(metaDataItem);
String metaDataItemItemData = metaDataItem.getItemData();
ConfigMetadata configMetadata = YamlParserUtil.loadObject(metaDataItemItemData, ConfigMetadata.class);
Assert.assertNotNull(configMetadata);
Assert.assertEquals(configMetadata.getMetadata().size(), 1);
ConfigMetadata.ConfigExportItem config1Metadata = new ConfigMetadata.ConfigExportItem();
config1Metadata.setDataId(dataId);
config1Metadata.setGroup(group);
config1Metadata.setType(configDetailResult.get("type").asText());
config1Metadata.setAppName(configDetailResult.get("appName") == null ? null : configDetailResult.get("appName").asText());
config1Metadata.setDesc(configDetailResult.get("desc") == null ? null : configDetailResult.get("desc").asText());
ConfigMetadata.ConfigExportItem configExportItem1 = configMetadata.getMetadata().get(0);
Assert.assertEquals(configExportItem1, config1Metadata);
}
Aggregations