1+ """
2+ space.py
3+
4+ Authors: Rasmus Welander, Diogo Castro, Giuseppe Lo Presti.
5+ Emails: rasmus.oscar.welander@cern.ch, diogo.castro@cern.ch, giuseppe.lopresti@cern.ch
6+ Last updated: 23/02/2026
7+ """
8+
9+ import logging
10+ from typing import Literal
11+ from cs3 .gateway .v1beta1 .gateway_api_pb2_grpc import GatewayAPIStub
12+
13+ from .config import Config
14+ from .statuscodehandler import StatusCodeHandler
15+ import cs3 .storage .provider .v1beta1 .spaces_api_pb2 as cs3spp
16+ import cs3 .storage .provider .v1beta1 .resources_pb2 as cs3spr
17+ import cs3 .identity .user .v1beta1 .resources_pb2 as cs3iur
18+
19+
20+
21+ class Space :
22+ """
23+ Space class to handle space related API calls with CS3 Gateway API.
24+ """
25+
26+ def __init__ (
27+ self ,
28+ config : Config ,
29+ log : logging .Logger ,
30+ gateway : GatewayAPIStub ,
31+ status_code_handler : StatusCodeHandler ,
32+ ) -> None :
33+ """
34+ Initializes the Group class with logger, auth, and gateway stub,
35+
36+ :param log: Logger instance for logging.
37+ :param gateway: GatewayAPIStub instance for interacting with CS3 Gateway.
38+ :param auth: An instance of the auth class.
39+ """
40+ self ._log : logging .Logger = log
41+ self ._gateway : GatewayAPIStub = gateway
42+ self ._config : Config = config
43+ self ._status_code_handler : StatusCodeHandler = status_code_handler
44+
45+ def list_storage_spaces (self , auth_token : tuple , filters ) -> list [cs3spr .StorageSpace ]:
46+ """
47+ Find a space based on a filter.
48+
49+ :param auth_token: tuple in the form ('x-access-token', <token>) (see auth.get_token/auth.check_token)
50+ :param filters: Filters to search for.
51+ :return: a list of space(s).
52+ :raises: NotFoundException (Space not found)
53+ :raises: AuthenticationException (Operation not permitted)
54+ :raises: UnknownException (Unknown error)
55+ """
56+ req = cs3spp .ListStorageSpacesRequest (filters = filters )
57+ res = self ._gateway .ListStorageSpaces (request = req , metadata = [auth_token ])
58+ self ._status_code_handler .handle_errors (res .status , "find storage spaces" )
59+ self ._log .debug (f'msg="Invoked FindStorageSpaces" filter="{ filter } " trace="{ res .status .trace } "' )
60+ return res .storage_spaces
61+
62+ @classmethod
63+ def create_storage_space_filter (cls , filter_type : Literal ["TYPE_ID" , "TYPE_OWNER" , "TYPE_SPACE_TYPE" , "TYPE_PATH" , "TYPE_USER" ], space_type : str = None , path : str = None , opaque_id : str = None , user_idp : str = None , user_type : str = None ) -> cs3spp .ListStorageSpacesRequest .Filter :
64+ """
65+ Create a filter for listing storage spaces.
66+
67+ :param filter_value: Value of the filter.
68+ :param filter_type: Type of the filter. Supported values are "TYPE_ID", "TYPE_OWNER", "TYPE_SPACE_TYPE", "TYPE_PATH" and "TYPE_USER".
69+ :param space_type: Space type to filter by (required if filter_type is "SPACE_TYPE").
70+ :param path: Path to filter by (required if filter_type is "PATH").
71+ :param opaque_id: Opaque ID to filter by (required if filter_type is "ID").
72+ :param user_idp: User identity provider to filter by (required if filter_type is "OWNER" or "USER").
73+ :param user_type: User type to filter by (required if filter_type is "OWNER" or "USER").
74+ :param filter_value: Value of the filter.
75+ :return: A cs3spp.ListStorageSpacesRequest.Filter object.
76+ :raises: ValueError (Unsupported filter type)
77+ """
78+ try :
79+ if filter_type is None :
80+ raise ValueError (f'Unsupported filter type: { filter_type } . Supported values are "TYPE_ID", "TYPE_OWNER", "TYPE_SPACE_TYPE", "TYPE_PATH" and "TYPE_USER".' )
81+ filter_type_value = cs3spp .ListStorageSpacesRequest .Filter .Type .Value (filter_type )
82+ if space_type and filter_type == "TYPE_SPACE_TYPE" :
83+ return cs3spp .ListStorageSpacesRequest .Filter (type = filter_type_value , space_type = space_type )
84+ if path and filter_type == "TYPE_PATH" :
85+ return cs3spp .ListStorageSpacesRequest .Filter (type = filter_type_value , path = path )
86+ if user_idp and user_type and opaque_id and filter_type == "TYPE_OWNER" :
87+ user_type = cs3iur .UserType .Value (user_type .upper ())
88+ user_id = cs3iur .UserId (idp = user_idp , type = user_type , opaque_id = opaque_id )
89+ return cs3spp .ListStorageSpacesRequest .Filter (type = filter_type_value , owner = user_id )
90+ if user_idp and user_type and opaque_id and filter_type == "TYPE_USER" :
91+ user_type = cs3iur .UserType .Value (user_type .upper ())
92+ user_id = cs3iur .UserId (idp = user_idp , type = user_type , opaque_id = opaque_id )
93+ return cs3spp .ListStorageSpacesRequest .Filter (type = filter_type_value , user = user_id )
94+ if opaque_id and filter_type == "TYPE_ID" :
95+ id = cs3spr .StorageSpaceId (opaque_id = opaque_id )
96+ return cs3spp .ListStorageSpacesRequest .Filter (type = filter_type_value , id = id )
97+ except ValueError as e :
98+ raise ValueError (f"Failed to create storage space filter: { e } " )
99+ raise ValueError (f'Unsupported filter type: { filter_type } . Supported values are "TYPE_ID", "TYPE_OWNER", "TYPE_SPACE_TYPE", "TYPE_PATH" and "TYPE_USER".' )
0 commit comments