-
Notifications
You must be signed in to change notification settings - Fork 69
NAS-3743 added user service #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # (C) 2022 GoodData Corporation |
1 change: 1 addition & 0 deletions
1
gooddata-sdk/gooddata_sdk/catalog/user/declarative_model/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # (C) 2022 GoodData Corporation |
60 changes: 60 additions & 0 deletions
60
gooddata-sdk/gooddata_sdk/catalog/user/declarative_model/user.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # (C) 2022 GoodData Corporation | ||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from typing import List, Optional, Type | ||
|
|
||
| import attr | ||
|
|
||
| from gooddata_metadata_client.model.declarative_user import DeclarativeUser | ||
| from gooddata_metadata_client.model.declarative_users import DeclarativeUsers | ||
| from gooddata_sdk.catalog.base import Base | ||
| from gooddata_sdk.catalog.identifier import CatalogUserGroupIdentifier | ||
| from gooddata_sdk.utils import create_directory, get_sorted_yaml_files, read_layout_from_file, write_layout_to_file | ||
|
|
||
| LAYOUT_USERS_DIR = "users" | ||
|
|
||
|
|
||
| @attr.s(auto_attribs=True, kw_only=True) | ||
| class CatalogDeclarativeUsers(Base): | ||
| users: List[CatalogDeclarativeUser] | ||
|
|
||
| @staticmethod | ||
| def client_class() -> Type[DeclarativeUsers]: | ||
| return DeclarativeUsers | ||
|
|
||
| @classmethod | ||
| def load_from_disk(cls, layout_organization_folder: Path) -> CatalogDeclarativeUsers: | ||
| users_directory = layout_organization_folder / LAYOUT_USERS_DIR | ||
| user_files = get_sorted_yaml_files(users_directory) | ||
| users = [] | ||
| for user_file in user_files: | ||
| users.append(CatalogDeclarativeUser.load_from_disk(user_file)) | ||
| return cls(users=users) | ||
|
|
||
| def store_to_disk(self, layout_organization_folder: Path) -> None: | ||
| users_directory = layout_organization_folder / LAYOUT_USERS_DIR | ||
| create_directory(users_directory) | ||
| for user in self.users: | ||
| user.store_to_disk(users_directory) | ||
|
|
||
|
|
||
| @attr.s(auto_attribs=True, kw_only=True) | ||
| class CatalogDeclarativeUser(Base): | ||
| id: str | ||
| auth_id: Optional[str] = None | ||
| user_groups: List[CatalogUserGroupIdentifier] = [] | ||
|
|
||
| @staticmethod | ||
| def client_class() -> Type[DeclarativeUser]: | ||
| return DeclarativeUser | ||
|
|
||
| def store_to_disk(self, users_directory: Path) -> None: | ||
| user_path = users_directory / f"{self.id}.yaml" | ||
| user_data = self.to_dict(camel_case=True) | ||
| write_layout_to_file(user_path, user_data) | ||
|
|
||
| @classmethod | ||
| def load_from_disk(cls, users_directory: Path) -> CatalogDeclarativeUser: | ||
| data = read_layout_from_file(users_directory) | ||
| return cls.from_dict(data, camel_case=True) |
59 changes: 59 additions & 0 deletions
59
gooddata-sdk/gooddata_sdk/catalog/user/declarative_model/user_group.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # (C) 2022 GoodData Corporation | ||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from typing import List, Optional, Type | ||
|
|
||
| import attr | ||
|
|
||
| from gooddata_metadata_client.model.declarative_user_group import DeclarativeUserGroup | ||
| from gooddata_metadata_client.model.declarative_user_groups import DeclarativeUserGroups | ||
| from gooddata_sdk.catalog.base import Base | ||
| from gooddata_sdk.catalog.identifier import CatalogUserGroupIdentifier | ||
| from gooddata_sdk.utils import create_directory, get_sorted_yaml_files, read_layout_from_file, write_layout_to_file | ||
|
|
||
| LAYOUT_USER_GROUPS_DIR = "user_groups" | ||
|
|
||
|
|
||
| @attr.s(auto_attribs=True, kw_only=True) | ||
| class CatalogDeclarativeUserGroups(Base): | ||
| user_groups: List[CatalogDeclarativeUserGroup] = [] | ||
|
|
||
| @staticmethod | ||
| def client_class() -> Type[DeclarativeUserGroups]: | ||
| return DeclarativeUserGroups | ||
|
|
||
| @classmethod | ||
| def load_from_disk(cls, layout_organization_folder: Path) -> CatalogDeclarativeUserGroups: | ||
| user_groups_directory = layout_organization_folder / LAYOUT_USER_GROUPS_DIR | ||
| user_group_files = get_sorted_yaml_files(user_groups_directory) | ||
| user_groups = [] | ||
| for user_group_file in user_group_files: | ||
| user_groups.append(CatalogDeclarativeUserGroup.load_from_disk(user_group_file)) | ||
| return cls(user_groups=user_groups) | ||
|
|
||
| def store_to_disk(self, layout_organization_folder: Path) -> None: | ||
| user_groups_directory = layout_organization_folder / LAYOUT_USER_GROUPS_DIR | ||
| create_directory(user_groups_directory) | ||
| for user_group in self.user_groups: | ||
| user_group.store_to_disk(user_groups_directory) | ||
|
|
||
|
|
||
| @attr.s(auto_attribs=True, kw_only=True) | ||
| class CatalogDeclarativeUserGroup(Base): | ||
| id: str | ||
| parents: Optional[List[CatalogUserGroupIdentifier]] = None | ||
|
|
||
| @staticmethod | ||
| def client_class() -> Type[DeclarativeUserGroup]: | ||
| return DeclarativeUserGroup | ||
|
|
||
| def store_to_disk(self, user_groups_directory: Path) -> None: | ||
| user_group_path = user_groups_directory / f"{self.id}.yaml" | ||
| user_data = self.to_dict(camel_case=True) | ||
| write_layout_to_file(user_group_path, user_data) | ||
|
|
||
| @classmethod | ||
| def load_from_disk(cls, user_groups_directory: Path) -> CatalogDeclarativeUserGroup: | ||
| data = read_layout_from_file(user_groups_directory) | ||
| return cls.from_dict(data, camel_case=True) |
1 change: 1 addition & 0 deletions
1
gooddata-sdk/gooddata_sdk/catalog/user/entity_model/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # (C) 2022 GoodData Corporation |
71 changes: 71 additions & 0 deletions
71
gooddata-sdk/gooddata_sdk/catalog/user/entity_model/user.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # (C) 2022 GoodData Corporation | ||
| from __future__ import annotations | ||
|
|
||
| from typing import List, Optional, Type | ||
|
|
||
| import attr | ||
|
|
||
| from gooddata_metadata_client.model.json_api_user_in import JsonApiUserIn | ||
| from gooddata_metadata_client.model.json_api_user_in_document import JsonApiUserInDocument | ||
| from gooddata_sdk.catalog.base import Base | ||
| from gooddata_sdk.catalog.user.entity_model.user_group import CatalogUserGroup | ||
|
|
||
|
|
||
| @attr.s(auto_attribs=True, kw_only=True) | ||
| class CatalogUserDocument(Base): | ||
| data: CatalogUser | ||
|
|
||
| @staticmethod | ||
| def client_class() -> Type[JsonApiUserInDocument]: | ||
| return JsonApiUserInDocument | ||
|
|
||
| @classmethod | ||
| def create_user( | ||
| cls, user_id: str, authentication_id: Optional[str] = None, user_groups: Optional[List[str]] = None | ||
| ) -> CatalogUserDocument: | ||
| attributes = CatalogUserAttributes(authentication_id=authentication_id) | ||
| relationships = None | ||
| if user_groups is not None: | ||
| relationships = CatalogUserRelationships( | ||
| user_groups=CatalogUserGroupsData(data=[CatalogUserGroup(id=user_group) for user_group in user_groups]) | ||
| ) | ||
| user = CatalogUser(id=user_id, attributes=attributes, relationships=relationships) | ||
| return cls(data=user) | ||
|
|
||
|
|
||
| @attr.s(auto_attribs=True, kw_only=True) | ||
| class CatalogUser(Base): | ||
| id: str | ||
| attributes: Optional[CatalogUserAttributes] = None | ||
| relationships: Optional[CatalogUserRelationships] = None | ||
|
|
||
| @staticmethod | ||
| def client_class() -> Type[JsonApiUserIn]: | ||
| return JsonApiUserIn | ||
|
|
||
| @property | ||
| def get_user_groups(self) -> Optional[List[str]]: | ||
| return self.relationships.get_user_groups if self.relationships is not None else None | ||
|
|
||
|
|
||
| @attr.s(auto_attribs=True, kw_only=True) | ||
| class CatalogUserAttributes(Base): | ||
| authentication_id: Optional[str] = None | ||
|
|
||
|
|
||
| @attr.s(auto_attribs=True, kw_only=True) | ||
| class CatalogUserRelationships(Base): | ||
| user_groups: Optional[CatalogUserGroupsData] = None | ||
|
|
||
| @property | ||
| def get_user_groups(self) -> Optional[List[str]]: | ||
| return self.user_groups.get_user_groups if self.user_groups is not None else None | ||
|
|
||
|
|
||
| @attr.s(auto_attribs=True, kw_only=True) | ||
| class CatalogUserGroupsData(Base): | ||
| data: List[CatalogUserGroup] | ||
|
|
||
| @property | ||
| def get_user_groups(self) -> Optional[List[str]]: | ||
| return [user_group.id for user_group in self.data] |
52 changes: 52 additions & 0 deletions
52
gooddata-sdk/gooddata_sdk/catalog/user/entity_model/user_group.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # (C) 2022 GoodData Corporation | ||
| from __future__ import annotations | ||
|
|
||
| from typing import List, Optional, Type | ||
|
|
||
| import attr | ||
|
|
||
| from gooddata_metadata_client.model.json_api_user_group_in import JsonApiUserGroupIn | ||
| from gooddata_metadata_client.model.json_api_user_group_in_document import JsonApiUserGroupInDocument | ||
| from gooddata_sdk.catalog.base import Base | ||
|
|
||
|
|
||
| @attr.s(auto_attribs=True, kw_only=True) | ||
| class CatalogUserGroupDocument(Base): | ||
| data: CatalogUserGroup | ||
|
|
||
| @staticmethod | ||
| def client_class() -> Type[JsonApiUserGroupInDocument]: | ||
| return JsonApiUserGroupInDocument | ||
|
|
||
| @classmethod | ||
| def create_user_group( | ||
| cls, user_group_id: str, user_group_parents_id: Optional[List[str]] = None | ||
| ) -> CatalogUserGroupDocument: | ||
| relationships = None | ||
| if user_group_parents_id is not None: | ||
| relationships = CatalogUserGroupRelationships( | ||
| parents=CatalogUserGroupParents( | ||
| data=[CatalogUserGroup(id=user_group_parent_id) for user_group_parent_id in user_group_parents_id] | ||
| ) | ||
| ) | ||
| return cls(data=CatalogUserGroup(id=user_group_id, relationships=relationships)) | ||
|
|
||
|
|
||
| @attr.s(auto_attribs=True, kw_only=True) | ||
| class CatalogUserGroup(Base): | ||
| id: str | ||
| relationships: Optional[CatalogUserGroupRelationships] = None | ||
|
|
||
| @staticmethod | ||
| def client_class() -> Type[JsonApiUserGroupIn]: | ||
| return JsonApiUserGroupIn | ||
|
|
||
|
|
||
| @attr.s(auto_attribs=True, kw_only=True) | ||
| class CatalogUserGroupRelationships(Base): | ||
| parents: Optional[CatalogUserGroupParents] = None | ||
|
|
||
|
|
||
| @attr.s(auto_attribs=True, kw_only=True) | ||
| class CatalogUserGroupParents(Base): | ||
| data: Optional[List[CatalogUserGroup]] = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| # (C) 2022 GoodData Corporation | ||
| from __future__ import annotations | ||
|
|
||
| import functools | ||
| from pathlib import Path | ||
| from typing import List, Optional | ||
|
|
||
| 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_group import CatalogDeclarativeUserGroups | ||
| 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.utils import load_all_entities, load_all_entities_dict | ||
|
|
||
|
|
||
| class CatalogUserService(CatalogServiceBase): | ||
| def list_users(self) -> List[CatalogUser]: | ||
| 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"]] | ||
|
|
||
| def list_user_groups(self) -> List[CatalogUserGroup]: | ||
| 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] | ||
|
|
||
| def get_user(self, user_id: str) -> CatalogUser: | ||
| 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 create_user( | ||
| self, user_id: str, authentication_id: Optional[str] = None, user_groups: Optional[list[str]] = None | ||
| ) -> None: | ||
| user_document = CatalogUserDocument.create_user(user_id, authentication_id, user_groups) | ||
|
jaceksan marked this conversation as resolved.
|
||
| self._entities_api.create_entity_users(json_api_user_in_document=user_document.to_api()) | ||
|
|
||
| def delete_user(self, user_id: str) -> None: | ||
| self._entities_api.delete_entity_users(id=user_id) | ||
|
|
||
| def get_user_group(self, user_group_id: str) -> CatalogUserGroup: | ||
| 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 create_user_group(self, user_group_id: str, user_group_parents_id: Optional[List[str]] = None) -> None: | ||
| user_group_document = CatalogUserGroupDocument.create_user_group( | ||
| user_group_id=user_group_id, user_group_parents_id=user_group_parents_id | ||
| ) | ||
| self._entities_api.create_entity_user_groups(user_group_document.to_api()) | ||
|
|
||
| def delete_user_group(self, user_group_id: str) -> None: | ||
| self._entities_api.delete_entity_user_groups(id=user_group_id) | ||
|
|
||
| # Declarative methods for User service are listed below | ||
|
|
||
| def get_declarative_users(self) -> CatalogDeclarativeUsers: | ||
| return CatalogDeclarativeUsers.from_api(self._layout_api.get_users_layout()) | ||
|
|
||
| def get_declarative_user_groups(self) -> CatalogDeclarativeUserGroups: | ||
| return CatalogDeclarativeUserGroups.from_api(self._layout_api.get_user_groups_layout()) | ||
|
|
||
| def put_declarative_users(self, users: CatalogDeclarativeUsers) -> None: | ||
| self._layout_api.put_users_layout(users.to_api()) | ||
|
|
||
| def put_declarative_user_groups(self, user_groups: CatalogDeclarativeUserGroups) -> None: | ||
| self._layout_api.put_user_groups_layout(user_groups.to_api()) | ||
|
|
||
| def load_declarative_users(self, layout_root_path: Path = Path.cwd()) -> CatalogDeclarativeUsers: | ||
| return CatalogDeclarativeUsers.load_from_disk(self.layout_organization_folder(layout_root_path)) | ||
|
|
||
| def load_declarative_user_groups(self, layout_root_path: Path = Path.cwd()) -> CatalogDeclarativeUserGroups: | ||
| return CatalogDeclarativeUserGroups.load_from_disk(self.layout_organization_folder(layout_root_path)) | ||
|
|
||
| def store_declarative_users(self, layout_root_path: Path = Path.cwd()) -> None: | ||
| self.get_declarative_users().store_to_disk(self.layout_organization_folder(layout_root_path)) | ||
|
|
||
| def store_declarative_user_groups(self, layout_root_path: Path = Path.cwd()) -> None: | ||
| self.get_declarative_user_groups().store_to_disk(self.layout_organization_folder(layout_root_path)) | ||
|
|
||
| def load_and_put_declarative_users(self, layout_root_path: Path = Path.cwd()) -> None: | ||
| declarative_users = self.load_declarative_users(layout_root_path) | ||
| self.put_declarative_users(declarative_users) | ||
|
|
||
| def load_and_put_declarative_user_groups(self, layout_root_path: Path = Path.cwd()) -> None: | ||
| declarative_user_groups = self.load_declarative_user_groups(layout_root_path) | ||
| self.put_declarative_user_groups(declarative_user_groups) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.