forked from gooddata/gooddata-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.py
More file actions
555 lines (429 loc) · 20.6 KB
/
Copy pathservice.py
File metadata and controls
555 lines (429 loc) · 20.6 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# (C) 2022 GoodData Corporation
from __future__ import annotations
import functools
from pathlib import Path
from gooddata_api_client.exceptions import NotFoundException
from gooddata_api_client.model.json_api_api_token_in import JsonApiApiTokenIn
from gooddata_api_client.model.json_api_api_token_in_document import JsonApiApiTokenInDocument
from gooddata_sdk.catalog.catalog_service_base import CatalogServiceBase
from gooddata_sdk.catalog.user.declarative_model.user import CatalogDeclarativeUsers
from gooddata_sdk.catalog.user.declarative_model.user_and_user_groups import CatalogDeclarativeUsersUserGroups
from gooddata_sdk.catalog.user.declarative_model.user_group import CatalogDeclarativeUserGroups
from gooddata_sdk.catalog.user.entity_model.api_token import CatalogApiToken
from gooddata_sdk.catalog.user.entity_model.user import CatalogUser, CatalogUserDocument
from gooddata_sdk.catalog.user.entity_model.user_group import CatalogUserGroup, CatalogUserGroupDocument
from gooddata_sdk.catalog.user.entity_model.user_setting import CatalogUserSetting, CatalogUserSettingDocument
from gooddata_sdk.catalog.user.management_model.management import (
CatalogPermissionAssignments,
CatalogPermissionsAssignment,
)
from gooddata_sdk.utils import load_all_entities, load_all_entities_dict
class CatalogUserService(CatalogServiceBase):
# Entity methods for users
def create_or_update_user(self, user: CatalogUser) -> None:
"""Creates a new user or overwrites an existing user.
Args:
user (CatalogUser):
User entity object.
Returns:
None
"""
try:
self.get_user(user_id=user.id)
user_document = CatalogUserDocument(data=user)
self._entities_api.update_entity_users(id=user.id, json_api_user_in_document=user_document.to_api())
except NotFoundException:
user_document = CatalogUserDocument(data=user)
self._entities_api.create_entity_users(json_api_user_in_document=user_document.to_api())
def get_user(self, user_id: str) -> CatalogUser:
"""Get an individual user using User id.
Args:
user_id (str):
User identification string. e.g. "123"
Returns:
CatalogUser:
User entity object.
"""
user_dict = self._entities_api.get_entity_users(id=user_id, include=["userGroups"]).data.to_dict(
camel_case=False
)
return CatalogUser.from_dict(user_dict, camel_case=False)
def delete_user(self, user_id: str) -> None:
"""Delete User using User id.
Args:
user_id (str):
User identification string. e.g. "123"
Returns:
None
"""
self._entities_api.delete_entity_users(id=user_id)
def list_users(self) -> list[CatalogUser]:
"""Get a list of all existing users.
Args:
None
Returns:
list[CatalogUser]:
List of all Users as User entity objects.
"""
get_users = functools.partial(
self._entities_api.get_all_entities_users,
include=["userGroups"],
_check_return_type=False,
)
users = load_all_entities_dict(get_users, camel_case=False)
return [CatalogUser.from_dict(v, camel_case=False) for v in users["data"]]
# Entity methods for user groups
def create_or_update_user_group(self, user_group: CatalogUserGroup) -> None:
"""Create a new user group or overwrite an existing user group.
Args:
user_group (CatalogUserGroup):
UserGroup entity object.
Returns:
None
"""
try:
self.get_user_group(user_group_id=user_group.id)
user_group_document = CatalogUserGroupDocument(data=user_group)
self._entities_api.update_entity_user_groups(
id=user_group.id, json_api_user_group_in_document=user_group_document.to_api()
)
except NotFoundException:
user_group_document = CatalogUserGroupDocument(data=user_group)
self._entities_api.create_entity_user_groups(user_group_document.to_api())
def get_user_group(self, user_group_id: str) -> CatalogUserGroup:
"""Get an individual user group using user group id.
Args:
user_group_id (str):
User Group identification string. e.g. "123"
Returns:
CatalogUserGroup:
UserGroup entity object.
"""
user_group = self._entities_api.get_entity_user_groups(id=user_group_id, include=["ALL"]).data.to_dict(
camel_case=False
)
return CatalogUserGroup.from_dict(user_group, camel_case=False)
def delete_user_group(self, user_group_id: str) -> None:
"""Delete User Group using User Group id.
Args:
user_group_id (str):
User Group identification string. e.g. "123"
Returns:
None
"""
self._entities_api.delete_entity_user_groups(id=user_group_id)
def list_user_groups(self) -> list[CatalogUserGroup]:
"""Get a list of all existing user groups.
Args:
None
Returns:
list[CatalogUserGroup]:
List of all User groups as UserGroup entity object.
"""
get_user_groups = functools.partial(
self._entities_api.get_all_entities_user_groups,
include=["userGroups"],
_check_return_type=False,
)
user_groups = load_all_entities(get_user_groups)
return [CatalogUserGroup.from_api(v) for v in user_groups.data]
# Declarative methods for users
def get_declarative_users(self) -> CatalogDeclarativeUsers:
"""Retrieve all users in a declarative form.
Args:
None
Returns:
CatalogDeclarativeUsers:
Declarative Users object.
"""
return CatalogDeclarativeUsers.from_api(self._layout_api.get_users_layout())
def put_declarative_users(self, users: CatalogDeclarativeUsers) -> None:
"""Set all users and their authentication properties.
Args:
users (CatalogDeclarativeUsers):
Declarative Users object, incuding authetication properties.
Returns:
None
"""
self._layout_api.put_users_layout(users.to_api())
def store_declarative_users(self, layout_root_path: Path = Path.cwd()) -> None:
"""Store users in directory hierarchy. Directly from server.
Args:
layout_root_path (Path, optional):
Path to the root of the layout directory.. Defaults to Path.cwd().
Returns:
None
"""
self.get_declarative_users().store_to_disk(self.layout_organization_folder(layout_root_path))
def load_declarative_users(self, layout_root_path: Path = Path.cwd()) -> CatalogDeclarativeUsers:
"""Load declarative users layout, which was stored using store_declarative_users.
Args:
layout_root_path (Path, optional):
Path to the root of the layout directory.. Defaults to Path.cwd().
Returns:
CatalogDeclarativeUsers:
Declarative Users object, incuding authetication properties.
"""
return CatalogDeclarativeUsers.load_from_disk(self.layout_organization_folder(layout_root_path))
def load_and_put_declarative_users(self, layout_root_path: Path = Path.cwd()) -> None:
"""Loads and sets the layouts stored using `store_declarative_users`.
This method combines `load_declarative_users` and `put_declarative_users`
methods to load and set layouts stored using `store_declarative_users`.
Args:
layout_root_path (Path, optional):
Path to the root of the layout directory.. Defaults to Path.cwd().
Returns:
None
"""
declarative_users = self.load_declarative_users(layout_root_path)
self.put_declarative_users(declarative_users)
# Declarative methods for user groups
def get_declarative_user_groups(self) -> CatalogDeclarativeUserGroups:
"""Retrieve all user groups in a declarative form.
Args:
None
Returns:
CatalogDeclarativeUserGroups:
Declarative User Groups object.
"""
return CatalogDeclarativeUserGroups.from_api(self._layout_api.get_user_groups_layout())
def put_declarative_user_groups(self, user_groups: CatalogDeclarativeUserGroups) -> None:
"""Set all user groups eventually with their parents.
Args:
user_groups (CatalogDeclarativeUserGroups):
Declarative User Groups object.
Returns:
None
"""
self._layout_api.put_user_groups_layout(user_groups.to_api())
def load_declarative_user_groups(self, layout_root_path: Path = Path.cwd()) -> CatalogDeclarativeUserGroups:
"""Load declarative users groups layout, which was stored using `store_declarative_user_groups`.
Args:
layout_root_path (Path, optional):
Path to the root of the layout directory.. Defaults to Path.cwd().
Returns:
CatalogDeclarativeUserGroups:
Declarative User Groups object.
"""
return CatalogDeclarativeUserGroups.load_from_disk(self.layout_organization_folder(layout_root_path))
def store_declarative_user_groups(self, layout_root_path: Path = Path.cwd()) -> None:
"""Stores all the user groups in a directory hierarchy.
Args:
layout_root_path (Path, optional):
Path to the root of the layout directory.. Defaults to Path.cwd().
Returns:
None
"""
self.get_declarative_user_groups().store_to_disk(self.layout_organization_folder(layout_root_path))
def load_and_put_declarative_user_groups(self, layout_root_path: Path = Path.cwd()) -> None:
"""Loads and sets the layouts stored using `store_declarative_users`.
This method combines load_declarative_users and put_declarative_users
methods to load and set layouts stored using store_declarative_users.
Args:
layout_root_path (Path, optional):
Path to the root of the layout directory.. Defaults to Path.cwd().
Returns:
None
"""
declarative_user_groups = self.load_declarative_user_groups(layout_root_path)
self.put_declarative_user_groups(declarative_user_groups)
# Declarative methods for usersUserGroups
def get_declarative_users_user_groups(self) -> CatalogDeclarativeUsersUserGroups:
"""Retrieves all users and user groups in a declarative form.
Args:
None
Returns:
CatalogDeclarativeUsersUserGroups:
Declarative Users and User Groups object.
"""
return CatalogDeclarativeUsersUserGroups.from_api(self._layout_api.get_users_user_groups_layout())
def put_declarative_users_user_groups(self, users_user_groups: CatalogDeclarativeUsersUserGroups) -> None:
"""Set all users and user groups.
Args:
users_user_groups (CatalogDeclarativeUsersUserGroups):
Declarative Users and User Groups object.
Returns:
None
"""
self._layout_api.put_users_user_groups_layout(declarative_users_user_groups=users_user_groups.to_api())
def load_declarative_users_user_groups(
self, layout_root_path: Path = Path.cwd()
) -> CatalogDeclarativeUsersUserGroups:
"""Load declarative users and user groups layout, which was stored using `store_declarative_users_user_groups`.
Args:
layout_root_path (Path, optional):
Path to the root of the layout directory.. Defaults to Path.cwd().
Returns:
CatalogDeclarativeUsersUserGroups:
Declarative Users and User Groups object.
"""
return CatalogDeclarativeUsersUserGroups.load_from_disk(
layout_organization_folder=self.layout_organization_folder(layout_root_path)
)
def store_declarative_users_user_groups(self, layout_root_path: Path = Path.cwd()) -> None:
"""Stores all the users and user groups in a directory hierarchy.
Args:
layout_root_path (Path, optional):
Path to the root of the layout directory.. Defaults to Path.cwd().
Returns:
None
"""
self.get_declarative_users_user_groups().store_to_disk(self.layout_organization_folder(layout_root_path))
def load_and_put_declarative_users_user_groups(self, layout_root_path: Path = Path.cwd()) -> None:
"""Loads and sets the layouts stored using `store_declarative_users_user_groups`.
This method combines `load_declarative_users` and `put_declarative_users_user_groups`
methods to load and set layouts stored using `store_declarative_users_user_groups`.
Args:
layout_root_path (Path, optional):
Path to the root of the layout directory.. Defaults to Path.cwd().
Returns:
None
"""
declarative_users_user_groups = self.load_declarative_users_user_groups(layout_root_path)
self.put_declarative_users_user_groups(declarative_users_user_groups)
# User management use case APIs
def get_user_permissions(self, user_id: str) -> CatalogPermissionAssignments:
"""Get permission assignments for a user.
Args:
user_id (str):
User identification string.
E.g. "demo.user"
Returns:
CatalogPermissionAssignments
"""
return CatalogPermissionAssignments.from_api(self._user_management_api.list_permissions_for_user(user_id))
def manage_user_permissions(self, user_id: str, permission_assignments: CatalogPermissionAssignments) -> None:
"""Set permission assignments for a user.
Args:
user_id (str):
User identification string.
E.g. "demo.user"
permission_assignments (CatalogPermissionAssignments):
Object containing permission assignments for workspaces and data sources.
"""
self._user_management_api.manage_permissions_for_user(user_id, permission_assignments.to_api())
def get_user_group_permissions(self, user_group_id: str) -> CatalogPermissionAssignments:
"""Get permission assignments for a user group.
Args:
user_group_id (str):
User group identification string.
E.g. "demo.users"
Returns:
CatalogPermissionAssignments
"""
return CatalogPermissionAssignments.from_api(
self._user_management_api.list_permissions_for_user_group(user_group_id)
)
def manage_user_group_permissions(
self, user_group_id: str, permission_assignments: CatalogPermissionAssignments
) -> None:
"""Set permission assignments for a user group.
Args:
user_group_id (str):
User group identification string.
E.g. "demo.users"
permission_assignments (CatalogPermissionAssignments):
Object containing permission assignments for workspaces and data sources.
"""
self._user_management_api.manage_permissions_for_user_group(user_group_id, permission_assignments.to_api())
def assign_permissions_bulk(self, permissions_assignment: CatalogPermissionsAssignment) -> None:
"""Assign permissions in bulk to a users or user groups.
Args:
permissions_assignment (CatalogPermissionsAssignment):
Object containing permission assignments for workspaces and data sources to be assigned.
Returns:
None
"""
self._user_management_api.assign_permissions(permissions_assignment.to_api())
def revoke_permissions_bulk(self, permissions_assignment: CatalogPermissionsAssignment) -> None:
"""Revoke permissions in bulk to a users or user groups.
Args:
permissions_assignment (CatalogPermissionsAssignment):
Object containing permission assignments for workspaces and data sources to be revoked.
Returns:
None
"""
self._user_management_api.revoke_permissions(permissions_assignment.to_api())
def list_user_api_tokens(self, user_id: str) -> list[CatalogApiToken]:
get_api_tokens = functools.partial(
self._entities_api.get_all_entities_api_tokens,
user_id,
_check_return_type=False,
)
api_tokens = load_all_entities(get_api_tokens)
return [CatalogApiToken(id=v["id"]) for v in api_tokens.data]
def create_user_api_token(self, user_id: str, api_token_id: str) -> CatalogApiToken:
document = JsonApiApiTokenInDocument(data=JsonApiApiTokenIn(id=api_token_id, type="apiToken"))
api_token = self._entities_api.create_entity_api_tokens(user_id, document, _check_return_type=False)
v = api_token.data
return CatalogApiToken(id=v["id"], bearer_token=v.get("attributes", {}).get("bearerToken"))
def get_user_api_token(self, user_id: str, api_token_id: str) -> CatalogApiToken:
api_token = self._entities_api.get_entity_api_tokens(user_id, api_token_id, _check_return_type=False)
v = api_token.data
return CatalogApiToken(id=v["id"])
def delete_user_api_token(self, user_id: str, api_token_id: str) -> None:
self._entities_api.delete_entity_api_tokens(user_id, api_token_id)
# Entity methods for user settings
def create_or_update_user_setting(self, user_id: str, user_setting: CatalogUserSetting) -> None:
"""Create a new user setting or overwrite an existing user setting with the same id.
Some settings are restricted to workspace/organization level only and cannot be set at user level.
The create operation will enforce these restrictions.
Args:
user_id (str):
User identification string. e.g. "demo.user"
user_setting (CatalogUserSetting):
User setting entity object.
Returns:
None
Raises:
ValueError: If trying to set a restricted setting at user level
"""
try:
self.get_user_setting(user_id, user_setting.id)
user_setting_document = CatalogUserSettingDocument(data=user_setting)
self._entities_api.update_entity_user_settings(
user_id, user_setting.id, user_setting_document.to_api()
)
except NotFoundException:
user_setting_document = CatalogUserSettingDocument(data=user_setting)
self._entities_api.create_entity_user_settings(user_id, user_setting_document.to_api())
def get_user_setting(self, user_id: str, user_setting_id: str) -> CatalogUserSetting:
"""Get an individual user setting using user id and user setting id.
Args:
user_id (str):
User identification string. e.g. "demo.user"
user_setting_id (str):
User Setting identification string. e.g. "locale"
Returns:
CatalogUserSetting:
User setting entity object.
"""
user_setting = self._entities_api.get_entity_user_settings(user_id, user_setting_id).data
return CatalogUserSetting.from_api(user_setting)
def delete_user_setting(self, user_id: str, user_setting_id: str) -> None:
"""Delete User Setting using User id and User Setting id.
Args:
user_id (str):
User identification string. e.g. "demo.user"
user_setting_id (str):
User Setting identification string. e.g. "locale"
Returns:
None
"""
self._entities_api.delete_entity_user_settings(user_id, user_setting_id)
def list_user_settings(self, user_id: str) -> list[CatalogUserSetting]:
"""Get a list of all existing user settings for a specific user.
Args:
user_id (str):
User identification string. e.g. "demo.user"
Returns:
list[CatalogUserSetting]:
List of all User Settings for the user as User Setting entity objects.
"""
get_user_settings = functools.partial(
self._entities_api.get_all_entities_user_settings,
user_id,
_check_return_type=False,
)
user_settings = load_all_entities(get_user_settings)
return [CatalogUserSetting.from_api(us) for us in user_settings.data]