Skip to content

Commit 9592c31

Browse files
committed
Allow workspace group in portal objects
1 parent 209f160 commit 9592c31

File tree

2 files changed

+75
-21
lines changed

2 files changed

+75
-21
lines changed

singlestoredb/fusion/handlers/workspace.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,30 @@ def run(self, params: Dict[str, Any]) -> Optional[FusionSQLResult]:
109109

110110
# Set workspace and database
111111
if params.get('with_database'):
112-
if workspace_name and not params.get('in_group'):
113-
portal.connection = workspace_name, params['with_database']
112+
if params.get('in_group'):
113+
# Use 3-element tuple: (workspace_group_id, workspace_name_or_id,
114+
# database)
115+
portal.connection = ( # type: ignore[assignment]
116+
workspace_group.id,
117+
workspace_name or workspace_id,
118+
params['with_database'],
119+
)
114120
else:
115-
portal.connection = workspace_id, params['with_database']
121+
# Use 2-element tuple: (workspace_name_or_id, database)
122+
portal.connection = (
123+
workspace_name or workspace_id,
124+
params['with_database'],
125+
)
116126
else:
117-
if workspace_name and not params.get('in_group'):
118-
portal.workspace = workspace_name
127+
if params.get('in_group'):
128+
# Use 2-element tuple: (workspace_group_id, workspace_name_or_id)
129+
portal.workspace = ( # type: ignore[assignment]
130+
workspace_group.id,
131+
workspace_name or workspace_id,
132+
)
119133
else:
120-
portal.workspace = workspace_id
134+
# Use string: workspace_name_or_id
135+
portal.workspace = workspace_name or workspace_id
121136

122137
return None
123138

singlestoredb/notebook/_portal.py

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from typing import List
1111
from typing import Optional
1212
from typing import Tuple
13+
from typing import Union
1314

1415
from . import _objects as obj
1516
from ..management import workspace as mgr
@@ -167,15 +168,32 @@ def workspace(self) -> obj.Workspace:
167168
return obj.workspace
168169

169170
@workspace.setter
170-
def workspace(self, name_or_id: str) -> None:
171+
def workspace(self, workspace_spec: Union[str, Tuple[str, str]]) -> None:
171172
"""Set workspace."""
172-
if re.match(
173-
r'[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}',
174-
name_or_id, flags=re.I,
175-
):
176-
w = mgr.get_workspace(name_or_id)
173+
if isinstance(workspace_spec, tuple):
174+
# 2-element tuple: (workspace_group_id, workspace_name_or_id)
175+
workspace_group_id, name_or_id = workspace_spec
176+
uuid_pattern = (
177+
r'[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}'
178+
)
179+
if re.match(uuid_pattern, name_or_id, flags=re.I):
180+
w = mgr.get_workspace(name_or_id)
181+
else:
182+
w = mgr.get_workspace_group(workspace_group_id).workspaces[
183+
name_or_id
184+
]
177185
else:
178-
w = mgr.get_workspace_group(self.workspace_group_id).workspaces[name_or_id]
186+
# String: workspace_name_or_id (existing behavior)
187+
name_or_id = workspace_spec
188+
uuid_pattern = (
189+
r'[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}'
190+
)
191+
if re.match(uuid_pattern, name_or_id, flags=re.I):
192+
w = mgr.get_workspace(name_or_id)
193+
else:
194+
w = mgr.get_workspace_group(
195+
self.workspace_group_id,
196+
).workspaces[name_or_id]
179197

180198
if w.state and w.state.lower() not in ['active', 'resumed']:
181199
raise RuntimeError('workspace is not active')
@@ -196,16 +214,37 @@ def connection(self) -> Tuple[obj.Workspace, Optional[str]]:
196214
return self.workspace, self.default_database
197215

198216
@connection.setter
199-
def connection(self, workspace_and_default_database: Tuple[str, str]) -> None:
217+
def connection(
218+
self,
219+
connection_spec: Union[Tuple[str, str], Tuple[str, str, str]],
220+
) -> None:
200221
"""Set workspace and default database name."""
201-
name_or_id, default_database = workspace_and_default_database
202-
if re.match(
203-
r'[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}',
204-
name_or_id, flags=re.I,
205-
):
206-
w = mgr.get_workspace(name_or_id)
222+
if len(connection_spec) == 3:
223+
# 3-element tuple: (workspace_group_id, workspace_name_or_id,
224+
# default_database)
225+
workspace_group_id, name_or_id, default_database = connection_spec
226+
uuid_pattern = (
227+
r'[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}'
228+
)
229+
if re.match(uuid_pattern, name_or_id, flags=re.I):
230+
w = mgr.get_workspace(name_or_id)
231+
else:
232+
w = mgr.get_workspace_group(workspace_group_id).workspaces[
233+
name_or_id
234+
]
207235
else:
208-
w = mgr.get_workspace_group(self.workspace_group_id).workspaces[name_or_id]
236+
# 2-element tuple: (workspace_name_or_id, default_database)
237+
# existing behavior
238+
name_or_id, default_database = connection_spec
239+
uuid_pattern = (
240+
r'[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}'
241+
)
242+
if re.match(uuid_pattern, name_or_id, flags=re.I):
243+
w = mgr.get_workspace(name_or_id)
244+
else:
245+
w = mgr.get_workspace_group(
246+
self.workspace_group_id,
247+
).workspaces[name_or_id]
209248

210249
if w.state and w.state.lower() not in ['active', 'resumed']:
211250
raise RuntimeError('workspace is not active')

0 commit comments

Comments
 (0)