|
| 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 ipaddress |
| 9 | + |
| 10 | +from dataclasses import dataclass |
| 11 | + |
| 12 | +FILE_STORAGE_TYPE = "FILE_STORAGE" |
| 13 | + |
| 14 | + |
| 15 | +@dataclass |
| 16 | +class DSCFileSystem: |
| 17 | + |
| 18 | + src: str = None |
| 19 | + dest: str = None |
| 20 | + storage_type: str = None |
| 21 | + destination_directory_name: str = None |
| 22 | + |
| 23 | + def update_to_dsc_model(self) -> dict: |
| 24 | + """Updates arguments to dsc model. |
| 25 | +
|
| 26 | + Returns |
| 27 | + ------- |
| 28 | + dict: |
| 29 | + A dictionary of arguments. |
| 30 | + """ |
| 31 | + pass |
| 32 | + |
| 33 | + @classmethod |
| 34 | + def update_from_dsc_model(cls, dsc_model) -> dict: |
| 35 | + """Updates arguments from dsc model. |
| 36 | +
|
| 37 | + Parameters |
| 38 | + ---------- |
| 39 | + dsc_model: oci.data_science.models.JobStorageMountConfigurationDetails |
| 40 | + An instance of oci.data_science.models.JobStorageMountConfigurationDetails. |
| 41 | +
|
| 42 | + Returns |
| 43 | + ------- |
| 44 | + dict |
| 45 | + A dictionary of arguments. |
| 46 | + """ |
| 47 | + pass |
| 48 | + |
| 49 | + |
| 50 | +@dataclass |
| 51 | +class OCIFileStorage(DSCFileSystem): |
| 52 | + |
| 53 | + mount_target_id: str = None |
| 54 | + mount_target: str = None |
| 55 | + export_id: str = None |
| 56 | + export_path: str = None |
| 57 | + storage_type: str = FILE_STORAGE_TYPE |
| 58 | + |
| 59 | + def update_to_dsc_model(self) -> dict: |
| 60 | + """Updates arguments to dsc model. |
| 61 | +
|
| 62 | + Returns |
| 63 | + ------- |
| 64 | + dict: |
| 65 | + A dictionary of arguments. |
| 66 | + """ |
| 67 | + arguments = { |
| 68 | + "destinationDirectoryName" : self.dest, |
| 69 | + "storageType" : self.storage_type |
| 70 | + } |
| 71 | + |
| 72 | + self._get_mount_target_and_export_ids(arguments) |
| 73 | + |
| 74 | + return arguments |
| 75 | + |
| 76 | + def _get_mount_target_and_export_ids(self, arguments: dict): |
| 77 | + """Gets the mount target id and export id from src. |
| 78 | +
|
| 79 | + Parameters |
| 80 | + ---------- |
| 81 | + arguments: dict |
| 82 | + A dictionary of arguments. |
| 83 | + """ |
| 84 | + resource_client = oci.resource_search.ResourceSearchClient(**ads.auth.default_signer()) |
| 85 | + src_list = self.src.split(":") |
| 86 | + first_segment = src_list[0] |
| 87 | + second_segment = src_list[1] |
| 88 | + |
| 89 | + if first_segment.startswith("ocid") and "mounttarget" in first_segment: |
| 90 | + arguments["mountTargetId"] = first_segment |
| 91 | + else: |
| 92 | + ip_resource = resource_client.search_resources( |
| 93 | + search_details=oci.resource_search.models.FreeTextSearchDetails( |
| 94 | + text=first_segment, |
| 95 | + matching_context_type="NONE" |
| 96 | + ) |
| 97 | + ).data.items |
| 98 | + |
| 99 | + ip_resource = sorted(ip_resource, key=lambda resource_summary: resource_summary.time_created) |
| 100 | + |
| 101 | + if not ip_resource or not hasattr(ip_resource[-1], "identifier"): |
| 102 | + raise ValueError(f"Can't find the identifier from ip {first_segment}. Specify a valid `src`.") |
| 103 | + |
| 104 | + mount_target_resource = resource_client.search_resources( |
| 105 | + search_details=oci.resource_search.models.FreeTextSearchDetails( |
| 106 | + text=ip_resource[-1].identifier, |
| 107 | + matching_context_type="NONE" |
| 108 | + ) |
| 109 | + ).data.items |
| 110 | + |
| 111 | + mount_targets = [ |
| 112 | + mount_target.identifier |
| 113 | + for mount_target in mount_target_resource |
| 114 | + if mount_target.resource_type == "MountTarget" |
| 115 | + ] |
| 116 | + if len(mount_targets) == 0: |
| 117 | + raise ValueError( |
| 118 | + f"No `mount_target_id` found under ip {first_segment}. Specify a valid `src`." |
| 119 | + ) |
| 120 | + if len(mount_targets) > 1: |
| 121 | + raise ValueError( |
| 122 | + f"Multiple `mount_target_id` found under ip {first_segment}. Specify the `mount_target_id` in `src` instead." |
| 123 | + ) |
| 124 | + |
| 125 | + arguments["mountTargetId"] = mount_targets[0] |
| 126 | + |
| 127 | + if second_segment.startswith("ocid") and "export" in second_segment: |
| 128 | + arguments["exportId"] = second_segment |
| 129 | + else: |
| 130 | + export_resource = resource_client.search_resources( |
| 131 | + search_details=oci.resource_search.models.FreeTextSearchDetails( |
| 132 | + text=second_segment, |
| 133 | + matching_context_type="NONE" |
| 134 | + ) |
| 135 | + ).data.items |
| 136 | + |
| 137 | + exports = [ |
| 138 | + export.identifier |
| 139 | + for export in export_resource |
| 140 | + if export.resource_type == "Export" |
| 141 | + ] |
| 142 | + if len(exports) == 0: |
| 143 | + raise ValueError( |
| 144 | + f"No `export_id` found with `export_path` {second_segment}. Specify a valid `src`." |
| 145 | + ) |
| 146 | + if len(exports) > 1: |
| 147 | + raise ValueError( |
| 148 | + f"Multiple `export_id` found with `export_path` {second_segment}. Specify the `export_id` in `src` instead." |
| 149 | + ) |
| 150 | + |
| 151 | + arguments["exportId"] = exports[0] |
| 152 | + |
| 153 | + @classmethod |
| 154 | + def update_from_dsc_model(cls, dsc_model) -> dict: |
| 155 | + """Updates arguments from dsc model. |
| 156 | +
|
| 157 | + Parameters |
| 158 | + ---------- |
| 159 | + dsc_model: oci.data_science.models.JobStorageMountConfigurationDetails |
| 160 | + An instance of oci.data_science.models.JobStorageMountConfigurationDetails. |
| 161 | +
|
| 162 | + Returns |
| 163 | + ------- |
| 164 | + dict |
| 165 | + A dictionary of arguments. |
| 166 | + """ |
| 167 | + if not dsc_model.mount_target_id: |
| 168 | + raise ValueError( |
| 169 | + "Missing parameter `mount_target_id` from service. Check service log to see the error." |
| 170 | + ) |
| 171 | + if not dsc_model.export_id: |
| 172 | + raise ValueError( |
| 173 | + "Missing parameter `export_id` from service. Check service log to see the error." |
| 174 | + ) |
| 175 | + if not dsc_model.destination_directory_name: |
| 176 | + raise ValueError( |
| 177 | + "Missing parameter `destination_directory_name` from service. Check service log to see the error." |
| 178 | + ) |
| 179 | + |
| 180 | + return { |
| 181 | + "src" : f"{dsc_model.mount_target_id}:{dsc_model.export_id}", |
| 182 | + "dest" : dsc_model.destination_directory_name |
| 183 | + } |
| 184 | + |
| 185 | + |
| 186 | +class DSCFileSystemManager: |
| 187 | + |
| 188 | + @classmethod |
| 189 | + def initialize(cls, arguments: dict) -> dict: |
| 190 | + """Initialize and update arguments to dsc model. |
| 191 | +
|
| 192 | + Parameters |
| 193 | + ---------- |
| 194 | + arguments: dict |
| 195 | + A dictionary of arguments. |
| 196 | + """ |
| 197 | + if "src" not in arguments: |
| 198 | + raise ValueError( |
| 199 | + "Parameter `src` is required for mounting file storage system." |
| 200 | + ) |
| 201 | + |
| 202 | + if "dest" not in arguments: |
| 203 | + raise ValueError( |
| 204 | + "Parameter `dest` is required for mounting file storage system." |
| 205 | + ) |
| 206 | + |
| 207 | + first_segment = arguments["src"].split(":")[0] |
| 208 | + # case <mount_target_id>:<export_id> |
| 209 | + if first_segment.startswith("ocid"): |
| 210 | + return OCIFileStorage(**arguments).update_to_dsc_model() |
| 211 | + |
| 212 | + # case <ip_address>:<export_path> |
| 213 | + try: |
| 214 | + ipaddress.IPv4Network(first_segment) |
| 215 | + return OCIFileStorage(**arguments).update_to_dsc_model() |
| 216 | + except: |
| 217 | + pass |
| 218 | + |
| 219 | + raise ValueError("Invalid dict for mounting file systems. Specify a valid one.") |
0 commit comments