From ce123f1f4eafc3cac996e7cfee0b36e3d7a0ce30 Mon Sep 17 00:00:00 2001 From: xcsnx <1192709687@qq.com> Date: Sat, 17 Aug 2024 22:36:28 +0800 Subject: [PATCH] [type:feat]Selector module adapts to namespace transformation (#5604) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * commit * commit * first draft,not verified * simplify verified * simplify verified * commit * commit * fix * fix e2e checkstyle * fix e2e * fix test * fix * fix * fix test * fix * fix * trigger ci * trigger ci * fix sql * fix sql * fix sql * fix * trigger ci * trigger ci * fix * trigger ci * fix * fix * fix * fix * trigger ci * trigger ci * fix * trigger ci * trigger ci * trigger ci * fix cr * fix cr * fix comment * fix comment * fix * fix * trigger ci * trigger ci * fix cr --------- Co-authored-by: ‘xcsnx’ <‘1192709687@qq.com’> --- db/init/mysql/schema.sql | 2 + db/init/og/create-table.sql | 4 + db/init/oracle/schema.sql | 4 + db/init/pg/create-table.sql | 2 + db/upgrade/2.6.1-upgrade-2.7.0-mysql.sql | 7 ++ db/upgrade/2.6.1-upgrade-2.7.0-og.sql | 5 + db/upgrade/2.6.1-upgrade-2.7.0-oracle.sql | 5 + db/upgrade/2.6.1-upgrade-2.7.0-pg.sql | 5 + .../admin/controller/SelectorController.java | 39 ++++---- .../mapper/NamespacePluginRelMapper.java | 33 +++---- .../shenyu/admin/mapper/SelectorMapper.java | 41 +++++--- .../model/dto/BatchNamespaceCommonDTO.java | 4 + .../shenyu/admin/model/dto/SelectorDTO.java | 73 ++++++++++---- .../shenyu/admin/model/entity/PluginDO.java | 32 ++++++ .../shenyu/admin/model/entity/SelectorDO.java | 69 ++++++++++--- .../BatchNamespacePluginDeletedEvent.java | 90 +++++++++++++++++ .../event/plugin/BatchPluginDeletedEvent.java | 7 +- .../event/selector/SelectorChangedEvent.java | 7 +- .../model/query/SelectorConditionQuery.java | 29 +++++- .../admin/model/query/SelectorQuery.java | 31 +++++- .../model/query/SelectorQueryCondition.java | 24 +++++ .../shenyu/admin/model/vo/NamespaceVO.java | 2 +- .../shenyu/admin/model/vo/SelectorVO.java | 34 ++++++- .../shenyu/admin/service/SelectorService.java | 81 +++++++++------- .../admin/service/impl/ApiServiceImpl.java | 4 +- .../service/impl/DiscoveryServiceImpl.java | 4 +- .../impl/NamespacePluginServiceImpl.java | 18 ++-- .../service/impl/NamespaceServiceImpl.java | 2 +- .../admin/service/impl/PluginServiceImpl.java | 13 ++- .../service/impl/ResourceServiceImpl.java | 9 +- .../service/impl/SelectorServiceImpl.java | 71 ++++++++------ .../service/impl/SyncDataServiceImpl.java | 6 +- .../service/impl/UpstreamCheckService.java | 5 +- .../NamespacePluginEventPublisher.java | 21 +++- .../service/publish/PluginEventPublisher.java | 4 +- ...stractShenyuClientRegisterServiceImpl.java | 8 +- ...ShenyuClientRegisterDivideServiceImpl.java | 7 +- .../ShenyuClientRegisterGrpcServiceImpl.java | 5 +- ...nyuClientRegisterWebSocketServiceImpl.java | 5 +- .../mappers/NamespacePluginRelMapper.xml | 11 +-- .../resources/mappers/selector-sqlmap.xml | 53 +++++++--- .../main/resources/sql-script/h2/schema.sql | 2 + .../controller/SelectorControllerTest.java | 78 ++++++++++----- .../admin/mapper/SelectorMapperTest.java | 12 ++- ...BatchNamespacePluginDeletedEventTest.java} | 8 +- .../selector/SelectorChangedEventTest.java | 8 +- .../service/DataPermissionServiceTest.java | 2 +- .../admin/service/PluginServiceTest.java | 9 +- .../shenyu/admin/service/RuleServiceTest.java | 7 +- .../admin/service/SelectorServiceTest.java | 15 +-- .../admin/service/SyncDataServiceTest.java | 8 +- .../service/UpstreamCheckServiceTest.java | 3 +- .../impl/LoadServiceDocEntryImplTest.java | 4 +- ...ctShenyuClientRegisterServiceImplTest.java | 4 +- .../common/constant/AdminConstants.java | 13 ++- .../shenyu/common/dto/SelectorData.java | 71 +++++++++++--- .../testcase/divide/DividePluginCases.java | 9 ++ .../shenyu/e2e/client/admin/AdminClient.java | 24 +++-- .../e2e/model/data/SelectorCacheData.java | 97 +++++++++++++------ .../shenyu/e2e/model/data/SelectorData.java | 34 +++++++ .../model/data/SelectorQueryCondition.java | 36 +++++++ .../common/dto/MetaDataRegisterDTO.java | 47 ++++++++- 62 files changed, 1040 insertions(+), 327 deletions(-) create mode 100644 shenyu-admin/src/main/java/org/apache/shenyu/admin/model/event/plugin/BatchNamespacePluginDeletedEvent.java rename shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/plugin/{BatchPluginDeletedEventTest.java => BatchNamespacePluginDeletedEventTest.java} (88%) diff --git a/db/init/mysql/schema.sql b/db/init/mysql/schema.sql index 5eab7a24387b..f7f0062218eb 100644 --- a/db/init/mysql/schema.sql +++ b/db/init/mysql/schema.sql @@ -2370,3 +2370,5 @@ INSERT INTO `shenyu`.`permission` (`id`, `object_id`, `resource_id`, `date_creat INSERT INTO `shenyu`.`permission` (`id`, `object_id`, `resource_id`, `date_created`, `date_updated`) VALUES ('1792779493541343265', '1346358560427216896', '1792749362445840484', '2024-06-25 20:00:00.000', '2024-06-25 20:00:00.000'); INSERT INTO `shenyu`.`permission` (`id`, `object_id`, `resource_id`, `date_created`, `date_updated`) VALUES ('1792779493541343266', '1346358560427216896', '1792749362445840485', '2024-06-25 20:00:00.000', '2024-06-25 20:00:00.000'); INSERT INTO `shenyu`.`permission` (`id`, `object_id`, `resource_id`, `date_created`, `date_updated`) VALUES ('1792779493541343267', '1346358560427216896', '1792749362445840486', '2024-06-25 20:00:00.000', '2024-06-25 20:00:00.000'); + +ALTER TABLE `shenyu`.`selector` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId' AFTER `match_restful`; \ No newline at end of file diff --git a/db/init/og/create-table.sql b/db/init/og/create-table.sql index e9227ce8cac6..ebc4a2cbc30b 100644 --- a/db/init/og/create-table.sql +++ b/db/init/og/create-table.sql @@ -2673,3 +2673,7 @@ INSERT INTO "public"."permission" VALUES ('1792779493541343265', '13463585604272 INSERT INTO "public"."permission" VALUES ('1792779493541343266', '1346358560427216896', '1792749362445840485', '2024-06-25 20:00:00.000', '2024-06-25 20:00:00.000'); INSERT INTO "public"."permission" VALUES ('1792779493541343267', '1346358560427216896', '1792749362445840486', '2024-06-25 20:00:00.000', '2024-06-25 20:00:00.000'); + +ALTER TABLE "public"."selector" ADD COLUMN namespace_id VARCHAR(50) NULL; +COMMENT ON COLUMN "public"."selector"."namespace_id" IS 'namespaceId'; + diff --git a/db/init/oracle/schema.sql b/db/init/oracle/schema.sql index 8e9bb14b5f47..1197f14fb219 100644 --- a/db/init/oracle/schema.sql +++ b/db/init/oracle/schema.sql @@ -2857,3 +2857,7 @@ INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (permission(id)) */ INTO permission (id, o INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (permission(id)) */ INTO permission (id, object_id, resource_id) VALUES ('1792779493541343265', '1346358560427216896', '1792749362445840484'); INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (permission(id)) */ INTO permission (id, object_id, resource_id) VALUES ('1792779493541343266', '1346358560427216896', '1792749362445840485'); INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (permission(id)) */ INTO permission (id, object_id, resource_id) VALUES ('1792779493541343267', '1346358560427216896', '1792749362445840486'); + + +ALTER TABLE selector ADD namespace_id VARCHAR2(50) NULL; +COMMENT ON COLUMN selector.namespace_id IS 'namespaceId'; \ No newline at end of file diff --git a/db/init/pg/create-table.sql b/db/init/pg/create-table.sql index 6e12aa6ed6bf..dec5fcbfb536 100644 --- a/db/init/pg/create-table.sql +++ b/db/init/pg/create-table.sql @@ -2791,3 +2791,5 @@ INSERT INTO "public"."permission" VALUES ('1792779493541343265', '13463585604272 INSERT INTO "public"."permission" VALUES ('1792779493541343266', '1346358560427216896', '1792749362445840485', '2024-06-25 20:00:00.000', '2024-06-25 20:00:00.000'); INSERT INTO "public"."permission" VALUES ('1792779493541343267', '1346358560427216896', '1792749362445840486', '2024-06-25 20:00:00.000', '2024-06-25 20:00:00.000'); +ALTER TABLE "public"."selector" ADD COLUMN namespace_id VARCHAR(50) NULL; +COMMENT ON COLUMN "public"."selector"."namespace_id" IS 'namespaceId'; \ No newline at end of file diff --git a/db/upgrade/2.6.1-upgrade-2.7.0-mysql.sql b/db/upgrade/2.6.1-upgrade-2.7.0-mysql.sql index 724a9b5a04b1..d0b2859f8aaa 100755 --- a/db/upgrade/2.6.1-upgrade-2.7.0-mysql.sql +++ b/db/upgrade/2.6.1-upgrade-2.7.0-mysql.sql @@ -168,3 +168,10 @@ INSERT INTO `shenyu`.`permission` (`id`, `object_id`, `resource_id`, `date_creat /* add column into dashboard_user table */ ALTER TABLE `shenyu`.`dashboard_user` ADD COLUMN `client_id` varchar(32) NULL DEFAULT NULL COMMENT 'client id'; + + +ALTER TABLE `shenyu`.`selector` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId' AFTER `match_restful`; + +UPDATE selector +SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' +WHERE namespaceId IS NULL; \ No newline at end of file diff --git a/db/upgrade/2.6.1-upgrade-2.7.0-og.sql b/db/upgrade/2.6.1-upgrade-2.7.0-og.sql index f1cfc8f5eb8e..12a3acfc1a0b 100644 --- a/db/upgrade/2.6.1-upgrade-2.7.0-og.sql +++ b/db/upgrade/2.6.1-upgrade-2.7.0-og.sql @@ -192,3 +192,8 @@ INSERT INTO "public"."permission" VALUES ('1792779493541343267', '13463585604272 /* add column into dashboard_user table */ ALTER TABLE "public"."dashboard_user" ADD COLUMN client_id VARCHAR(32) NULL; COMMENT ON COLUMN "public"."dashboard_user".client_id IS 'client id'; + +ALTER TABLE "public"."selector" ADD COLUMN namespace_id VARCHAR(50) NULL; +COMMENT ON COLUMN "public"."selector"."namespace_id" IS 'namespaceId'; + +UPDATE "public"."selector" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL; \ No newline at end of file diff --git a/db/upgrade/2.6.1-upgrade-2.7.0-oracle.sql b/db/upgrade/2.6.1-upgrade-2.7.0-oracle.sql index 3de423095c0d..05209e1cba4f 100755 --- a/db/upgrade/2.6.1-upgrade-2.7.0-oracle.sql +++ b/db/upgrade/2.6.1-upgrade-2.7.0-oracle.sql @@ -218,3 +218,8 @@ INSERT /*+ IGNORE_ROW_ON_DUPKEY_INDEX (permission(id)) */ INTO permission (id, o /* add column into dashboard_user table */ ALTER TABLE dashboard_user ADD client_id VARCHAR(32) NULL; COMMENT ON COLUMN dashboard_user.client_id IS 'client id'; + +ALTER TABLE selector ADD namespace_id VARCHAR2(50) NULL; +COMMENT ON COLUMN selector.namespace_id IS 'namespaceId'; + +UPDATE selector SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL; \ No newline at end of file diff --git a/db/upgrade/2.6.1-upgrade-2.7.0-pg.sql b/db/upgrade/2.6.1-upgrade-2.7.0-pg.sql index 514de6e363d5..dc24306f3c8c 100755 --- a/db/upgrade/2.6.1-upgrade-2.7.0-pg.sql +++ b/db/upgrade/2.6.1-upgrade-2.7.0-pg.sql @@ -193,3 +193,8 @@ INSERT INTO "public"."permission" VALUES ('1792779493541343267', '13463585604272 /* add column into dashboard_user table */ ALTER TABLE "public"."dashboard_user" ADD COLUMN client_id VARCHAR(32) NULL; COMMENT ON COLUMN "public"."dashboard_user".client_id IS 'client id'; + +ALTER TABLE "public"."selector" ADD COLUMN namespace_id VARCHAR(50) NULL; +COMMENT ON COLUMN "public"."selector"."namespace_id" IS 'namespaceId'; + +UPDATE "public"."selector" SET namespace_id = '649330b6-c2d7-4edc-be8e-8a54df9eb385' WHERE namespace_id IS NULL; \ No newline at end of file diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/SelectorController.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/SelectorController.java index 58f8a9440da5..3be19be82333 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/SelectorController.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/SelectorController.java @@ -17,9 +17,13 @@ package org.apache.shenyu.admin.controller; +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; import org.apache.shenyu.admin.aspect.annotation.RestApi; +import org.apache.shenyu.admin.mapper.NamespaceMapper; import org.apache.shenyu.admin.mapper.SelectorMapper; import org.apache.shenyu.admin.model.dto.BatchCommonDTO; +import org.apache.shenyu.admin.model.dto.BatchNamespaceCommonDTO; import org.apache.shenyu.admin.model.dto.SelectorDTO; import org.apache.shenyu.admin.model.page.CommonPager; import org.apache.shenyu.admin.model.page.PageCondition; @@ -41,12 +45,6 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; -import jakarta.validation.Valid; -import jakarta.validation.constraints.NotBlank; -import jakarta.validation.constraints.NotEmpty; -import jakarta.validation.constraints.NotNull; -import java.util.List; - /** * this is selector controller. */ @@ -66,16 +64,21 @@ public SelectorController(final SelectorService selectorService) { * @param name selector name. * @param currentPage current page. * @param pageSize page size. + * @param namespaceId namespaceId. * @return {@linkplain ShenyuAdminResult} */ @GetMapping("") public AdminResult> querySelectors(final String pluginId, final String name, - @RequestParam @NotNull final Integer currentPage, - @RequestParam @NotNull final Integer pageSize) { + @RequestParam @NotNull final Integer currentPage, + @RequestParam @NotNull final Integer pageSize, + @Valid @Existed(message = "namespaceId is not existed", + provider = NamespaceMapper.class) final String namespaceId + ) { final SelectorQueryCondition condition = new SelectorQueryCondition(); condition.setUserId(SessionUtil.visitor().getUserId()); condition.setPlugin(ListUtil.of(pluginId)); condition.setKeyword(name); + condition.setNamespaceId(namespaceId); return searchAdaptor(new PageCondition<>(currentPage, pageSize, condition)); } @@ -83,12 +86,15 @@ public AdminResult> querySelectors(final String pluginId * detail selector. * * @param id selector id. + * @param namespaceId namespaceId. * @return {@linkplain ShenyuAdminResult} */ - @GetMapping("/{id}") + @GetMapping("/{id}/{namespaceId}") public ShenyuAdminResult detailSelector(@PathVariable("id") @Valid - @Existed(provider = SelectorMapper.class, message = "selector is not existed") final String id) { - SelectorVO selectorVO = selectorService.findById(id); + @Existed(provider = SelectorMapper.class, message = "selector is not existed") final String id, + @PathVariable("namespaceId") @Valid + @Existed(provider = NamespaceMapper.class, message = "namespaceId is not existed") final String namespaceId) { + SelectorVO selectorVO = selectorService.findByIdAndNamespaceId(id, namespaceId); return ShenyuAdminResult.success(ShenyuResultMessage.DETAIL_SUCCESS, selectorVO); } @@ -113,8 +119,7 @@ public ShenyuAdminResult createSelector(@Valid @RequestBody final SelectorDTO se */ @PutMapping("/{id}") public ShenyuAdminResult updateSelector(@PathVariable("id") @Valid - @Existed(provider = SelectorMapper.class, - message = "selector is not existed") final String id, + @Existed(provider = SelectorMapper.class, message = "selector is not existed") final String id, @Valid @RequestBody final SelectorDTO selectorDTO) { selectorDTO.setId(id); Integer updateCount = selectorService.createOrUpdate(selectorDTO); @@ -129,7 +134,7 @@ public ShenyuAdminResult updateSelector(@PathVariable("id") @Valid */ @PostMapping("/batchEnabled") public ShenyuAdminResult batchEnabled(@Valid @RequestBody final BatchCommonDTO batchCommonDTO) { - if (!selectorService.enabled(batchCommonDTO.getIds(), batchCommonDTO.getEnabled())) { + if (!selectorService.enabledByIdsAndNamespaceId(batchCommonDTO.getIds(), batchCommonDTO.getEnabled(), batchCommonDTO.getNamespaceId())) { return ShenyuAdminResult.error(ShenyuResultMessage.NOT_FOUND_EXCEPTION); } return ShenyuAdminResult.success(ShenyuResultMessage.ENABLE_SUCCESS); @@ -138,12 +143,12 @@ public ShenyuAdminResult batchEnabled(@Valid @RequestBody final BatchCommonDTO b /** * delete Selectors. * - * @param ids primary key. + * @param batchNamespaceCommonDTO batchNamespaceCommonDTO. * @return {@linkplain ShenyuAdminResult} */ @DeleteMapping("/batch") - public ShenyuAdminResult deleteSelector(@RequestBody @NotEmpty final List<@NotBlank String> ids) { - Integer deleteCount = selectorService.delete(ids); + public ShenyuAdminResult deleteSelector(@Valid @RequestBody final BatchNamespaceCommonDTO batchNamespaceCommonDTO) { + Integer deleteCount = selectorService.deleteByNamespaceId(batchNamespaceCommonDTO.getIds(), batchNamespaceCommonDTO.getNamespaceId()); return ShenyuAdminResult.success(ShenyuResultMessage.DELETE_SUCCESS, deleteCount); } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/NamespacePluginRelMapper.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/NamespacePluginRelMapper.java index f5b4187015a7..639129c03d11 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/NamespacePluginRelMapper.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/NamespacePluginRelMapper.java @@ -63,22 +63,21 @@ public interface NamespacePluginRelMapper extends ExistProvider { List selectByQuery(NamespacePluginQuery namespacePluginQuery); /** - * select plugin by namespacePluginId. + * select plugin by PluginId and namespaceId. * - * @param id primary key. - * @param namespaceId namespace id. + * @param pluginId primary key. + * @param namespaceId namespaceId. * @return {@linkplain PluginVO} */ - NamespacePluginVO selectById(String id, String namespaceId); + NamespacePluginVO selectByPluginIdAndNamespaceId(String pluginId, String namespaceId); /** * select plugin by pluginId. * * @param pluginId primary key. - * @param namespaceId namespace id. * @return {@linkplain PluginVO} */ - NamespacePluginVO selectByPluginId(String pluginId, String namespaceId); + NamespacePluginVO selectByPluginId(String pluginId); /** * search by condition. @@ -93,7 +92,7 @@ public interface NamespacePluginRelMapper extends ExistProvider { * * @param name name * @param exclude exclude - * @param namespaceId namespace id. + * @param namespaceId namespaceId. * @return existed */ Boolean nameExistedExclude(@Param("name") Serializable name, @Param("exclude") List exclude, @Param("namespaceId") String namespaceId); @@ -108,38 +107,38 @@ public interface NamespacePluginRelMapper extends ExistProvider { int updateSelective(NamespacePluginRelDO namespacePluginRelDO); /** - * select plugin by id. + * select plugin by ids and namespaceId. * * @param ids primary keys. - * @param namespaceId namespace id. + * @param namespaceId namespaceId. * @return {@linkplain NamespacePluginRelDO} */ - List selectByIds(List ids, String namespaceId); + List selectByIdsAndNamespaceId(List ids, String namespaceId); /** - * delete plugin. + * delete plugin by ids and namespaceId. * * @param ids primary keys. - * @param namespaceId namespace id. + * @param namespaceId namespaceId. * @return rows int */ - int deleteByIds(List ids, String namespaceId); + int deleteByIdsAndNamespaceId(List ids, String namespaceId); /** - * select all. + * select all by namespaceId. * - * @param namespaceId namespace id. + * @param namespaceId namespaceId. * @return {@linkplain List} */ - List selectAll(String namespaceId); + List selectAllByNamespaceId(String namespaceId); /** * select all. * * @return {@linkplain List} */ - List selectAll(); + List selectAllByNamespaceId(); /** * enable data by a list of ids. diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/SelectorMapper.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/SelectorMapper.java index 871972c0792c..b0857834cfe7 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/SelectorMapper.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/SelectorMapper.java @@ -44,6 +44,15 @@ public interface SelectorMapper extends ExistProvider { @Override Boolean existed(@Param("id") Serializable id); + /** + * select selector by id and namespaceId. + * + * @param id primary key. + * @param namespaceId namespaceId. + * @return {@linkplain SelectorDO} + */ + SelectorDO selectByIdAndNamespaceId(String id, String namespaceId); + /** * select selector by id. * @@ -69,37 +78,43 @@ public interface SelectorMapper extends ExistProvider { List selectByQuery(SelectorQuery selectorQuery); /** - * Find by plugin id list. + * Find list by plugin id and namespaceId. * - * @param pluginId the plugin id + * @param pluginId the plugin id + * @param namespaceId namespaceId. * @return the list */ - List findByPluginId(String pluginId); + List findByPluginIdAndNamespaceId(String pluginId, String namespaceId); /** - * Find by plugin id list. + * Find list by plugin id and namespaceId. * - * @param pluginIds the plugin ids + * @param pluginIds the plugin ids + * @param namespaceId namespaceId. * @return the list */ - List findByPluginIds(List pluginIds); + List findByPluginIdsAndNamespaceId(@Param("list") List pluginIds, String namespaceId); /** - * select select by name. + * select list by name and namespaceId. * - * @param name the name + * @param name the name + * @param namespaceId namespaceId. * @return selector do list */ - List selectByName(String name); + List selectByNameAndNamespaceId(String name, String namespaceId); /** - * Find by name and plugin id selector do. + * Find selector entity by name and plugin id and namespaceId. * - * @param name the name - * @param pluginId the plugin id + * @param name the name + * @param pluginId the plugin id + * @param namespaceId namespaceId. * @return the selector do */ - SelectorDO findByNameAndPluginId(@Param("name") String name, @Param("pluginId") String pluginId); + SelectorDO findByNameAndPluginIdAndNamespaceId(@Param("name") String name, + @Param("pluginId") String pluginId, + @Param("namespaceId") String namespaceId); /** * count selector by query. diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/BatchNamespaceCommonDTO.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/BatchNamespaceCommonDTO.java index 3452868d2b25..575153cc4721 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/BatchNamespaceCommonDTO.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/BatchNamespaceCommonDTO.java @@ -20,6 +20,9 @@ import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; +import org.apache.shenyu.admin.mapper.NamespaceMapper; +import org.apache.shenyu.admin.validation.annotation.Existed; + import java.io.Serializable; import java.util.List; import java.util.Objects; @@ -37,6 +40,7 @@ public class BatchNamespaceCommonDTO implements Serializable { private Boolean enabled; @NotEmpty + @Existed(message = "namespaceId is not existed", provider = NamespaceMapper.class) private String namespaceId; /** diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/SelectorDTO.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/SelectorDTO.java index c66c6031f0d6..841582abbbd6 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/SelectorDTO.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/SelectorDTO.java @@ -17,6 +17,7 @@ package org.apache.shenyu.admin.model.dto; +import org.apache.shenyu.admin.mapper.NamespaceMapper; import org.apache.shenyu.admin.mapper.PluginMapper; import org.apache.shenyu.admin.mapper.SelectorMapper; import org.apache.shenyu.admin.validation.annotation.Existed; @@ -112,6 +113,13 @@ public final class SelectorDTO implements Serializable { * selector rules. */ private List selectorRules; + + /** + * namespaceId. + */ + @NotBlank + @Existed(message = "namespaceId is not existed", provider = NamespaceMapper.class) + private String namespaceId; public SelectorDTO() { } @@ -128,7 +136,8 @@ public SelectorDTO(final String id, final String handle, @Valid final List selectorConditions, @NotNull final Boolean matchRestful, - @Valid final List selectorRules) { + @Valid final List selectorRules, + @Valid final String namespaceId) { this.id = id; this.pluginId = pluginId; this.name = name; @@ -142,6 +151,7 @@ public SelectorDTO(final String id, this.selectorConditions = selectorConditions; this.matchRestful = matchRestful; this.selectorRules = selectorRules; + this.namespaceId = namespaceId; } /** @@ -377,6 +387,24 @@ public List getSelectorRules() { public void setSelectorRules(final List selectorRules) { this.selectorRules = selectorRules; } + + /** + * get namespaceId. + * + * @return namespaceId + */ + public String getNamespaceId() { + return namespaceId; + } + + /** + * set namespaceId. + * + * @param namespaceId namespaceId + */ + public void setNamespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + } /** * builder method. @@ -386,36 +414,30 @@ public void setSelectorRules(final List selectorRules) { public static SelectorDTO.SelectorDTOBuilder builder() { return new SelectorDTO.SelectorDTOBuilder(); } - + @Override public boolean equals(final Object o) { if (this == o) { return true; } - if (!(o instanceof SelectorDTO)) { + if (o == null || getClass() != o.getClass()) { return false; } SelectorDTO that = (SelectorDTO) o; - return Objects.equals(id, that.id) - && Objects.equals(pluginId, that.pluginId) - && Objects.equals(name, that.name) - && Objects.equals(matchMode, that.matchMode) - && Objects.equals(type, that.type) - && Objects.equals(sort, that.sort) - && Objects.equals(enabled, that.enabled) - && Objects.equals(loged, that.loged) - && Objects.equals(continued, that.continued) - && Objects.equals(handle, that.handle) - && Objects.equals(selectorConditions, that.selectorConditions) - && Objects.equals(matchRestful, that.matchRestful); + return Objects.equals(id, that.id) && Objects.equals(pluginId, that.pluginId) && Objects.equals(name, that.name) + && Objects.equals(matchMode, that.matchMode) && Objects.equals(type, that.type) && Objects.equals(sort, that.sort) + && Objects.equals(enabled, that.enabled) && Objects.equals(loged, that.loged) && Objects.equals(continued, that.continued) + && Objects.equals(handle, that.handle) && Objects.equals(selectorConditions, that.selectorConditions) + && Objects.equals(matchRestful, that.matchRestful) && Objects.equals(selectorRules, that.selectorRules) + && Objects.equals(namespaceId, that.namespaceId); } - + @Override public int hashCode() { return Objects.hash(id, pluginId, name, matchMode, type, sort, enabled, loged, continued, handle, - selectorConditions, matchRestful); + selectorConditions, matchRestful, namespaceId); } - + public static final class SelectorDTOBuilder { private String id; @@ -443,6 +465,8 @@ public static final class SelectorDTOBuilder { private Boolean matchRestful; private List selectorRules; + + private String namespaceId; private SelectorDTOBuilder() { } @@ -589,6 +613,17 @@ public SelectorDTOBuilder selectorRules(final List selectorRules) { this.selectorRules = selectorRules; return this; } + + /** + * namespaceId. + * + * @param namespaceId namespaceId + * @return SelectorDOBuilder + */ + public SelectorDTOBuilder namespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + return this; + } /** * build method. @@ -597,7 +632,7 @@ public SelectorDTOBuilder selectorRules(final List selectorRules) { */ public SelectorDTO build() { return new SelectorDTO(id, pluginId, name, matchMode, type, sort, enabled, loged, continued, handle, - selectorConditions, matchRestful, selectorRules); + selectorConditions, matchRestful, selectorRules, namespaceId); } } } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/PluginDO.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/PluginDO.java index aba8ba41ab0d..f3275625ed0d 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/PluginDO.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/PluginDO.java @@ -19,6 +19,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.shenyu.admin.model.dto.PluginDTO; +import org.apache.shenyu.admin.model.vo.NamespacePluginVO; import org.apache.shenyu.common.utils.UUIDUtils; import org.opengauss.util.Base64; @@ -222,6 +223,37 @@ public static PluginDO buildPluginDO(final PluginDTO pluginDTO) { }).orElse(null); } + /** + * build pluginDO. + * + * @param namespacePluginVO {@linkplain NamespacePluginVO} + * @return {@linkplain PluginDO} + */ + public static PluginDO buildPluginDO(final NamespacePluginVO namespacePluginVO) { + return Optional.ofNullable(namespacePluginVO).map(item -> { + Timestamp currentTime = new Timestamp(System.currentTimeMillis()); + PluginDO pluginDO = PluginDO.builder() + .name(item.getName()) + .config(item.getConfig()) + .enabled(item.getEnabled()) + .role(item.getRole()) + .sort(item.getSort()) + .dateUpdated(currentTime) + .build(); + + if (StringUtils.isEmpty(item.getId())) { + pluginDO.setId(UUIDUtils.getInstance().generateShortUuid()); + pluginDO.setDateCreated(currentTime); + } else { + pluginDO.setId(item.getId()); + } + if (Objects.nonNull(item.getFile())) { + pluginDO.setPluginJar(Base64.decode(item.getFile())); + } + return pluginDO; + }).orElse(null); + } + @Override public boolean equals(final Object o) { if (this == o) { diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/SelectorDO.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/SelectorDO.java index 592a85f98e61..c7c217184040 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/SelectorDO.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/SelectorDO.java @@ -82,12 +82,17 @@ public final class SelectorDO extends BaseDO { * handle. */ private String handle; - + /** * match restful. */ private Boolean matchRestful; + /** + * namespaceId. + */ + private String namespaceId; + public SelectorDO() { } @@ -100,7 +105,8 @@ public SelectorDO(final String pluginId, final Boolean loged, final Boolean continued, final String handle, - final Boolean matchRestful) { + final Boolean matchRestful, + final String namespaceId) { this.pluginId = pluginId; this.name = name; this.matchMode = matchMode; @@ -111,8 +117,10 @@ public SelectorDO(final String pluginId, this.continued = continued; this.handle = handle; this.matchRestful = matchRestful; + this.namespaceId = namespaceId; } + /** * Gets the value of pluginId. * @@ -274,7 +282,7 @@ public String getHandle() { public void setHandle(final String handle) { this.handle = handle; } - + /** * get match restful. * @@ -283,7 +291,7 @@ public void setHandle(final String handle) { public Boolean getMatchRestful() { return matchRestful; } - + /** * set match restful. * @@ -292,7 +300,25 @@ public Boolean getMatchRestful() { public void setMatchRestful(final Boolean matchRestful) { this.matchRestful = matchRestful; } - + + /** + * get namespaceId. + * + * @return namespaceId + */ + public String getNamespaceId() { + return namespaceId; + } + + /** + * set namespaceId. + * + * @param namespaceId namespaceId + */ + public void setNamespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + } + /** * builder method. * @@ -323,12 +349,13 @@ public boolean equals(final Object o) { && Objects.equals(loged, that.loged) && Objects.equals(continued, that.continued) && Objects.equals(handle, that.handle) - && Objects.equals(matchRestful, that.matchRestful); + && Objects.equals(matchRestful, that.matchRestful) + && Objects.equals(namespaceId, that.namespaceId); } @Override public int hashCode() { - return Objects.hash(super.hashCode(), pluginId, name, matchMode, type, sort, enabled, loged, continued, handle, matchRestful); + return Objects.hash(super.hashCode(), pluginId, name, matchMode, type, sort, enabled, loged, continued, handle, matchRestful, namespaceId); } /** @@ -351,6 +378,7 @@ public static SelectorDO buildSelectorDO(final SelectorDTO selectorDTO) { .pluginId(item.getPluginId()) .name(item.getName()) .matchRestful(item.getMatchRestful()) + .namespaceId(item.getNamespaceId()) .build(); if (StringUtils.isEmpty(item.getId())) { selectorDO.setId(UUIDUtils.getInstance().generateShortUuid()); @@ -370,9 +398,9 @@ public static SelectorDO buildSelectorDO(final SelectorDTO selectorDTO) { /** * Trans from selector data. * - * @param selectorDO the selector do - * @param pluginName the plugin name - * @param conditionDataList the condition data list + * @param selectorDO the selector do + * @param pluginName the plugin name + * @param conditionDataList the condition data list * @param beforeConditionDataList before condition data list * @return the selector data */ @@ -393,9 +421,10 @@ public static SelectorData transFrom(final SelectorDO selectorDO, final String p .conditionList(conditionDataList) .matchRestful(selectorDO.getMatchRestful()) .beforeConditionList(beforeConditionDataList) + .namespaceId(selectorDO.getNamespaceId()) .build(); } - + /** * selector data transform. * @@ -433,9 +462,11 @@ public static final class SelectorDOBuilder { private Boolean continued; private String handle; - + private Boolean matchRestful; + private String namespaceId; + private SelectorDOBuilder() { } @@ -570,7 +601,7 @@ public SelectorDOBuilder handle(final String handle) { this.handle = handle; return this; } - + /** * match restful. * @@ -582,6 +613,17 @@ public SelectorDOBuilder matchRestful(final Boolean matchRestful) { return this; } + /** + * namespaceId. + * + * @param namespaceId namespaceId + * @return SelectorDOBuilder + */ + public SelectorDOBuilder namespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + return this; + } + /** * build method. * @@ -602,6 +644,7 @@ public SelectorDO build() { selectorDO.setContinued(continued); selectorDO.setHandle(handle); selectorDO.setMatchRestful(matchRestful); + selectorDO.setNamespaceId(namespaceId); return selectorDO; } } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/event/plugin/BatchNamespacePluginDeletedEvent.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/event/plugin/BatchNamespacePluginDeletedEvent.java new file mode 100644 index 000000000000..87e28af14c53 --- /dev/null +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/event/plugin/BatchNamespacePluginDeletedEvent.java @@ -0,0 +1,90 @@ +/* + * 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.model.event.plugin; + +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.model.entity.BaseDO; +import org.apache.shenyu.admin.model.entity.PluginDO; +import org.apache.shenyu.admin.model.enums.EventTypeEnum; +import org.apache.shenyu.common.utils.ListUtil; + +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; + +/** + * BatchPluginDeletedEvent. + */ +public class BatchNamespacePluginDeletedEvent extends BatchPluginChangedEvent { + + private final List deletedPluginIds; + + private final String namespaceId; + + /** + * Create a new {@code PluginChangedEvent}.operator is unknown. + * + * @param source Current plugin state + * @param operator operator + * @param namespaceId namespaceId + */ + public BatchNamespacePluginDeletedEvent(final Collection source, final String operator, final String namespaceId) { + super(source, null, EventTypeEnum.PLUGIN_DELETE, operator); + this.deletedPluginIds = ListUtil.map(source, BaseDO::getId); + this.namespaceId = namespaceId; + } + + @Override + public String buildContext() { + final String plugins = ((Collection) getSource()) + .stream() + .map(s -> ((PluginDO) s).getName()) + .collect(Collectors.joining(",")); + return String.format("the plugins[%s] is %s", plugins, StringUtils.lowerCase(getType().getType().toString())); + } + + /** + * get plugins. + * + * @return plugins + */ + public List getPlugins() { + return ((Collection) getSource()) + .stream() + .map(PluginDO.class::cast) + .collect(Collectors.toList()); + } + + /** + * get deleted iss. + * + * @return list + */ + public List getDeletedPluginIds() { + return deletedPluginIds; + } + + /** + * Gets the namespace id. + * + * @return the plugin handle list + */ + public String getNamespaceId() { + return namespaceId; + } +} diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/event/plugin/BatchPluginDeletedEvent.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/event/plugin/BatchPluginDeletedEvent.java index a1c74bdec60c..12db9d7ddaa3 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/event/plugin/BatchPluginDeletedEvent.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/event/plugin/BatchPluginDeletedEvent.java @@ -31,13 +31,13 @@ * BatchPluginDeletedEvent. */ public class BatchPluginDeletedEvent extends BatchPluginChangedEvent { - + private final List deletedPluginIds; - + /** * Create a new {@code PluginChangedEvent}.operator is unknown. * - * @param source Current plugin state + * @param source Current plugin state * @param operator operator */ public BatchPluginDeletedEvent(final Collection source, final String operator) { @@ -74,4 +74,5 @@ public List getPlugins() { public List getDeletedPluginIds() { return deletedPluginIds; } + } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/event/selector/SelectorChangedEvent.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/event/selector/SelectorChangedEvent.java index 479702a34df4..d417aa7eac94 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/event/selector/SelectorChangedEvent.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/event/selector/SelectorChangedEvent.java @@ -46,9 +46,9 @@ public SelectorChangedEvent(final SelectorDO source, final SelectorDO before, fi public String buildContext() { final SelectorDO after = (SelectorDO) getAfter(); if (Objects.isNull(getBefore())) { - return String.format("the selector [%s] is %s", after.getName(), StringUtils.lowerCase(getType().getType().toString())); + return String.format("the namespace [%s] selector [%s] is %s", after.getNamespaceId(), after.getName(), StringUtils.lowerCase(getType().getType().toString())); } - return String.format("the selector [%s] is %s : %s", after.getName(), StringUtils.lowerCase(getType().getType().toString()), contrast()); + return String.format("the namespace [%s] selector [%s] is %s : %s", after.getNamespaceId(), after.getName(), StringUtils.lowerCase(getType().getType().toString()), contrast()); } @@ -79,6 +79,9 @@ private String contrast() { if (!Objects.equals(before.getLoged(), after.getLoged())) { builder.append(String.format("loged[%s => %s] ", before.getLoged(), after.getLoged())); } + if (!Objects.equals(before.getNamespaceId(), after.getNamespaceId())) { + builder.append(String.format("namespaceId[%s => %s] ", before.getNamespaceId(), after.getNamespaceId())); + } return builder.toString(); } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/SelectorConditionQuery.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/SelectorConditionQuery.java index fa12f0149664..c315181e3acc 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/SelectorConditionQuery.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/SelectorConditionQuery.java @@ -32,6 +32,11 @@ public class SelectorConditionQuery implements Serializable { */ private String selectorId; + /** + * namespace id. + */ + private String namespaceId; + public SelectorConditionQuery() { } @@ -57,20 +62,38 @@ public void setSelectorId(final String selectorId) { this.selectorId = selectorId; } + /** + * Gets the value of name. + * + * @return the value of name + */ + public String getNamespaceId() { + return namespaceId; + } + + /** + * Sets the namespaceId. + * + * @param namespaceId namespaceId + */ + public void setNamespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + } + @Override public boolean equals(final Object o) { if (this == o) { return true; } - if (!(o instanceof SelectorConditionQuery)) { + if (o == null || getClass() != o.getClass()) { return false; } SelectorConditionQuery that = (SelectorConditionQuery) o; - return Objects.equals(selectorId, that.selectorId); + return Objects.equals(selectorId, that.selectorId) && Objects.equals(namespaceId, that.namespaceId); } @Override public int hashCode() { - return Objects.hash(selectorId); + return Objects.hash(selectorId, namespaceId); } } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/SelectorQuery.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/SelectorQuery.java index 12177eedac8c..2b44be2711dd 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/SelectorQuery.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/SelectorQuery.java @@ -18,6 +18,7 @@ package org.apache.shenyu.admin.model.query; import java.util.List; + import org.apache.shenyu.admin.model.page.PageParameter; import java.util.Objects; @@ -49,6 +50,11 @@ public class SelectorQuery extends FilterQuery { */ private PageParameter pageParameter; + /** + * namespace id. + */ + private String namespaceId; + public SelectorQuery() { } @@ -136,23 +142,42 @@ public void setPageParameter(final PageParameter pageParameter) { this.pageParameter = pageParameter; } + /** + * Gets the value of name. + * + * @return the value of name + */ + public String getNamespaceId() { + return namespaceId; + } + + /** + * Sets the namespaceId. + * + * @param namespaceId namespaceId + */ + public void setNamespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + } + @Override public boolean equals(final Object o) { if (this == o) { return true; } - if (!(o instanceof SelectorQuery)) { + if (o == null || getClass() != o.getClass()) { return false; } if (!super.equals(o)) { return false; } SelectorQuery that = (SelectorQuery) o; - return Objects.equals(pluginId, that.pluginId) && Objects.equals(name, that.name) && Objects.equals(pageParameter, that.pageParameter); + return Objects.equals(pluginId, that.pluginId) && Objects.equals(pluginIds, that.pluginIds) && Objects.equals(name, that.name) + && Objects.equals(pageParameter, that.pageParameter) && Objects.equals(namespaceId, that.namespaceId); } @Override public int hashCode() { - return Objects.hash(super.hashCode(), pluginId, name, pageParameter); + return Objects.hash(super.hashCode(), pluginId, pluginIds, name, pageParameter, namespaceId); } } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/SelectorQueryCondition.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/SelectorQueryCondition.java index 5999314a2757..aa73bd87ec54 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/SelectorQueryCondition.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/SelectorQueryCondition.java @@ -43,6 +43,11 @@ public class SelectorQueryCondition extends BaseExcludedSearchCondition implemen * selector from plugin. */ private List plugin; + + /** + * namespace id. + */ + private String namespaceId; /** * user id. @@ -125,4 +130,23 @@ public List getPlugin() { public void setPlugin(final List plugin) { this.plugin = plugin; } + + /** + * Gets the value of name. + * + * @return the value of name + */ + public String getNamespaceId() { + return namespaceId; + } + + /** + * Sets the namespaceId. + * + * @param namespaceId namespaceId + */ + public void setNamespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + } + } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/vo/NamespaceVO.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/vo/NamespaceVO.java index 8064430c44c8..b780e25a6d4d 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/vo/NamespaceVO.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/vo/NamespaceVO.java @@ -32,7 +32,7 @@ public class NamespaceVO implements Serializable { private String id; /** - * name. + * namespaceId. */ private String namespaceId; diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/vo/SelectorVO.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/vo/SelectorVO.java index e4b8d26b76c4..02f51d22929a 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/vo/SelectorVO.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/vo/SelectorVO.java @@ -129,6 +129,11 @@ public class SelectorVO implements Serializable { */ private List selectorRules; + /** + * namespaceId. + */ + private String namespaceId; + public SelectorVO() { } @@ -147,7 +152,8 @@ public SelectorVO(final String id, final String handle, final List selectorConditions, final String dateCreated, - final String dateUpdated) { + final String dateUpdated, + final String namespaceId) { this.id = id; this.pluginId = pluginId; this.name = name; @@ -164,6 +170,7 @@ public SelectorVO(final String id, this.selectorConditions = selectorConditions; this.dateCreated = dateCreated; this.dateUpdated = dateUpdated; + this.namespaceId = namespaceId; } /** @@ -544,13 +551,34 @@ public static SelectorVO buildSelectorVO(final SelectorDO selectorDO) { * @param selectorConditions {@linkplain List} * @return {@linkplain SelectorVO} */ - public static SelectorVO buildSelectorVO(final SelectorDO selectorDO, final List selectorConditions) { + public static SelectorVO buildSelectorVO(final SelectorDO selectorDO, + final List selectorConditions) { return new SelectorVO(selectorDO.getId(), selectorDO.getPluginId(), selectorDO.getName(), selectorDO.getMatchMode(), MatchModeEnum.getMatchModeByCode(selectorDO.getMatchMode()), selectorDO.getType(), SelectorTypeEnum.getSelectorTypeByCode(selectorDO.getType()), selectorDO.getSort(), selectorDO.getEnabled(), selectorDO.getLoged(), selectorDO.getContinued(), selectorDO.getMatchRestful(), selectorDO.getHandle(), selectorConditions, DateUtils.localDateTimeToString(selectorDO.getDateCreated().toLocalDateTime()), - DateUtils.localDateTimeToString(selectorDO.getDateUpdated().toLocalDateTime())); + DateUtils.localDateTimeToString(selectorDO.getDateUpdated().toLocalDateTime()), + selectorDO.getNamespaceId()); + } + + /** + * Gets the value of namespaceId. + * + * @return the value of namespaceId + */ + public String getNamespaceId() { + return namespaceId; } + + /** + * Sets the namespaceId. + * + * @param namespaceId namespaceId + */ + public void setNamespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + } + } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/SelectorService.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/SelectorService.java index 8e4e9a7f01d7..5e3b59d5136e 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/SelectorService.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/SelectorService.java @@ -105,57 +105,63 @@ default int createOrUpdate(SelectorDTO selectorDTO) { int updateSelective(SelectorDO selectorDO); /** - * delete selectors. + * delete selectors by ids and namespaceId. * - * @param ids primary key. + * @param ids primary key. + * @param namespaceId namespaceId. * @return rows int */ - int delete(List ids); + int deleteByNamespaceId(List ids, String namespaceId); /** - * find selector by id. + * find selector by id and namespaceId. * - * @param id primary key. + * @param id primary key. + * @param namespaceId namespaceId. * @return {@linkplain SelectorVO} */ - SelectorVO findById(String id); + SelectorVO findByIdAndNamespaceId(String id, String namespaceId); /** - * find selector by name. + * find selector by name and namespaceId. * - * @param name the name + * @param name the name + * @param namespaceId namespaceId. + * Therefore, it is recommended to: {@linkplain SelectorService#findListByNameAndNamespaceId(java.lang.String, java.lang.String)} * @return selector do * @deprecated sice 2.6.0 Deprecated. By querying under this condition, multiple data are usually obtained. - * Therefore, it is recommended to: {@linkplain SelectorService#findListByName(java.lang.String)} */ @Deprecated - SelectorDO findByName(String name); + SelectorDO findByNameAndNamespaceId(String name, String namespaceId); /** - * find selector list by name. + * find selector list by name and namespaceId. * - * @param name name + * @param name name + * @param namespaceId namespaceId. * @return list */ - List findListByName(String name); + List findListByNameAndNamespaceId(String name, String namespaceId); /** - * Find by name and plugin id selector do. + * Find selector do by name and pluginName and namespaceId. * - * @param name the name - * @param pluginName the plugin name + * @param name the name + * @param pluginName the plugin name + * @param namespaceId the namespaceId * @return the selector do */ - SelectorDO findByNameAndPluginName(String name, String pluginName); + SelectorDO findByNameAndPluginNameAndNamespaceId(String name, String pluginName, String namespaceId); /** - * findByNameAndPluginNameForUpdate. + * find selector entity by selector name, plugin name and namespace id. * - * @param name name - * @param pluginName pluginName + * @param name name + * @param pluginName pluginName + * @param namespaceId namespaceId. * @return SelectorDO */ - SelectorDO findByNameAndPluginNameForUpdate(String name, String pluginName); + SelectorDO findByNameAndPluginNameAndNamespaceIdForUpdate(String name, String pluginName, String namespaceId); /** * Find selectorDO list by name and plugin name list. @@ -167,21 +173,23 @@ default int createOrUpdate(SelectorDTO selectorDTO) { List findByNameAndPluginNames(String name, List pluginNames); /** - * Build by name selector data. + * Build selector data by name and namespaceId. * - * @param name the name + * @param name the name + * @param namespaceId namespaceId. * @return the selector data */ - SelectorData buildByName(String name); + SelectorData buildByNameAndNamespaceId(String name, String namespaceId); /** - * Build by name selector data. + * Build selector data by name and plugin name and namespaceId. * - * @param name the name - * @param pluginName the plugin name + * @param name the name + * @param pluginName the plugin name + * @param namespaceId the namespaceId * @return the selector data */ - SelectorData buildByName(String name, String pluginName); + SelectorData buildByNameAndPluginNameAndNamespaceId(String name, String pluginName, String namespaceId); /** * find page of selector by query. @@ -200,12 +208,13 @@ default int createOrUpdate(SelectorDTO selectorDTO) { CommonPager listByPage(SelectorQuery selectorQuery); /** - * Find by plugin id list. + * Find list by plugin id and namespaceId. * - * @param pluginId the plugin id + * @param pluginId the plugin id + * @param namespaceId the namespaceId * @return the list */ - List findByPluginId(String pluginId); + List findByPluginIdAndNamespaceId(String pluginId, String namespaceId); /** * List all list. @@ -223,17 +232,19 @@ default int createOrUpdate(SelectorDTO selectorDTO) { /** * Import the plugin selector list. + * * @param selectorList the plugin selector list * @return config import result */ ConfigImportResult importData(List selectorList); /** - * Enabled string. + * Enabled by ids and namespaceId. * - * @param ids the ids - * @param enabled the enable + * @param ids the ids + * @param enabled the enabled + * @param namespaceId the namespaceId * @return the result */ - Boolean enabled(List ids, Boolean enabled); + Boolean enabledByIdsAndNamespaceId(List ids, Boolean enabled, String namespaceId); } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ApiServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ApiServiceImpl.java index 29749faf54f4..303f8eff8691 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ApiServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ApiServiceImpl.java @@ -63,6 +63,8 @@ import java.util.Optional; import java.util.stream.Collectors; +import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_ID; + /** * Implementation of the {@link org.apache.shenyu.admin.service.ApiService}. */ @@ -186,7 +188,7 @@ private void removeRegister(final ApiDO apiDO) { } }); if (CollectionUtils.isNotEmpty(selectorIds)) { - selectorService.delete(selectorIds); + selectorService.deleteByNamespaceId(selectorIds, SYS_DEFAULT_NAMESPACE_ID); } //clean metadata Optional.ofNullable(metaDataService.findByPath(path)) diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryServiceImpl.java index 637fe0b0ae94..68da75ad6cfd 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryServiceImpl.java @@ -63,6 +63,8 @@ import java.util.Set; import java.util.stream.Collectors; +import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_ID; + @Service public class DiscoveryServiceImpl implements DiscoveryService { @@ -125,7 +127,7 @@ public void registerDiscoveryConfig(final DiscoveryConfigRegisterDTO discoveryCo private SelectorDO findAndLockOnDB(final String selectorName, final String pluginName) { SelectorDO selectorDO = null; for (int i = 0; i < 3; i++) { - selectorDO = selectorService.findByNameAndPluginNameForUpdate(selectorName, pluginName); + selectorDO = selectorService.findByNameAndPluginNameAndNamespaceIdForUpdate(selectorName, pluginName, SYS_DEFAULT_NAMESPACE_ID); if (selectorDO != null) { return selectorDO; } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespacePluginServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespacePluginServiceImpl.java index 98340657738d..b65fa25e340c 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespacePluginServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespacePluginServiceImpl.java @@ -68,11 +68,11 @@ public NamespacePluginServiceImpl(final NamespacePluginRelMapper namespacePlugin @Override @Transactional(rollbackFor = Exception.class) public String update(final NamespacePluginDTO namespacePluginDTO) { - final NamespacePluginVO before = namespacePluginRelMapper.selectById(namespacePluginDTO.getPluginId(), + final NamespacePluginVO before = namespacePluginRelMapper.selectByPluginIdAndNamespaceId(namespacePluginDTO.getPluginId(), namespacePluginDTO.getNamespaceId()); NamespacePluginRelDO namespacePluginRelDO = NamespacePluginRelDO.buildPluginNsRelDO(namespacePluginDTO); if (namespacePluginRelMapper.updateSelective(namespacePluginRelDO) > 0) { - final NamespacePluginVO now = namespacePluginRelMapper.selectByPluginId(namespacePluginDTO.getPluginId(), namespacePluginDTO.getNamespaceId()); + final NamespacePluginVO now = namespacePluginRelMapper.selectByPluginIdAndNamespaceId(namespacePluginDTO.getPluginId(), namespacePluginDTO.getNamespaceId()); // publish update event. namespacePluginEventPublisher.onUpdated(now, before); } @@ -83,12 +83,12 @@ public String update(final NamespacePluginDTO namespacePluginDTO) { @Transactional(rollbackFor = Exception.class) public String delete(final List ids, final String namespaceId) { // select plugin id. - List namespacePluginVOS = this.namespacePluginRelMapper.selectByIds(ids, namespaceId); + List namespacePluginVOS = this.namespacePluginRelMapper.selectByIdsAndNamespaceId(ids, namespaceId); if (CollectionUtils.isEmpty(namespacePluginVOS)) { return AdminConstants.SYS_PLUGIN_ID_NOT_EXIST; } // delete plugins. - if (this.namespacePluginRelMapper.deleteByIds(ListUtil.map(namespacePluginVOS, NamespacePluginVO::getId), namespaceId) > 0) { + if (this.namespacePluginRelMapper.deleteByIdsAndNamespaceId(ListUtil.map(namespacePluginVOS, NamespacePluginVO::getId), namespaceId) > 0) { // publish deleted event. synchronously delete and link data[selector,rule,condition,resource] namespacePluginEventPublisher.onDeleted(namespacePluginVOS); } @@ -97,7 +97,7 @@ public String delete(final List ids, final String namespaceId) { @Override public NamespacePluginVO findById(final String id, final String namespaceId) { - return this.namespacePluginRelMapper.selectById(id, namespaceId); + return this.namespacePluginRelMapper.selectByPluginIdAndNamespaceId(id, namespaceId); } @Override @@ -107,12 +107,12 @@ public CommonPager listByPage(final NamespacePluginQuery name @Override public List listAll(final String namespaceId) { - return ListUtil.map(namespacePluginRelMapper.selectAll(namespaceId), PluginTransfer.INSTANCE::mapToData); + return ListUtil.map(namespacePluginRelMapper.selectAllByNamespaceId(namespaceId), PluginTransfer.INSTANCE::mapToData); } @Override public List listAll() { - return ListUtil.map(namespacePluginRelMapper.selectAll(), PluginTransfer.INSTANCE::mapToData); + return ListUtil.map(namespacePluginRelMapper.selectAllByNamespaceId(), PluginTransfer.INSTANCE::mapToData); } @Override @@ -121,7 +121,7 @@ public List listAllData(final String namespaceId) { .stream() .collect(Collectors.groupingBy(PluginHandleVO::getPluginId)); - return namespacePluginRelMapper.selectAll(namespaceId) + return namespacePluginRelMapper.selectAllByNamespaceId(namespaceId) .stream() .filter(Objects::nonNull) .peek(namespacePluginVO -> { @@ -138,7 +138,7 @@ public List listAllData(final String namespaceId) { @Override public String enabled(final List ids, final Boolean enabled, final String namespaceId) { - List namespacePluginVOList = namespacePluginRelMapper.selectByIds(ids, namespaceId); + List namespacePluginVOList = namespacePluginRelMapper.selectByIdsAndNamespaceId(ids, namespaceId); if (CollectionUtils.isEmpty(namespacePluginVOList)) { return AdminConstants.SYS_PLUGIN_ID_NOT_EXIST; } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceServiceImpl.java index 499ec9ba4bfd..1608247e9651 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceServiceImpl.java @@ -80,7 +80,7 @@ public CommonPager listByPage(final NamespaceQuery namespaceQuery) @Override public String delete(final List ids) { - if (ids.contains(AdminConstants.SYS_DEFAULT_NAMESPACE_ID)) { + if (ids.contains(AdminConstants.DEFAULT_NAMESPACE_PRIMARY_KEY)) { return AdminConstants.SYS_DEFAULT_NAMESPACE_ID_DELETE; } List namespaceDOS = namespaceMapper.selectByIds(ids); diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java index ba02dba962de..6d294d74d25e 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java @@ -21,6 +21,7 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.shenyu.admin.aspect.annotation.Pageable; +import org.apache.shenyu.admin.mapper.NamespacePluginRelMapper; import org.apache.shenyu.admin.mapper.PluginMapper; import org.apache.shenyu.admin.model.dto.PluginDTO; import org.apache.shenyu.admin.model.dto.PluginHandleDTO; @@ -79,12 +80,16 @@ public class PluginServiceImpl implements PluginService { private final PluginHandleService pluginHandleService; + private final NamespacePluginRelMapper namespacePluginRelMapper; + public PluginServiceImpl(final PluginMapper pluginMapper, final PluginEventPublisher pluginEventPublisher, - final PluginHandleService pluginHandleService) { + final PluginHandleService pluginHandleService, + final NamespacePluginRelMapper namespacePluginRelMapper) { this.pluginMapper = pluginMapper; this.pluginEventPublisher = pluginEventPublisher; this.pluginHandleService = pluginHandleService; + this.namespacePluginRelMapper = namespacePluginRelMapper; } @Override @@ -130,6 +135,12 @@ public String delete(final List ids) { if (CollectionUtils.isEmpty(plugins)) { return AdminConstants.SYS_PLUGIN_ID_NOT_EXIST; } + Optional exist = plugins.stream() + .filter(value -> Objects.nonNull(this.namespacePluginRelMapper.selectByPluginId(value.getId()))) + .findAny(); + if (exist.isPresent()) { + return AdminConstants.NAMESPACE_PLUGIN_NOT_DELETE; + } // delete plugins. if (this.pluginMapper.deleteByIds(ListUtil.map(plugins, PluginDO::getId)) > 0) { // publish deleted event. synchronously delete and link data[selector,rule,condition,resource] diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ResourceServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ResourceServiceImpl.java index 05ec859f2a32..ef5540f09917 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ResourceServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ResourceServiceImpl.java @@ -35,18 +35,17 @@ import org.apache.shenyu.admin.service.ResourceService; import org.apache.shenyu.admin.service.publish.ResourceEventPublisher; import org.apache.shenyu.admin.utils.Assert; -import org.apache.shenyu.common.utils.ListUtil; import org.apache.shenyu.admin.utils.ResourceUtil; import org.apache.shenyu.common.constant.AdminConstants; import org.apache.shenyu.common.enums.AdminResourceEnum; - -import java.util.List; -import java.util.stream.Collectors; - +import org.apache.shenyu.common.utils.ListUtil; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; +import java.util.stream.Collectors; + /** * Implementation of the {@link org.apache.shenyu.admin.service.ResourceService}. */ diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SelectorServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SelectorServiceImpl.java index 8810c816efe4..8d8deec59873 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SelectorServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SelectorServiceImpl.java @@ -45,7 +45,7 @@ import org.apache.shenyu.admin.model.entity.PluginDO; import org.apache.shenyu.admin.model.entity.SelectorConditionDO; import org.apache.shenyu.admin.model.entity.SelectorDO; -import org.apache.shenyu.admin.model.event.plugin.BatchPluginDeletedEvent; +import org.apache.shenyu.admin.model.event.plugin.BatchNamespacePluginDeletedEvent; import org.apache.shenyu.admin.model.page.CommonPager; import org.apache.shenyu.admin.model.page.PageResultUtils; import org.apache.shenyu.admin.model.query.SelectorConditionQuery; @@ -90,6 +90,8 @@ import java.util.TreeSet; import java.util.stream.Collectors; +import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_ID; + /** * Implementation of the {@link org.apache.shenyu.admin.service.SelectorService}. * Maintain {@link SelectorDO} and {@link SelectorConditionDO} related data. @@ -173,10 +175,13 @@ public String registerDefault(final SelectorDTO selectorDTO) { @Override public String registerDefault(final MetaDataRegisterDTO dto, final String pluginName, final String selectorHandler) { String contextPath = ContextPathUtils.buildContextPath(dto.getContextPath(), dto.getAppName()); - SelectorDO selectorDO = findByNameAndPluginName(contextPath, pluginName); + // todo:[To be refactored with namespace] Temporarily hardcode + String namespaceId = StringUtils.defaultIfEmpty(dto.getNamespaceId(), SYS_DEFAULT_NAMESPACE_ID); + SelectorDO selectorDO = findByNameAndPluginNameAndNamespaceId(contextPath, pluginName, namespaceId); if (Objects.isNull(selectorDO)) { SelectorDTO selectorDTO = SelectorUtil.buildSelectorDTO(contextPath, pluginMapper.selectByName(pluginName).getId()); selectorDTO.setHandle(selectorHandler); + selectorDTO.setNamespaceId(namespaceId); return registerDefault(selectorDTO); } return selectorDO.getId(); @@ -210,7 +215,7 @@ public int create(final SelectorDTO selectorDTO) { @Override public int update(final SelectorDTO selectorDTO) { - final SelectorDO before = selectorMapper.selectById(selectorDTO.getId()); + final SelectorDO before = selectorMapper.selectByIdAndNamespaceId(selectorDTO.getId(), selectorDTO.getNamespaceId()); SelectorDO selectorDO = SelectorDO.buildSelectorDO(selectorDTO); final int selectorCount = selectorMapper.updateSelective(selectorDO); @@ -248,7 +253,7 @@ public int update(final SelectorDTO selectorDTO) { @Override public int updateSelective(final SelectorDO selectorDO) { - final SelectorDO before = selectorMapper.selectById(selectorDO.getId()); + final SelectorDO before = selectorMapper.selectByIdAndNamespaceId(selectorDO.getId(), selectorDO.getNamespaceId()); final int updateCount = selectorMapper.updateSelective(selectorDO); if (updateCount > 0) { selectorEventPublisher.onUpdated(selectorDO, before); @@ -257,14 +262,16 @@ public int updateSelective(final SelectorDO selectorDO) { } /** - * delete selectors. + * delete selectors by ids and namespaceId. * * @param ids primary key. + * @param namespaceId namespaceId. * @return rows */ @Override @Transactional(rollbackFor = Exception.class) - public int delete(final List ids) { + public int deleteByNamespaceId(final List ids, final String namespaceId) { + //todo:[Namespace] To be renovated final List selectors = selectorMapper.selectByIdSet(new TreeSet<>(ids)); List pluginDOS = pluginMapper.selectByIds(ListUtil.map(selectors, SelectorDO::getPluginId)); unbindDiscovery(selectors, pluginDOS); @@ -302,15 +309,16 @@ private void unbindDiscovery(final List selectors, final List conditions = ListUtil.map(selectorConditionMapper.selectByQuery(new SelectorConditionQuery(id)), SelectorConditionVO::buildSelectorConditionVO); - SelectorVO selectorVO = SelectorVO.buildSelectorVO(selectorMapper.selectById(id), conditions); + SelectorVO selectorVO = SelectorVO.buildSelectorVO(selectorMapper.selectByIdAndNamespaceId(id, namespaceId), conditions); DiscoveryHandlerDO discoveryHandlerDO = discoveryHandlerMapper.selectBySelectorId(id); if (Objects.nonNull(discoveryHandlerDO)) { selectorVO.setDiscoveryHandler(DiscoveryTransfer.INSTANCE.mapToVo(discoveryHandlerDO)); @@ -328,33 +336,35 @@ public SelectorVO findById(final String id) { } @Override - public SelectorDO findByName(final String name) { - List doList = selectorMapper.selectByName(name); + public SelectorDO findByNameAndNamespaceId(final String name, final String namespaceId) { + List doList = selectorMapper.selectByNameAndNamespaceId(name, namespaceId); return CollectionUtils.isNotEmpty(doList) ? doList.get(0) : null; } @Override - public List findListByName(final String name) { - return selectorMapper.selectByName(name); + public List findListByNameAndNamespaceId(final String name, final String namespaceId) { + return selectorMapper.selectByNameAndNamespaceId(name, namespaceId); } /** - * Find selectorDO by name and plugin name. + * Find selectorDO by name and plugin name and namespaceId. * * @param name the name * @param pluginName the plugin name + * @param namespaceId namespaceId * @return the selector do */ @Override - public SelectorDO findByNameAndPluginName(final String name, final String pluginName) { + public SelectorDO findByNameAndPluginNameAndNamespaceId(final String name, final String pluginName, final String namespaceId) { PluginDO pluginDO = pluginMapper.selectByName(pluginName); - return selectorMapper.findByNameAndPluginId(name, pluginDO.getId()); + return selectorMapper.findByNameAndPluginIdAndNamespaceId(name, pluginDO.getId(), namespaceId); } @Override - public SelectorDO findByNameAndPluginNameForUpdate(final String name, final String pluginName) { + public SelectorDO findByNameAndPluginNameAndNamespaceIdForUpdate(final String name, final String pluginName, final String namespaceId) { PluginDO pluginDO = pluginMapper.selectByNameForUpdate(pluginName); - return selectorMapper.findByNameAndPluginId(name, pluginDO.getId()); + // todo:[To be refactored with namespace] Temporarily hardcode + return selectorMapper.findByNameAndPluginIdAndNamespaceId(name, pluginDO.getId(), namespaceId); } @Override @@ -371,20 +381,21 @@ public List findByNameAndPluginNames(final String name, final List listByPage(final SelectorQuery selectorQuery) { } @Override - public List findByPluginId(final String pluginId) { - return this.buildSelectorDataList(selectorMapper.findByPluginId(pluginId)); + public List findByPluginIdAndNamespaceId(final String pluginId, final String namespaceId) { + return this.buildSelectorDataList(selectorMapper.findByPluginIdAndNamespaceId(pluginId, namespaceId)); } @Override @@ -476,9 +487,9 @@ public ConfigImportResult importData(final List selectorList) { @Override @Transactional(rollbackFor = Exception.class) - public Boolean enabled(final List ids, final Boolean enabled) { + public Boolean enabledByIdsAndNamespaceId(final List ids, final Boolean enabled, final String namespaceId) { ids.forEach(id -> { - SelectorDO selectorDO = selectorMapper.selectById(id); + SelectorDO selectorDO = selectorMapper.selectByIdAndNamespaceId(id, namespaceId); SelectorDO before = JsonUtils.jsonToObject(JsonUtils.toJson(selectorDO), SelectorDO.class); selectorDO.setEnabled(enabled); if (selectorMapper.updateEnable(id, enabled) > 0) { @@ -493,9 +504,9 @@ public Boolean enabled(final List ids, final Boolean enabled) { * * @param event event */ - @EventListener(value = BatchPluginDeletedEvent.class) - public void onPluginDeleted(final BatchPluginDeletedEvent event) { - deleteSelector(selectorMapper.findByPluginIds(event.getDeletedPluginIds()), event.getPlugins()); + @EventListener(value = BatchNamespacePluginDeletedEvent.class) + public void onPluginDeleted(final BatchNamespacePluginDeletedEvent event) { + deleteSelector(selectorMapper.findByPluginIdsAndNamespaceId(event.getDeletedPluginIds(), event.getNamespaceId()), event.getPlugins()); } private void createCondition(final String selectorId, final List selectorConditions) { diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SyncDataServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SyncDataServiceImpl.java index 7f962d8b5530..a9cd1db100bb 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SyncDataServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SyncDataServiceImpl.java @@ -44,7 +44,7 @@ import java.util.List; import java.util.stream.Collectors; -import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_NAMESPACE_ID; +import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_ID; /** * Implementation of the {@link org.apache.shenyu.admin.service.SyncDataService}. @@ -111,7 +111,7 @@ public boolean syncAll(final DataEventTypeEnum type) { List pluginDataList = namespacePluginService.listAll(); //todo:[Namespace] Temporarily only synchronize plugin data for the default namespace - List pluginDataListFilter = pluginDataList.stream().filter(v -> v.getNamespaceId().equals(SYS_DEFAULT_NAMESPACE_NAMESPACE_ID)).collect(Collectors.toList()); + List pluginDataListFilter = pluginDataList.stream().filter(v -> v.getNamespaceId().equals(SYS_DEFAULT_NAMESPACE_ID)).collect(Collectors.toList()); eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.PLUGIN, type, pluginDataListFilter)); List selectorDataList = selectorService.listAll(); @@ -131,7 +131,7 @@ public boolean syncPluginData(final String pluginId, final String namespaceId) { eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.PLUGIN, DataEventTypeEnum.UPDATE, Collections.singletonList(PluginTransfer.INSTANCE.mapToData(namespacePluginVO)))); - List selectorDataList = selectorService.findByPluginId(pluginId); + List selectorDataList = selectorService.findByPluginIdAndNamespaceId(pluginId, namespaceId); if (!CollectionUtils.isEmpty(selectorDataList)) { eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.SELECTOR, DataEventTypeEnum.REFRESH, selectorDataList)); diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java index 5815745299f3..7884c501b0c7 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/UpstreamCheckService.java @@ -76,6 +76,8 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; +import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_ID; + /** * This is the upstream check service. */ @@ -437,7 +439,8 @@ public void fetchUpstreamData() { } Map pluginMap = pluginDOList.stream().filter(Objects::nonNull) .collect(Collectors.toMap(PluginDO::getId, PluginDO::getName, (value1, value2) -> value1)); - final List selectorDOList = selectorMapper.findByPluginIds(new ArrayList<>(pluginMap.keySet())); + // todo:[To be refactored with namespace] Temporarily hardcode + final List selectorDOList = selectorMapper.findByPluginIdsAndNamespaceId(new ArrayList<>(pluginMap.keySet()), SYS_DEFAULT_NAMESPACE_ID); long currentTimeMillis = System.currentTimeMillis(); Optional.ofNullable(selectorDOList).orElseGet(ArrayList::new).stream() .filter(Objects::nonNull) diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/publish/NamespacePluginEventPublisher.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/publish/NamespacePluginEventPublisher.java index 572fb528940c..5b48b1668b17 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/publish/NamespacePluginEventPublisher.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/publish/NamespacePluginEventPublisher.java @@ -18,9 +18,11 @@ package org.apache.shenyu.admin.service.publish; import org.apache.shenyu.admin.listener.DataChangedEvent; +import org.apache.shenyu.admin.model.entity.PluginDO; import org.apache.shenyu.admin.model.enums.EventTypeEnum; import org.apache.shenyu.admin.model.event.AdminDataModelChangedEvent; import org.apache.shenyu.admin.model.event.plugin.BatchNamespacePluginChangedEvent; +import org.apache.shenyu.admin.model.event.plugin.BatchNamespacePluginDeletedEvent; import org.apache.shenyu.admin.model.event.plugin.NamespacePluginChangedEvent; import org.apache.shenyu.admin.model.event.plugin.NamespacePluginCreatedEvent; import org.apache.shenyu.admin.model.vo.NamespacePluginVO; @@ -33,9 +35,12 @@ import java.util.Collection; import java.util.Collections; +import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_ID; + /** * NamespacePluginEventPublisher. */ @@ -77,9 +82,21 @@ public void onDeleted(final NamespacePluginVO namespacePluginVO) { @Override public void onDeleted(final Collection namespacePlugin) { - publish(new BatchNamespacePluginChangedEvent(namespacePlugin, null, EventTypeEnum.PLUGIN_UPDATE, SessionUtil.visitorName())); - publisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.PLUGIN, DataEventTypeEnum.UPDATE, + String namespaceId = ((Collection) namespacePlugin) + .stream() + .map(NamespacePluginVO.class::cast) + .findFirst() + .map(NamespacePluginVO::getNamespaceId) + .orElse(SYS_DEFAULT_NAMESPACE_ID); + List pluginDOList = ((Collection) namespacePlugin) + .stream() + .map(NamespacePluginVO.class::cast) + .map(PluginDO::buildPluginDO) + .collect(Collectors.toList()); + publish(new BatchNamespacePluginDeletedEvent(pluginDOList, SessionUtil.visitorName(), namespaceId)); + publisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.PLUGIN, DataEventTypeEnum.DELETE, namespacePlugin.stream().map(PluginTransfer.INSTANCE::mapToData).collect(Collectors.toList()))); + } /** diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/publish/PluginEventPublisher.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/publish/PluginEventPublisher.java index 35268cabf763..a6590e5bb0e6 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/publish/PluginEventPublisher.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/publish/PluginEventPublisher.java @@ -72,7 +72,7 @@ public void onUpdated(final PluginDO plugin, final PluginDO before) { public void onDeleted(final PluginDO plugin) { publish(new PluginChangedEvent(plugin, null, EventTypeEnum.PLUGIN_DELETE, SessionUtil.visitorName())); } - + /** * on plugin deleted. * @@ -82,7 +82,7 @@ public void onDeleted(final PluginDO plugin) { public void onDeleted(final Collection plugins) { publish(new BatchPluginDeletedEvent(plugins, SessionUtil.visitorName())); } - + /** * on plugin batch enabled. * diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/AbstractShenyuClientRegisterServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/AbstractShenyuClientRegisterServiceImpl.java index 1320972160bd..01baa4aee2ae 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/AbstractShenyuClientRegisterServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/AbstractShenyuClientRegisterServiceImpl.java @@ -58,6 +58,8 @@ import java.util.Objects; import java.util.stream.Collectors; +import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_ID; + /** * Abstract strategy. */ @@ -171,7 +173,8 @@ public String doRegisterURI(final String selectorName, final List buildDivideUpstreamList(final List public String offline(final String selectorName, final List uriList) { final SelectorService selectorService = getSelectorService(); String pluginName = PluginNameAdapter.rpcTypeAdapter(rpcType()); - SelectorDO selectorDO = selectorService.findByNameAndPluginName(selectorName, pluginName); + // todo:[To be refactored with namespace] Temporarily hardcode + SelectorDO selectorDO = selectorService.findByNameAndPluginNameAndNamespaceId(selectorName, pluginName, SYS_DEFAULT_NAMESPACE_ID); if (Objects.isNull(selectorDO)) { return Constants.SUCCESS; } @@ -133,7 +136,7 @@ public String offline(final String selectorName, final List uriL existList.removeAll(needToRemove); final String handler = GsonUtils.getInstance().toJson(existList); selectorDO.setHandle(handler); - SelectorData selectorData = selectorService.buildByName(selectorName, PluginNameAdapter.rpcTypeAdapter(rpcType())); + SelectorData selectorData = selectorService.buildByNameAndPluginNameAndNamespaceId(selectorName, PluginNameAdapter.rpcTypeAdapter(rpcType()), SYS_DEFAULT_NAMESPACE_ID); selectorData.setHandle(handler); // update db selectorService.updateSelective(selectorDO); diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterGrpcServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterGrpcServiceImpl.java index 17a8772544a9..eaf5f18513c9 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterGrpcServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterGrpcServiceImpl.java @@ -46,6 +46,8 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.stream.Collectors; +import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_ID; + /** * grpc service register. */ @@ -124,7 +126,8 @@ private List buildGrpcUpstreamList(final List uriL public String offline(final String selectorName, final List offlineList) { String pluginName = PluginNameAdapter.rpcTypeAdapter(rpcType()); SelectorService selectorService = getSelectorService(); - SelectorDO selectorDO = selectorService.findByNameAndPluginName(selectorName, pluginName); + // todo:[To be refactored with namespace] Temporarily hardcode + SelectorDO selectorDO = selectorService.findByNameAndPluginNameAndNamespaceId(selectorName, pluginName, SYS_DEFAULT_NAMESPACE_ID); if (Objects.isNull(selectorDO)) { return Constants.SUCCESS; } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterWebSocketServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterWebSocketServiceImpl.java index dd5df3dbb415..8c8ee8593d1e 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterWebSocketServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/ShenyuClientRegisterWebSocketServiceImpl.java @@ -44,6 +44,8 @@ import org.apache.shenyu.register.common.dto.URIRegisterDTO; import org.springframework.stereotype.Service; +import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_ID; + /** * spring mvc websocket service register. */ @@ -105,7 +107,8 @@ private List buildWebSocketUpstreamList(final List offlineList) { String pluginName = PluginNameAdapter.rpcTypeAdapter(rpcType()); SelectorService selectorService = getSelectorService(); - SelectorDO selectorDO = selectorService.findByNameAndPluginName(selectorName, pluginName); + // todo:[To be refactored with namespace] Temporarily hardcode + SelectorDO selectorDO = selectorService.findByNameAndPluginNameAndNamespaceId(selectorName, pluginName, SYS_DEFAULT_NAMESPACE_ID); if (Objects.isNull(selectorDO)) { return Constants.SUCCESS; } diff --git a/shenyu-admin/src/main/resources/mappers/NamespacePluginRelMapper.xml b/shenyu-admin/src/main/resources/mappers/NamespacePluginRelMapper.xml index 39895c5db02a..035326420578 100644 --- a/shenyu-admin/src/main/resources/mappers/NamespacePluginRelMapper.xml +++ b/shenyu-admin/src/main/resources/mappers/NamespacePluginRelMapper.xml @@ -110,7 +110,7 @@ pnr.id ASC - + - + DELETE FROM plugin_ns_rel WHERE id IN @@ -257,7 +256,7 @@ and namespace_id=#{namespaceId, jdbcType=VARCHAR} - SELECT pnr.id as id, pnr.namespace_id AS namespaceId, diff --git a/shenyu-admin/src/main/resources/mappers/selector-sqlmap.xml b/shenyu-admin/src/main/resources/mappers/selector-sqlmap.xml index 4e0f3d259215..cd3c29912c25 100644 --- a/shenyu-admin/src/main/resources/mappers/selector-sqlmap.xml +++ b/shenyu-admin/src/main/resources/mappers/selector-sqlmap.xml @@ -32,6 +32,7 @@ + @@ -47,7 +48,8 @@ loged, continued, match_restful, - handle + handle, + namespace_id @@ -63,16 +65,26 @@ ${alis}.loged, ${alis}.continued, ${alis}.match_restful, - ${alis}.handle + ${alis}.handle, + ${alis}.namespace_id - SELECT FROM selector - WHERE id = #{id, jdbcType=VARCHAR} + WHERE id = #{id, jdbcType=VARCHAR} + AND namespace_id = #{namespaceId, jdbcType=VARCHAR} + + + + - SELECT FROM selector WHERE name = #{name, jdbcType=VARCHAR} + AND namespace_id = #{namespaceId, jdbcType=VARCHAR} - SELECT FROM selector WHERE name = #{name, jdbcType=VARCHAR} AND plugin_id = #{pluginId, jdbcType=VARCHAR} + AND namespace_id = #{namespaceId, jdbcType=VARCHAR} + - SELECT FROM selector @@ -142,6 +157,7 @@ #{id, jdbcType=VARCHAR} + AND namespace_id = #{namespaceId, jdbcType=VARCHAR} @@ -243,7 +265,8 @@ loged, continued, match_restful, - handle) + handle, + namespace_id) VALUES (#{id, jdbcType=VARCHAR}, #{dateCreated, jdbcType=TIMESTAMP}, @@ -257,7 +280,8 @@ #{loged, jdbcType=TINYINT}, #{continued, jdbcType=TINYINT}, #{matchRestful, jdbcType=TINYINT}, - #{handle, jdbcType=VARCHAR}) + #{handle, jdbcType=VARCHAR}, + #{namespaceId, jdbcType=VARCHAR}) @@ -300,6 +324,9 @@ handle, + + namespace_id, + #{id, jdbcType=VARCHAR}, @@ -339,6 +366,9 @@ #{handle, jdbcType=VARCHAR}, + + #{namespaceId, jdbcType=VARCHAR}, + @@ -400,7 +430,8 @@ handle = #{handle, jdbcType=VARCHAR}, - WHERE id = #{id, jdbcType=VARCHAR} + WHERE id = #{id, jdbcType=VARCHAR} + AND namespace_id = #{namespaceId, jdbcType=VARCHAR} diff --git a/shenyu-admin/src/main/resources/sql-script/h2/schema.sql b/shenyu-admin/src/main/resources/sql-script/h2/schema.sql index 4bf8330f6f42..3070489cee29 100755 --- a/shenyu-admin/src/main/resources/sql-script/h2/schema.sql +++ b/shenyu-admin/src/main/resources/sql-script/h2/schema.sql @@ -1341,3 +1341,5 @@ INSERT IGNORE INTO `permission` (`id`, `object_id`, `resource_id`, `date_created INSERT IGNORE INTO `permission` (`id`, `object_id`, `resource_id`, `date_created`, `date_updated`) VALUES ('1792779493541343265', '1346358560427216896', '1792749362445840484', '2024-06-25 20:00:00.000', '2024-06-25 20:00:00.000'); INSERT IGNORE INTO `permission` (`id`, `object_id`, `resource_id`, `date_created`, `date_updated`) VALUES ('1792779493541343266', '1346358560427216896', '1792749362445840485', '2024-06-25 20:00:00.000', '2024-06-25 20:00:00.000'); INSERT IGNORE INTO `permission` (`id`, `object_id`, `resource_id`, `date_created`, `date_updated`) VALUES ('1792779493541343267', '1346358560427216896', '1792749362445840486', '2024-06-25 20:00:00.000', '2024-06-25 20:00:00.000'); + +ALTER TABLE `selector` ADD COLUMN `namespace_id` varchar(50) NULL COMMENT 'namespaceId' AFTER `match_restful`; \ No newline at end of file diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/controller/SelectorControllerTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/controller/SelectorControllerTest.java index 99a3468481d6..cd785714ce48 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/controller/SelectorControllerTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/controller/SelectorControllerTest.java @@ -18,10 +18,12 @@ package org.apache.shenyu.admin.controller; import org.apache.shenyu.admin.exception.ExceptionHandlers; +import org.apache.shenyu.admin.mapper.NamespaceMapper; import org.apache.shenyu.admin.mapper.PluginMapper; import org.apache.shenyu.admin.mapper.SelectorMapper; import org.apache.shenyu.admin.model.custom.UserInfo; import org.apache.shenyu.admin.model.dto.BatchCommonDTO; +import org.apache.shenyu.admin.model.dto.BatchNamespaceCommonDTO; import org.apache.shenyu.admin.model.dto.SelectorDTO; import org.apache.shenyu.admin.model.page.CommonPager; import org.apache.shenyu.admin.model.page.PageParameter; @@ -52,6 +54,7 @@ import java.util.Arrays; import java.util.Collections; +import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_ID; import static org.hamcrest.core.Is.is; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; @@ -81,10 +84,14 @@ public final class SelectorControllerTest { @Mock private PluginMapper pluginMapper; + @Mock + private NamespaceMapper namespaceMapper; + private final SelectorVO selectorVO = new SelectorVO("1", "2", "selector-1", MatchModeEnum.AND.getCode(), MatchModeEnum.AND.getName(), SelectorTypeEnum.FULL_FLOW.getCode(), SelectorTypeEnum.FULL_FLOW.getName(), 1, true, true, true, false, "handle", Collections.emptyList(), - DateUtils.localDateTimeToString(LocalDateTime.now()), DateUtils.localDateTimeToString(LocalDateTime.now())); + DateUtils.localDateTimeToString(LocalDateTime.now()), DateUtils.localDateTimeToString(LocalDateTime.now()), + SYS_DEFAULT_NAMESPACE_ID); private final CommonPager commonPager = new CommonPager<>(new PageParameter(), Collections.singletonList(selectorVO)); @@ -123,16 +130,19 @@ public void createSelector() throws Exception { .matchRestful(false) .pluginId("2") .sort(1) + .namespaceId(SYS_DEFAULT_NAMESPACE_ID) .build(); SpringBeanUtils.getInstance().setApplicationContext(mock(ConfigurableApplicationContext.class)); when(SpringBeanUtils.getInstance().getBean(SelectorMapper.class)).thenReturn(selectorMapper); when(selectorMapper.existed(selectorDTO.getId())).thenReturn(true); when(SpringBeanUtils.getInstance().getBean(PluginMapper.class)).thenReturn(pluginMapper); when(pluginMapper.existed(selectorDTO.getPluginId())).thenReturn(true); + when(SpringBeanUtils.getInstance().getBean(NamespaceMapper.class)).thenReturn(namespaceMapper); + when(namespaceMapper.existed(SYS_DEFAULT_NAMESPACE_ID)).thenReturn(true); given(this.selectorService.createOrUpdate(selectorDTO)).willReturn(1); this.mockMvc.perform(MockMvcRequestBuilders.post("/selector") - .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtils.getInstance().toJson(selectorDTO))) + .contentType(MediaType.APPLICATION_JSON) + .content(GsonUtils.getInstance().toJson(selectorDTO))) .andExpect(status().isOk()) .andExpect(jsonPath("$.message", is(ShenyuResultMessage.CREATE_SUCCESS))) .andReturn(); @@ -150,16 +160,19 @@ public void updateSelector() throws Exception { .matchRestful(false) .pluginId("2") .sort(1) + .namespaceId(SYS_DEFAULT_NAMESPACE_ID) .build(); SpringBeanUtils.getInstance().setApplicationContext(mock(ConfigurableApplicationContext.class)); when(SpringBeanUtils.getInstance().getBean(SelectorMapper.class)).thenReturn(selectorMapper); when(selectorMapper.existed(selectorDTO.getId())).thenReturn(true); when(SpringBeanUtils.getInstance().getBean(PluginMapper.class)).thenReturn(pluginMapper); when(pluginMapper.existed(selectorDTO.getPluginId())).thenReturn(true); + when(SpringBeanUtils.getInstance().getBean(NamespaceMapper.class)).thenReturn(namespaceMapper); + when(namespaceMapper.existed(SYS_DEFAULT_NAMESPACE_ID)).thenReturn(true); given(this.selectorService.createOrUpdate(selectorDTO)).willReturn(1); this.mockMvc.perform(MockMvcRequestBuilders.put("/selector/{id}", "123") - .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtils.getInstance().toJson(selectorDTO))) + .contentType(MediaType.APPLICATION_JSON) + .content(GsonUtils.getInstance().toJson(selectorDTO))) .andExpect(status().isOk()) .andExpect(jsonPath("$.message", is(ShenyuResultMessage.UPDATE_SUCCESS))) .andReturn(); @@ -167,10 +180,16 @@ public void updateSelector() throws Exception { @Test public void deleteSelector() throws Exception { - given(this.selectorService.delete(Collections.singletonList("123"))).willReturn(1); + SpringBeanUtils.getInstance().setApplicationContext(mock(ConfigurableApplicationContext.class)); + when(SpringBeanUtils.getInstance().getBean(NamespaceMapper.class)).thenReturn(namespaceMapper); + when(namespaceMapper.existed(SYS_DEFAULT_NAMESPACE_ID)).thenReturn(true); + given(this.selectorService.deleteByNamespaceId(Collections.singletonList("123"), SYS_DEFAULT_NAMESPACE_ID)).willReturn(1); + final BatchNamespaceCommonDTO batchNamespaceCommonDTO = new BatchNamespaceCommonDTO(); + batchNamespaceCommonDTO.setNamespaceId(SYS_DEFAULT_NAMESPACE_ID); + batchNamespaceCommonDTO.setIds(Collections.singletonList("123")); this.mockMvc.perform(MockMvcRequestBuilders.delete("/selector/batch") - .contentType(MediaType.APPLICATION_JSON) - .content("[\"123\"]")) + .contentType(MediaType.APPLICATION_JSON) + .content(GsonUtils.getInstance().toJson(batchNamespaceCommonDTO))) .andExpect(status().isOk()) .andExpect(jsonPath("$.message", is(ShenyuResultMessage.DELETE_SUCCESS))) .andReturn(); @@ -178,8 +197,13 @@ public void deleteSelector() throws Exception { @Test public void detailSelector() throws Exception { - given(this.selectorService.findById("1")).willReturn(selectorVO); - this.mockMvc.perform(MockMvcRequestBuilders.get("/selector/{id}", "1")) + SpringBeanUtils.getInstance().setApplicationContext(mock(ConfigurableApplicationContext.class)); + + when(SpringBeanUtils.getInstance().getBean(NamespaceMapper.class)).thenReturn(namespaceMapper); + when(namespaceMapper.existed(SYS_DEFAULT_NAMESPACE_ID)).thenReturn(true); + given(this.selectorService.findByIdAndNamespaceId("1", SYS_DEFAULT_NAMESPACE_ID)).willReturn(selectorVO); + + this.mockMvc.perform(MockMvcRequestBuilders.get("/selector/{id}/{namespaceId}", "1", SYS_DEFAULT_NAMESPACE_ID)) .andExpect(status().isOk()) .andExpect(jsonPath("$.message", is(ShenyuResultMessage.DETAIL_SUCCESS))) .andExpect(jsonPath("$.data.id", is(selectorVO.getId()))) @@ -189,30 +213,32 @@ public void detailSelector() throws Exception { @Test public void enableSelector() throws Exception { SelectorDTO selectorDTO = SelectorDTO.builder() - .id("123") - .name("test123") - .continued(true) - .type(1) - .loged(true) - .enabled(true) - .matchRestful(false) - .pluginId("2") - .sort(1) - .build(); + .id("123") + .name("test123") + .continued(true) + .type(1) + .loged(true) + .enabled(true) + .matchRestful(false) + .pluginId("2") + .sort(1) + .namespaceId(SYS_DEFAULT_NAMESPACE_ID) + .build(); SpringBeanUtils.getInstance().setApplicationContext(mock(ConfigurableApplicationContext.class)); when(SpringBeanUtils.getInstance().getBean(SelectorMapper.class)).thenReturn(selectorMapper); when(selectorMapper.existed(selectorDTO.getId())).thenReturn(true); when(SpringBeanUtils.getInstance().getBean(PluginMapper.class)).thenReturn(pluginMapper); when(pluginMapper.existed(selectorDTO.getPluginId())).thenReturn(true); - given(this.selectorService.enabled(Arrays.asList(selectorDTO.getId()), false)).willReturn(true); + given(this.selectorService.enabledByIdsAndNamespaceId(Arrays.asList(selectorDTO.getId()), false, SYS_DEFAULT_NAMESPACE_ID)).willReturn(true); BatchCommonDTO batchCommonDTO = new BatchCommonDTO(); batchCommonDTO.setIds(Arrays.asList(selectorDTO.getId())); batchCommonDTO.setEnabled(false); + batchCommonDTO.setNamespaceId(SYS_DEFAULT_NAMESPACE_ID); this.mockMvc.perform(MockMvcRequestBuilders.post("/selector/batchEnabled") - .contentType(MediaType.APPLICATION_JSON) - .content(GsonUtils.getInstance().toJson(batchCommonDTO))) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.message", is(ShenyuResultMessage.ENABLE_SUCCESS))) - .andReturn(); + .contentType(MediaType.APPLICATION_JSON) + .content(GsonUtils.getInstance().toJson(batchCommonDTO))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.message", is(ShenyuResultMessage.ENABLE_SUCCESS))) + .andReturn(); } } diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/mapper/SelectorMapperTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/mapper/SelectorMapperTest.java index 830c0c73f625..6478db06b318 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/mapper/SelectorMapperTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/mapper/SelectorMapperTest.java @@ -17,6 +17,7 @@ package org.apache.shenyu.admin.mapper; +import jakarta.annotation.Resource; import org.apache.shenyu.admin.AbstractSpringIntegrationTest; import org.apache.shenyu.admin.model.entity.SelectorDO; import org.apache.shenyu.admin.model.page.PageParameter; @@ -24,17 +25,17 @@ import org.apache.shenyu.common.utils.UUIDUtils; import org.junit.jupiter.api.Test; -import jakarta.annotation.Resource; import java.sql.Timestamp; import java.util.List; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_ID; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasItems; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.hamcrest.MatcherAssert.assertThat; /** @@ -51,7 +52,7 @@ public void testSelectById() { int insert = selectorMapper.insert(selectorDO); assertEquals(1, insert); - SelectorDO selector = selectorMapper.selectById(selectorDO.getId()); + SelectorDO selector = selectorMapper.selectByIdAndNamespaceId(selectorDO.getId(), SYS_DEFAULT_NAMESPACE_ID); assertNotNull(selector); assertEquals(selectorDO.getId(), selector.getId()); assertEquals(selectorDO.getContinued(), selector.getContinued()); @@ -100,7 +101,7 @@ public void testFindByPluginId() { int insert = selectorMapper.insert(selectorDO); assertEquals(1, insert); - List list = selectorMapper.findByPluginId(selectorDO.getPluginId()); + List list = selectorMapper.findByPluginIdAndNamespaceId(selectorDO.getPluginId(), selectorDO.getNamespaceId()); assertNotNull(list); assertEquals(list.size(), 1); assertNotNull(selectorDO.getPluginId(), list.get(0).getPluginId()); @@ -114,7 +115,7 @@ public void testSelectByName() { SelectorDO selectorDO = buildSelectorDO(); int insert = selectorMapper.insert(selectorDO); assertEquals(1, insert); - List doList = selectorMapper.selectByName(selectorDO.getName()); + List doList = selectorMapper.selectByNameAndNamespaceId(selectorDO.getName(), SYS_DEFAULT_NAMESPACE_ID); assertEquals(doList.size(), 1); assertNotNull(doList.get(0)); assertEquals(selectorDO.getName(), doList.get(0).getName()); @@ -237,6 +238,7 @@ private SelectorDO buildSelectorDO() { .matchRestful(false) .continued(Boolean.TRUE) .handle("handle") + .namespaceId(SYS_DEFAULT_NAMESPACE_ID) .build(); } } diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/plugin/BatchPluginDeletedEventTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/plugin/BatchNamespacePluginDeletedEventTest.java similarity index 88% rename from shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/plugin/BatchPluginDeletedEventTest.java rename to shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/plugin/BatchNamespacePluginDeletedEventTest.java index f2def452e9ac..c4b097e00a81 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/plugin/BatchPluginDeletedEventTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/plugin/BatchNamespacePluginDeletedEventTest.java @@ -26,15 +26,15 @@ import org.junit.jupiter.api.Test; /** - * test cast for {@link BatchPluginDeletedEvent}. + * test cast for {@link BatchNamespacePluginDeletedEvent}. */ -public class BatchPluginDeletedEventTest { +public class BatchNamespacePluginDeletedEventTest { private PluginDO one; private PluginDO two; - private BatchPluginDeletedEvent deletedEvent; + private BatchNamespacePluginDeletedEvent deletedEvent; @BeforeEach public void setUp() { @@ -60,7 +60,7 @@ public void setUp() { .dateCreated(now) .build(); - deletedEvent = new BatchPluginDeletedEvent(Arrays.asList(one, two), "test-operator"); + deletedEvent = new BatchNamespacePluginDeletedEvent(Arrays.asList(one, two), "test-operator", "namespaceId"); } @Test diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/selector/SelectorChangedEventTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/selector/SelectorChangedEventTest.java index 96b964838ff1..b8d2f5b90110 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/selector/SelectorChangedEventTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/selector/SelectorChangedEventTest.java @@ -50,7 +50,7 @@ void buildContextAndBeforeIsNull() { = new SelectorChangedEvent(before, null, EventTypeEnum.SELECTOR_CREATE, "test-operator"); SelectorDO after = (SelectorDO) selectorChangedEvent.getAfter(); - String expectMsg = String.format("the selector [%s] is %s", after.getName(), StringUtils.lowerCase(selectorChangedEvent.getType().getType().toString())); + String expectMsg = String.format("the namespace [%s] selector [%s] is %s", after.getNamespaceId(), after.getName(), StringUtils.lowerCase(selectorChangedEvent.getType().getType().toString())); String actualMsg = selectorChangedEvent.buildContext(); @@ -64,7 +64,8 @@ void buildContextAndBeforeNotNullAndNoChange() { String changeMsg = "it no change"; SelectorDO after = (SelectorDO) selectorChangedEvent.getAfter(); - String expectMsg = String.format("the selector [%s] is %s : %s", after.getName(), StringUtils.lowerCase(selectorChangedEvent.getType().getType().toString()), changeMsg); + String expectMsg = String.format("the namespace [%s] selector [%s] is %s : %s", after.getNamespaceId(), after.getName(), + StringUtils.lowerCase(selectorChangedEvent.getType().getType().toString()), changeMsg); String actualMsg = selectorChangedEvent.buildContext(); @@ -88,7 +89,8 @@ void buildContextAndBeforeNotNullAndAllChange() { builder.append(String.format("loged[%s => %s] ", before.getLoged(), after.getLoged())); String changeMsg = builder.toString(); - String expectMsg = String.format("the selector [%s] is %s : %s", after.getName(), StringUtils.lowerCase(selectorChangedEvent.getType().getType().toString()), changeMsg); + String expectMsg = String.format("the namespace [%s] selector [%s] is %s : %s", after.getNamespaceId(), after.getName(), + StringUtils.lowerCase(selectorChangedEvent.getType().getType().toString()), changeMsg); String actualMsg = selectorChangedEvent.buildContext(); diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/DataPermissionServiceTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/DataPermissionServiceTest.java index e3cf415e795d..f35edf975158 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/DataPermissionServiceTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/DataPermissionServiceTest.java @@ -84,7 +84,7 @@ public void setup() { dataPermissionDTO.setUserId("1001"); dataPermissionDTO.setDataId("1"); dataPermissionDTO.setIsSelected(true); - selectorDTO = new SelectorDTO("1", "1", "test", 1, 1, 1, true, false, true, "test", null, false, null); + selectorDTO = new SelectorDTO("1", "1", "test", 1, 1, 1, true, false, true, "test", null, false, null, null); } @Test diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/PluginServiceTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/PluginServiceTest.java index 3b3876475589..1006e03fa2af 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/PluginServiceTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/PluginServiceTest.java @@ -18,6 +18,7 @@ package org.apache.shenyu.admin.service; import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.mapper.NamespacePluginRelMapper; import org.apache.shenyu.admin.mapper.PluginMapper; import org.apache.shenyu.admin.mapper.SelectorMapper; import org.apache.shenyu.admin.model.dto.BatchCommonDTO; @@ -53,6 +54,7 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; +import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_ID; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -84,9 +86,12 @@ public final class PluginServiceTest { @Mock private PluginHandleService pluginHandleService; + @Mock + private NamespacePluginRelMapper namespacePluginRelMapper; + @BeforeEach public void setUp() { - pluginService = new PluginServiceImpl(pluginMapper, modelDataEventPublisher, pluginHandleService); + pluginService = new PluginServiceImpl(pluginMapper, modelDataEventPublisher, pluginHandleService, namespacePluginRelMapper); } @Test @@ -104,7 +109,7 @@ public void testDelete() { final List selectorDOList = new ArrayList<>(); selectorDOList.add(SelectorDO.builder().id("101").build()); - when(selectorMapper.findByPluginIds(Collections.singletonList("101"))).thenReturn(selectorDOList); + when(selectorMapper.findByPluginIdsAndNamespaceId(Collections.singletonList("101"), SYS_DEFAULT_NAMESPACE_ID)).thenReturn(selectorDOList); assertEquals(StringUtils.EMPTY, pluginService.delete(Collections.singletonList("123"))); } diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/RuleServiceTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/RuleServiceTest.java index 531cb47825b7..e8d76fe9fb50 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/RuleServiceTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/RuleServiceTest.java @@ -64,6 +64,7 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; +import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_ID; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.greaterThan; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -201,7 +202,7 @@ public void testListAllWithSelectorNull() { } private void mockFindSelectorIsNull() { - given(this.selectorMapper.selectById("456")).willReturn(null); + given(this.selectorMapper.selectByIdAndNamespaceId("456", SYS_DEFAULT_NAMESPACE_ID)).willReturn(null); given(this.pluginMapper.selectById("789")).willReturn(buildPluginDO()); } @@ -212,7 +213,7 @@ public void testListAllWithPluginNull() { } private void mockFindPluginIsNull() { - given(this.selectorMapper.selectById("456")).willReturn(buildSelectorDO()); + given(this.selectorMapper.selectByIdAndNamespaceId("456", SYS_DEFAULT_NAMESPACE_ID)).willReturn(buildSelectorDO()); given(this.pluginMapper.selectById("789")).willReturn(null); } @@ -259,7 +260,7 @@ public void testFindBySelectorIdList() { private void publishEvent() { PluginDO pluginDO = buildPluginDO(); SelectorDO selectorDO = buildSelectorDO(); - given(this.selectorMapper.selectById("456")).willReturn(selectorDO); + given(this.selectorMapper.selectByIdAndNamespaceId("456", SYS_DEFAULT_NAMESPACE_ID)).willReturn(selectorDO); given(this.pluginMapper.selectById("789")).willReturn(pluginDO); given(this.selectorMapper.selectByIdSet(Sets.newHashSet("456"))).willReturn(Collections.singletonList(selectorDO)); given(this.pluginMapper.selectByIds(Lists.newArrayList("789"))).willReturn(Collections.singletonList(pluginDO)); diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/SelectorServiceTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/SelectorServiceTest.java index 94ec5b0abcf1..b22d87ac806f 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/SelectorServiceTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/SelectorServiceTest.java @@ -72,6 +72,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_ID; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.notNullValue; @@ -186,19 +187,19 @@ public void testDelete() { // mock for test for-each statement. // RuleDO mockedRuleDo = mock(RuleDO.class); -// when(ruleMapper.deleteByIds(Collections.singletonList(mockedRuleDo.getId()))).thenReturn(1); +// when(ruleMapper.deleteByIdsAndNamespaceId(Collections.singletonList(mockedRuleDo.getId()))).thenReturn(1); // when(ruleConditionMapper.deleteByRuleIds(Collections.singletonList(mockedRuleDo.getId()))).thenReturn(1); final List ids = Collections.singletonList(correctId); given(selectorMapper.deleteByIds(ids)).willReturn(ids.size()); - assertEquals(selectorService.delete(ids), ids.size()); + assertEquals(selectorService.deleteByNamespaceId(ids, any()), ids.size()); } @Test public void testFindById() { SelectorDO selectorDO = buildSelectorDO(); - given(this.selectorMapper.selectById(eq("123"))).willReturn(selectorDO); - SelectorVO selectorVO = this.selectorService.findById("123"); + given(this.selectorMapper.selectByIdAndNamespaceId(eq("123"), any())).willReturn(selectorDO); + SelectorVO selectorVO = this.selectorService.findByIdAndNamespaceId("123", any()); assertNotNull(selectorDO); assertEquals(selectorVO.getId(), selectorDO.getId()); @@ -209,8 +210,8 @@ public void testFindById() { @Test public void testFindByName() { List selectorDO1List = Collections.singletonList(buildSelectorDO()); - given(this.selectorMapper.selectByName(eq("kuan"))).willReturn(selectorDO1List); - SelectorDO selectorDO2 = this.selectorService.findByName("kuan"); + given(this.selectorMapper.selectByNameAndNamespaceId(eq("kuan"), eq(SYS_DEFAULT_NAMESPACE_ID))).willReturn(selectorDO1List); + SelectorDO selectorDO2 = this.selectorService.findByNameAndNamespaceId("kuan", SYS_DEFAULT_NAMESPACE_ID); assertNotNull(selectorDO2); assertEquals(selectorDO1List.size(), 1); assertEquals(selectorDO1List.get(0).getId(), selectorDO2.getId()); @@ -229,7 +230,7 @@ public void testListByPage() { @Test public void testFindByPluginId() { - List res = this.selectorService.findByPluginId("789"); + List res = this.selectorService.findByPluginIdAndNamespaceId("789", SYS_DEFAULT_NAMESPACE_ID); res.forEach(selectorData -> assertEquals("789", selectorData.getPluginId())); } diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/SyncDataServiceTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/SyncDataServiceTest.java index b8c1ff021f94..dc33e858e53a 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/SyncDataServiceTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/SyncDataServiceTest.java @@ -39,6 +39,7 @@ import java.time.LocalDateTime; import java.util.Collections; +import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_ID; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.greaterThan; import static org.mockito.BDDMockito.given; @@ -107,12 +108,11 @@ public void syncAllTest() { public void syncPluginDataTest() { PluginVO pluginVO = buildPluginVO(); NamespacePluginVO namespacePluginVO = new NamespacePluginVO(); - String namespaceId = "test1"; - given(this.namespacePluginService.findById(pluginVO.getId(), namespaceId)).willReturn(namespacePluginVO); + given(this.namespacePluginService.findById(pluginVO.getId(), SYS_DEFAULT_NAMESPACE_ID)).willReturn(namespacePluginVO); SelectorData selectorData = buildSelectorData(); - given(this.selectorService.findByPluginId(pluginVO.getId())).willReturn(Collections.singletonList(selectorData)); + given(this.selectorService.findByPluginIdAndNamespaceId(pluginVO.getId(), SYS_DEFAULT_NAMESPACE_ID)).willReturn(Collections.singletonList(selectorData)); - assertThat(syncDataService.syncPluginData(pluginVO.getId(), namespaceId), greaterThan(false)); + assertThat(syncDataService.syncPluginData(pluginVO.getId(), SYS_DEFAULT_NAMESPACE_ID), greaterThan(false)); } diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/UpstreamCheckServiceTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/UpstreamCheckServiceTest.java index 01bb6f04afeb..72049fa97577 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/UpstreamCheckServiceTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/UpstreamCheckServiceTest.java @@ -65,6 +65,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mockStatic; @@ -257,7 +258,7 @@ public void testFetchUpstreamData() { .status(0) .build(); when(pluginMapper.selectByNames(anyList())).thenReturn(Lists.newArrayList(pluginDO)); - when(selectorMapper.findByPluginIds(anyList())).thenReturn(Lists.newArrayList(selectorDOWithUrlError, selectorDOWithUrlReachable)); + when(selectorMapper.findByPluginIdsAndNamespaceId(anyList(), any())).thenReturn(Lists.newArrayList(selectorDOWithUrlError, selectorDOWithUrlReachable)); when(discoveryUpstreamService.findBySelectorId(anyString())).thenReturn(Lists.newArrayList(discoveryUpstreamData)); upstreamCheckService.fetchUpstreamData(); assertTrue(upstreamMap.containsKey(MOCK_SELECTOR_NAME)); diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/manager/impl/LoadServiceDocEntryImplTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/manager/impl/LoadServiceDocEntryImplTest.java index f17d9d2163dc..bd106d353b56 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/manager/impl/LoadServiceDocEntryImplTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/manager/impl/LoadServiceDocEntryImplTest.java @@ -48,6 +48,7 @@ import java.util.Set; import java.util.Collections; +import static org.apache.shenyu.common.constant.AdminConstants.SYS_DEFAULT_NAMESPACE_ID; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import static org.mockito.Mockito.verify; @@ -112,7 +113,8 @@ public void testLoadApiDocument() { "[{\"weight\":1}]", new ArrayList<>(), formattedDateString, - formattedDateString + formattedDateString, + SYS_DEFAULT_NAMESPACE_ID ); list.add(selectorVO); commonPager.setDataList(list); diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/register/AbstractShenyuClientRegisterServiceImplTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/register/AbstractShenyuClientRegisterServiceImplTest.java index e9f23ca524dc..0dea64a0a83f 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/register/AbstractShenyuClientRegisterServiceImplTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/register/AbstractShenyuClientRegisterServiceImplTest.java @@ -144,9 +144,9 @@ public void testDoRegisterURI() { assertThrows(ShenyuException.class, () -> abstractShenyuClientRegisterService.doRegisterURI("Selector_Name", Collections.singletonList(uriRegisterDTO))); SelectorDO selectorDO = mock(SelectorDO.class); - when(selectorService.findByNameAndPluginName(any(), any())).thenReturn(selectorDO); + when(selectorService.findByNameAndPluginNameAndNamespaceId(any(), any(), any())).thenReturn(selectorDO); SelectorData selectorData = new SelectorData(); - when(selectorService.buildByName(any(), any())).thenReturn(selectorData); + when(selectorService.buildByNameAndPluginNameAndNamespaceId(any(), any(), any())).thenReturn(selectorData); assertEquals(ShenyuResultMessage.SUCCESS, abstractShenyuClientRegisterService.doRegisterURI("Selector_Name", Collections.singletonList(uriRegisterDTO))); } diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/AdminConstants.java b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/AdminConstants.java index 3f672df0dba9..e3389226cb3d 100644 --- a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/AdminConstants.java +++ b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/AdminConstants.java @@ -62,20 +62,25 @@ public final class AdminConstants { */ public static final String SYS_NAMESPACE_ID_NOT_EXIST = "The namespace(s) does not exist!"; + /** + * The constant NAMESPACE_PLUGIN_NOT_DELETE. + */ + public static final String NAMESPACE_PLUGIN_NOT_DELETE = "The plugin that exists under a certain namespace cannot be deleted."; + /** * The constant SYS_DEFAULT_NAMESPACE_ID_DELETE. */ public static final String SYS_DEFAULT_NAMESPACE_ID_DELETE = "The default namespace can't deleted!"; /** - * The constant SYS_DEFAULT_NAMESPACE_NAMESPACE_ID. + * The constant SYS_DEFAULT_NAMESPACE_ID. */ - public static final String SYS_DEFAULT_NAMESPACE_NAMESPACE_ID = "649330b6-c2d7-4edc-be8e-8a54df9eb385"; + public static final String SYS_DEFAULT_NAMESPACE_ID = "649330b6-c2d7-4edc-be8e-8a54df9eb385"; /** - * The constant SYS_DEFAULT_NAMESPACE_ID. + * The constant DEFAULT_NAMESPACE_PRIMARY_KEY. */ - public static final String SYS_DEFAULT_NAMESPACE_ID = "1"; + public static final String DEFAULT_NAMESPACE_PRIMARY_KEY = "1"; /** * The constant SYS_API_ID_NOT_EXIST. diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/dto/SelectorData.java b/shenyu-common/src/main/java/org/apache/shenyu/common/dto/SelectorData.java index f4b7555be76f..b7cfbcfa0f0b 100644 --- a/shenyu-common/src/main/java/org/apache/shenyu/common/dto/SelectorData.java +++ b/shenyu-common/src/main/java/org/apache/shenyu/common/dto/SelectorData.java @@ -59,14 +59,19 @@ public class SelectorData { private String handle; private List conditionList; - + private List beforeConditionList; - + /** * match restful api. */ private Boolean matchRestful; - + + /** + * namespaceId. + */ + private String namespaceId; + /** * no args constructor. */ @@ -93,6 +98,7 @@ private SelectorData(final Builder builder) { this.conditionList = builder.conditionList; this.matchRestful = builder.matchRestful; this.beforeConditionList = builder.beforeConditionList; + this.namespaceId = builder.namespaceId; } /** @@ -322,7 +328,7 @@ public List getConditionList() { public void setConditionList(final List conditionList) { this.conditionList = conditionList; } - + /** * get match restful. * @@ -331,7 +337,7 @@ public void setConditionList(final List conditionList) { public Boolean getMatchRestful() { return matchRestful; } - + /** * set match restful. * @@ -340,7 +346,7 @@ public Boolean getMatchRestful() { public void setMatchRestful(final Boolean matchRestful) { this.matchRestful = matchRestful; } - + /** * get before condition list. * @@ -349,7 +355,7 @@ public void setMatchRestful(final Boolean matchRestful) { public List getBeforeConditionList() { return beforeConditionList; } - + /** * set before condition list. * @@ -358,7 +364,25 @@ public List getBeforeConditionList() { public void setBeforeConditionList(final List beforeConditionList) { this.beforeConditionList = beforeConditionList; } - + + /** + * get namespaceId. + * + * @return namespaceId + */ + public String getNamespaceId() { + return namespaceId; + } + + /** + * set namespaceId. + * + * @param namespaceId namespaceId + */ + public void setNamespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + } + @Override public boolean equals(final Object o) { if (this == o) { @@ -371,14 +395,14 @@ public boolean equals(final Object o) { return Objects.equals(id, that.id) && Objects.equals(pluginId, that.pluginId) && Objects.equals(pluginName, that.pluginName) && Objects.equals(name, that.name) && Objects.equals(matchMode, that.matchMode) && Objects.equals(type, that.type) && Objects.equals(sort, that.sort) && Objects.equals(enabled, that.enabled) && Objects.equals(logged, that.logged) - && Objects.equals(continued, that.continued) && Objects.equals(handle, that.handle) - && Objects.equals(conditionList, that.conditionList) && Objects.equals(matchRestful, that.matchRestful) - && Objects.equals(beforeConditionList, that.beforeConditionList); + && Objects.equals(continued, that.continued) && Objects.equals(handle, that.handle) && Objects.equals(conditionList, that.conditionList) + && Objects.equals(beforeConditionList, that.beforeConditionList) && Objects.equals(matchRestful, that.matchRestful) + && Objects.equals(namespaceId, that.namespaceId); } @Override public int hashCode() { - return Objects.hash(id, pluginId, pluginName, name, matchMode, type, sort, enabled, logged, continued, handle, conditionList, matchRestful, beforeConditionList); + return Objects.hash(id, pluginId, pluginName, name, matchMode, type, sort, enabled, logged, continued, handle, conditionList, beforeConditionList, matchRestful, namespaceId); } @Override @@ -415,6 +439,8 @@ public String toString() { + conditionList + ", matchRestful=" + matchRestful + + ", namespaceId=" + + namespaceId + '}'; } @@ -446,11 +472,13 @@ public static final class Builder { private String handle; private List conditionList; - + private Boolean matchRestful; - + private List beforeConditionList; + private String namespaceId; + /** * no args constructor. */ @@ -597,7 +625,7 @@ public Builder conditionList(final List conditionList) { this.conditionList = conditionList; return this; } - + /** * build match restful. * @@ -608,7 +636,7 @@ public Builder matchRestful(final Boolean matchRestful) { this.matchRestful = matchRestful; return this; } - + /** * build beforeConditionList. * @@ -619,5 +647,16 @@ public Builder beforeConditionList(final List beforeConditionList this.beforeConditionList = beforeConditionList; return this; } + + /** + * build namespaceId. + * + * @param namespaceId namespaceId + * @return this + */ + public Builder namespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + return this; + } } } diff --git a/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-storage/src/test/java/org/apache/shenyu/e2e/testcase/divide/DividePluginCases.java b/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-storage/src/test/java/org/apache/shenyu/e2e/testcase/divide/DividePluginCases.java index 1b06238094e8..2c597bb9516e 100644 --- a/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-storage/src/test/java/org/apache/shenyu/e2e/testcase/divide/DividePluginCases.java +++ b/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-storage/src/test/java/org/apache/shenyu/e2e/testcase/divide/DividePluginCases.java @@ -31,6 +31,7 @@ import java.util.List; +import static org.apache.shenyu.e2e.constant.Constants.SYS_DEFAULT_NAMESPACE_NAMESPACE_ID; import static org.apache.shenyu.e2e.template.ResourceDataTemplate.newCondition; import static org.apache.shenyu.e2e.template.ResourceDataTemplate.newConditions; import static org.apache.shenyu.e2e.template.ResourceDataTemplate.newDivideRuleHandle; @@ -68,6 +69,7 @@ private ShenYuScenarioSpec testDivideWithUriEquals() { newSelectorBuilder("httpbin", Plugin.DIVIDE) .handle(newUpstreamsBuilder("httpbin.org")) .conditionList(newConditions(ParamType.URI, Operator.EQUAL, ANYTHING)) + .namespaceId(SYS_DEFAULT_NAMESPACE_NAMESPACE_ID) .build(), newBindingData("httpbin", Plugin.DIVIDE.getAlias(), "httpbin.org"), newRuleBuilder("rule") @@ -104,6 +106,7 @@ public ShenYuScenarioSpec testDivideWithUriPathPattern() { newSelectorBuilder("httpbin", Plugin.DIVIDE) .handle(newUpstreamsBuilder("httpbin.org")) .conditionList(newConditions(ParamType.URI, Operator.PATH_PATTERN, "/anything/xx/**")) + .namespaceId(SYS_DEFAULT_NAMESPACE_NAMESPACE_ID) .build(), newBindingData("httpbin", Plugin.DIVIDE.getAlias(), "httpbin.org"), newRuleBuilder("rule") @@ -143,6 +146,7 @@ public ShenYuScenarioSpec testDivideWithUriStartWith() { newSelectorBuilder("httpbin", Plugin.DIVIDE) .handle(newUpstreamsBuilder("httpbin.org")) .conditionList(newConditions(ParamType.URI, Operator.STARTS_WITH, ANYTHING + "/xx")) + .namespaceId(SYS_DEFAULT_NAMESPACE_NAMESPACE_ID) .build(), newBindingData("httpbin", Plugin.DIVIDE.getAlias(), "httpbin.org"), newRuleBuilder("rule") @@ -181,6 +185,7 @@ public ShenYuScenarioSpec testDivideWithEndWith() { newSelectorBuilder("httpbin", Plugin.DIVIDE) .handle(newUpstreamsBuilder("httpbin.org")) .conditionList(newConditions(ParamType.URI, Operator.ENDS_WITH, "/200")) + .namespaceId(SYS_DEFAULT_NAMESPACE_NAMESPACE_ID) .build(), newBindingData("httpbin", Plugin.DIVIDE.getAlias(), "httpbin.org"), newRuleBuilder("rule") @@ -224,6 +229,7 @@ public ShenYuScenarioSpec testDivideWithMethodGet() { newCondition(ParamType.METHOD, Operator.EQUAL, "GET"), newCondition(ParamType.URI, Operator.EQUAL, ANYTHING) )) + .namespaceId(SYS_DEFAULT_NAMESPACE_NAMESPACE_ID) .build(), newBindingData("httpbin", Plugin.DIVIDE.getAlias(), "httpbin.org"), newRuleBuilder("rule") @@ -269,6 +275,7 @@ public ShenYuScenarioSpec testDivideWithMethodPost() { newCondition(ParamType.METHOD, Operator.EQUAL, "POST"), newCondition(ParamType.URI, Operator.EQUAL, ANYTHING) )) + .namespaceId(SYS_DEFAULT_NAMESPACE_NAMESPACE_ID) .build(), newBindingData("httpbin", Plugin.DIVIDE.getAlias(), "httpbin.org"), newRuleBuilder("rule") @@ -314,6 +321,7 @@ public ShenYuScenarioSpec testDivideWithMethodPut() { newCondition(ParamType.METHOD, Operator.EQUAL, "PUT"), newCondition(ParamType.URI, Operator.EQUAL, ANYTHING) )) + .namespaceId(SYS_DEFAULT_NAMESPACE_NAMESPACE_ID) .build(), newBindingData("httpbin", Plugin.DIVIDE.getAlias(), "httpbin.org"), newRuleBuilder("rule") @@ -359,6 +367,7 @@ public ShenYuScenarioSpec testDivideWithMethodDelete() { newCondition(ParamType.METHOD, Operator.EQUAL, "DELETE"), newCondition(ParamType.URI, Operator.EQUAL, ANYTHING) )) + .namespaceId(SYS_DEFAULT_NAMESPACE_NAMESPACE_ID) .build(), newBindingData("httpbin", Plugin.DIVIDE.getAlias(), "httpbin.org"), newRuleBuilder("rule") diff --git a/shenyu-e2e/shenyu-e2e-client/src/main/java/org/apache/shenyu/e2e/client/admin/AdminClient.java b/shenyu-e2e/shenyu-e2e-client/src/main/java/org/apache/shenyu/e2e/client/admin/AdminClient.java index 88a0871a6275..ccad101d0e9d 100644 --- a/shenyu-e2e/shenyu-e2e-client/src/main/java/org/apache/shenyu/e2e/client/admin/AdminClient.java +++ b/shenyu-e2e/shenyu-e2e-client/src/main/java/org/apache/shenyu/e2e/client/admin/AdminClient.java @@ -61,7 +61,9 @@ import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Properties; import java.util.stream.Collectors; @@ -241,6 +243,7 @@ public SearchedResources searchSelectors(final String keyword, fina .keyword(keyword) .plugins(plugins) .switchStatus(true) + .namespaceId(SYS_DEFAULT_NAMESPACE_NAMESPACE_ID) .build(); return search("/selector/list/search", condition, SEARCHED_SELECTORS_TYPE_REFERENCE); } @@ -248,8 +251,8 @@ public SearchedResources searchSelectors(final String keyword, fina /** * Fetch the selectors by the given conditions. * - * @param keyword expected selectors included the word. return all if absent. - * @param page page. + * @param keyword expected selectors included the word. return all if absent. + * @param page page. * @param pageSize size. * @param plugins expected selectors under specified plugins. return all if absent. * @return paginated info with list of {@link SelectorDTO}s @@ -266,7 +269,7 @@ public SearchedResources searchSelectors(final String keyword, fina /** * Fetch the rules by the given conditions. * - * @param keyword expected selectors included the word. return all if absent. + * @param keyword expected selectors included the word. return all if absent. * @param selectors expected selectors under specified plugins. return all if absent. * @return paginated info with list of {@link RuleDTO}s */ @@ -295,9 +298,9 @@ private SearchedResources search(final String uri, fi ResponseEntity response = template.postForEntity(baseURL + uri, entity, ShenYuResult.class); ShenYuResult rst = assertAndGet(response, "query success"); - return Assertions.assertDoesNotThrow( - () -> mapper.readValue(rst.getData().traverse(), valueType), - "checking cast to SearchedResources" + return Assertions.assertDoesNotThrow(() -> mapper.readValue( + rst.getData().traverse(), valueType), + "checking cast to SearchedResources" ); } @@ -306,7 +309,7 @@ private List getSearch(final String uri, final TypeRe ShenYuResult rst = assertAndGet(response, "query success"); return Assertions.assertDoesNotThrow( () -> mapper.readValue(rst.getData().traverse(), valueType), - "checking cast to SearchedResources" + "checking cast to SearchedResources" ); } @@ -317,6 +320,7 @@ private List getSearch(final String uri, final TypeRe * @return SelectorDTO */ public SelectorDTO create(final SelectorData selector) { + selector.setNamespaceId(SYS_DEFAULT_NAMESPACE_NAMESPACE_ID); SelectorDTO dto = create("/selector", selector); Selectors.INSTANCE.put(selector.getName(), dto.getId()); return dto; @@ -436,8 +440,10 @@ private void delete(final String uri, final List ids) { log.info("delete resources, effected size: 0, cause by: there is not resources in ShenYuAdmin"); return; } - - HttpEntity> entity = new HttpEntity<>(ids, basicAuth); + Map body = new HashMap<>(); + body.put("ids", ids); + body.put("namespaceId", SYS_DEFAULT_NAMESPACE_NAMESPACE_ID); + HttpEntity> entity = new HttpEntity<>(body, basicAuth); ResponseEntity response = template.exchange(baseURL + uri, HttpMethod.DELETE, entity, ShenYuResult.class); ShenYuResult rst = assertAndGet(response, "delete success"); Integer deleted = Assertions.assertDoesNotThrow(() -> rst.toObject(Integer.class), "checking to cast object"); diff --git a/shenyu-e2e/shenyu-e2e-common/src/main/java/org/apache/shenyu/e2e/model/data/SelectorCacheData.java b/shenyu-e2e/shenyu-e2e-common/src/main/java/org/apache/shenyu/e2e/model/data/SelectorCacheData.java index 963300d2e923..9f4843091b65 100644 --- a/shenyu-e2e/shenyu-e2e-common/src/main/java/org/apache/shenyu/e2e/model/data/SelectorCacheData.java +++ b/shenyu-e2e/shenyu-e2e-common/src/main/java/org/apache/shenyu/e2e/model/data/SelectorCacheData.java @@ -22,80 +22,86 @@ /** * SelectorData. + * * @since 2.0.0 */ -public class SelectorCacheData { - +public final class SelectorCacheData { + /** * id. */ private String id; - + /** * plugin id. */ private String pluginId; - + /** * plugin name. */ private String pluginName; - + /** * select name. */ private String name; - + /** * matchMode(0 and 1 or). */ private Integer matchMode; - + /** * type(false full,true custom). */ private Integer type; - + /** * sort. */ private Integer sort; - + /** * enabled. */ private Boolean enabled; - + /** * logged. */ private Boolean logged; - + /** * continued. */ private Boolean continued = Boolean.TRUE; - + /** * handle. */ private String handle; - + /** * condition list. */ private List conditionList; - + /** * orginal condition list. */ private List beforeConditionList; - + /** * match restful api. */ private Boolean matchRestful; - + + /** + * namespaceId. + */ + private String namespaceId; + /** * no args constructor. */ @@ -122,6 +128,7 @@ private SelectorCacheData(final Builder builder) { this.conditionList = builder.conditionList; this.matchRestful = builder.matchRestful; this.beforeConditionList = builder.beforeConditionList; + this.namespaceId = builder.namespaceId; } /** @@ -351,7 +358,7 @@ public List getConditionList() { public void setConditionList(final List conditionList) { this.conditionList = conditionList; } - + /** * get match restful. * @@ -360,7 +367,7 @@ public void setConditionList(final List conditionList) { public Boolean getMatchRestful() { return matchRestful; } - + /** * set match restful. * @@ -369,7 +376,7 @@ public Boolean getMatchRestful() { public void setMatchRestful(final Boolean matchRestful) { this.matchRestful = matchRestful; } - + /** * get before condition list. * @@ -378,7 +385,7 @@ public void setMatchRestful(final Boolean matchRestful) { public List getBeforeConditionList() { return beforeConditionList; } - + /** * set before condition list. * @@ -387,7 +394,25 @@ public List getBeforeConditionList() { public void setBeforeConditionList(final List beforeConditionList) { this.beforeConditionList = beforeConditionList; } - + + /** + * get namespaceId. + * + * @return namespaceId + */ + public String getNamespaceId() { + return namespaceId; + } + + /** + * set namespaceId. + * + * @param namespaceId namespaceId + */ + public void setNamespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + } + @Override public boolean equals(final Object o) { if (this == o) { @@ -401,13 +426,14 @@ public boolean equals(final Object o) { && Objects.equals(name, that.name) && Objects.equals(matchMode, that.matchMode) && Objects.equals(type, that.type) && Objects.equals(sort, that.sort) && Objects.equals(enabled, that.enabled) && Objects.equals(logged, that.logged) && Objects.equals(continued, that.continued) && Objects.equals(handle, that.handle) - && Objects.equals(conditionList, that.conditionList) && Objects.equals(matchRestful, that.matchRestful) - && Objects.equals(beforeConditionList, that.beforeConditionList); + && Objects.equals(conditionList, that.conditionList) && Objects.equals(beforeConditionList, that.beforeConditionList) + && Objects.equals(matchRestful, that.matchRestful) && Objects.equals(namespaceId, that.namespaceId); } @Override public int hashCode() { - return Objects.hash(id, pluginId, pluginName, name, matchMode, type, sort, enabled, logged, continued, handle, conditionList, matchRestful, beforeConditionList); + return Objects.hash(id, pluginId, pluginName, name, matchMode, type, sort, enabled, logged, continued, handle, conditionList, + beforeConditionList, matchRestful, namespaceId); } @Override @@ -444,6 +470,8 @@ public String toString() { + conditionList + ", matchRestful=" + matchRestful + + ", namespaceId=" + + namespaceId + '}'; } @@ -475,11 +503,13 @@ public static final class Builder { private String handle; private List conditionList; - + private Boolean matchRestful; - + private List beforeConditionList; + private String namespaceId; + /** * no args constructor. */ @@ -626,7 +656,7 @@ public Builder conditionList(final List conditionList) { this.conditionList = conditionList; return this; } - + /** * build match restful. * @@ -637,7 +667,7 @@ public Builder matchRestful(final Boolean matchRestful) { this.matchRestful = matchRestful; return this; } - + /** * build beforeConditionList. * @@ -648,5 +678,16 @@ public Builder beforeConditionList(final List beforeConditionList this.beforeConditionList = beforeConditionList; return this; } + + /** + * build namespaceId. + * + * @param namespaceId namespaceId + * @return this + */ + public Builder namespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + return this; + } } } diff --git a/shenyu-e2e/shenyu-e2e-common/src/main/java/org/apache/shenyu/e2e/model/data/SelectorData.java b/shenyu-e2e/shenyu-e2e-common/src/main/java/org/apache/shenyu/e2e/model/data/SelectorData.java index 5050200f0bc5..0aa689b10a91 100644 --- a/shenyu-e2e/shenyu-e2e-common/src/main/java/org/apache/shenyu/e2e/model/data/SelectorData.java +++ b/shenyu-e2e/shenyu-e2e-common/src/main/java/org/apache/shenyu/e2e/model/data/SelectorData.java @@ -68,6 +68,8 @@ public final class SelectorData implements ResourceData { private boolean matchRestful; + private String namespaceId; + /** * builder constructor. * @@ -86,6 +88,7 @@ private SelectorData(final SelectorDataBuilder builder) { this.conditionList = builder.conditionList; this.sort = builder.sort; this.matchRestful = builder.matchRestful; + this.namespaceId = builder.namespaceId; } /** @@ -316,6 +319,24 @@ public void setMatchRestful(final boolean matchRestful) { this.matchRestful = matchRestful; } + /** + * get namespaceId. + * + * @return namespaceId + */ + public String getNamespaceId() { + return namespaceId; + } + + /** + * set namespaceId. + * + * @param namespaceId namespaceId + */ + public void setNamespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + } + static class PluginHandleSerializer extends JsonSerializer { private static final ObjectMapper MAPPER = new ObjectMapper(); @@ -359,6 +380,8 @@ public static final class SelectorDataBuilder { private boolean matchRestful; + private String namespaceId; + /** * no args constructor. */ @@ -506,5 +529,16 @@ public SelectorDataBuilder matchRestful(final boolean matchRestful) { this.matchRestful = matchRestful; return this; } + + /** + * build namespaceId. + * + * @param namespaceId namespaceId + * @return this + */ + public SelectorDataBuilder namespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + return this; + } } } diff --git a/shenyu-e2e/shenyu-e2e-common/src/main/java/org/apache/shenyu/e2e/model/data/SelectorQueryCondition.java b/shenyu-e2e/shenyu-e2e-common/src/main/java/org/apache/shenyu/e2e/model/data/SelectorQueryCondition.java index a575f07cda9f..f4b419de271f 100644 --- a/shenyu-e2e/shenyu-e2e-common/src/main/java/org/apache/shenyu/e2e/model/data/SelectorQueryCondition.java +++ b/shenyu-e2e/shenyu-e2e-common/src/main/java/org/apache/shenyu/e2e/model/data/SelectorQueryCondition.java @@ -37,6 +37,8 @@ public final class SelectorQueryCondition implements QueryCondition { private boolean switchStatus; + private String namespaceId; + /** * builder constructor. * @@ -47,6 +49,7 @@ private SelectorQueryCondition(final Builder builder) { this.keyword = builder.keyword; this.plugins = builder.plugins; this.switchStatus = builder.switchStatus; + this.namespaceId = builder.namespaceId; } /** @@ -133,6 +136,24 @@ public void setSwitchStatus(final boolean switchStatus) { this.switchStatus = switchStatus; } + /** + * get namespaceId. + * + * @return namespaceId + */ + public String getNamespaceId() { + return namespaceId; + } + + /** + * set namespaceId. + * + * @param namespaceId namespaceId + */ + public void setNamespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + } + @Override public String toString() { return "SelectorQueryCondition{" @@ -146,6 +167,8 @@ public String toString() { + Arrays.toString(plugins) + ", switchStatus=" + switchStatus + + ", namespaceId=" + + namespaceId + '}'; } @@ -163,6 +186,8 @@ public static final class Builder { private boolean switchStatus; + private String namespaceId; + /** * no args constructor. */ @@ -222,5 +247,16 @@ public Builder switchStatus(final boolean switchStatus) { this.switchStatus = switchStatus; return this; } + + /** + * namespaceId. + * + * @param namespaceId namespaceId + * @return SelectorDOBuilder + */ + public Builder namespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + return this; + } } } diff --git a/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/dto/MetaDataRegisterDTO.java b/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/dto/MetaDataRegisterDTO.java index 596b39917870..42c417fc79e5 100644 --- a/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/dto/MetaDataRegisterDTO.java +++ b/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/dto/MetaDataRegisterDTO.java @@ -63,6 +63,8 @@ public class MetaDataRegisterDTO implements DataTypeParent { private boolean addPrefixed; + private String namespaceId; + /** * Instantiates a new Meta data register dto. * @@ -82,6 +84,7 @@ public class MetaDataRegisterDTO implements DataTypeParent { * @param pluginNames the plugin names * @param registerMetaData the register meta data * @param addPrefixed the prefix forward status + * @param namespaceId the namespaceId */ public MetaDataRegisterDTO(final String appName, final String contextPath, final String path, final String pathDesc, @@ -90,7 +93,8 @@ public MetaDataRegisterDTO(final String appName, final String contextPath, final String parameterTypes, final String rpcExt, final boolean enabled, final String host, final Integer port, final List pluginNames, - final boolean registerMetaData, final boolean addPrefixed) { + final boolean registerMetaData, final boolean addPrefixed, + final String namespaceId) { this.appName = appName; this.contextPath = contextPath; this.path = path; @@ -108,6 +112,7 @@ public MetaDataRegisterDTO(final String appName, final String contextPath, this.registerMetaData = registerMetaData; this.timeMillis = System.currentTimeMillis(); this.addPrefixed = addPrefixed; + this.namespaceId = namespaceId; } /** @@ -134,6 +139,7 @@ private MetaDataRegisterDTO(final Builder builder) { registerMetaData = builder.registerMetaData; timeMillis = System.currentTimeMillis(); addPrefixed = builder.addPrefixed; + namespaceId = builder.namespaceId; } /** @@ -456,6 +462,24 @@ public void setAddPrefixed(final boolean prefixForwardEnable) { this.addPrefixed = prefixForwardEnable; } + /** + * Gets the value of namespaceId. + * + * @return the value of namespaceId + */ + public String getNamespaceId() { + return namespaceId; + } + + /** + * Sets the namespaceId. + * + * @param namespaceId namespaceId + */ + public void setNamespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + } + @Override public boolean equals(final Object o) { if (this == o) { @@ -473,14 +497,15 @@ public boolean equals(final Object o) { && Objects.equals(enabled, that.enabled) && Objects.equals(host, that.host) && Objects.equals(port, that.port) && Objects.equals(pluginNames, that.pluginNames) && Objects.equals(registerMetaData, that.registerMetaData) - && Objects.equals(addPrefixed, that.addPrefixed); + && Objects.equals(addPrefixed, that.addPrefixed) + && Objects.equals(namespaceId, that.namespaceId); } @Override public int hashCode() { return Objects.hash(appName, contextPath, path, pathDesc, rpcType, serviceName, methodName, ruleName, parameterTypes, rpcExt, enabled, host, port, pluginNames, - registerMetaData, addPrefixed); + registerMetaData, addPrefixed, namespaceId); } @Override @@ -520,6 +545,8 @@ public String toString() { + timeMillis + ", addPrefixed=" + addPrefixed + + ", namespaceId=" + + namespaceId + '}'; } @@ -562,6 +589,8 @@ public static final class Builder { private boolean addPrefixed; + private String namespaceId; + private Builder() { } @@ -752,6 +781,18 @@ public Builder addPrefixed(final boolean addPrefixed) { return this; } + + /** + * namespaceId. + * + * @param namespaceId namespaceId + * @return SelectorDOBuilder + */ + public Builder namespaceId(final String namespaceId) { + this.namespaceId = namespaceId; + return this; + } + /** * build. *