Search in sources :

Example 1 with ConfigMetadata

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;
}
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 2 with ConfigMetadata

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);
}
Also used : ArrayList(java.util.ArrayList) ConfigMetadata(com.alibaba.nacos.config.server.model.ConfigMetadata) Test(org.junit.Test)

Example 3 with ConfigMetadata

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);
}
Also used : ConfigMetadata(com.alibaba.nacos.config.server.model.ConfigMetadata) Test(org.junit.Test)

Example 4 with ConfigMetadata

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ArrayList(java.util.ArrayList) ZipUtils(com.alibaba.nacos.config.server.utils.ZipUtils) ConfigMetadata(com.alibaba.nacos.config.server.model.ConfigMetadata) Date(java.util.Date) ResponseEntity(org.springframework.http.ResponseEntity) ConfigAllInfo(com.alibaba.nacos.config.server.model.ConfigAllInfo) GetMapping(org.springframework.web.bind.annotation.GetMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 5 with ConfigMetadata

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);
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ZipUtils(com.alibaba.nacos.config.server.utils.ZipUtils) ConfigMetadata(com.alibaba.nacos.config.server.model.ConfigMetadata) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ConfigMetadata (com.alibaba.nacos.config.server.model.ConfigMetadata)5 ZipUtils (com.alibaba.nacos.config.server.utils.ZipUtils)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 Secured (com.alibaba.nacos.auth.annotation.Secured)2 ConfigAllInfo (com.alibaba.nacos.config.server.model.ConfigAllInfo)2 ConfigType (com.alibaba.nacos.api.config.ConfigType)1 NacosException (com.alibaba.nacos.api.exception.NacosException)1 RestResult (com.alibaba.nacos.common.model.RestResult)1 RestResultUtils (com.alibaba.nacos.common.model.RestResultUtils)1 DateFormatUtils (com.alibaba.nacos.common.utils.DateFormatUtils)1 MapUtil (com.alibaba.nacos.common.utils.MapUtil)1 NamespaceUtil (com.alibaba.nacos.common.utils.NamespaceUtil)1 Pair (com.alibaba.nacos.common.utils.Pair)1 StringUtils (com.alibaba.nacos.common.utils.StringUtils)1 Constants (com.alibaba.nacos.config.server.constant.Constants)1 SameNamespaceCloneConfigBean (com.alibaba.nacos.config.server.controller.parameters.SameNamespaceCloneConfigBean)1 ConfigAdvanceInfo (com.alibaba.nacos.config.server.model.ConfigAdvanceInfo)1 ConfigInfo (com.alibaba.nacos.config.server.model.ConfigInfo)1 ConfigInfo4Beta (com.alibaba.nacos.config.server.model.ConfigInfo4Beta)1