-
Notifications
You must be signed in to change notification settings - Fork 96
Implementation of an Ownership factory #3072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
cc50b87
f5a9e80
37fc786
5991123
37e29e9
7c21f78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
from __future__ import annotations | ||
import logging | ||
from abc import ABC, abstractmethod | ||
from collections.abc import Callable, Iterable, Sequence | ||
from dataclasses import dataclass | ||
from datetime import timedelta | ||
from functools import cached_property | ||
from typing import Generic, TypeVar, final | ||
from typing import Generic, TypeVar, final, cast | ||
|
||
from databricks.labs.blueprint.paths import WorkspacePath | ||
from databricks.sdk import WorkspaceClient | ||
|
@@ -169,8 +171,15 @@ def get_workspace_administrator(self) -> str: | |
class Ownership(ABC, Generic[Record]): | ||
"""Determine an owner for a given type of object.""" | ||
|
||
def __init__(self, administrator_locator: AdministratorLocator) -> None: | ||
_factories: dict[type, Ownership] = {} | ||
|
||
@classmethod | ||
def for_record_type(cls, record_type: type) -> Ownership[Record]: | ||
return cast(Ownership[Record], cls._factories[record_type]) | ||
|
||
def __init__(self, administrator_locator: AdministratorLocator, record_type: type) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this control flow confuses
this will:
class AnyOwnership(Ownership[Any]):
def __init__(self, ownerships: list[Ownership]): ...
def owner_of(self, record: Any) -> str:
for o for self._ownerships: if o.is_applicable(record): return o.owner_of(record)
return self._administrator_locator.... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
self._administrator_locator = administrator_locator | ||
self._factories[record_type] = self | ||
|
||
@final | ||
def owner_of(self, record: Record) -> str: | ||
|
@@ -198,7 +207,7 @@ def _maybe_direct_owner(self, record: Record) -> str | None: | |
|
||
class WorkspacePathOwnership(Ownership[WorkspacePath]): | ||
def __init__(self, administrator_locator: AdministratorLocator, ws: WorkspaceClient) -> None: | ||
super().__init__(administrator_locator) | ||
super().__init__(administrator_locator, WorkspacePath) | ||
self._ws = ws | ||
|
||
def owner_of_path(self, path: str) -> str: | ||
|
@@ -242,14 +251,19 @@ def _infer_from_first_can_manage(object_permissions): | |
return None | ||
|
||
|
||
class LegacyQueryOwnership(Ownership[str]): | ||
@dataclass | ||
class LegacyQueryPath: | ||
path: str | ||
|
||
|
||
class LegacyQueryOwnership(Ownership[LegacyQueryPath]): | ||
def __init__(self, administrator_locator: AdministratorLocator, workspace_client: WorkspaceClient) -> None: | ||
super().__init__(administrator_locator) | ||
super().__init__(administrator_locator, LegacyQueryPath) | ||
self._workspace_client = workspace_client | ||
|
||
def _maybe_direct_owner(self, record: str) -> str | None: | ||
def _maybe_direct_owner(self, record: LegacyQueryPath) -> str | None: | ||
try: | ||
legacy_query = self._workspace_client.queries.get(record) | ||
legacy_query = self._workspace_client.queries.get(record.path) | ||
return legacy_query.owner_user_name | ||
except NotFound: | ||
return None | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please explicitly init components - we're doing that for permissions migration already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done