-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathgooddata_sdk.py
More file actions
332 lines (284 loc) · 11.8 KB
/
gooddata_sdk.py
File metadata and controls
332 lines (284 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# (C) 2025 GoodData Corporation
"""Interaction with GoodData Cloud via the Gooddata Python SDK."""
from pathlib import Path
from typing import Callable
from gooddata_sdk.catalog.permission.declarative_model.permission import (
CatalogDeclarativeWorkspacePermissions,
)
from gooddata_sdk.catalog.user.entity_model.user import CatalogUser
from gooddata_sdk.catalog.user.entity_model.user_group import CatalogUserGroup
from gooddata_sdk.catalog.workspace.declarative_model.workspace.workspace import (
CatalogDeclarativeWorkspaceDataFilters,
)
from gooddata_sdk.catalog.workspace.entity_model.user_data_filter import (
CatalogUserDataFilter,
)
from gooddata_sdk.catalog.workspace.entity_model.workspace import (
CatalogWorkspace,
)
from gooddata_sdk.sdk import GoodDataSdk
from gooddata_pipelines.api.utils import raise_with_context
def apply_to_all_methods(decorator: Callable) -> Callable:
def decorate(cls: type) -> type:
for attr in cls.__dict__:
if callable(getattr(cls, attr)) and not attr.startswith("__"):
setattr(cls, attr, decorator(getattr(cls, attr)))
return cls
return decorate
@apply_to_all_methods(raise_with_context())
class SdkMethods:
"""
Class to intaract with GoodData Cloud via the Gooddata Python SDK.
"""
_sdk: GoodDataSdk
def get_organization_id(self) -> str:
return self._sdk.catalog_organization.organization_id
def check_workspace_exists(self, workspace_id: str) -> bool:
try:
self._sdk.catalog_workspace.get_workspace(workspace_id)
return True
except Exception:
return False
def delete_panther_workspace(self, workspace_id: str) -> None:
"""
Calls GoodData Python SDK to delete a workspace by its ID.
Args:
workspace_id (str): The ID of the workspace to delete.
Raises:
GoodDataApiException: If the workspace cannot be deleted, an exception
is raised with additional context information.
"""
self._sdk.catalog_workspace.delete_workspace(workspace_id)
def create_or_update_panther_workspace(
self,
workspace_id: str,
workspace_name: str,
parent_id: str | None,
**_: str,
) -> None:
"""
Calls GoodData Python SDK to create or update a workspace with the given ID,
name, and parent ID.
Args:
workspace_id (str): The ID of the workspace to create or update.
workspace_name (str): The name of the workspace.
parent_id (str | None): The ID of the parent workspace, if any.
Returns:
None
Raises:
GoodDataApiException: If the workspace cannot be created or updated,
an exception is raised with additional context information.
"""
return self._sdk.catalog_workspace.create_or_update(
CatalogWorkspace(
workspace_id=workspace_id,
name=workspace_name,
parent_id=parent_id,
)
)
def get_panther_children_workspaces(
self, parent_workspace_ids: set[str]
) -> list[CatalogWorkspace]:
"""
Calls GoodData Python SDK to retrieve all workspaces in domain and filters the
result by the set of parent workspace IDs.
Args:
parent_workspace_ids (set[str]): A set of parent workspace IDs to filter
child workspaces.
Returns:
list[CatalogWorkspace]: List of child workspaces in the parent workspace.
"""
all_workspaces: list[CatalogWorkspace] = self.list_workspaces()
children: list[CatalogWorkspace] = [
workspace
for workspace in all_workspaces
if workspace.parent_id in parent_workspace_ids
]
return children
def list_workspaces(self) -> list[CatalogWorkspace]:
"""Retrieves all workspaces in the GoodData Cloud domain.
Returns:
list[CatalogWorkspace]: A list of all workspaces in the domain.
Raises:
GoodDataApiException: If the workspaces cannot be retrieved, an exception
is raised with additional context information.
"""
return self._sdk.catalog_workspace.list_workspaces()
def get_declarative_permissions(
self, workspace_id: str
) -> CatalogDeclarativeWorkspacePermissions:
"""
Retrieves the declarative permissions for a given workspace.
Args:
workspace_id (str): The ID of the workspace for which to retrieve
permissions.
Returns:
CatalogDeclarativeWorkspacePermissions: The declarative permissions
for the workspace.
Raises:
GoodDataApiException: If the permissions cannot be retrieved, an exception
is raised with additional context information.
"""
return self._sdk.catalog_permission.get_declarative_permissions(
workspace_id
)
def put_declarative_permissions(
self,
workspace_id: str,
ws_permissions: CatalogDeclarativeWorkspacePermissions,
) -> None:
"""
Updates the declarative permissions for a given workspace.
Args:
workspace_id (str): The ID of the workspace for which to update
permissions.
ws_permissions (CatalogDeclarativeWorkspacePermissions): The new
declarative permissions to set for the workspace.
Returns:
None
Raises:
GoodDataApiException: If the permissions cannot be updated, an exception
is raised with additional context information.
"""
return self._sdk.catalog_permission.put_declarative_permissions(
workspace_id, ws_permissions
)
def create_or_update_user(self, user: CatalogUser, **_: str) -> None:
"""
Calls GoodData Python SDK to create or update a user.
Args:
user (CatalogUser): The user object to create or update.
Returns:
None
Raises:
GoodDataApiException: If the user cannot be created or updated,
an exception is raised with additional context information.
"""
return self._sdk.catalog_user.create_or_update_user(user)
def delete_user(self, user_id: str, **_: str) -> None:
"""
Calls GoodData Python SDK to delete a user by its ID.
Args:
user_id (str): The ID of the user to delete.
Returns:
None
Raises:
GoodDataApiException: If the user cannot be deleted, an exception
is raised with additional context information.
"""
return self._sdk.catalog_user.delete_user(user_id)
def list_user_groups(self) -> list[CatalogUserGroup]:
"""
Calls GoodData Python SDK to retrieve all user groups.
Returns:
list[CatalogUserGroup]: A list of all user groups in the domain.
Raises:
GoodDataApiException: If the user groups cannot be retrieved, an
exception is raised with additional context information.
"""
return self._sdk.catalog_user.list_user_groups()
def list_users(self) -> list[CatalogUser]:
"""Calls GoodData Python SDK to retrieve all users.
Returns:
list[CatalogUser]: A list of all users in the domain.
"""
return self._sdk.catalog_user.list_users()
def create_or_update_user_group(
self, catalog_user_group: CatalogUserGroup, **_: str
) -> None:
"""Calls GoodData Python SDK to create or update a user group.
Args:
catalog_user_group (CatalogUserGroup): The user group object to create or update.
Returns:
None
Raises:
GoodDataApiException: If the user group cannot be created or updated,
an exception is raised with additional context information.
"""
return self._sdk.catalog_user.create_or_update_user_group(
catalog_user_group
)
def delete_user_group(self, user_group_id: str) -> None:
"""Calls GoodData Python SDK to delete a user group by its ID.
Args:
user_group_id (str): The ID of the user group to delete.
Returns:
None
Raises:
GoodDataApiException: If the user group cannot be deleted, an exception
is raised with additional context information.
"""
return self._sdk.catalog_user.delete_user_group(user_group_id)
def get_declarative_workspace_data_filters(
self,
) -> CatalogDeclarativeWorkspaceDataFilters:
"""Retrieves the declarative workspace data filters.
Returns:
CatalogDeclarativeWorkspaceDataFilters: The declarative workspace data filters.
Raises:
GoodDataApiException: If the declarative workspace data filters cannot be retrieved,
an exception is raised with additional context information.
"""
return (
self._sdk.catalog_workspace.get_declarative_workspace_data_filters()
)
def list_user_data_filters(
self, workspace_id: str
) -> list[CatalogUserDataFilter]:
"""Lists all user data filters for a given workspace.
Args:
workspace_id (str): The ID of the workspace for which to list user data filters.
Returns:
list[CatalogUserDataFilter]: A list of user data filters for the specified workspace.
Raises:
GoodDataApiException: If the user data filters cannot be listed, an exception
is raised with additional context information.
"""
return self._sdk.catalog_workspace.list_user_data_filters(workspace_id)
def delete_user_data_filter(
self, workspace_id: str, user_data_filter_id: str
) -> None:
"""Deletes a user data filter by its ID in the specified workspace.
Args:
workspace_id (str): The ID of the workspace containing the user data filter.
user_data_filter_id (str): The ID of the user data filter to delete.
Returns:
None
Raises:
GoodDataApiException: If the user data filter cannot be deleted, an exception
is raised with additional context information.
"""
self._sdk.catalog_workspace.delete_user_data_filter(
workspace_id, user_data_filter_id
)
def create_or_update_user_data_filter(
self, workspace_id: str, user_data_filter: CatalogUserDataFilter
) -> None:
"""Creates or updates a user data filter in the specified workspace.
Args:
workspace_id (str): The ID of the workspace where the user data filter
should be created or updated.
user_data_filter (CatalogUserDataFilter): The user data filter object to create or update.
Returns:
None
Raises:
GoodDataApiException: If the user data filter cannot be created or updated,
an exception is raised with additional context information.
"""
self._sdk.catalog_workspace.create_or_update_user_data_filter(
workspace_id, user_data_filter
)
def store_declarative_workspace(
self, workspace_id: str, export_path: Path
) -> None:
"""Stores the declarative workspace in the specified export path."""
self._sdk.catalog_workspace.store_declarative_workspace(
workspace_id, export_path
)
def store_declarative_filter_views(
self, workspace_id: str, export_path: Path
) -> None:
"""Stores the declarative filter views in the specified export path."""
self._sdk.catalog_workspace.store_declarative_filter_views(
workspace_id, export_path
)