diff --git a/amclient/amclient.py b/amclient/amclient.py index 601bfc0..8fce95b 100755 --- a/amclient/amclient.py +++ b/amclient/amclient.py @@ -115,6 +115,7 @@ def __init__(self, **kwargs): param: enhanced_errors param: event_reason param: pipeline_uuid + param: location_uuid param: pipeline_uuids param: ss_user_id param: ss_user_email @@ -423,6 +424,15 @@ def download_package(self, uuid): else: LOGGER.warning("Unable to download package %s", uuid) + def get_pipeline_details(self): + """SS GET /api/v2/pipeline/. Retrieve the details of a specific + pipeline given a pipeline uuid. + """ + return utils._call_url_json( + f"{self.ss_url}/api/v2/pipeline/{self.pipeline_uuid}", + headers=self._ss_auth_headers(), + ) + def get_pipelines(self): """GET Archivematica Pipelines (dashboard instances from the storage service. @@ -589,6 +599,15 @@ def delete_aip(self): self.ss_user_email, ) + def get_location_details(self): + """SS GET /api/v2/location/. Retrieve the details of a specific + location given a location uuid. + """ + return utils._call_url_json( + f"{self.ss_url}/api/v2/location/{self.location_uuid}", + headers=self._ss_auth_headers(), + ) + def list_storage_locations(self): """List all Storage Service locations.""" params = {} diff --git a/tests/test_amclient.py b/tests/test_amclient.py index 658bca1..546fb5f 100644 --- a/tests/test_amclient.py +++ b/tests/test_amclient.py @@ -1888,6 +1888,80 @@ def test_reingest_non_aip(call_url: mock.Mock): ] +@mock.patch( + "amclient.utils._call_url", + side_effect=[ + { + "description": "Archivematica on am-local", + "remote_name": "http://192.168.168.192", + "resource_uri": "/api/v2/pipeline/23129471-09e3-467e-88b6-eb4714afb5ac/", + "uuid": "23129471-09e3-467e-88b6-eb4714afb5ac", + } + ], +) +def test_get_pipeline_details(call_url: mock.Mock): + """Test that amclient can retrieve details about a pipeline.""" + pipeline_uuid = "23129471-09e3-467e-88b6-eb4714afb5ac" + response = amclient.AMClient( + ss_api_key=SS_API_KEY, + ss_user_name=SS_USER_NAME, + ss_url=SS_URL, + pipeline_uuid=pipeline_uuid, + ).get_pipeline_details() + + description = response["description"] + remote_name = response["remote_name"] + resource_uri = response["resource_uri"] + uuid = response["uuid"] + + assert description == "Archivematica on am-local" + assert remote_name == AM_URL + assert resource_uri == "/api/v2/pipeline/23129471-09e3-467e-88b6-eb4714afb5ac/" + assert uuid == pipeline_uuid + + assert call_url.mock_calls == [ + mock.call( + f"{SS_URL}/api/v2/pipeline/23129471-09e3-467e-88b6-eb4714afb5ac", + method="GET", + params=None, + headers={"Authorization": f"ApiKey {SS_USER_NAME}:{SS_API_KEY}"}, + assume_json=True, + ) + ] + + +@mock.patch( + "amclient.utils._call_url", + side_effect=[ + requests.exceptions.HTTPError(response=mock.Mock(**{"status_code": 404})), + ], +) +def test_get_pipeline_details_invalid_uuid(call_url: mock.Mock): + """Test amlient's response when an invalid pipeline uuid is provided to + the get pipeline details endpoint. + """ + pipeline_uuid = "23129471-baad-f00d-88b6-eb4714afb5ac" + response = amclient.AMClient( + ss_api_key=SS_API_KEY, + ss_user_name=SS_USER_NAME, + ss_url=SS_URL, + pipeline_uuid=pipeline_uuid, + ).get_pipeline_details() + assert ( + errors.error_lookup(response) == errors.error_codes[errors.ERR_INVALID_RESPONSE] + ) + + assert call_url.mock_calls == [ + mock.call( + f"{SS_URL}/api/v2/pipeline/23129471-baad-f00d-88b6-eb4714afb5ac", + method="GET", + params=None, + headers={"Authorization": f"ApiKey {SS_USER_NAME}:{SS_API_KEY}"}, + assume_json=True, + ) + ] + + @mock.patch( "amclient.utils._call_url", side_effect=[ @@ -2095,6 +2169,101 @@ def test_get_default_storage_locations(call_url: mock.Mock): ] +@mock.patch( + "amclient.utils._call_url", + side_effect=[ + { + "description": "Store AIP in standard Archivematica Directory", + "enabled": True, + "path": "/var/archivematica/sharedDirectory/www/AIPsStore", + "pipeline": ["/api/v2/pipeline/788ca0fa-eb38-4a16-8cf3-976b2d867a5f/"], + "purpose": "AS", + "quota": None, + "relative_path": "var/archivematica/sharedDirectory/www/AIPsStore", + "resource_uri": "/api/v2/location/23129471-09e3-467e-88b6-eb4714afb5ac/", + "space": "/api/v2/space/1a8b19d3-34a3-4800-ab1c-3aee2d195e47/", + "used": 7954625, + "uuid": "23129471-09e3-467e-88b6-eb4714afb5ac", + } + ], +) +def test_get_location_details(call_url: mock.Mock): + """Test that amclient can retrieve details about a location.""" + location_uuid = "23129471-09e3-467e-88b6-eb4714afb5ac" + response = amclient.AMClient( + ss_api_key=SS_API_KEY, + ss_user_name=SS_USER_NAME, + ss_url=SS_URL, + location_uuid=location_uuid, + ).get_location_details() + + description = response["description"] + enabled = response["enabled"] + path = response["path"] + pipeline = response["pipeline"] + purpose = response["purpose"] + quota = response["quota"] + relative_path = response["relative_path"] + resource_uri = response["resource_uri"] + space = response["space"] + used = response["used"] + uuid = response["uuid"] + + assert description == "Store AIP in standard Archivematica Directory" + assert enabled == True + assert path == "/var/archivematica/sharedDirectory/www/AIPsStore" + assert pipeline == ["/api/v2/pipeline/788ca0fa-eb38-4a16-8cf3-976b2d867a5f/"] + assert purpose == "AS" + assert quota is None + assert relative_path == "var/archivematica/sharedDirectory/www/AIPsStore" + assert resource_uri == "/api/v2/location/23129471-09e3-467e-88b6-eb4714afb5ac/" + assert space == "/api/v2/space/1a8b19d3-34a3-4800-ab1c-3aee2d195e47/" + assert used == 7954625 + assert uuid == "23129471-09e3-467e-88b6-eb4714afb5ac" + + assert call_url.mock_calls == [ + mock.call( + f"{SS_URL}/api/v2/location/23129471-09e3-467e-88b6-eb4714afb5ac", + method="GET", + params=None, + headers={"Authorization": f"ApiKey {SS_USER_NAME}:{SS_API_KEY}"}, + assume_json=True, + ) + ] + + +@mock.patch( + "amclient.utils._call_url", + side_effect=[ + requests.exceptions.HTTPError(response=mock.Mock(**{"status_code": 404})), + ], +) +def test_get_location_details_invalid_uuid(call_url: mock.Mock): + """Test amlient's response when an invalid location uuid is provided to + the get location details endpoint. + """ + location_uuid = "23129471-baad-f00d-88b6-eb4714afb5ac" + response = amclient.AMClient( + ss_api_key=SS_API_KEY, + ss_user_name=SS_USER_NAME, + ss_url=SS_URL, + location_uuid=location_uuid, + ).get_location_details() + assert ( + errors.error_lookup(response) == errors.error_codes[errors.ERR_INVALID_RESPONSE] + ) + + assert call_url.mock_calls == [ + mock.call( + f"{SS_URL}/api/v2/location/23129471-baad-f00d-88b6-eb4714afb5ac", + method="GET", + params=None, + headers={"Authorization": f"ApiKey {SS_USER_NAME}:{SS_API_KEY}"}, + assume_json=True, + ) + ] + + @mock.patch( "amclient.utils._call_url", side_effect=[