44 resolve_lakehouse_id ,
55 resolve_workspace_name_and_id ,
66)
7- from typing import Optional
7+ from typing import Optional , Union
88import sempy_labs ._icons as icons
99from sempy .fabric .exceptions import FabricHTTPException
10+ from uuid import UUID
1011
1112
1213def create_shortcut_onelake (
@@ -87,57 +88,139 @@ def create_shortcut_onelake(
8788 ) from e
8889
8990
90- def create_shortcut (
91+ def create_shortcut_adls (
9192 shortcut_name : str ,
9293 location : str ,
9394 subpath : str ,
94- source : str ,
95- connection_id : str ,
95+ connection : Union [ str , UUID ] ,
96+ path : str = "Tables" ,
9697 lakehouse : Optional [str ] = None ,
9798 workspace : Optional [str ] = None ,
9899):
99100 """
100- Creates a `shortcut <https://learn.microsoft.com/fabric/onelake/onelake-shortcuts>`_ to an ADLS Gen2 or Amazon S3 source.
101+ Creates a `shortcut <https://learn.microsoft.com/fabric/onelake/onelake-shortcuts>`_ to an ADLS Gen2 source.
101102
102103 Parameters
103104 ----------
104105 shortcut_name : str
105106 location : str
106107 subpath : str
107- source : str
108- connection_id: str
109- lakehouse : str
108+ connection: Union[str, UUID]
109+ A string representing the connection that is bound with the shortcut. This can either be the connection Id or the connection name.
110+ path : str, default="Tables"
111+ A string representing the full path where the shortcut is created, including either "Files" or "Tables".
112+ lakehouse : str, default=None
110113 The Fabric lakehouse in which the shortcut will be created.
114+ Defaults to None which resolves to the default lakehouse attached to the notebook.
111115 workspace : str, default=None
112116 The name of the Fabric workspace in which the shortcut will be created.
113117 Defaults to None which resolves to the workspace of the attached lakehouse
114118 or if no lakehouse attached, resolves to the workspace of the notebook.
115119 """
116120
117- source_titles = {"adlsGen2" : "ADLS Gen2" , "amazonS3" : "Amazon S3" }
121+ _create_shortcut_base (
122+ shortcut_name = shortcut_name ,
123+ location = location ,
124+ subpath = subpath ,
125+ source = "adlsGen2" ,
126+ connection = connection ,
127+ path = path ,
128+ lakehouse = lakehouse ,
129+ workspace = workspace ,
130+ )
131+
132+
133+ def create_shortcut_amazons3 (
134+ shortcut_name : str ,
135+ location : str ,
136+ subpath : str ,
137+ connection : Union [str , UUID ],
138+ path : str = "Tables" ,
139+ lakehouse : Optional [str ] = None ,
140+ workspace : Optional [str ] = None ,
141+ ):
142+ """
143+ Creates a `shortcut <https://learn.microsoft.com/fabric/onelake/onelake-shortcuts>`_ to an Amazon S3 source.
144+
145+ Parameters
146+ ----------
147+ shortcut_name : str
148+ location : str
149+ subpath : str
150+ connection: Union[str, UUID]
151+ A string representing the connection that is bound with the shortcut. This can either be the connection Id or the connection name.
152+ path : str, default="Tables"
153+ A string representing the full path where the shortcut is created, including either "Files" or "Tables".
154+ lakehouse : str, default=None
155+ The Fabric lakehouse in which the shortcut will be created.
156+ Defaults to None which resolves to the default lakehouse attached to the notebook.
157+ workspace : str, default=None
158+ The name of the Fabric workspace in which the shortcut will be created.
159+ Defaults to None which resolves to the workspace of the attached lakehouse
160+ or if no lakehouse attached, resolves to the workspace of the notebook.
161+ """
162+
163+ _create_shortcut_base (
164+ shortcut_name = shortcut_name ,
165+ location = location ,
166+ subpath = subpath ,
167+ source = "amazonS3" ,
168+ connection = connection ,
169+ path = path ,
170+ lakehouse = lakehouse ,
171+ workspace = workspace ,
172+ )
173+
174+
175+ def _create_shortcut_base (
176+ shortcut_name : str ,
177+ location : str ,
178+ subpath : str ,
179+ source : str ,
180+ connection : Union [str , UUID ],
181+ path : str = "Tables" ,
182+ lakehouse : Optional [str ] = None ,
183+ workspace : Optional [str ] = None ,
184+ ):
185+
186+ from sempy_labs ._connections import list_connections
118187
119- sourceValues = list (source_titles .keys ())
188+ source_titles = {"adlsGen2" : "ADLS Gen2" , "amazonS3" : "Amazon S3" }
189+ sources = list (source_titles .keys ())
120190
121- if source not in sourceValues :
191+ if source not in sources :
122192 raise ValueError (
123- f"{ icons .red_dot } The 'source' parameter must be one of these values: { sourceValues } ."
193+ f"{ icons .red_dot } The 'source' parameter must be one of these values: { sources } ."
124194 )
125195
126- sourceTitle = source_titles [source ]
127-
128196 (workspace , workspace_id ) = resolve_workspace_name_and_id (workspace )
129197
130198 if lakehouse is None :
131199 lakehouse_id = fabric .get_lakehouse_id ()
132200 else :
133201 lakehouse_id = resolve_lakehouse_id (lakehouse , workspace )
202+ lakehouse = resolve_lakehouse_name (lakehouse_id , workspace )
134203
135204 client = fabric .FabricRestClient ()
136- shortcutActualName = shortcut_name .replace (" " , "" )
205+ shortcut_name_no_spaces = shortcut_name .replace (" " , "" )
206+
207+ # Validate connection
208+ dfC = list_connections ()
209+ dfC_filt = dfC [dfC ["Connection Id" ] == connection ]
210+
211+ if len (dfC_filt ) == 0 :
212+ dfC_filt = dfC [dfC ["Connection Name" ] == connection ]
213+ if len (dfC_filt ) == 0 :
214+ raise ValueError (
215+ f"{ icons .red_dot } The '{ connection } ' connection does not exist."
216+ )
217+ connection_id = dfC_filt ["Connection Id" ].iloc [0 ]
218+ else :
219+ connection_id = dfC_filt ["Connection Id" ].iloc [0 ]
137220
138221 request_body = {
139- "path" : "Tables" ,
140- "name" : shortcutActualName ,
222+ "path" : path ,
223+ "name" : shortcut_name_no_spaces ,
141224 "target" : {
142225 source : {
143226 "location" : location ,
@@ -147,22 +230,17 @@ def create_shortcut(
147230 },
148231 }
149232
150- try :
151- response = client .post (
152- f"/v1/workspaces/{ workspace_id } /items/{ lakehouse_id } /shortcuts" ,
153- json = request_body ,
154- )
155- if response .status_code == 201 :
156- print (
157- f"{ icons .green_dot } The shortcut '{ shortcutActualName } ' was created in the '{ lakehouse } ' lakehouse within"
158- f" the '{ workspace } workspace. It is based on the '{ subpath } ' table in '{ sourceTitle } '."
159- )
160- else :
161- print (response .status_code )
162- except Exception as e :
163- raise ValueError (
164- f"{ icons .red_dot } Failed to create a shortcut for the '{ shortcut_name } ' table."
165- ) from e
233+ response = client .post (
234+ f"/v1/workspaces/{ workspace_id } /items/{ lakehouse_id } /shortcuts" ,
235+ json = request_body ,
236+ )
237+
238+ if response .status_code != 201 :
239+ raise FabricHTTPException (response )
240+
241+ print (
242+ f"{ icons .green_dot } The shortcut '{ shortcut_name_no_spaces } ' was created in the '{ lakehouse } ' lakehouse within the '{ workspace } workspace. It is based on the '{ subpath } ' table in '{ source_titles [source ]} '."
243+ )
166244
167245
168246def delete_shortcut (
0 commit comments