|
| 1 | +from typing import AsyncIterator |
| 2 | + |
| 3 | +from uipath.platform.connections import ConnectionsService |
| 4 | +from uipath.platform.errors import EnrichedException, FolderNotFoundException |
| 5 | +from uipath.platform.resource_catalog import ( |
| 6 | + Resource, |
| 7 | + ResourceCatalogService, |
| 8 | + ResourceType, |
| 9 | +) |
| 10 | + |
| 11 | +from .._utils._studio_project import ( |
| 12 | + ReferencedResourceFolder, |
| 13 | + ReferencedResourceRequest, |
| 14 | + VirtualResourceRequest, |
| 15 | +) |
| 16 | +from ..models.runtime_schema import BindingResource, Bindings |
| 17 | +from ._resource_actions import CreateReference, CreateVirtual, ResourceAction, Skip |
| 18 | + |
| 19 | +_NOT_FOUND_SUFFIX = "was not found and will not be added to the solution." |
| 20 | + |
| 21 | + |
| 22 | +async def resolve_bindings( |
| 23 | + bindings: Bindings, |
| 24 | + resource_catalog: ResourceCatalogService, |
| 25 | + connections: ConnectionsService, |
| 26 | + supported_virtual_kinds: set[str], |
| 27 | +) -> AsyncIterator[ResourceAction]: |
| 28 | + """Yield one ResourceAction per importable binding. |
| 29 | +
|
| 30 | + Bindings that should be silently ignored (e.g. guardrail bindings without a |
| 31 | + folderPath) are filtered out here. |
| 32 | + """ |
| 33 | + for binding in bindings.resources: |
| 34 | + action = await _resolve_binding( |
| 35 | + binding, resource_catalog, connections, supported_virtual_kinds |
| 36 | + ) |
| 37 | + if action is not None: |
| 38 | + yield action |
| 39 | + |
| 40 | + |
| 41 | +async def _resolve_binding( |
| 42 | + binding: BindingResource, |
| 43 | + resource_catalog: ResourceCatalogService, |
| 44 | + connections: ConnectionsService, |
| 45 | + supported_virtual_kinds: set[str], |
| 46 | +) -> ResourceAction | None: |
| 47 | + if binding.resource == "connection": |
| 48 | + return await _resolve_connection(binding, resource_catalog, connections) |
| 49 | + return await _resolve_regular(binding, resource_catalog, supported_virtual_kinds) |
| 50 | + |
| 51 | + |
| 52 | +async def _resolve_connection( |
| 53 | + binding: BindingResource, |
| 54 | + resource_catalog: ResourceCatalogService, |
| 55 | + connections: ConnectionsService, |
| 56 | +) -> ResourceAction | None: |
| 57 | + connection_id_value = binding.value.get("ConnectionId") |
| 58 | + if connection_id_value is None: |
| 59 | + raise ValueError( |
| 60 | + f"Connection binding {binding.key!r} is missing required field 'ConnectionId'" |
| 61 | + ) |
| 62 | + connection_key = connection_id_value.default_value |
| 63 | + |
| 64 | + try: |
| 65 | + connection = await connections.retrieve_async(connection_key) |
| 66 | + except EnrichedException: |
| 67 | + connector_name = (binding.metadata or {}).get("Connector") |
| 68 | + return Skip( |
| 69 | + message=( |
| 70 | + f"Connection with key '{connection_key}' of type " |
| 71 | + f"'{connector_name}' {_NOT_FOUND_SUFFIX}" |
| 72 | + ) |
| 73 | + ) |
| 74 | + |
| 75 | + resource_name: str = connection.name |
| 76 | + folder_path: str = connection.folder.get("path") |
| 77 | + |
| 78 | + found = await _find_in_resource_catalog( |
| 79 | + resource_catalog, "connection", resource_name, folder_path |
| 80 | + ) |
| 81 | + if found is None: |
| 82 | + return Skip( |
| 83 | + message=( |
| 84 | + f"Resource '{resource_name}' of type 'connection' at folder path " |
| 85 | + f"'{folder_path}' {_NOT_FOUND_SUFFIX}" |
| 86 | + ) |
| 87 | + ) |
| 88 | + return _build_create_reference(found, resource_name) |
| 89 | + |
| 90 | + |
| 91 | +async def _resolve_regular( |
| 92 | + binding: BindingResource, |
| 93 | + resource_catalog: ResourceCatalogService, |
| 94 | + supported_virtual_kinds: set[str], |
| 95 | +) -> ResourceAction | None: |
| 96 | + name_value = binding.value.get("name") |
| 97 | + folder_path_value = binding.value.get("folderPath") |
| 98 | + if not folder_path_value: |
| 99 | + # guardrail resource, nothing to import |
| 100 | + return None |
| 101 | + if name_value is None: |
| 102 | + raise ValueError(f"Binding {binding.key!r} is missing required field 'name'") |
| 103 | + resource_name: str = name_value.default_value |
| 104 | + folder_path: str = folder_path_value.default_value |
| 105 | + resource_type: str = binding.resource |
| 106 | + |
| 107 | + found = await _find_in_resource_catalog( |
| 108 | + resource_catalog, resource_type, resource_name, folder_path |
| 109 | + ) |
| 110 | + if found is not None: |
| 111 | + return _build_create_reference(found, resource_name) |
| 112 | + |
| 113 | + if resource_type not in supported_virtual_kinds: |
| 114 | + return Skip( |
| 115 | + message=( |
| 116 | + f"Cannot create virtual resource '{resource_name}' — " |
| 117 | + f"kind '{resource_type}' is not supported." |
| 118 | + ) |
| 119 | + ) |
| 120 | + |
| 121 | + sub_type: str | None = (binding.metadata or {}).get("SubType") |
| 122 | + return CreateVirtual( |
| 123 | + request=VirtualResourceRequest( |
| 124 | + kind=resource_type, |
| 125 | + name=resource_name, |
| 126 | + type=sub_type, |
| 127 | + ) |
| 128 | + ) |
| 129 | + |
| 130 | + |
| 131 | +async def _find_in_resource_catalog( |
| 132 | + resource_catalog: ResourceCatalogService, |
| 133 | + resource_type: str, |
| 134 | + name: str, |
| 135 | + folder_path: str, |
| 136 | +) -> Resource | None: |
| 137 | + """Look up a single resource in the Resource Catalog. |
| 138 | +
|
| 139 | + Returns the first match or None if the catalog can't search this kind, the |
| 140 | + folder is unknown, or no resource matches. |
| 141 | + """ |
| 142 | + catalog_type = next( |
| 143 | + (m for m in ResourceType if m.value == resource_type.lower()), None |
| 144 | + ) |
| 145 | + if catalog_type is None: |
| 146 | + return None |
| 147 | + |
| 148 | + resources = resource_catalog.list_by_type_async( |
| 149 | + resource_type=catalog_type, name=name, folder_path=folder_path |
| 150 | + ) |
| 151 | + try: |
| 152 | + return await anext(resources, None) |
| 153 | + except FolderNotFoundException: |
| 154 | + return None |
| 155 | + finally: |
| 156 | + await resources.aclose() |
| 157 | + |
| 158 | + |
| 159 | +def _build_create_reference( |
| 160 | + found_resource: Resource, resource_name: str |
| 161 | +) -> CreateReference: |
| 162 | + folder = next(iter(found_resource.folders)) |
| 163 | + return CreateReference( |
| 164 | + request=ReferencedResourceRequest( |
| 165 | + key=found_resource.resource_key, |
| 166 | + kind=found_resource.resource_type, |
| 167 | + type=found_resource.resource_sub_type, |
| 168 | + folder=ReferencedResourceFolder( |
| 169 | + folder_key=folder.key, |
| 170 | + fully_qualified_name=folder.fully_qualified_name, |
| 171 | + path=folder.path, |
| 172 | + ), |
| 173 | + ), |
| 174 | + resource_name=resource_name, |
| 175 | + kind=found_resource.resource_type, |
| 176 | + sub_type=found_resource.resource_sub_type, |
| 177 | + ) |
0 commit comments