Skip to content

Commit

Permalink
[type:fix] configs import and export refactor (#5827)
Browse files Browse the repository at this point in the history
Co-authored-by: aias00 <liuhongyu@apache.org>
  • Loading branch information
eye-gu and Aias00 authored Dec 27, 2024
1 parent f5733b3 commit e3e7b61
Show file tree
Hide file tree
Showing 43 changed files with 1,149 additions and 352 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public ResponseEntity<byte[]> exportConfigsByNamespace(final String namespace, f
throw new ShenyuException(result.getMessage());
}
HttpHeaders headers = new HttpHeaders();
String fileName = generateFileName();
String fileName = generateFileName(namespace);
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
headers.add("Content-Disposition", "attachment;filename=" + fileName);
return new ResponseEntity<>((byte[]) result.getData(), headers, HttpStatus.OK);
Expand All @@ -115,6 +115,12 @@ private String generateFileName() {
+ ExportImportConstants.EXPORT_CONFIG_FILE_NAME_EXT;
}

private String generateFileName(final String namespace) {
return ExportImportConstants.EXPORT_CONFIG_FILE_NAME + namespace + "_" + DateFormatUtils.format(new Date(), ExportImportConstants.EXPORT_CONFIG_FILE_NAME_DATE_FORMAT)
+ ExportImportConstants.EXPORT_CONFIG_FILE_NAME_EXT;
}


/**
* Import configs.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@ public interface MetaDataMapper extends ExistProvider {
* the path is existed.
*
* @param path path
* @param namespaceId namespaceId
* @return existed
*/
Boolean pathExisted(@Param("path") Serializable path);
Boolean pathExisted(@Param("path") Serializable path, @Param("namespaceId") String namespaceId);

/**
* the path is existed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.shenyu.admin.model.dto.DiscoveryHandlerDTO;
import org.apache.shenyu.admin.model.result.ConfigImportResult;
import org.apache.shenyu.admin.model.vo.DiscoveryVO;
import org.apache.shenyu.admin.service.configs.ConfigsImportContext;
import org.apache.shenyu.register.common.dto.DiscoveryConfigRegisterDTO;

import java.util.List;
Expand Down Expand Up @@ -131,7 +132,8 @@ public interface DiscoveryService {
*
* @param namespace the namespace
* @param discoveryList the discovery data
* @param context import context
* @return config import result
*/
ConfigImportResult importData(String namespace, List<DiscoveryDTO> discoveryList);
ConfigImportResult importData(String namespace, List<DiscoveryDTO> discoveryList, ConfigsImportContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.shenyu.admin.model.dto.DiscoveryUpstreamDTO;
import org.apache.shenyu.admin.model.result.ConfigImportResult;
import org.apache.shenyu.admin.model.vo.DiscoveryUpstreamVO;
import org.apache.shenyu.admin.service.configs.ConfigsImportContext;
import org.apache.shenyu.common.dto.DiscoverySyncData;
import org.apache.shenyu.common.dto.DiscoveryUpstreamData;

Expand Down Expand Up @@ -130,7 +131,9 @@ public interface DiscoveryUpstreamService {
*
* @param namespace the namespace
* @param discoveryUpstreamList the discoveryUpstream data
* @param context import context
* @return config import result
*/
ConfigImportResult importData(String namespace, List<DiscoveryUpstreamDTO> discoveryUpstreamList);
ConfigImportResult importData(String namespace, List<DiscoveryUpstreamDTO> discoveryUpstreamList,
ConfigsImportContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.shenyu.admin.model.vo.NamespacePluginVO;
import org.apache.shenyu.admin.model.vo.PluginSnapshotVO;
import org.apache.shenyu.admin.model.vo.PluginVO;
import org.apache.shenyu.admin.service.configs.ConfigsImportContext;
import org.apache.shenyu.common.dto.PluginData;

import java.util.List;
Expand Down Expand Up @@ -149,9 +150,10 @@ public interface NamespacePluginService extends PageService<NamespacePluginQuery
*
* @param namespace the namespace
* @param pluginList the plugin data
* @param context the import context
* @return config import result
*/
ConfigImportResult importData(String namespace, List<NamespacePluginDTO> pluginList);
ConfigImportResult importData(String namespace, List<NamespacePluginDTO> pluginList, ConfigsImportContext context);

/**
* List by namespace.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import org.apache.shenyu.admin.model.dto.PluginHandleDTO;
import org.apache.shenyu.admin.model.page.CommonPager;
import org.apache.shenyu.admin.model.query.PluginHandleQuery;
import org.apache.shenyu.admin.model.result.ShenyuAdminResult;
import org.apache.shenyu.admin.model.result.ConfigImportResult;
import org.apache.shenyu.admin.model.vo.PluginHandleVO;
import org.apache.shenyu.admin.service.configs.ConfigsImportContext;

import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -108,7 +109,8 @@ default Integer createOrUpdate(PluginHandleDTO pluginHandleDTO) {
/**
* import plugin handle list.
* @param pluginHandleList the plugin handle list
* @param context the import context
* @return shenyu admin result
*/
ShenyuAdminResult importData(List<PluginHandleDTO> pluginHandleList);
ConfigImportResult importData(List<PluginHandleDTO> pluginHandleList, ConfigsImportContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.shenyu.admin.model.result.ConfigImportResult;
import org.apache.shenyu.admin.model.vo.PluginSnapshotVO;
import org.apache.shenyu.admin.model.vo.PluginVO;
import org.apache.shenyu.admin.service.configs.ConfigsImportContext;
import org.apache.shenyu.common.dto.PluginData;

import java.util.List;
Expand Down Expand Up @@ -130,16 +131,8 @@ public interface PluginService extends PageService<PluginQueryCondition, PluginV
* import plugin data.
*
* @param pluginList the plugin data
* @param context the import context
* @return config import result
*/
ConfigImportResult importData(List<PluginDTO> pluginList);

/**
* import plugin data.
*
* @param namespace the namespace
* @param pluginList the plugin data
* @return config import result
*/
ConfigImportResult importData(String namespace, List<PluginDTO> pluginList);
ConfigImportResult importData(List<PluginDTO> pluginList, ConfigsImportContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.shenyu.admin.model.query.ProxySelectorQuery;
import org.apache.shenyu.admin.model.result.ConfigImportResult;
import org.apache.shenyu.admin.model.vo.ProxySelectorVO;
import org.apache.shenyu.admin.service.configs.ConfigsImportContext;
import org.apache.shenyu.common.dto.ProxySelectorData;

import java.util.List;
Expand Down Expand Up @@ -111,7 +112,8 @@ public interface ProxySelectorService {
*
* @param namespace namespace
* @param proxySelectorList proxy selector data list
* @param context import context
* @return config import result
*/
ConfigImportResult importData(String namespace, List<ProxySelectorData> proxySelectorList);
ConfigImportResult importData(String namespace, List<ProxySelectorData> proxySelectorList, ConfigsImportContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.shenyu.admin.model.query.RuleQueryCondition;
import org.apache.shenyu.admin.model.result.ConfigImportResult;
import org.apache.shenyu.admin.model.vo.RuleVO;
import org.apache.shenyu.admin.service.configs.ConfigsImportContext;
import org.apache.shenyu.common.dto.RuleData;
import org.apache.shenyu.common.enums.OperatorEnum;
import org.apache.shenyu.common.enums.ParamTypeEnum;
Expand Down Expand Up @@ -188,9 +189,10 @@ default int createOrUpdate(final RuleDTO ruleDTO) {
*
* @param namespace namespace
* @param ruleList rule list
* @param context import context
* @return config import result
*/
ConfigImportResult importData(String namespace, List<RuleDTO> ruleList);
ConfigImportResult importData(String namespace, List<RuleDTO> ruleList, ConfigsImportContext context);

/**
* Enabled string by ids and namespaceId.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.shenyu.admin.model.query.SelectorQueryCondition;
import org.apache.shenyu.admin.model.result.ConfigImportResult;
import org.apache.shenyu.admin.model.vo.SelectorVO;
import org.apache.shenyu.admin.service.configs.ConfigsImportContext;
import org.apache.shenyu.admin.utils.Assert;
import org.apache.shenyu.common.dto.SelectorData;
import org.apache.shenyu.common.enums.SelectorTypeEnum;
Expand Down Expand Up @@ -260,9 +261,10 @@ default int createOrUpdate(SelectorDTO selectorDTO) {
*
* @param namespace the namespace
* @param selectorList the plugin selector list
* @param context import context
* @return config import result
*/
ConfigImportResult importData(String namespace, List<SelectorDTO> selectorList);
ConfigImportResult importData(String namespace, List<SelectorDTO> selectorList, ConfigsImportContext context);

/**
* Enabled by ids and namespaceId.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.service.configs;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.admin.model.dto.AppAuthDTO;
import org.apache.shenyu.admin.model.result.ConfigImportResult;
import org.apache.shenyu.admin.model.vo.AppAuthVO;
import org.apache.shenyu.admin.service.AppAuthService;
import org.apache.shenyu.common.constant.ExportImportConstants;
import org.apache.shenyu.common.utils.GsonUtils;
import org.apache.shenyu.common.utils.JsonUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;


@Component
public class AuthConfigsExportImportHandler implements ConfigsExportImportHandler {

private final AppAuthService appAuthService;

public AuthConfigsExportImportHandler(final AppAuthService appAuthService) {
this.appAuthService = appAuthService;
}

@Override
public ConfigsExportImportEnum configsEnum() {
return ConfigsExportImportEnum.Auth;
}

@Override
public Optional<String> configsExport(final String namespaceId) {
List<AppAuthVO> authDataList = appAuthService.listAllDataByNamespace(namespaceId);
if (CollectionUtils.isNotEmpty(authDataList)) {
authDataList.forEach(appAuthVO -> appAuthVO.setNamespaceId(null));
return Optional.of(JsonUtils.toJson(authDataList));
}
return Optional.empty();
}

@Override
public void configsImport(final String namespaceId, final String data, final ConfigsImportContext context) {
List<AppAuthDTO> authDataList = GsonUtils.getInstance().fromList(data, AppAuthDTO.class);
ConfigImportResult configImportResult = appAuthService.importData(namespaceId, authDataList);
context.getResult().put(ExportImportConstants.AUTH_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount());
if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) {
context.getResult().put(ExportImportConstants.AUTH_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.service.configs;

public enum ConfigsExportImportEnum {

/**
* auth.
*/
Auth("auth.json", 0),

/**
* meta.
*/
Meta("meta.json", 1),

/**
* plugin template.
*/
PluginTemplate("plugin_template.json", 2),

/**
* plugin handle.
*/
PluginHandle("plugin_handle.json", 3),

/**
* namespace plugin.
*/
NamespacePlugin("namespace_plugin.json", 4),

/**
* selector.
*/
Selector("selector.json", 5),

/**
* rule.
*/
Rule("rule.json", 6),

/**
* dict.
*/
Dict("dict.json", 7),

/**
* proxy selector.
*/
ProxySelector("proxy_selector.json", 8),

/**
* discovery.
*/
Discovery("discovery.json", 9),

/**
* discovery upstream.
*/
DiscoveryUpstream("discovery_upstream.json", 10);

/**
* zip item name.
*/
private String configName;

/**
* the import order.
*
* @return The smaller, the earlier it will be executed
*/
private int importOrder;

ConfigsExportImportEnum(final String configName, final int importOrder) {
this.configName = configName;
this.importOrder = importOrder;
}

public String getConfigName() {
return configName;
}

public int getImportOrder() {
return importOrder;
}
}
Loading

0 comments on commit e3e7b61

Please sign in to comment.