From 96cc20dc0d9a5d52dee920620ae129e94c680607 Mon Sep 17 00:00:00 2001 From: jdar8 <69840459+jdar8@users.noreply.github.com> Date: Tue, 15 Oct 2024 12:53:58 -0700 Subject: [PATCH] add delete partition pointcut (#6314) Co-authored-by: jdar --- .../ca/uhn/fhir/interceptor/api/Pointcut.java | 26 ++++++++++++++ ...313-add-pointcut-for-delete-partition.yaml | 6 ++++ .../jpa/partition/PartitionLookupSvcImpl.java | 6 ++++ .../PartitionSettingsSvcImplTest.java | 34 +++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_6_0/6313-add-pointcut-for-delete-partition.yaml diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/interceptor/api/Pointcut.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/interceptor/api/Pointcut.java index d2c87d99ab5..a3baa55eb45 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/interceptor/api/Pointcut.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/interceptor/api/Pointcut.java @@ -2174,6 +2174,32 @@ public enum Pointcut implements IPointcut { "ca.uhn.fhir.rest.api.server.RequestDetails", "ca.uhn.fhir.rest.server.servlet.ServletRequestDetails"), + /** + * Storage Hook: + * Invoked when a partition has been deleted, typically meaning the $partition-management-delete-partition + * operation has been invoked. + *

+ * This hook will only be called if + * partitioning is enabled in the JPA server. + *

+ *

+ * Hooks may accept the following parameters: + *

+ * + *

+ * Hooks must return void. + *

+ */ + STORAGE_PARTITION_DELETED( + // Return type + void.class, + // Params + "ca.uhn.fhir.interceptor.model.RequestPartitionId"), + /** * Storage Hook: * Invoked before any partition aware FHIR operation, when the selected partition has been identified (ie. after the diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_6_0/6313-add-pointcut-for-delete-partition.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_6_0/6313-add-pointcut-for-delete-partition.yaml new file mode 100644 index 00000000000..53a5736a15d --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_6_0/6313-add-pointcut-for-delete-partition.yaml @@ -0,0 +1,6 @@ +--- +type: add +issue: 6313 +jira: SMILE-8847 +title: "The `STORAGE_PARTITION_DELETED` pointcut has been added and will be called upon deleting a partition +using the `$partition-management-delete-partition` operation." diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/partition/PartitionLookupSvcImpl.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/partition/PartitionLookupSvcImpl.java index 84af0eb972c..adcf2923864 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/partition/PartitionLookupSvcImpl.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/partition/PartitionLookupSvcImpl.java @@ -203,6 +203,12 @@ public class PartitionLookupSvcImpl implements IPartitionLookupSvc { myPartitionDao.delete(partition.get()); + if (myInterceptorService.hasHooks(Pointcut.STORAGE_PARTITION_DELETED)) { + HookParams params = new HookParams() + .add(RequestPartitionId.class, partition.get().toRequestPartitionId()); + myInterceptorService.callHooks(Pointcut.STORAGE_PARTITION_DELETED, params); + } + invalidateCaches(); } diff --git a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/partition/PartitionSettingsSvcImplTest.java b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/partition/PartitionSettingsSvcImplTest.java index 10b01751efc..971409d67f4 100644 --- a/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/partition/PartitionSettingsSvcImplTest.java +++ b/hapi-fhir-jpaserver-test-r4/src/test/java/ca/uhn/fhir/jpa/partition/PartitionSettingsSvcImplTest.java @@ -1,5 +1,16 @@ package ca.uhn.fhir.jpa.partition; +import ca.uhn.fhir.interceptor.api.Hook; +import ca.uhn.fhir.interceptor.api.IInterceptorService; + +import ca.uhn.fhir.interceptor.api.Interceptor; + +import ca.uhn.fhir.interceptor.api.Pointcut; + +import ca.uhn.fhir.interceptor.model.RequestPartitionId; + +import java.util.ArrayList; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import ca.uhn.fhir.i18n.Msg; @@ -16,7 +27,11 @@ import java.util.List; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.fail; +import org.springframework.beans.factory.annotation.Autowired; + public class PartitionSettingsSvcImplTest extends BaseJpaR4Test { + @Autowired + IInterceptorService myInterceptorService; @AfterEach public void after() { @@ -56,6 +71,8 @@ public class PartitionSettingsSvcImplTest extends BaseJpaR4Test { @Test public void testDeletePartition() { + DeletedPartitionsInterceptor deletedPartitionsInterceptor = new DeletedPartitionsInterceptor(); + myInterceptorService.registerInterceptor(deletedPartitionsInterceptor); PartitionEntity partition = new PartitionEntity(); partition.setId(123); @@ -67,6 +84,8 @@ public class PartitionSettingsSvcImplTest extends BaseJpaR4Test { assertEquals("NAME123", partition.getName()); myPartitionConfigSvc.deletePartition(123); + assertEquals(1, deletedPartitionsInterceptor.getDeletedPartitions().size()); + assertThat(deletedPartitionsInterceptor.getDeletedPartitions().get(0).getFirstPartitionIdOrNull().intValue()).isEqualTo(123); try { myPartitionConfigSvc.getPartitionById(123); @@ -75,6 +94,21 @@ public class PartitionSettingsSvcImplTest extends BaseJpaR4Test { assertEquals("No partition exists with ID 123", e.getMessage()); } + myInterceptorService.unregisterInterceptor(deletedPartitionsInterceptor); + } + + @Interceptor + public static class DeletedPartitionsInterceptor { + private List myDeletedPartitions = new ArrayList<>(); + + @Hook(Pointcut.STORAGE_PARTITION_DELETED) + public void partitionDeleted(RequestPartitionId partitionId) { + myDeletedPartitions.add(partitionId); + } + + public List getDeletedPartitions() { + return myDeletedPartitions; + } } @Test