|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8; -*- |
| 3 | + |
| 4 | +# Copyright (c) 2023 Oracle and/or its affiliates. |
| 5 | +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ |
| 6 | +import ads |
| 7 | +import oci |
| 8 | +import copy |
| 9 | + |
| 10 | +from ads.common import utils |
| 11 | +from dataclasses import asdict, dataclass |
| 12 | + |
| 13 | +FILE_STORAGE_TYPE = "FILE_STORAGE" |
| 14 | + |
| 15 | + |
| 16 | +@dataclass |
| 17 | +class DSCFileSystem: |
| 18 | + |
| 19 | + destination_directory_name: str = None |
| 20 | + |
| 21 | + def to_dict(self) -> dict: |
| 22 | + """Converts the object to dictionary.""" |
| 23 | + return {utils.snake_to_camel(k): v for k, v in asdict(self).items() if v} |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def from_dict(cls, env: dict) -> "DSCFileSystem": |
| 27 | + """Initialize the object from a Python dictionary.""" |
| 28 | + return cls(**{utils.camel_to_snake(k): v for k, v in env.items()}) |
| 29 | + |
| 30 | + def _update_to_dsc_model(self, **kwargs) -> dict: |
| 31 | + return self.to_dict() |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def _update_from_dsc_model(cls, dsc_model: dict) -> "DSCFileSystem": |
| 35 | + return cls.from_dict(dsc_model) |
| 36 | + |
| 37 | + |
| 38 | +@dataclass |
| 39 | +class DSCFileStorage(DSCFileSystem): |
| 40 | + |
| 41 | + mount_target: str = None |
| 42 | + mount_target_id: str = None |
| 43 | + export_path: str = None |
| 44 | + export_id: str = None |
| 45 | + storage_type: str = FILE_STORAGE_TYPE |
| 46 | + |
| 47 | + def __post_init__(self): |
| 48 | + if not self.destination_directory_name: |
| 49 | + raise ValueError( |
| 50 | + "Parameter `destination_directory_name` must be provided to mount file system." |
| 51 | + ) |
| 52 | + |
| 53 | + if not self.mount_target and not self.mount_target_id: |
| 54 | + raise ValueError( |
| 55 | + "Either parameter `mount_target` or `mount_target_id` must be provided to mount file system." |
| 56 | + ) |
| 57 | + |
| 58 | + if not self.export_path and not self.export_id: |
| 59 | + raise ValueError( |
| 60 | + "Either parameter `export_path` or `export_id` must be provided to mount file system." |
| 61 | + ) |
| 62 | + |
| 63 | + def _update_to_dsc_model(self, **kwargs) -> dict: |
| 64 | + """Updates arguments to dsc model. |
| 65 | +
|
| 66 | + Returns |
| 67 | + ------- |
| 68 | + dict: |
| 69 | + A dictionary of arguments. |
| 70 | + """ |
| 71 | + auth = ads.auth.default_signer() |
| 72 | + file_storage_client = oci.file_storage.FileStorageClient(**auth) |
| 73 | + identity_client = oci.identity.IdentityClient(**auth) |
| 74 | + |
| 75 | + arguments = self.to_dict() |
| 76 | + |
| 77 | + compartment_id = kwargs["compartment_id"] |
| 78 | + if "mountTargetId" not in arguments: |
| 79 | + list_availability_domains_response = ( |
| 80 | + identity_client.list_availability_domains( |
| 81 | + compartment_id=compartment_id |
| 82 | + ).data |
| 83 | + ) |
| 84 | + mount_targets = [] |
| 85 | + for availability_domain in list_availability_domains_response: |
| 86 | + mount_targets.extend( |
| 87 | + file_storage_client.list_mount_targets( |
| 88 | + compartment_id=compartment_id, |
| 89 | + availability_domain=availability_domain.name, |
| 90 | + ).data |
| 91 | + ) |
| 92 | + mount_targets = [ |
| 93 | + mount_target.id |
| 94 | + for mount_target in mount_targets |
| 95 | + if mount_target.display_name == self.mount_target |
| 96 | + ] |
| 97 | + if len(mount_targets) == 0: |
| 98 | + raise ValueError( |
| 99 | + f"No `mount_target` with value {self.mount_target} found under compartment {compartment_id}. Specify a valid one." |
| 100 | + ) |
| 101 | + if len(mount_targets) > 1: |
| 102 | + raise ValueError( |
| 103 | + f"Multiple `mount_target` with value {self.mount_target} found under compartment {compartment_id}. Specify `mount_target_id` of the file system instead." |
| 104 | + ) |
| 105 | + arguments["mountTargetId"] = mount_targets[0] |
| 106 | + arguments.pop("mountTarget") |
| 107 | + |
| 108 | + if "exportId" not in arguments: |
| 109 | + list_exports_response = file_storage_client.list_exports( |
| 110 | + compartment_id=compartment_id |
| 111 | + ).data |
| 112 | + exports = [ |
| 113 | + export.id |
| 114 | + for export in list_exports_response |
| 115 | + if export.path == self.export_path |
| 116 | + ] |
| 117 | + if len(exports) == 0: |
| 118 | + raise ValueError( |
| 119 | + f"No `export_path` with value {self.export_path} found under compartment {compartment_id}. Specify a valid one." |
| 120 | + ) |
| 121 | + if len(exports) > 1: |
| 122 | + raise ValueError( |
| 123 | + f"Multiple `export_path` with value {self.export_path} found under compartment {compartment_id}. Specify `export_id` of the file system instead." |
| 124 | + ) |
| 125 | + arguments["exportId"] = exports[0] |
| 126 | + arguments.pop("exportPath") |
| 127 | + |
| 128 | + return arguments |
| 129 | + |
| 130 | + @classmethod |
| 131 | + def _update_from_dsc_model(cls, dsc_model: dict) -> DSCFileSystem: |
| 132 | + """Updates arguments and builds DSCFileSystem object from dsc model. |
| 133 | +
|
| 134 | + Parameters |
| 135 | + ---------- |
| 136 | + dsc_model: dict |
| 137 | + A dictionary of arguments from dsc model. |
| 138 | +
|
| 139 | + Returns |
| 140 | + ------- |
| 141 | + DSCFileSystem |
| 142 | + An instance of DSCFileSystem. |
| 143 | + """ |
| 144 | + argument = copy.deepcopy(dsc_model) |
| 145 | + |
| 146 | + file_storage_client = oci.file_storage.FileStorageClient( |
| 147 | + **ads.auth.default_signer() |
| 148 | + ) |
| 149 | + if "mountTargetId" not in argument: |
| 150 | + raise ValueError( |
| 151 | + "Missing parameter `mountTargetId` from service. Check service log to see the error." |
| 152 | + ) |
| 153 | + argument["mountTarget"] = file_storage_client.get_mount_target( |
| 154 | + mount_target_id=argument.get("mountTargetId") |
| 155 | + ).data.display_name |
| 156 | + if "exportId" not in argument: |
| 157 | + raise ValueError( |
| 158 | + "Missing parameter `exportId` from service. Check service log to see the error." |
| 159 | + ) |
| 160 | + argument["exportPath"] = file_storage_client.get_export( |
| 161 | + export_id=argument.get("exportId") |
| 162 | + ).data.path |
| 163 | + |
| 164 | + return super().from_dict(argument) |
0 commit comments