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;
}
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;
}
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);
}
}
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;
}
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();
}
Aggregations