use of com.alibaba.nacos.config.server.model.ConfigAllInfo in project nacos by alibaba.
the class ConfigControllerTest method testCloneConfig.
@Test
public void testCloneConfig() throws Exception {
SameNamespaceCloneConfigBean sameNamespaceCloneConfigBean = new SameNamespaceCloneConfigBean();
sameNamespaceCloneConfigBean.setCfgId(1L);
sameNamespaceCloneConfigBean.setDataId("test");
sameNamespaceCloneConfigBean.setGroup("test");
List<SameNamespaceCloneConfigBean> configBeansList = new ArrayList<>();
configBeansList.add(sameNamespaceCloneConfigBean);
when(persistService.tenantInfoCountByTenantId("public")).thenReturn(1);
ConfigAllInfo configAllInfo = new ConfigAllInfo();
configAllInfo.setDataId("test");
configAllInfo.setGroup("test");
List<ConfigAllInfo> queryedDataList = new ArrayList<>();
queryedDataList.add(configAllInfo);
List<Long> idList = new ArrayList<>(configBeansList.size());
idList.add(sameNamespaceCloneConfigBean.getCfgId());
when(persistService.findAllConfigInfo4Export(null, null, null, null, idList)).thenReturn(queryedDataList);
Map<String, Object> map = new HashMap<>();
map.put("test", "test");
when(persistService.batchInsertOrUpdate(anyList(), anyString(), anyString(), any(), any(), anyBoolean(), any())).thenReturn(map);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post(Constants.CONFIG_CONTROLLER_PATH).param("clone", "true").param("src_user", "test").param("tenant", "public").param("policy", "ABORT").content(JacksonUtils.toJson(configBeansList)).contentType(MediaType.APPLICATION_JSON);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
String code = JacksonUtils.toObj(actualValue).get("code").toString();
Assert.assertEquals("200", code);
Map<String, Object> resultMap = JacksonUtils.toObj(JacksonUtils.toObj(actualValue).get("data").toString(), Map.class);
Assert.assertEquals(map.get("test"), resultMap.get("test").toString());
}
use of com.alibaba.nacos.config.server.model.ConfigAllInfo 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.ConfigAllInfo in project nacos by alibaba.
the class ConfigController method exportConfig.
/**
* Execute export config operation.
*
* @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 = "export=true")
@Secured(action = ActionTypes.READ, signType = SignType.CONFIG)
public ResponseEntity<byte[]> exportConfig(@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<>();
StringBuilder metaData = null;
for (ConfigInfo ci : dataList) {
if (StringUtils.isNotBlank(ci.getAppName())) {
// Handle appName
if (metaData == null) {
metaData = new StringBuilder();
}
String metaDataId = ci.getDataId();
if (metaDataId.contains(".")) {
metaDataId = metaDataId.substring(0, metaDataId.lastIndexOf(".")) + "~" + metaDataId.substring(metaDataId.lastIndexOf(".") + 1);
}
metaData.append(ci.getGroup()).append('.').append(metaDataId).append(".app=").append(ci.getAppName()).append("\r\n");
}
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()));
}
if (metaData != null) {
zipItemList.add(new ZipUtils.ZipItem(Constants.CONFIG_EXPORT_METADATA, metaData.toString()));
}
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);
}
Aggregations