-
Notifications
You must be signed in to change notification settings - Fork 96
Crawler: support for object ownership #2774
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
Changes from 1 commit
573fca2
b8f7e69
07fa875
28daa7e
f4e247e
d0c22db
467f912
33cb841
3a1868c
ec23bb0
b9dd2a3
a6b46c1
57bf8c3
7db7aa0
7676f7c
7010237
d1e24eb
9155e19
9980c20
fded489
53da23d
83044e8
ed38942
5d4c994
348d9b0
5cf6f30
8d4de1f
4a597a2
1818d46
c13b4eb
7e66e70
f7942aa
3cb9abf
2120322
e2189f4
0d8e48b
28ab56a
cc7db1c
0dd3f11
64048e1
8f0265f
8b944b3
86582ad
b2e66f2
953ff62
7037b6a
8282051
3d769c6
3c0a5b4
9b39e30
7029bf9
8d8191d
94a601d
b627890
ae8d194
a6b5da0
33d9c13
d677179
b68a6c4
c6de109
4deaf93
47d5343
d74c241
736ecfb
409bdf9
215a1be
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,5 +1,11 @@ | ||
import functools | ||
import logging | ||
import subprocess | ||
from collections.abc import Iterable | ||
|
||
from databricks.sdk import WorkspaceClient | ||
from databricks.sdk.service.iam import User | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
@@ -22,6 +28,55 @@ def escape_sql_identifier(path: str, *, maxsplit: int = 2) -> str: | |
return ".".join(escaped) | ||
|
||
|
||
def _has_role(user: User, role: str) -> bool: | ||
asnare marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return user.roles is not None and any(r.value == role for r in user.roles) | ||
|
||
|
||
def find_workspace_admins(ws: WorkspaceClient) -> Iterable[User]: | ||
"""Enumerate the active workspace administrators in a given workspace. | ||
|
||
Arguments: | ||
ws (WorkspaceClient): The client for the workspace whose administrators should be enumerated. | ||
Returns: | ||
Iterable[User]: The active workspace administrators, if any. | ||
""" | ||
all_users = ws.users.list(attributes="id,active,userName,roles") | ||
return (user for user in all_users if user.active and _has_role(user, "workspace_admin")) | ||
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. do we have that role and it's no longer the 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. No, this was just a guess while I figured out how it's supposed to be done. (And indeed, it now uses membership of the |
||
|
||
|
||
def find_account_admins(ws: WorkspaceClient) -> Iterable[User]: | ||
"""Enumerate the active account administrators associated with a given workspace. | ||
|
||
Arguments: | ||
ws (WorkspaceClient): The client for the workspace whose account administrators should be enumerated. | ||
Returns: | ||
Iterable[User]: The active account administrators, if any. | ||
""" | ||
response = ws.api_client.do( | ||
"GET", "/api/2.0/account/scim/v2/Users", query={"attributes": "id,active,userName,roles"} | ||
) | ||
assert isinstance(response, dict) | ||
all_users = (User.from_dict(resource) for resource in response.get("Resources", [])) | ||
return (user for user in all_users if user.active and _has_role(user, "account_admin")) | ||
|
||
|
||
def find_an_admin(ws: WorkspaceClient) -> User | None: | ||
"""Locate an active administrator for the current workspace. | ||
|
||
If an active workspace administrator can be located, this is returned. When there are multiple, they are sorted | ||
alphabetically by user-name and the first is returned. If there are no workspace administrators then an active | ||
account administrator is sought, again returning the first alphabetically by user-name if there is more than one. | ||
|
||
Arguments: | ||
ws (WorkspaceClient): The client for the workspace for which an administrator should be located. | ||
Returns: | ||
the first (alphabetically by user-name) active workspace or account administrator, or `None` if neither can be | ||
found. | ||
""" | ||
first_user = functools.partial(min, default=None, key=lambda user: user.name) | ||
return first_user(find_workspace_admins(ws)) or first_user(find_account_admins(ws)) | ||
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. don't forget to sort, because each invocation first user will be different. 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. |
||
|
||
|
||
def run_command(command: str | list[str]) -> tuple[int, str, str]: | ||
args = command.split() if isinstance(command, str) else command | ||
logger.info(f"Invoking command: {args!r}") | ||
|
Uh oh!
There was an error while loading. Please reload this page.