Skip to content

Added ability to target all Fabric data store types with create_shortcut_onelake #501

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions src/sempy_labs/lakehouse/_shortcuts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sempy.fabric as fabric
from sempy_labs._helper_functions import (
resolve_item_name_and_id,
resolve_lakehouse_name,
resolve_lakehouse_id,
resolve_workspace_name_and_id,
Expand All @@ -13,13 +14,15 @@

def create_shortcut_onelake(
table_name: str,
source_lakehouse: str,
source_workspace: str | UUID,
destination_lakehouse: str,
destination_workspace: Optional[str | UUID] = None,
shortcut_name: Optional[str] = None,
source_item: str | UUID = None,
source_item_type: str = "Lakehouse",
source_path: str = "Tables",
destination_path: str = "Tables",
**kwargs,
):
"""
Creates a `shortcut <https://learn.microsoft.com/fabric/onelake/onelake-shortcuts>`_ to a delta table in OneLake.
Expand All @@ -30,10 +33,8 @@ def create_shortcut_onelake(
----------
table_name : str
The table name for which a shortcut will be created.
source_lakehouse : str
The Fabric lakehouse in which the table resides.
source_workspace : str | uuid.UUID
The name or ID of the Fabric workspace in which the source lakehouse exists.
The name or ID of the Fabric workspace in which the source data store exists.
destination_lakehouse : str
The Fabric lakehouse in which the shortcut will be created.
destination_workspace : str | uuid.UUID, default=None
Expand All @@ -42,12 +43,24 @@ def create_shortcut_onelake(
or if no lakehouse attached, resolves to the workspace of the notebook.
shortcut_name : str, default=None
The name of the shortcut 'table' to be created. This defaults to the 'table_name' parameter value.
source_item : str | uuid.UUID, default=None
The source Fabric data store item in which the table resides. Can be either the Name or ID of the item.
source_item_type: str, default="Lakehouse"
The source Fabric data store item type. Options are 'Lakehouse', 'Warehouse', 'MirroredDatabase', 'SQLDatabase', and 'KQLDatabase'.
source_path : str, default="Tables"
A string representing the full path to the table/file in the source lakehouse, including either "Files" or "Tables". Examples: Tables/FolderName/SubFolderName; Files/FolderName/SubFolderName.
destination_path: str, default="Tables"
A string representing the full path where the shortcut is created, including either "Files" or "Tables". Examples: Tables/FolderName/SubFolderName; Files/FolderName/SubFolderName.
"""

if source_item is None:
if "source_lakehouse" in kwargs:
source_item = kwargs.get("source_lakehouse")
else:
raise ValueError(
f"{icons.red_dot} The 'source_item' parameter must be provided."
)

if not (source_path.startswith("Files") or source_path.startswith("Tables")):
raise ValueError(
f"{icons.red_dot} The 'source_path' parameter must be either 'Files' or 'Tables'."
Expand All @@ -58,13 +71,20 @@ def create_shortcut_onelake(
raise ValueError(
f"{icons.red_dot} The 'destination_path' parameter must be either 'Files' or 'Tables'."
)
if not (
source_item_type
in ["Lakehouse", "Warehouse", "MirroredDatabase", "SQLDatabase", "KQLDatabase"]
):
raise ValueError(
f"{icons.red_dot} The 'source_item_type' parameter must be 'Lakehouse', 'Warehouse', 'MirroredDatabase', 'SQLDatabase', or 'KQLDatabase'"
)

(source_workspace_name, source_workspace_id) = resolve_workspace_name_and_id(
source_workspace
)
source_lakehouse_id = resolve_lakehouse_id(source_lakehouse, source_workspace_id)
source_lakehouse_name = fabric.resolve_item_name(
item_id=source_lakehouse_id, type="Lakehouse", workspace=source_workspace_id

(source_item_name, source_item_id) = resolve_item_name_and_id(
item=source_item, type=source_item_type, workspace=source_workspace_id
)

if destination_workspace is None:
Expand All @@ -77,13 +97,8 @@ def create_shortcut_onelake(
)

destination_workspace_id = fabric.resolve_workspace_id(destination_workspace)
destination_lakehouse_id = resolve_lakehouse_id(
destination_lakehouse, destination_workspace
)
destination_lakehouse_name = fabric.resolve_item_name(
item_id=destination_lakehouse_id,
type="Lakehouse",
workspace=destination_workspace_id,
(destination_lakehouse_name, destination_lakehouse_id) = resolve_item_name_and_id(
item=destination_lakehouse, type="Lakehouse", workspace=destination_workspace_id
)

if shortcut_name is None:
Expand All @@ -97,7 +112,7 @@ def create_shortcut_onelake(
"target": {
"oneLake": {
"workspaceId": source_workspace_id,
"itemId": source_lakehouse_id,
"itemId": source_item_id,
"path": source_full_path,
}
},
Expand All @@ -111,7 +126,7 @@ def create_shortcut_onelake(
)

print(
f"{icons.green_dot} The shortcut '{shortcut_name}' was created in the '{destination_lakehouse_name}' lakehouse within the '{destination_workspace_name} workspace. It is based on the '{table_name}' table in the '{source_lakehouse_name}' lakehouse within the '{source_workspace_name}' workspace."
f"{icons.green_dot} The shortcut '{shortcut_name}' was created in the '{destination_lakehouse_name}' lakehouse within the '{destination_workspace_name} workspace. It is based on the '{table_name}' table in the '{source_item_name}' {source_item_type} within the '{source_workspace_name}' workspace."
)


Expand Down