-
Notifications
You must be signed in to change notification settings - Fork 0
Implement ListStorageSpaces call #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rawe0
wants to merge
1
commit into
main
Choose a base branch
from
list-storage-space
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+176
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| """ | ||
| space.py | ||
|
|
||
| Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti. | ||
| Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch | ||
| Last updated: 23/02/2026 | ||
| """ | ||
|
|
||
| import logging | ||
| from cs3.gateway.v1beta1.gateway_api_pb2_grpc import GatewayAPIStub | ||
|
|
||
| from .config import Config | ||
| from .statuscodehandler import StatusCodeHandler | ||
| import cs3.storage.provider.v1beta1.spaces_api_pb2 as cs3spp | ||
| import cs3.storage.provider.v1beta1.resources_pb2 as cs3spr | ||
| import cs3.identity.user.v1beta1.resources_pb2 as cs3iur | ||
|
|
||
|
|
||
|
|
||
| class Space: | ||
| """ | ||
| Space class to handle space related API calls with CS3 Gateway API. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| config: Config, | ||
| log: logging.Logger, | ||
| gateway: GatewayAPIStub, | ||
| status_code_handler: StatusCodeHandler, | ||
| ) -> None: | ||
| """ | ||
| Initializes the Group class with logger, auth, and gateway stub, | ||
|
|
||
| :param log: Logger instance for logging. | ||
| :param gateway: GatewayAPIStub instance for interacting with CS3 Gateway. | ||
| :param auth: An instance of the auth class. | ||
| """ | ||
| self._log: logging.Logger = log | ||
| self._gateway: GatewayAPIStub = gateway | ||
| self._config: Config = config | ||
| self._status_code_handler: StatusCodeHandler = status_code_handler | ||
|
|
||
| def list_storage_spaces(self, auth_token: tuple, filters) -> list[cs3spr.StorageSpace]: | ||
| """ | ||
| Find a space based on a filter. | ||
|
|
||
| :param auth_token: tuple in the form ('x-access-token', <token>) (see auth.get_token/auth.check_token) | ||
| :param filters: Filters to search for. | ||
| :return: a list of space(s). | ||
| :raises: NotFoundException (Space not found) | ||
| :raises: AuthenticationException (Operation not permitted) | ||
| :raises: UnknownException (Unknown error) | ||
| """ | ||
| req = cs3spp.ListStorageSpacesRequest(filters=filters) | ||
| res = self._gateway.ListStorageSpaces(request=req, metadata=[auth_token]) | ||
| self._status_code_handler.handle_errors(res.status, "find storage spaces") | ||
| self._log.debug(f'msg="Invoked FindStorageSpaces" filter="{filter}" trace="{res.status.trace}"') | ||
| return res.storage_spaces | ||
|
|
||
| @classmethod | ||
| def create_storage_space_filter(cls, filter_type: str, space_type: str = None, path: str = None, opaque_id: str = None, user_idp: str = None, user_type: str = None) -> cs3spp.ListStorageSpacesRequest.Filter: | ||
| """ | ||
| Create a filter for listing storage spaces. | ||
|
|
||
| :param filter_value: Value of the filter. | ||
| :param filter_type: Type of the filter. Supported values are "ID", "OWNER", "SPACE_TYPE", "PATH" and "USER". | ||
| :param space_type: Space type to filter by (required if filter_type is "SPACE_TYPE"). | ||
| :param path: Path to filter by (required if filter_type is "PATH"). | ||
| :param opaque_id: Opaque ID to filter by (required if filter_type is "ID"). | ||
| :param user_idp: User identity provider to filter by (required if filter_type is "OWNER" or "USER"). | ||
| :param user_type: User type to filter by (required if filter_type is "OWNER" or "USER"). | ||
| :param filter_value: Value of the filter. | ||
| :return: A cs3spp.ListStorageSpacesRequest.Filter object. | ||
| :raises: ValueError (Unsupported filter type) | ||
| """ | ||
| try: | ||
| filter_type_value = cs3spp.ListStorageSpacesRequest.Filter.Type.Value(filter_type) | ||
|
|
||
| if filter_type is None: | ||
| raise ValueError(f'Unsupported filter type: {filter_type}. Supported values are "Id", "Owner", "SpaceType", "Path" and "User".') | ||
| if space_type and filter_type == "TYPE_SPACE_TYPE": | ||
| return cs3spp.ListStorageSpacesRequest.Filter(type=filter_type_value, space_type=space_type) | ||
| if path and filter_type == "TYPE_PATH": | ||
| return cs3spp.ListStorageSpacesRequest.Filter(type=filter_type_value, path=path) | ||
| if user_idp and user_type and opaque_id and filter_type == "TYPE_OWNER": | ||
| user_type = cs3iur.UserType.Value(user_type.upper()) | ||
| user_id = cs3iur.UserId(idp=user_idp, type=user_type, opaque_id=opaque_id) | ||
| return cs3spp.ListStorageSpacesRequest.Filter(type=filter_type_value, owner=user_id) | ||
| if user_idp and user_type and opaque_id and filter_type == "TYPE_USER": | ||
| user_type = cs3iur.UserType.Value(user_type.upper()) | ||
| user_id = cs3iur.UserId(idp=user_idp, type=user_type, opaque_id=opaque_id) | ||
| return cs3spp.ListStorageSpacesRequest.Filter(type=filter_type_value, user=user_id) | ||
| if opaque_id and filter_type == "TYPE_ID": | ||
| id = cs3spr.StorageSpaceId(opaque_id=opaque_id) | ||
| return cs3spp.ListStorageSpacesRequest.Filter(type=filter_type_value, id=id) | ||
| except ValueError as e: | ||
| raise ValueError(f"Failed to create storage space filter: {e}") | ||
| return None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """ | ||
| test_space.py | ||
|
|
||
| Tests that the Space class methods work as expected. | ||
|
|
||
| Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti. | ||
| Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch | ||
| Last updated: 23/02/2026 | ||
| """ | ||
|
|
||
|
|
||
| import pytest | ||
| from unittest.mock import Mock, patch | ||
| import cs3.rpc.v1beta1.code_pb2 as cs3code | ||
| import cs3.storage.provider.v1beta1.spaces_api_pb2 as cs3spp | ||
| import cs3.identity.user.v1beta1.resources_pb2 as cs3iur | ||
| import cs3.storage.provider.v1beta1.resources_pb2 as cs3spr | ||
|
|
||
| from cs3client.exceptions import ( | ||
| AuthenticationException, | ||
| UnknownException, | ||
| ) | ||
| from .fixtures import ( # noqa: F401 (they are used, the framework is not detecting it) | ||
| mock_config, | ||
| mock_logger, | ||
| mock_gateway, | ||
| mock_status_code_handler, | ||
| ) | ||
|
|
||
| @pytest.fixture | ||
| def space_instance(mock_config, mock_logger, mock_gateway, mock_status_code_handler): # noqa: F811 | ||
| """ | ||
| Fixture for creating a Space instance with mocked dependencies. | ||
| """ | ||
| from cs3client.space import Space | ||
|
|
||
| return Space(mock_config, mock_logger, mock_gateway, mock_status_code_handler) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "status_code, status_message, expected_exception", | ||
| [ | ||
| (cs3code.CODE_OK, None, None), | ||
| (cs3code.CODE_UNAUTHENTICATED, "error", AuthenticationException), | ||
| (cs3code.CODE_INTERNAL, "error", UnknownException), | ||
| ], | ||
| ) | ||
| def test_list_storage_spaces(space_instance, status_code, status_message, expected_exception): # noqa: F811 | ||
| mock_response = Mock() | ||
| mock_response.status.code = status_code | ||
| mock_response.status.message = status_message | ||
| mock_response.storage_spaces = ["space1", "space2"] | ||
| auth_token = ('x-access-token', "some_token") | ||
|
|
||
| with patch.object(space_instance._gateway, "ListStorageSpaces", return_value=mock_response): | ||
| if expected_exception: | ||
| with pytest.raises(expected_exception): | ||
| space_instance.list_storage_spaces(auth_token, filters=[]) | ||
| else: | ||
| result = space_instance.list_storage_spaces(auth_token, filters=[]) | ||
| assert result == ["space1", "space2"] | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "filter_type, space_type, path, opaque_id, user_idp, user_type, expected_filter", | ||
| [ | ||
| ("TYPE_SPACE_TYPE", "home", None, None, None, None, cs3spp.ListStorageSpacesRequest.Filter(type="TYPE_SPACE_TYPE", space_type="home")), | ||
| ("TYPE_PATH", None, "/path/to/space", None, None, None, cs3spp.ListStorageSpacesRequest.Filter(type="TYPE_PATH", path="/path/to/space")), | ||
| ("TYPE_OWNER", None, None, "opaque_id", "user_idp", "USER_TYPE_PRIMARY", cs3spp.ListStorageSpacesRequest.Filter(type="TYPE_OWNER", owner=cs3iur.UserId(idp="user_idp", type=cs3iur.UserType.Value("USER_TYPE_PRIMARY"), opaque_id="opaque_id"))), | ||
| ("TYPE_USER", None, None, "opaque_id", "user_idp", "USER_TYPE_PRIMARY", cs3spp.ListStorageSpacesRequest.Filter(type="TYPE_USER", user=cs3iur.UserId(idp="user_idp", type=cs3iur.UserType.Value("USER_TYPE_PRIMARY"), opaque_id="opaque_id"))), | ||
| ("TYPE_ID", None, None, "opaque_id", None, None, cs3spp.ListStorageSpacesRequest.Filter(type="TYPE_ID", id=cs3spr.StorageSpaceId(opaque_id="opaque_id"))), | ||
| ], | ||
| ) | ||
| def test_create_storage_space_filter(space_instance, filter_type, space_type, path, opaque_id, user_idp, user_type, expected_filter): # noqa: F811 | ||
| result = space_instance.create_storage_space_filter(filter_type, space_type, path, opaque_id, user_idp, user_type) | ||
| assert result == expected_filter |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we use a literal (with the possible values) instead of a string?