Skip to content

Commit d4ddbc0

Browse files
authored
Added ability to target all Fabric data store types with create_shortcut_onelake (#501)
* Added ability to use any type of Fabric data store for the source of the create_shortcut_onelake function. * better provide backwards compatibility * optimize resolving of source and destination item names and ids * Merge latest upstream changes from main branch
1 parent 351ac09 commit d4ddbc0

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/sempy_labs/lakehouse/_shortcuts.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sempy.fabric as fabric
22
import pandas as pd
33
from sempy_labs._helper_functions import (
4+
resolve_item_name_and_id,
45
resolve_lakehouse_name_and_id,
56
resolve_workspace_id,
67
resolve_workspace_name_and_id,
@@ -18,14 +19,16 @@
1819
@log
1920
def create_shortcut_onelake(
2021
table_name: str,
21-
source_lakehouse: str | UUID,
2222
source_workspace: str | UUID,
2323
destination_lakehouse: Optional[str | UUID] = None,
2424
destination_workspace: Optional[str | UUID] = None,
2525
shortcut_name: Optional[str] = None,
26+
source_item: str | UUID = None,
27+
source_item_type: str = "Lakehouse",
2628
source_path: str = "Tables",
2729
destination_path: str = "Tables",
2830
shortcut_conflict_policy: Optional[str] = None,
31+
**kwargs,
2932
):
3033
"""
3134
Creates a `shortcut <https://learn.microsoft.com/fabric/onelake/onelake-shortcuts>`_ to a delta table in OneLake.
@@ -38,10 +41,8 @@ def create_shortcut_onelake(
3841
----------
3942
table_name : str
4043
The table name for which a shortcut will be created.
41-
source_lakehouse : str | uuid.UUID
42-
The Fabric lakehouse in which the table resides.
4344
source_workspace : str | uuid.UUID
44-
The name or ID of the Fabric workspace in which the source lakehouse exists.
45+
The name or ID of the Fabric workspace in which the source data store exists.
4546
destination_lakehouse : str | uuid.UUID, default=None
4647
The Fabric lakehouse in which the shortcut will be created.
4748
Defaults to None which resolves to the lakehouse attached to the notebook.
@@ -51,6 +52,10 @@ def create_shortcut_onelake(
5152
or if no lakehouse attached, resolves to the workspace of the notebook.
5253
shortcut_name : str, default=None
5354
The name of the shortcut 'table' to be created. This defaults to the 'table_name' parameter value.
55+
source_item : str | uuid.UUID, default=None
56+
The source Fabric data store item in which the table resides. Can be either the Name or ID of the item.
57+
source_item_type: str, default="Lakehouse"
58+
The source Fabric data store item type. Options are 'Lakehouse', 'Warehouse', 'MirroredDatabase', 'SQLDatabase', and 'KQLDatabase'.
5459
source_path : str, default="Tables"
5560
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.
5661
destination_path: str, default="Tables"
@@ -59,6 +64,14 @@ def create_shortcut_onelake(
5964
When provided, it defines the action to take when a shortcut with the same name and path already exists. The default action is 'Abort'. Additional ShortcutConflictPolicy types may be added over time.
6065
"""
6166

67+
if source_item is None:
68+
if "source_lakehouse" in kwargs:
69+
source_item = kwargs.get("source_lakehouse")
70+
else:
71+
raise ValueError(
72+
f"{icons.red_dot} The 'source_item' parameter must be provided."
73+
)
74+
6275
if not (source_path.startswith("Files") or source_path.startswith("Tables")):
6376
raise ValueError(
6477
f"{icons.red_dot} The 'source_path' parameter must be either 'Files' or 'Tables'."
@@ -69,13 +82,20 @@ def create_shortcut_onelake(
6982
raise ValueError(
7083
f"{icons.red_dot} The 'destination_path' parameter must be either 'Files' or 'Tables'."
7184
)
85+
if not (
86+
source_item_type
87+
in ["Lakehouse", "Warehouse", "MirroredDatabase", "SQLDatabase", "KQLDatabase"]
88+
):
89+
raise ValueError(
90+
f"{icons.red_dot} The 'source_item_type' parameter must be 'Lakehouse', 'Warehouse', 'MirroredDatabase', 'SQLDatabase', or 'KQLDatabase'"
91+
)
7292

7393
(source_workspace_name, source_workspace_id) = resolve_workspace_name_and_id(
7494
source_workspace
7595
)
7696

77-
(source_lakehouse_name, source_lakehouse_id) = resolve_lakehouse_name_and_id(
78-
lakehouse=source_lakehouse, workspace=source_workspace_id
97+
(source_item_name, source_item_id) = resolve_item_name_and_id(
98+
item=source_item, type=source_item_type, workspace=source_workspace_id
7999
)
80100

81101
(destination_workspace_name, destination_workspace_id) = (
@@ -99,9 +119,9 @@ def create_shortcut_onelake(
99119
"name": actual_shortcut_name,
100120
"target": {
101121
"oneLake": {
102-
"itemId": source_lakehouse_id,
103-
"path": source_full_path,
104122
"workspaceId": source_workspace_id,
123+
"itemId": source_item_id,
124+
"path": source_full_path,
105125
}
106126
},
107127
}
@@ -144,7 +164,7 @@ def create_shortcut_onelake(
144164
)
145165

146166
print(
147-
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."
167+
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."
148168
)
149169

150170

0 commit comments

Comments
 (0)