fromdataclassesimportdataclassfromtypingimportListimportmanilaclientimportswiftclientfromchi.clientsimportmanilafromchi.contextimportsessionfromchi.exceptionimportCHIValueError,ResourceErrordef_get_default_share_type_id():# we only support one share type - cephfsnfstypeshare_types=manila().share_types.list()ifnotshare_types:raiseCHIValueError("No share types found")eliflen(share_types)>1:raiseResourceError("Multiple share types found")returnshare_types[0].id
[docs]classShare:"""Represents a manilla share Args: name (str): name of new share. size (int): size in GiB. description (str): description of a share. metadata (str): metadata for the share. is_public (bool): whether to set share as public or not. Fields: id (str): id of the share export_locations: list of mount paths """def__init__(self,name:str,size:int,description:str=None,metadata:str=None,is_public:bool=False,):self.name=nameself.size=sizeself.description=descriptionself.metadata=metadataself.is_public=is_publicdef_from_manilla_share(cls,share):s=cls(share.name,share.size,share.description,share.metadata,share.is_public)s.id=share.ids.export_locations=share.export_locationspass
[docs]defsubmit(self,idempotent:bool=False,):""" Create the share. Args: idempotent (bool, optional): Whether to create the share only if it doesn't already exist. """# TODOifidempotent:passshare=manila().shares.create(share_proto="NFS",size=self.size,name=self.name,description=self.description,metadata=self.metadata,share_type=_get_default_share_type_id(),is_public=self.is_public,)self.id=share.idself.export_locations=share.export_locationsreturnshare
[docs]defdelete(self):"""Delete the share."""manila().shares.delete(self.id)
[docs]defextend(self,new_size:int):"""Extend the size of the specific share. Args: new_size: desired size to extend share to. """manila().shares.extend(self.id,new_size)
[docs]defshrink(self,new_size:int):"""Shrink the size of the specific share. Args: new_size: desired size to extend share to. """manila().shares.shrink(self.id,new_size)
[docs]deflist_shares()->List[Share]:"""Get a list of all available flavors. Returns: A list of all flavors. """return[Share._from_manilla_share(s)forsinmanila().shares.list()]
[docs]defget_share(ref)->Share:"""Get a share by its ID or name. Args: ref (str): The ID or name of the share. Returns: The share matching the ID or name. Raises: NotFound: If the share could not be found. """try:share=manila().shares.get(ref)exceptmanilaclient.exceptions.NotFound:shares=list(manila().shares.list(search_opts={"name":ref}))ifnotshares:raiseCHIValueError(f'No shares found matching name "{ref}"')eliflen(shares)>1:raiseResourceError(f'Multiple shares found matching name "{ref}"')share=shares[0]returnShare._from_manilla_share(share)
[docs]classObjectBucket:"""Class representing an object store bucket Args: name (str): name of the bucket """def__init__(self,name:str):self.name=namedefsubmit(self,idempotent:bool=False):conn=swiftclient.Connection(session=session())# TODO idempotentconn.put_container(self.name)deflist_objects(self)->List[Object]:conn=swiftclient.Connection(session=session())container_info,res_objects=conn.get_container(self.name)objects=[]forobjinres_objects:objects.append(Object(container=self.name,name=obj["name"],size=obj["bytes"]))returnobjectsdefupload(self,file_src:str):self.swift=swiftclient.service.SwiftService()self.upload_object=swiftclient.service.SwiftUploadObject(file_src,object_name=self.name)defdownload(self,object_name:str,file_dest:str):Object(container=self.name,name=object_name).download(file_dest)