from.clientsimportmanilafrom.exceptionimportCHIValueError,ResourceErrorfrommanilaclient.exceptionsimportNotFound__all__=["create_share","delete_share","extend_share","get_access_rules","get_share","get_share_id","list_shares","shrink_share",]def_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]defcreate_share(size,name=None,description=None,metadata=None,is_public=False):"""Create a share. Args: size (int): size in GiB. name (str): name of new share. description (str): description of a share. is_public (bool): whether to set share as public or not. Returns: The created share. """share=manila().shares.create(share_proto="NFS",size=size,name=name,description=description,metadata=metadata,share_type=_get_default_share_type_id(),is_public=is_public,)returnshare
[docs]defdelete_share(share):"""Delete a share. Args: share: either share object or text with its ID. """manila().shares.delete(share)
[docs]defextend_share(share,new_size):"""Extend the size of the specific share. Args: share: either share object or text with its ID. new_size: desired size to extend share to. """manila().shares.extend(share,new_size)
[docs]defget_access_rules(share):"""Get access list to a share. Args: share: either share object or text with its ID. Returns: A list of access rules. """returnmanila().shares.access_list(share)
[docs]defget_share(ref):"""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:returnmanila().shares.get(ref)exceptNotFound:returnmanila().shares.get(get_share_id(ref))
[docs]defget_share_id(name):"""Look up a share's ID from its name. Args: name (str): The name of the share. Returns: The ID of the found share. Raises: ValueError: If the share could not be found, or if multiple shares matched the name. """shares=list(manila().shares.list(search_opts={"name":name}))ifnotshares:raiseCHIValueError(f'No shares found matching name "{name}"')eliflen(shares)>1:raiseResourceError(f'Multiple shares found matching name "{name}"')returnshares[0].id
[docs]deflist_shares():"""List all shares under the current project. Returns: All shares associated with the current project. """returnlist(manila().shares.list())
[docs]defshrink_share(share,new_size):"""Shrink the size of the specific share. Args: share: either share object or text with its ID. new_size: desired size to shrink share to. """manila().shares.shrink(share,new_size)