Search in sources :

Example 6 with RestResult

use of com.alibaba.nacos.common.model.RestResult 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 7 with RestResult

use of com.alibaba.nacos.common.model.RestResult in project nacos by alibaba.

the class ConfigOpsController method importDerby.

/**
 * // TODO the front page should appear operable The external data source is imported into derby.
 *
 * <p>mysqldump --defaults-file="XXX" --host=0.0.0.0 --protocol=tcp --user=XXX --extended-insert=FALSE \
 * --complete-insert=TRUE \ --skip-triggers --no-create-info --skip-column-statistics "{SCHEMA}" "{TABLE_NAME}"
 *
 * @param multipartFile {@link MultipartFile}
 * @return {@link DeferredResult}
 */
@PostMapping(value = "/data/removal")
@Secured(action = ActionTypes.WRITE, resource = "nacos/admin")
public DeferredResult<RestResult<String>> importDerby(@RequestParam(value = "file") MultipartFile multipartFile) {
    DeferredResult<RestResult<String>> response = new DeferredResult<>();
    if (!PropertyUtil.isEmbeddedStorage()) {
        response.setResult(RestResultUtils.failed("Limited to embedded storage mode"));
        return response;
    }
    DatabaseOperate databaseOperate = ApplicationUtils.getBean(DatabaseOperate.class);
    WebUtils.onFileUpload(multipartFile, file -> {
        NotifyCenter.publishEvent(new DerbyImportEvent(false));
        databaseOperate.dataImport(file).whenComplete((result, ex) -> {
            NotifyCenter.publishEvent(new DerbyImportEvent(true));
            if (Objects.nonNull(ex)) {
                response.setResult(RestResultUtils.failed(ex.getMessage()));
                return;
            }
            response.setResult(result);
        });
    }, response);
    return response;
}
Also used : RestResult(com.alibaba.nacos.common.model.RestResult) DerbyImportEvent(com.alibaba.nacos.config.server.model.event.DerbyImportEvent) DatabaseOperate(com.alibaba.nacos.config.server.service.repository.embedded.DatabaseOperate) DeferredResult(org.springframework.web.context.request.async.DeferredResult) PostMapping(org.springframework.web.bind.annotation.PostMapping) Secured(com.alibaba.nacos.auth.annotation.Secured)

Example 8 with RestResult

use of com.alibaba.nacos.common.model.RestResult in project nacos by alibaba.

the class ConfigDerbyImport_CITCase method testDerbyImport.

@Test()
public void testDerbyImport() throws Throwable {
    DatabaseOperate operate = context.getBean(DatabaseOperate.class);
    File file = DiskUtils.createTmpFile("derby_import" + System.currentTimeMillis(), ".tmp");
    DiskUtils.writeFile(file, ByteUtils.toBytes(SQL_SCRIPT_CONTEXT), false);
    try {
        List<Integer> ids = operate.queryMany("SELECT id FROM config_info", new Object[] {}, Integer.class);
        for (Integer each : ids) {
            System.out.println("current id in table config_info contain: " + each);
        }
        CompletableFuture<RestResult<String>> future = operate.dataImport(file);
        RestResult<String> result = future.join();
        System.out.println(result);
        Assert.assertTrue(result.ok());
        final String queryDataId = "people";
        final String queryGroup = "DEFAULT_GROUP";
        final String expectContent = "people.enable=true";
        PersistService persistService = context.getBean(PersistService.class);
        ConfigInfo configInfo = persistService.findConfigInfo(queryDataId, queryGroup, "");
        System.out.println(configInfo);
        Assert.assertNotNull(configInfo);
        Assert.assertEquals(queryDataId, configInfo.getDataId());
        Assert.assertEquals(queryGroup, configInfo.getGroup());
        Assert.assertEquals("", configInfo.getTenant());
        Assert.assertEquals(expectContent, configInfo.getContent());
    } finally {
        DiskUtils.deleteQuietly(file);
    }
}
Also used : RestResult(com.alibaba.nacos.common.model.RestResult) PersistService(com.alibaba.nacos.config.server.service.repository.PersistService) ConfigInfo(com.alibaba.nacos.config.server.model.ConfigInfo) DatabaseOperate(com.alibaba.nacos.config.server.service.repository.embedded.DatabaseOperate) File(java.io.File) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 9 with RestResult

use of com.alibaba.nacos.common.model.RestResult in project nacos by alibaba.

the class CapacityController method getCapacity.

@GetMapping
public RestResult<Capacity> getCapacity(HttpServletResponse response, @RequestParam(required = false) String group, @RequestParam(required = false) String tenant) {
    if (group == null && tenant == null) {
        RestResult<Capacity> restResult = new RestResult<>();
        response.setStatus(STATUS400);
        restResult.setCode(STATUS400);
        restResult.setMessage("The parameter group and tenant cannot be empty at the same time");
        return restResult;
    }
    if (group == null && StringUtils.isBlank(tenant)) {
        RestResult<Capacity> restResult = new RestResult<>();
        response.setStatus(STATUS400);
        restResult.setCode(STATUS400);
        restResult.setMessage("tenant cannot be an empty string");
        return restResult;
    }
    RestResult<Capacity> restResult = new RestResult<>();
    try {
        response.setStatus(STATUS200);
        restResult.setCode(STATUS200);
        Capacity capacity = capacityService.getCapacityWithDefault(group, tenant);
        if (capacity == null) {
            LOGGER.warn("[getCapacity] capacity not exist,need init group: {}, tenant: {}", group, tenant);
            capacityService.initCapacity(group, tenant);
            capacity = capacityService.getCapacityWithDefault(group, tenant);
        }
        if (capacity != null) {
            restResult.setData(capacity);
        }
    } catch (Exception e) {
        LOGGER.error("[getCapacity] ", e);
        response.setStatus(STATUS500);
        restResult.setCode(STATUS500);
        restResult.setMessage(e.getMessage());
    }
    return restResult;
}
Also used : RestResult(com.alibaba.nacos.common.model.RestResult) Capacity(com.alibaba.nacos.config.server.model.capacity.Capacity) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 10 with RestResult

use of com.alibaba.nacos.common.model.RestResult in project nacos by alibaba.

the class JRaftServer method peerChange.

boolean peerChange(JRaftMaintainService maintainService, Set<String> newPeers) {
    // This is only dealing with node deletion, the Raft protocol, where the node adds itself to the cluster when it starts up
    Set<String> oldPeers = new HashSet<>(this.raftConfig.getMembers());
    this.raftConfig.setMembers(localPeerId.toString(), newPeers);
    oldPeers.removeAll(newPeers);
    if (oldPeers.isEmpty()) {
        return true;
    }
    Set<String> waitRemove = oldPeers;
    AtomicInteger successCnt = new AtomicInteger(0);
    multiRaftGroup.forEach(new BiConsumer<String, RaftGroupTuple>() {

        @Override
        public void accept(String group, RaftGroupTuple tuple) {
            Map<String, String> params = new HashMap<>();
            params.put(JRaftConstants.GROUP_ID, group);
            params.put(JRaftConstants.COMMAND_NAME, JRaftConstants.REMOVE_PEERS);
            params.put(JRaftConstants.COMMAND_VALUE, StringUtils.join(waitRemove, StringUtils.COMMA));
            RestResult<String> result = maintainService.execute(params);
            if (result.ok()) {
                successCnt.incrementAndGet();
            } else {
                Loggers.RAFT.error("Node removal failed : {}", result);
            }
        }
    });
    return successCnt.get() == multiRaftGroup.size();
}
Also used : RestResult(com.alibaba.nacos.common.model.RestResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Aggregations

RestResult (com.alibaba.nacos.common.model.RestResult)10 PostMapping (org.springframework.web.bind.annotation.PostMapping)5 Map (java.util.Map)4 Secured (com.alibaba.nacos.auth.annotation.Secured)3 NacosAsyncRestTemplate (com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate)3 RestResultUtils (com.alibaba.nacos.common.model.RestResultUtils)3 StringUtils (com.alibaba.nacos.common.utils.StringUtils)3 ConfigInfo (com.alibaba.nacos.config.server.model.ConfigInfo)3 PersistService (com.alibaba.nacos.config.server.service.repository.PersistService)3 Member (com.alibaba.nacos.core.cluster.Member)3 GenericType (com.alibaba.nacos.core.utils.GenericType)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Collectors (java.util.stream.Collectors)3 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)3 ConfigType (com.alibaba.nacos.api.config.ConfigType)2 NacosException (com.alibaba.nacos.api.exception.NacosException)2 DateFormatUtils (com.alibaba.nacos.common.utils.DateFormatUtils)2 MapUtil (com.alibaba.nacos.common.utils.MapUtil)2