Search in sources :

Example 11 with ConfigAllInfo

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());
}
Also used : SameNamespaceCloneConfigBean(com.alibaba.nacos.config.server.controller.parameters.SameNamespaceCloneConfigBean) ConfigAllInfo(com.alibaba.nacos.config.server.model.ConfigAllInfo) HashMap(java.util.HashMap) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 12 with ConfigAllInfo

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);
}
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 13 with ConfigAllInfo

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ArrayList(java.util.ArrayList) ZipUtils(com.alibaba.nacos.config.server.utils.ZipUtils) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) 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)

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