Skip to content

Commit

Permalink
add unmount method for dsw
Browse files Browse the repository at this point in the history
  • Loading branch information
pitt-liang committed Sep 19, 2024
1 parent 50dc31f commit ee0c0d1
Showing 1 changed file with 72 additions and 5 deletions.
77 changes: 72 additions & 5 deletions pai/dsw.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def mount(
option_type: Optional[OptionType] = None,
) -> str:
"""
Dynamic mount a data source to the DSW Instance.
Dynamic mount a dataset to the DSW Instance.
Args:
source (str): The source to be mounted, can be a dataset id or an OSS uri.
Expand Down Expand Up @@ -109,16 +109,40 @@ def mount(
)


def list_dataset_configs() -> List[Dict[str, Any]]:
def unmount(mount_point: str) -> None:
"""
Unmount a dynamic mount dataset from the DSW Instance.
Dynamic mount dataset is a special dataset in DSW, which is used to mount data sources
dynamically when the instance is running. When unmount it, the dataset will be removed
from the instance.
Args:
mount_point (str): The mount point to be unmounted.
Returns:
None
"""
instance = _default_instance()
instance.unmount(mount_point)


def list_dataset_configs(dynamic_only: bool = False) -> List[Dict[str, Any]]:
"""
List all the datasets available in the DSW Instance.
Args:
dynamic_only (bool): Whether to list only the dynamic mount datasets.
Returns:
list: A list of dataset details.
"""
instance = _default_instance()

return [d.to_map() for d in instance._get_instance_info().datasets]
datasets = instance._get_instance_info().datasets
if dynamic_only:
datasets = [ds for ds in datasets if ds.dynamic]
return [ds.to_map() for ds in datasets]


def default_dynamic_mount_point():
Expand Down Expand Up @@ -183,7 +207,7 @@ def mount(
mount_point: str = None,
options: Union[str] = None,
option_type: Union[OptionType] = None,
):
) -> str:
"""
Dynamic mount a data source to the DSW Instance.
Expand All @@ -195,6 +219,9 @@ def mount(
specified with option_type.
option_type(str): Preset data source mount options, can not be specified with
options.
Returns:
str: The mount point of the data source.
"""
if options and option_type:
raise ValueError(
Expand Down Expand Up @@ -262,3 +289,43 @@ def mount(
instance_id=self.instance_id, request=request
)
return mount_point

def unmount(self, mount_point: str) -> None:
"""
Unmount a data source from the DSW Instance.
Args:
mount_point (str): The mount point to be unmounted.
Returns:
None
"""
sess = get_default_session()

resp: GetInstanceResponse = sess._acs_dsw_client.get_instance(self.instance_id)
datasets = [
UpdateInstanceRequestDatasets().from_map(ds.to_map())
for ds in resp.body.datasets
]

datasets = [ds for ds in datasets if ds.mount_path == mount_point]

if not datasets:
raise ValueError(f"Not found dataset to unmount: {mount_point}")
if len(datasets) > 1:
raise RuntimeError(f"Found multiple datasets to unmount: {mount_point}")

dataset = datasets[0]
if not dataset.dynamic:
raise ValueError(f"Dataset is not a dynamic mount point: {mount_point}")

request = UpdateInstanceRequest(
datasets=[
UpdateInstanceRequestDatasets().from_map(ds.to_map())
for ds in resp.body.datasets
if ds.mount_path != mount_point
]
)
sess._acs_dsw_client.update_instance(
instance_id=self.instance_id, request=request
)

0 comments on commit ee0c0d1

Please sign in to comment.