From f15e8c583521263e80a17c5690caebe1baef10c8 Mon Sep 17 00:00:00 2001 From: Josh Borrow Date: Thu, 29 Feb 2024 11:21:48 -0500 Subject: [PATCH] Add remote instance creation in complete endpoint. --- hera_librarian/client.py | 8 ++++++-- hera_librarian/models/clone.py | 2 ++ librarian_background/recieve_clone.py | 1 + librarian_server/api/clone.py | 14 +++++++++++++- tests/server_unit_test/test_clone.py | 2 ++ 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/hera_librarian/client.py b/hera_librarian/client.py index ab29adb..c0ba2ca 100644 --- a/hera_librarian/client.py +++ b/hera_librarian/client.py @@ -911,6 +911,7 @@ def ingest_manifest_entry( uploader: str, source: str, deletion_policy: DeletionPolicy, + source_transfer_id: int, local_path: Path, ): """ @@ -936,6 +937,8 @@ def ingest_manifest_entry( The source of the file. deletion_policy : DeletionPolicy The deletion policy of the instance. + source_transfer_id : int + The ID of the outgoing transfer. local_path : Path The path to the instance on the store. """ @@ -948,12 +951,13 @@ def ingest_manifest_entry( upload_checksum=checksum, upload_name=name.name, destination_location=name, - # Uploader is SPECIFICALLY kept as a param here because it + # Uploader is SPECIFICALLY kept as a param here because it is the + # source librarian, not us doing the ingestion. uploader=uploader, source=source, # TODO: As part of the manifest generation process, we should generate # outbound transfers for all the files. - source_transfer_id=-1, + source_transfer_id=source_transfer_id, ) initiaton_response: CloneInitiationResponse = self.post( diff --git a/hera_librarian/models/clone.py b/hera_librarian/models/clone.py index 2c5bd4c..1f10445 100644 --- a/hera_librarian/models/clone.py +++ b/hera_librarian/models/clone.py @@ -104,6 +104,8 @@ class CloneCompleteRequest(BaseModel): "The ID of the transfer. Note that this is the OutgoingTransfer ID." destination_transfer_id: int "The ID of the transfer. Note that this is the IncomingTransfer ID." + store_id: int + "The ID of the store that was the ultimate destination of the transfer." class CloneCompleteResponse(BaseModel): diff --git a/librarian_background/recieve_clone.py b/librarian_background/recieve_clone.py index b4615f7..df4af4d 100644 --- a/librarian_background/recieve_clone.py +++ b/librarian_background/recieve_clone.py @@ -161,6 +161,7 @@ def core(self, session: Session): request = CloneCompleteRequest( source_transfer_id=transfer.id, destination_instance_id=instance.id, + store_id=store.id, ) try: diff --git a/librarian_server/api/clone.py b/librarian_server/api/clone.py index 829d25a..009d0fe 100644 --- a/librarian_server/api/clone.py +++ b/librarian_server/api/clone.py @@ -40,6 +40,7 @@ from ..database import yield_session from ..logger import log from ..orm.file import File +from ..orm.instance import RemoteInstance from ..orm.storemetadata import StoreMetadata from ..orm.transfer import IncomingTransfer, OutgoingTransfer, TransferStatus from .auth import CallbackUserDependency, ReadappendUserDependency @@ -335,7 +336,8 @@ def complete( ): """ The callback from librarian B to librarian A that it has completed the - transfer. Used to update anything in our OutgiongTransfers that needs it. + transfer. Used to update anything in our OutgiongTransfers that needs it, + as well as create the appropriate remote instances. Possible response codes: @@ -388,6 +390,16 @@ def complete( ) transfer.status = TransferStatus.COMPLETED + + # Create new remote instance for this file that was just completed. + remote_instance = RemoteInstance.new_instance( + file=transfer.file, + store_id=request.store_id, + librarian=transfer.destination, + ) + + session.add(remote_instance) + session.commit() response.status_code = status.HTTP_200_OK diff --git a/tests/server_unit_test/test_clone.py b/tests/server_unit_test/test_clone.py index d74535d..30b7c4c 100644 --- a/tests/server_unit_test/test_clone.py +++ b/tests/server_unit_test/test_clone.py @@ -317,6 +317,7 @@ def test_incoming_transfer_endpoints( request = CloneCompleteRequest( source_transfer_id=transfer_id, destination_transfer_id=transfer_id, + store_id=store.id, ) response = test_client.post_with_auth( @@ -366,6 +367,7 @@ def test_complete_no_transfer(test_client, test_server, test_orm): request = CloneCompleteRequest( source_transfer_id=-1, destination_transfer_id=-1, + store_id=-1, ) response = test_client.post_with_auth(