Skip to content

Commit

Permalink
feat: delete entrypoint linked to org
Browse files Browse the repository at this point in the history
  • Loading branch information
gcusnieux committed Dec 20, 2024
1 parent 7b388c6 commit 408ccee
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,4 +31,13 @@ Optional<Entrypoint> findByIdAndReference(String entrypointId, String referenceI
throws TechnicalException;

Set<Entrypoint> 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<String> deleteByReferenceIdAndReferenceType(String referenceId, EntrypointReferenceType referenceType) throws TechnicalException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -103,4 +104,28 @@ public Set<Entrypoint> findByReference(String referenceId, EntrypointReferenceTy
throw new TechnicalException("Failed to find " + getOrm().getTableName() + " entrypoints by referenceId and referenceType", ex);
}
}

@Override
public List<String> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,20 @@ public Set<Entrypoint> findByReference(String referenceId, EntrypointReferenceTy
public Set<Entrypoint> findAll() throws TechnicalException {
return internalEntryPointRepo.findAll().stream().map(entrypointMongo -> mapper.map(entrypointMongo)).collect(Collectors.toSet());
}

@Override
public List<String> deleteByReferenceIdAndReferenceType(final String referenceId, final EntrypointReferenceType referenceType)
throws TechnicalException {
LOGGER.debug("Delete entrypoint by reference [{}, {}]", referenceType, referenceId);
try {
List<EntrypointMongo> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -35,4 +36,7 @@ Optional<EntrypointMongo> findByIdAndReferenceIdAndReferenceType(
String referenceId,
EntrypointReferenceType referenceType
);

@Query(value = "{ 'referenceId': ?0, 'referenceType': ?1 }", delete = true)
List<EntrypointMongo> deleteByReferenceIdAndReferenceType(String referenceId, String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class DeleteOrganizationCommandHandler implements CommandHandler<DeleteOr
private final AuditRepository auditRepository;
private final CommandRepository commandRepository;
private final CustomUserFieldsRepository customUserFieldsRepository;
private final EntrypointRepository entrypointRepository;
private final EnvironmentService environmentService;
private final FlowRepository flowRepository;
private final IdentityProviderActivationRepository identityProviderActivationRepository;
Expand All @@ -77,6 +78,7 @@ public DeleteOrganizationCommandHandler(
@Lazy AuditRepository auditRepository,
@Lazy CommandRepository commandRepository,
@Lazy CustomUserFieldsRepository customUserFieldsRepository,
@Lazy EntrypointRepository entrypointRepository,
@Lazy FlowRepository flowRepository,
@Lazy IdentityProviderActivationRepository identityProviderActivationRepository,
@Lazy IdentityProviderRepository identityProviderRepository,
Expand All @@ -103,6 +105,7 @@ public DeleteOrganizationCommandHandler(
this.auditRepository = auditRepository;
this.commandRepository = commandRepository;
this.customUserFieldsRepository = customUserFieldsRepository;
this.entrypointRepository = entrypointRepository;
this.environmentService = environmentService;
this.flowRepository = flowRepository;
this.identityProviderActivationRepository = identityProviderActivationRepository;
Expand Down Expand Up @@ -217,5 +220,6 @@ private void deleteOrganization(ExecutionContext executionContext, OrganizationE
organization.getId(),
NotificationTemplateReferenceType.ORGANIZATION
);
entrypointRepository.deleteByReferenceIdAndReferenceType(organization.getId(), EntrypointReferenceType.ORGANIZATION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public class DeleteCommandHandlerTest {
InstallationRepository.class,
// Forgot repositories
TicketRepository.class,
QualityRuleRepository.class,
EntrypointRepository.class
QualityRuleRepository.class
);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.gravitee.repository.management.api.AuditRepository;
import io.gravitee.repository.management.api.CommandRepository;
import io.gravitee.repository.management.api.CustomUserFieldsRepository;
import io.gravitee.repository.management.api.EntrypointRepository;
import io.gravitee.repository.management.api.FlowRepository;
import io.gravitee.repository.management.api.IdentityProviderActivationRepository;
import io.gravitee.repository.management.api.IdentityProviderRepository;
Expand All @@ -49,6 +50,7 @@
import io.gravitee.repository.management.model.AccessPointReferenceType;
import io.gravitee.repository.management.model.Audit;
import io.gravitee.repository.management.model.CustomUserFieldReferenceType;
import io.gravitee.repository.management.model.EntrypointReferenceType;
import io.gravitee.repository.management.model.License;
import io.gravitee.repository.management.model.MembershipReferenceType;
import io.gravitee.repository.management.model.MetadataReferenceType;
Expand Down Expand Up @@ -159,6 +161,9 @@ public class DeleteOrganizationCommandHandlerTest {
@Mock
private NotificationTemplateRepository notificationTemplateRepository;

@Mock
private EntrypointRepository entrypointRepository;

private DeleteOrganizationCommandHandler cut;

@Before
Expand All @@ -179,6 +184,7 @@ public void setUp() throws Exception {
auditRepository,
commandRepository,
customUserFieldsRepository,
entrypointRepository,
flowRepository,
identityProviderActivationRepository,
identityProviderRepository,
Expand Down Expand Up @@ -292,6 +298,8 @@ private void verifyDeleteOrganization(ExecutionContext executionContext) throws
verify(organizationService).delete(executionContext.getOrganizationId());
verify(notificationTemplateRepository)
.deleteByReferenceIdAndReferenceType(executionContext.getOrganizationId(), NotificationTemplateReferenceType.ORGANIZATION);
verify(entrypointRepository)
.deleteByReferenceIdAndReferenceType(executionContext.getOrganizationId(), EntrypointReferenceType.ORGANIZATION);
}

private void verifyDisableOrganization(ExecutionContext context) {
Expand Down

0 comments on commit 408ccee

Please sign in to comment.