From 408ccee847e4c357357dd0961e74628298d6c4fe Mon Sep 17 00:00:00 2001 From: Guillaume Cusnieux Date: Fri, 20 Dec 2024 15:27:16 +0100 Subject: [PATCH] feat: delete entrypoint linked to org --- .../management/api/EntrypointRepository.java | 11 +++++++- .../management/JdbcEntryPointRepository.java | 25 +++++++++++++++++++ .../management/MongoEntrypointRepository.java | 16 ++++++++++++ .../api/EntrypointMongoRepository.java | 4 +++ .../DeleteOrganizationCommandHandler.java | 4 +++ .../handler/DeleteCommandHandlerTest.java | 3 +-- .../DeleteOrganizationCommandHandlerTest.java | 8 ++++++ 7 files changed, 68 insertions(+), 3 deletions(-) diff --git a/gravitee-apim-repository/gravitee-apim-repository-api/src/main/java/io/gravitee/repository/management/api/EntrypointRepository.java b/gravitee-apim-repository/gravitee-apim-repository-api/src/main/java/io/gravitee/repository/management/api/EntrypointRepository.java index b56f2f84595..d143fb9c706 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-api/src/main/java/io/gravitee/repository/management/api/EntrypointRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-api/src/main/java/io/gravitee/repository/management/api/EntrypointRepository.java @@ -18,7 +18,7 @@ import io.gravitee.repository.exceptions.TechnicalException; import io.gravitee.repository.management.model.Entrypoint; import io.gravitee.repository.management.model.EntrypointReferenceType; -import java.util.Collection; +import java.util.List; import java.util.Optional; import java.util.Set; @@ -31,4 +31,13 @@ Optional findByIdAndReference(String entrypointId, String referenceI throws TechnicalException; Set findByReference(String referenceId, EntrypointReferenceType referenceType) throws TechnicalException; + + /** + * Delete entrypoints by reference + * @param referenceId + * @param referenceType + * @return List of IDs for deleted entrypoints + * @throws TechnicalException + */ + List deleteByReferenceIdAndReferenceType(String referenceId, EntrypointReferenceType referenceType) throws TechnicalException; } diff --git a/gravitee-apim-repository/gravitee-apim-repository-jdbc/src/main/java/io/gravitee/repository/jdbc/management/JdbcEntryPointRepository.java b/gravitee-apim-repository/gravitee-apim-repository-jdbc/src/main/java/io/gravitee/repository/jdbc/management/JdbcEntryPointRepository.java index f92d06529f0..37225990537 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-jdbc/src/main/java/io/gravitee/repository/jdbc/management/JdbcEntryPointRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-jdbc/src/main/java/io/gravitee/repository/jdbc/management/JdbcEntryPointRepository.java @@ -20,6 +20,7 @@ import io.gravitee.repository.exceptions.TechnicalException; import io.gravitee.repository.jdbc.orm.JdbcObjectMapper; import io.gravitee.repository.management.api.EntrypointRepository; +import io.gravitee.repository.management.model.AccessPointReferenceType; import io.gravitee.repository.management.model.Entrypoint; import io.gravitee.repository.management.model.EntrypointReferenceType; import io.gravitee.repository.management.model.Tag; @@ -103,4 +104,28 @@ public Set findByReference(String referenceId, EntrypointReferenceTy throw new TechnicalException("Failed to find " + getOrm().getTableName() + " entrypoints by referenceId and referenceType", ex); } } + + @Override + public List deleteByReferenceIdAndReferenceType(final String referenceId, final EntrypointReferenceType referenceType) + throws TechnicalException { + try { + final var rows = jdbcTemplate.queryForList( + "select id from " + tableName + " where reference_id = ? and reference_type = ?", + String.class, + referenceId, + referenceType.name() + ); + + if (!rows.isEmpty()) { + jdbcTemplate.update( + "delete from " + tableName + " where reference_id = ? and reference_type = ?", + referenceId, + referenceType.name() + ); + } + return rows; + } catch (final Exception ex) { + throw new TechnicalException("Failed to delete entrypoints by reference", ex); + } + } } diff --git a/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/MongoEntrypointRepository.java b/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/MongoEntrypointRepository.java index df3b930bd0f..0fbeec05313 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/MongoEntrypointRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/MongoEntrypointRepository.java @@ -140,4 +140,20 @@ public Set findByReference(String referenceId, EntrypointReferenceTy public Set findAll() throws TechnicalException { return internalEntryPointRepo.findAll().stream().map(entrypointMongo -> mapper.map(entrypointMongo)).collect(Collectors.toSet()); } + + @Override + public List deleteByReferenceIdAndReferenceType(final String referenceId, final EntrypointReferenceType referenceType) + throws TechnicalException { + LOGGER.debug("Delete entrypoint by reference [{}, {}]", referenceType, referenceId); + try { + List entrypointMongos = internalEntryPointRepo.deleteByReferenceIdAndReferenceType( + referenceId, + referenceType.name() + ); + LOGGER.debug("Delete entrypoint by reference [{}, {}] - Done", referenceType, referenceId); + return entrypointMongos.stream().map(EntrypointMongo::getId).toList(); + } catch (Exception e) { + throw new TechnicalException("An error occurred while deleting entrypoint", e); + } + } } diff --git a/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/api/EntrypointMongoRepository.java b/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/api/EntrypointMongoRepository.java index c7256a53714..722eb3281cb 100644 --- a/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/api/EntrypointMongoRepository.java +++ b/gravitee-apim-repository/gravitee-apim-repository-mongodb/src/main/java/io/gravitee/repository/mongodb/management/internal/api/EntrypointMongoRepository.java @@ -20,6 +20,7 @@ import java.util.List; import java.util.Optional; import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; import org.springframework.stereotype.Repository; /** @@ -35,4 +36,7 @@ Optional findByIdAndReferenceIdAndReferenceType( String referenceId, EntrypointReferenceType referenceType ); + + @Query(value = "{ 'referenceId': ?0, 'referenceType': ?1 }", delete = true) + List deleteByReferenceIdAndReferenceType(String referenceId, String name); } diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/cockpit/command/handler/DeleteOrganizationCommandHandler.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/cockpit/command/handler/DeleteOrganizationCommandHandler.java index ea3301be674..f73168f82a0 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/cockpit/command/handler/DeleteOrganizationCommandHandler.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/cockpit/command/handler/DeleteOrganizationCommandHandler.java @@ -52,6 +52,7 @@ public class DeleteOrganizationCommandHandler implements CommandHandler