moremind commented on code in PR #5474: URL: https://github.com/apache/shenyu/pull/5474#discussion_r1522584003
########## shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ConfigsExportImportController.java: ########## @@ -0,0 +1,423 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shenyu.admin.controller; + +import com.alibaba.nacos.common.utils.DateFormatUtils; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.aspect.annotation.RestApi; +import org.apache.shenyu.admin.model.dto.AppAuthDTO; +import org.apache.shenyu.admin.model.dto.DiscoveryDTO; +import org.apache.shenyu.admin.model.dto.DiscoveryUpstreamDTO; +import org.apache.shenyu.admin.model.dto.MetaDataDTO; +import org.apache.shenyu.admin.model.dto.PluginDTO; +import org.apache.shenyu.admin.model.dto.RuleDTO; +import org.apache.shenyu.admin.model.dto.SelectorDTO; +import org.apache.shenyu.admin.model.dto.ShenyuDictDTO; +import org.apache.shenyu.admin.model.result.ConfigImportResult; +import org.apache.shenyu.admin.model.result.ShenyuAdminResult; +import org.apache.shenyu.admin.model.vo.AppAuthVO; +import org.apache.shenyu.admin.model.vo.DiscoveryUpstreamVO; +import org.apache.shenyu.admin.model.vo.DiscoveryVO; +import org.apache.shenyu.admin.model.vo.MetaDataVO; +import org.apache.shenyu.admin.model.vo.PluginVO; +import org.apache.shenyu.admin.model.vo.RuleVO; +import org.apache.shenyu.admin.model.vo.SelectorVO; +import org.apache.shenyu.admin.model.vo.ShenyuDictVO; +import org.apache.shenyu.admin.service.AppAuthService; +import org.apache.shenyu.admin.service.DiscoveryService; +import org.apache.shenyu.admin.service.DiscoveryUpstreamService; +import org.apache.shenyu.admin.service.MetaDataService; +import org.apache.shenyu.admin.service.PluginService; +import org.apache.shenyu.admin.service.ProxySelectorService; +import org.apache.shenyu.admin.service.RuleService; +import org.apache.shenyu.admin.service.SelectorService; +import org.apache.shenyu.admin.service.ShenyuDictService; +import org.apache.shenyu.admin.utils.ShenyuResultMessage; +import org.apache.shenyu.admin.utils.ZipUtil; +import org.apache.shenyu.common.constant.ExportImportConstants; +import org.apache.shenyu.common.dto.ProxySelectorData; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.common.utils.JsonUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** + * this is configs controller. + */ +@RestApi("/configs") +public class ConfigsExportImportController { + + private static final Logger LOG = LoggerFactory.getLogger(ConfigsExportImportController.class); + + /** + * The AppAuth service. + */ + private final AppAuthService appAuthService; + + /** + * The Plugin service. + */ + private final PluginService pluginService; + + /** + * The Selector service. + */ + private final SelectorService selectorService; + + /** + * The Rule service. + */ + private final RuleService ruleService; + + /** + * The Metadata service. + */ + private final MetaDataService metaDataService; + + /** + * The Dict service. + */ + private final ShenyuDictService shenyuDictService; + + /** + * The ProxySelector service. + */ + private final ProxySelectorService proxySelectorService; + + /** + * The discovery service. + */ + private final DiscoveryService discoveryService; + + /** + * The discovery upstream service. + */ + private final DiscoveryUpstreamService discoveryUpstreamService; + + public ConfigsExportImportController(final AppAuthService appAuthService, + final PluginService pluginService, + final SelectorService selectorService, + final RuleService ruleService, + final MetaDataService metaDataService, + final ShenyuDictService shenyuDictService, + final ProxySelectorService proxySelectorService, + final DiscoveryService discoveryService, + final DiscoveryUpstreamService discoveryUpstreamService) { + this.appAuthService = appAuthService; + this.pluginService = pluginService; + this.selectorService = selectorService; + this.ruleService = ruleService; + this.metaDataService = metaDataService; + this.shenyuDictService = shenyuDictService; + this.proxySelectorService = proxySelectorService; + this.discoveryService = discoveryService; + this.discoveryUpstreamService = discoveryUpstreamService; + } + + /** + * Export all configs. + * + * @param response response + * @return the shenyu result + */ + @GetMapping("/export") + public ResponseEntity<byte[]> exportConfigs(final HttpServletResponse response) { + List<ZipUtil.ZipItem> zipItemList = Lists.newArrayList(); + exportAuthData(zipItemList); + + exportMetadata(zipItemList); + + exportPluginData(zipItemList); + + exportSelectorData(zipItemList); + + exportRuleData(zipItemList); + + exportDictData(zipItemList); + + exportProxySelectorData(zipItemList); + + exportDiscoveryData(zipItemList); + + exportDiscoveryUpstreamData(zipItemList); + + HttpHeaders headers = new HttpHeaders(); + String fileName = generateFileName(); + response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); + headers.add("Content-Disposition", "attachment;filename=" + fileName); + return new ResponseEntity<>(ZipUtil.zip(zipItemList), headers, HttpStatus.OK); + } + + private void exportDiscoveryUpstreamData(final List<ZipUtil.ZipItem> zipItemList) { + List<DiscoveryUpstreamVO> discoveryUpstreamList = discoveryUpstreamService.listAllVO(); + if (CollectionUtils.isNotEmpty(discoveryUpstreamList)) { + zipItemList.add(new ZipUtil.ZipItem(ExportImportConstants.DISCOVERY_UPSTREAM_JSON, JsonUtils.toJson(discoveryUpstreamList))); + } + } + Review Comment: put these methods to service(implement) ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java: ########## @@ -249,6 +259,73 @@ public ShenyuAdminResult syncData() { return ShenyuAdminResult.success(); } + @Override + @Transactional(rollbackFor = Exception.class) + public ConfigImportResult importData(final List<AppAuthDTO> authDataList) { + if (CollectionUtils.isEmpty(authDataList)) { + return ConfigImportResult.success(); + } + try { + StringBuilder errorMsgBuilder = new StringBuilder(); Review Comment: transaction is missing? ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/ProxySelectorDO.java: ########## @@ -94,6 +96,39 @@ public static ProxySelectorDO buildProxySelectorDO(final ProxySelectorAddDTO pro }).orElse(null); } + /** + * buildProxySelectorDO. + * + * @param proxySelectorData proxySelectorData + * @return ProxySelectorDO + */ + public static ProxySelectorDO buildProxySelectorDO(final ProxySelectorData proxySelectorData) { + + return Optional.ofNullable(proxySelectorData).map(item -> { + Timestamp currentTime = new Timestamp(System.currentTimeMillis()); + ProxySelectorDO proxySelectorDO = ProxySelectorDO.builder() + .name(item.getName()) + .pluginName(PluginEnum.TCP.getName()) + .forwardPort(item.getForwardPort()) + .type(item.getType()) + .props(JsonUtils.toJson(item.getProps())) + .dateUpdated(currentTime).build(); + if (StringUtils.hasLength(item.getId())) { + proxySelectorDO.setId(item.getId()); + } else { + proxySelectorDO.setId(UUIDUtils.getInstance().generateShortUuid()); + proxySelectorDO.setDateCreated(currentTime); + } + if (null == proxySelectorDO.getDateCreated()) { Review Comment: use Objects.isNull ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryServiceImpl.java: ########## @@ -309,4 +345,61 @@ public String registerDefaultDiscovery(final String selectorId, final String plu discoveryRelMapper.insertSelective(discoveryRelDO); return discoveryHandlerId; } + + @Override + @Transactional(rollbackFor = Exception.class) + public ConfigImportResult importData(final List<DiscoveryDTO> discoveryList) { + if (CollectionUtils.isEmpty(discoveryList)) { + return ConfigImportResult.success(); + } + + Map<String, List<DiscoveryDO>> pluginDiscoveryMap = discoveryMapper + .selectAll() + .stream() + .collect(Collectors.groupingBy(DiscoveryDO::getPluginName)); + int successCount = 0; + StringBuilder errorMsgBuilder = new StringBuilder(); + for (DiscoveryDTO discoveryDTO : discoveryList) { + String pluginName = discoveryDTO.getPluginName(); + String discoveryName = discoveryDTO.getName(); + Set<String> existDiscoveryNameSet = pluginDiscoveryMap + .getOrDefault(pluginName, Lists.newArrayList()) + .stream() + .map(DiscoveryDO::getName) + .collect(Collectors.toSet()); + if (existDiscoveryNameSet.contains(discoveryName)) { + errorMsgBuilder + .append(discoveryName) + .append(","); + continue; + } + String discoveryId = UUIDUtils.getInstance().generateShortUuid(); + discoveryDTO.setId(discoveryId); + create(discoveryDTO); + successCount++; + + // import discovery handler data + if (null != discoveryDTO.getDiscoveryHandler()) { Review Comment: use Objects.nonNull ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryUpstreamServiceImpl.java: ########## @@ -213,6 +228,45 @@ public void deleteBySelectorIdAndUrl(final String selectorId, final String url) } } + @Override + @Transactional(rollbackFor = Exception.class) + public ConfigImportResult importData(final List<DiscoveryUpstreamDTO> discoveryUpstreamList) { + if (CollectionUtils.isEmpty(discoveryUpstreamList)) { + return ConfigImportResult.success(); + } + int successCount = 0; + StringBuilder errorMsgBuilder = new StringBuilder(); + Map<String, List<DiscoveryUpstreamDO>> discoveryHandlerUpstreamMap = discoveryUpstreamMapper + .selectAll() + .stream() + .collect(Collectors.groupingBy(DiscoveryUpstreamDO::getDiscoveryHandlerId)); + for (DiscoveryUpstreamDTO discoveryUpstreamDTO : discoveryUpstreamList) { + String discoveryHandlerId = discoveryUpstreamDTO.getDiscoveryHandlerId(); + String url = discoveryUpstreamDTO.getUrl(); + Set<String> existsUpstreamUrlSet = discoveryHandlerUpstreamMap + .getOrDefault(discoveryHandlerId, Lists.newArrayList()) + .stream() + .map(DiscoveryUpstreamDO::getUrl) + .collect(Collectors.toSet()); + if (existsUpstreamUrlSet.contains(url)) { + errorMsgBuilder + .append(url) + .append(","); + continue; + } + discoveryUpstreamDTO.setId(null); + DiscoveryUpstreamDO discoveryUpstreamDO = DiscoveryUpstreamDO.buildDiscoveryUpstreamDO(discoveryUpstreamDTO); + discoveryUpstreamMapper.insert(discoveryUpstreamDO); + successCount++; + } + if (org.apache.commons.lang3.StringUtils.isNotEmpty(errorMsgBuilder)) { Review Comment: replace the methods, donot import package here. ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java: ########## @@ -209,15 +244,70 @@ public PluginDO findByName(final String name) { } /** - * activate plugin snapshot. + * activate plugin snapshot. * * @return List of plugins snapshot */ @Override public List<PluginSnapshotVO> activePluginSnapshot() { return pluginMapper.activePluginSnapshot(SessionUtil.isAdmin() ? null : SessionUtil.visitor().getUserId()); } - + + @Override + @Transactional(rollbackFor = Exception.class) + public ConfigImportResult importData(final List<PluginDTO> pluginList) { + if (CollectionUtils.isEmpty(pluginList)) { + return ConfigImportResult.success(); + } + Map<String, PluginDO> existPluginMap = pluginMapper.selectAll() + .stream() + .filter(Objects::nonNull) + .collect(Collectors.toMap(PluginDO::getName, x -> x)); + try { Review Comment: same try ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SelectorServiceImpl.java: ########## @@ -411,6 +418,67 @@ public List<SelectorData> listAll() { return this.buildSelectorDataList(selectorMapper.selectAll()); } + @Override + public List<SelectorVO> listAllVO() { + return this.buildSelectorExportVOList(selectorMapper.selectAll()); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public ConfigImportResult importData(final List<SelectorDTO> selectorList) { + if (CollectionUtils.isEmpty(selectorList)) { + return ConfigImportResult.success(); + } + try { + StringBuilder errorMsgBuilder = new StringBuilder(); + int successCount = 0; + Map<String, List<SelectorDO>> pluginSelectorMap = selectorMapper.selectAll().stream() + .filter(Objects::nonNull) + .collect(Collectors.groupingBy(SelectorDO::getPluginId)); + + Map<String, List<SelectorDTO>> importSelectorMap = selectorList.stream() + .collect(Collectors.groupingBy(SelectorDTO::getPluginId)); + + for (Map.Entry<String, List<SelectorDTO>> selectorEntry : importSelectorMap.entrySet()) { + // the import selector's pluginId + String pluginId = selectorEntry.getKey(); + List<SelectorDTO> selectorDTOList = selectorEntry.getValue(); + if (CollectionUtils.isNotEmpty(selectorDTOList)) { + + Set<String> existSelectorSet = Optional + .ofNullable(pluginSelectorMap.get(pluginId)) + .orElse(Lists.newArrayList()) Review Comment: orElseGet ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java: ########## @@ -506,4 +604,51 @@ private Map<String, List<AuthPathData>> prepareAuthPathData(final List<String> a })); } + /** + * prepare the Map with authIds. + * + * @param authIds auth id + * @return a map consist of param vo info + */ + private Map<String, List<AuthParamVO>> prepareAuthParamVO(final List<String> authIds) { + + List<AuthParamDO> authPathDOList = authParamMapper.findByAuthIdList(authIds); + + return Optional.ofNullable(authPathDOList).orElseGet(ArrayList::new) + .stream().collect(Collectors.toMap(AuthParamDO::getAuthId, + data -> { + List<AuthParamVO> dataList = new ArrayList<>(); + AuthParamVO authParamVO = new AuthParamVO(); + BeanUtils.copyProperties(data, authParamVO); Review Comment: not use copy properties, add map method to solve it. ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ProxySelectorServiceImpl.java: ########## @@ -410,4 +413,78 @@ public List<ProxySelectorData> listAll() { return proxySelectorMapper.selectAll().stream() .map(DiscoveryTransfer.INSTANCE::mapToData).collect(Collectors.toList()); } + + @Override + public List<ProxySelectorVO> listAllVO() { + List<ProxySelectorVO> result = Lists.newArrayList(); + proxySelectorMapper.selectAll().forEach(proxySelectorDO -> { + ProxySelectorVO vo = new ProxySelectorVO(); + vo.setId(proxySelectorDO.getId()); + vo.setName(proxySelectorDO.getName()); + vo.setType(proxySelectorDO.getType()); + vo.setForwardPort(proxySelectorDO.getForwardPort()); + vo.setCreateTime(proxySelectorDO.getDateCreated()); + vo.setUpdateTime(proxySelectorDO.getDateUpdated()); + vo.setProps(proxySelectorDO.getProps()); + DiscoveryRelDO discoveryRelDO = discoveryRelMapper.selectByProxySelectorId(proxySelectorDO.getId()); + if (!Objects.isNull(discoveryRelDO)) { Review Comment: just use Objects.nonNull ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ProxySelectorServiceImpl.java: ########## @@ -410,4 +413,78 @@ public List<ProxySelectorData> listAll() { return proxySelectorMapper.selectAll().stream() .map(DiscoveryTransfer.INSTANCE::mapToData).collect(Collectors.toList()); } + + @Override + public List<ProxySelectorVO> listAllVO() { + List<ProxySelectorVO> result = Lists.newArrayList(); + proxySelectorMapper.selectAll().forEach(proxySelectorDO -> { + ProxySelectorVO vo = new ProxySelectorVO(); + vo.setId(proxySelectorDO.getId()); + vo.setName(proxySelectorDO.getName()); + vo.setType(proxySelectorDO.getType()); + vo.setForwardPort(proxySelectorDO.getForwardPort()); + vo.setCreateTime(proxySelectorDO.getDateCreated()); + vo.setUpdateTime(proxySelectorDO.getDateUpdated()); + vo.setProps(proxySelectorDO.getProps()); + DiscoveryRelDO discoveryRelDO = discoveryRelMapper.selectByProxySelectorId(proxySelectorDO.getId()); + if (!Objects.isNull(discoveryRelDO)) { + DiscoveryHandlerDO discoveryHandlerDO = discoveryHandlerMapper.selectById(discoveryRelDO.getDiscoveryHandlerId()); + if (!Objects.isNull(discoveryHandlerDO)) { Review Comment: just use Objects.nonNull ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryServiceImpl.java: ########## @@ -309,4 +345,61 @@ public String registerDefaultDiscovery(final String selectorId, final String plu discoveryRelMapper.insertSelective(discoveryRelDO); return discoveryHandlerId; } + + @Override + @Transactional(rollbackFor = Exception.class) + public ConfigImportResult importData(final List<DiscoveryDTO> discoveryList) { + if (CollectionUtils.isEmpty(discoveryList)) { + return ConfigImportResult.success(); + } + + Map<String, List<DiscoveryDO>> pluginDiscoveryMap = discoveryMapper + .selectAll() + .stream() + .collect(Collectors.groupingBy(DiscoveryDO::getPluginName)); + int successCount = 0; + StringBuilder errorMsgBuilder = new StringBuilder(); + for (DiscoveryDTO discoveryDTO : discoveryList) { + String pluginName = discoveryDTO.getPluginName(); + String discoveryName = discoveryDTO.getName(); + Set<String> existDiscoveryNameSet = pluginDiscoveryMap + .getOrDefault(pluginName, Lists.newArrayList()) + .stream() + .map(DiscoveryDO::getName) + .collect(Collectors.toSet()); + if (existDiscoveryNameSet.contains(discoveryName)) { + errorMsgBuilder + .append(discoveryName) + .append(","); + continue; + } + String discoveryId = UUIDUtils.getInstance().generateShortUuid(); + discoveryDTO.setId(discoveryId); + create(discoveryDTO); + successCount++; + + // import discovery handler data + if (null != discoveryDTO.getDiscoveryHandler()) { + DiscoveryHandlerDO discoveryHandlerDO = DiscoveryTransfer + .INSTANCE + .mapToDO(discoveryDTO.getDiscoveryHandler()); + discoveryHandlerDO.setDiscoveryId(discoveryId); + discoveryHandlerMapper.insertSelective(discoveryHandlerDO); + } + + // import discovery rel data + if (null != discoveryDTO.getDiscoveryRel()) { + DiscoveryRelDO discoveryRelDO = DiscoveryTransfer + .INSTANCE + .mapToDO(discoveryDTO.getDiscoveryRel()); + discoveryRelMapper.insertSelective(discoveryRelDO); + } + } + if (StringUtils.isNotEmpty(errorMsgBuilder)) { + errorMsgBuilder.setLength(errorMsgBuilder.length() - 1); + return ConfigImportResult + .fail(successCount, "import fail discovery: " + errorMsgBuilder); Review Comment: when success, you should sync data to gateway ########## shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java: ########## @@ -506,4 +604,51 @@ private Map<String, List<AuthPathData>> prepareAuthPathData(final List<String> a })); } + /** + * prepare the Map with authIds. + * + * @param authIds auth id + * @return a map consist of param vo info + */ + private Map<String, List<AuthParamVO>> prepareAuthParamVO(final List<String> authIds) { + + List<AuthParamDO> authPathDOList = authParamMapper.findByAuthIdList(authIds); + + return Optional.ofNullable(authPathDOList).orElseGet(ArrayList::new) + .stream().collect(Collectors.toMap(AuthParamDO::getAuthId, + data -> { + List<AuthParamVO> dataList = new ArrayList<>(); + AuthParamVO authParamVO = new AuthParamVO(); + BeanUtils.copyProperties(data, authParamVO); + dataList.add(authParamVO); + return dataList; + }, (List<AuthParamVO> dataList1, List<AuthParamVO> dataList2) -> { + dataList1.addAll(dataList2); + return dataList1; + })); + } + + /** + * prepare the Map with authIds. + * + * @param authIds auth id + * @return a map consist of path vo info + */ + private Map<String, List<AuthPathVO>> prepareAuthPathVO(final List<String> authIds) { + + List<AuthPathDO> authPathDOList = authPathMapper.findByAuthIdList(authIds); + return Optional.ofNullable(authPathDOList).orElseGet(ArrayList::new) + .stream().collect(Collectors.toMap(AuthPathDO::getAuthId, + data -> { + List<AuthPathVO> dataList = new ArrayList<>(); + AuthPathVO authPathVO = new AuthPathVO(); + BeanUtils.copyProperties(data, authPathVO); Review Comment: same copy -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
