Skip to content

Commit

Permalink
[type:fix] configs import and export refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
eye-gu committed Dec 10, 2024
1 parent 8f200ec commit 4d11eda
Show file tree
Hide file tree
Showing 33 changed files with 934 additions and 297 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 @@ -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,88 @@
/*
* 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.
*/
Plugin("plugin.json", 2),

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

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

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

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

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

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

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,16 @@
* limitations under the License.
*/

package org.apache.shenyu.admin.service.provider;
package org.apache.shenyu.admin.service.configs;

import org.apache.shenyu.admin.mapper.MetaDataMapper;
import org.apache.shenyu.admin.validation.ExistProvider;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Optional;

/**
* MetaDataPathProvider.
*/
@Component
public class MetaDataPathProvider implements ExistProvider {

private final MetaDataMapper metaDataMapper;

public MetaDataPathProvider(final MetaDataMapper metaDataMapper) {
this.metaDataMapper = metaDataMapper;
}

@Override
public Boolean existed(final Serializable key) {
return metaDataMapper.pathExisted(key);
}
public interface ConfigsExportImportHandler {

ConfigsExportImportEnum configsEnum();

Optional<String> configsExport(String namespaceId);

void configsImport(String namespaceId, String data, ConfigsImportContext context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 com.google.common.collect.Maps;

import java.util.Map;

public class ConfigsImportContext {

/**
* the import result.
*/
private final Map<String, Object> result = Maps.newHashMap();

/**
* old selector id -> new selector id.
*/
private final Map<String, String> selectorIdMapping = Maps.newHashMap();

/**
* old proxy selector id -> new proxy selector id.
*/
private final Map<String, String> proxySelectorIdMapping = Maps.newHashMap();

/**
* old discovery handler id -> new discovery handler id.
*/
private final Map<String, String> discoveryHandlerIdMapping = Maps.newHashMap();

public Map<String, Object> getResult() {
return result;
}

public Map<String, String> getSelectorIdMapping() {
return selectorIdMapping;
}

public Map<String, String> getProxySelectorIdMapping() {
return proxySelectorIdMapping;
}

public Map<String, String> getDiscoveryHandlerIdMapping() {
return discoveryHandlerIdMapping;
}
}
Loading

0 comments on commit 4d11eda

Please sign in to comment.