|
3 | 3 | import logging |
4 | 4 | from collections.abc import Sequence, Iterable |
5 | 5 |
|
| 6 | +from databricks.labs.blueprint.paths import WorkspacePath |
| 7 | +from databricks.sdk import WorkspaceClient |
| 8 | + |
6 | 9 | from databricks.labs.ucx.framework.crawlers import CrawlerBase |
7 | 10 | from databricks.labs.lsql.backends import SqlBackend |
8 | | -from databricks.sdk.errors import DatabricksError |
| 11 | +from databricks.sdk.errors import DatabricksError, NotFound |
9 | 12 |
|
10 | | -from databricks.labs.ucx.framework.owners import Ownership |
| 13 | +from databricks.labs.ucx.framework.owners import Ownership, AdministratorLocator, WorkspacePathOwnership |
11 | 14 | from databricks.labs.ucx.framework.utils import escape_sql_identifier |
12 | 15 | from databricks.labs.ucx.source_code.base import DirectFsAccess |
13 | 16 |
|
@@ -62,10 +65,35 @@ class DirectFsAccessOwnership(Ownership[DirectFsAccess]): |
62 | 65 |
|
63 | 66 | - For queries, the creator of the query (if known). |
64 | 67 | - For jobs, the owner of the path for the notebook or source (if known). |
65 | | -
|
66 | | - At present this information is not gathered during the crawling process, so it can't be reported here. |
67 | 68 | """ |
68 | 69 |
|
69 | | - def _maybe_direct_owner(self, record: DirectFsAccess) -> None: |
70 | | - # TODO: Implement this once the creator/ownership information is exposed during crawling. |
| 70 | + def __init__( |
| 71 | + self, |
| 72 | + administrator_locator: AdministratorLocator, |
| 73 | + workspace_path_ownership: WorkspacePathOwnership, |
| 74 | + workspace_client: WorkspaceClient, |
| 75 | + ) -> None: |
| 76 | + super().__init__(administrator_locator) |
| 77 | + self._workspace_path_ownership = workspace_path_ownership |
| 78 | + self._workspace_client = workspace_client |
| 79 | + |
| 80 | + def _maybe_direct_owner(self, record: DirectFsAccess) -> str | None: |
| 81 | + if record.source_type == 'QUERY': |
| 82 | + return self._query_owner(record) |
| 83 | + if record.source_type in {'NOTEBOOK', 'FILE'}: |
| 84 | + return self._notebook_owner(record) |
| 85 | + logger.warning(f"Unknown source type {record.source_type} for {record.source_id}") |
71 | 86 | return None |
| 87 | + |
| 88 | + def _notebook_owner(self, record): |
| 89 | + try: |
| 90 | + workspace_path = WorkspacePath(self._workspace_client, record.source_id) |
| 91 | + owner = self._workspace_path_ownership.owner_of(workspace_path) |
| 92 | + return owner |
| 93 | + except NotFound: |
| 94 | + return None |
| 95 | + |
| 96 | + def _query_owner(self, record): |
| 97 | + query_id = record.source_lineage[-1].object_id.split('/')[1] |
| 98 | + legacy_query = self._workspace_client.queries.get(query_id) |
| 99 | + return legacy_query.owner_user_name |
0 commit comments