44
55from typing import TypeVar
66
7- from gooddata_pipelines .api .exceptions import GoodDataApiException
7+ from gooddata_api_client .exceptions import NotFoundException # type: ignore
8+
89from gooddata_pipelines .provisioning .entities .users .models .permissions import (
910 EntityType ,
1011 PermissionDeclaration ,
1415 WSPermissionsDeclarations ,
1516)
1617from gooddata_pipelines .provisioning .provisioning import Provisioning
17- from gooddata_pipelines .provisioning .utils .exceptions import BaseUserException
1818
1919# Type variable for permission models (PermissionIncrementalLoad or PermissionFullLoad)
2020PermissionModel = TypeVar (
@@ -109,20 +109,32 @@ def _check_user_group_exists(self, ug_id: str) -> None:
109109
110110 def _validate_permission (
111111 self , permission : PermissionFullLoad | PermissionIncrementalLoad
112- ) -> None :
113- """Validates if the permission is correctly defined."""
114- if permission .entity_type == EntityType .user :
115- self ._api .get_user (
116- permission .entity_id , error_message = "User not found"
112+ ) -> bool :
113+ """Validates that all entities referenced in the permission exist.
114+
115+ Raises:
116+ RuntimeError: If an unexpected error is encountered.
117+ """
118+ all_entities_exist = True
119+ try :
120+ if permission .entity_type == EntityType .user :
121+ self ._api ._sdk .catalog_user .get_user (permission .entity_id )
122+ else :
123+ self ._api ._sdk .catalog_user .get_user_group (permission .entity_id )
124+
125+ self ._api ._sdk .catalog_workspace .get_workspace (
126+ permission .workspace_id
117127 )
118- else :
119- self ._api .get_user_group (
120- permission .entity_id , error_message = "User group not found"
128+
129+ except NotFoundException :
130+ all_entities_exist = False
131+
132+ except Exception as e :
133+ raise RuntimeError (
134+ f"Failed to validate permission: { e .__class__ .__name__ } : { e } "
121135 )
122136
123- self ._api .get_workspace (
124- permission .workspace_id , error_message = "Workspace not found"
125- )
137+ return all_entities_exist
126138
127139 def _filter_invalid_permissions (
128140 self ,
@@ -132,11 +144,9 @@ def _filter_invalid_permissions(
132144 valid_permissions : list [PermissionModel ] = []
133145
134146 for permission in permissions :
135- try :
136- self ._validate_permission (permission )
137- except (BaseUserException , GoodDataApiException ) as e :
147+ if not self ._validate_permission (permission ):
138148 self .logger .error (
139- f"Skipping { permission } . Error: { e . error_message } "
149+ f"Skipping { permission } . Error: Permission references non-existent entities. "
140150 + f"Context: { permission .__dict__ } "
141151 )
142152 continue
0 commit comments