python databricks sdk get object path from id

BriGuy
New Contributor II

when using the databricks SDK to get permissions of objects we get 

inherited_from_object=['/directories/1636517342231743']

from what I can see the workspace list and get_status methods only work with the actual path.  Is there a way to look up that directory path from the ID?

Regards

CURIOUS_DE
Valued Contributor

Did you try with the GET /api/2.0/workspace/get-status endpoint
Solution 1:-
Brute Force with workspace.list() and Recursion - This can be achieved using get_Stataus() method.

Let me know if you require code snippet.

Databricks Solution Architect

BriGuy
New Contributor II

I'd appreciate a snippet if you have one.

CURIOUS_DE
Valued Contributor

@BriGuy  Here I have small code snippet which we have used. Hope this works well with you

from databricks.sdk import WorkspaceClient

w = WorkspaceClient()

def find_path_by_object_id(target_id, base_path="/"):
    items = w.workspace.list(path=base_path)
    for item in items:
        try:
            status = w.workspace.get_status(item.path)
            if str(status.object_id) == str(target_id):
                return item.path
            if item.object_type == "DIRECTORY":
                found = find_path_by_object_id(target_id, item.path)
                if found:
                    return found
        except Exception:
            continue
    return None

target_object_id = "<yourObejctID>"
resolved_path = find_path_by_object_id(target_object_id)

print(f"Path for ID {target_object_id} is: {resolved_path}")
Databricks Solution Architect