pumpwood_communication.microservice_abc.system
Module for pumpwood internals calls.
13class ABCSystemMicroservice(ABC, PumpWoodMicroServiceBase): 14 """ABC class to define system associated requests. 15 16 System associated request involve list of routes, services that were 17 registed on Pumpwood, but also some test usefull features such as 18 cache invalidation and health check calls. 19 """ 20 21 def list_registered_routes(self, auth_header: dict = None): 22 """List routes that have been registed at Kong.""" 23 list_url = 'rest/pumpwood/routes/' 24 routes = self.request_get( 25 url=list_url, auth_header=auth_header) 26 for key, item in routes.items(): 27 item.sort() 28 return routes 29 30 def is_microservice_registered(self, microservice: str, 31 auth_header: dict = None) -> bool: 32 """Check if a microservice (kong service) is registered at Kong. 33 34 Args: 35 microservice (str): 36 Service associated with microservice registered on 37 Pumpwood Kong. 38 auth_header (dict): 39 Auth header to substitute the microservice original 40 at the request (user impersonation). 41 42 Returns: 43 Return true if microservice is registered. 44 """ 45 routes = self.list_registered_routes(auth_header=auth_header) 46 return microservice in routes.keys() 47 48 def list_registered_endpoints(self, auth_header: dict = None, 49 availability: str = 'front_avaiable' 50 ) -> list: 51 """List all routes and services that have been registed at Kong. 52 53 It is possible to restrict the return to end-points that should be 54 avaiable at the frontend. Using this feature it is possibel to 'hide' 55 services from GUI keeping them avaiable for programatic calls. 56 57 Args: 58 auth_header: 59 Auth header to substitute the microservice original 60 at the request (user impersonation). 61 availability: 62 Set the availability that is associated with the service. 63 So far it is implemented 'front_avaiable' and 'all'. 64 65 Returns: 66 Return a list of serialized services objects containing the 67 routes associated with at `route_set`. 68 69 Service and routes have `notes__verbose` and `description__verbose` 70 that are the repective strings associated with note and 71 description but translated using Pumpwood's I8s, 72 73 Raises: 74 PumpWoodWrongParameters: 75 Raise PumpWoodWrongParameters if availability passed as 76 paraemter is not implemented. 77 """ 78 list_url = 'rest/pumpwood/endpoints/' 79 routes = self.request_get( 80 url=list_url, parameters={'availability': availability}, 81 auth_header=auth_header) 82 return routes 83 84 def dummy_call(self, payload: dict = None, 85 auth_header: dict = None) -> dict: 86 """Return a dummy call to ensure headers and payload reaching app. 87 88 The request just bounce on the server and return the headers and 89 payload that reached the application. It is usefull for probing 90 proxy servers, API gateways and other security and load balance 91 tools. 92 93 Args: 94 payload: 95 Payload to be returned by the dummy call end-point. 96 auth_header: 97 Auth header to substitute the microservice original 98 at the request (user impersonation). 99 100 Returns: 101 Return a dictonary with: 102 - **full_path**: Full path of the request. 103 - **method**: Method used at the call 104 - **headers**: Headers at the request. 105 - **data**: Post payload sent at the request. 106 """ 107 list_url = 'rest/pumpwood/dummy-call/' 108 if payload is None: 109 return self.request_get( 110 url=list_url, auth_header=auth_header) 111 else: 112 return self.request_post( 113 url=list_url, data=payload, 114 auth_header=auth_header) 115 116 def dummy_raise(self, exception_class: str, exception_deep: int, 117 payload: dict = {}, auth_header: dict = None) -> None: 118 """Raise an Pumpwood error with the payload. 119 120 This and point raises an Arbitrary PumpWoodException error, it can be 121 used for debuging error treatment. 122 123 Args: 124 exception_class: 125 Class of the exception to be raised. 126 exception_deep: 127 Deep of the exception in microservice calls. This arg will 128 make error recusive, calling the end-point it self for 129 `exception_deep` time before raising the error. 130 payload: 131 Payload that will be returned with error. 132 auth_header: 133 Auth header to substitute the microservice original 134 at the request (user impersonation). 135 136 Returns: 137 Should not return any results, all possible call should result 138 in raising the correspondent error. 139 140 Raises: 141 Should raise the correspondent error passed on exception_class 142 arg, with payload. 143 """ 144 url = 'rest/pumpwood/dummy-raise/' 145 payload["exception_class"] = exception_class 146 payload["exception_deep"] = exception_deep 147 self.request_post(url=url, data=payload, auth_header=auth_header) 148 149 def clear_service_cache(self, service: str, 150 auth_header: dict = None) -> bool: 151 """Clear disk cache associated with service container. 152 153 This request will ask service to clear disk cache. If there are 154 replicas at same service it is possible that the cache might be 155 invalidated on just one of the replicas leading to inconsistent 156 cache behavior. 157 158 Args: 159 service (str): 160 Pumpwood service to have its cache invalidated, it is expected 161 that the service implement the invalidation end-point 162 `/rest/[service-name]/clear-diskcache`. 163 auth_header (dict): 164 Auth header to substitute the microservice original 165 at the request (user impersonation). 166 167 Returns: 168 True if cache was invalidated. 169 """ 170 # Service url begging are related to service general functions 171 url = 'service/{service}/clear-diskcache/'\ 172 .format(service=service) 173 response = self.request_post( 174 url=url, data={}, auth_header=auth_header) 175 return response 176 177 def health_check_service(self, service: str, 178 raise_error: bool = False) -> bool: 179 """Hits the health_check, it is not necessary authentication. 180 181 Helth check end-points are simple and does not involve connection 182 with database or storage. It will just check if container is up 183 and receiving requests. It is usefull for tests and for inter-service 184 calls. 185 186 If `raise_error` is set as True, function will raise 187 PumpWoodMicroserviceUnavailableError. 188 189 Args: 190 service (str): 191 Pumpwood service to have its cache invalidated, it is expected 192 that the service implement the invalidation end-point 193 `/rest/[service-name]/clear-diskcache`. 194 raise_error (bool): 195 If request does not return 200 status, it will raise a 196 PumpWoodMicroserviceUnavailableError. 197 198 Returns: 199 True if service is healthy. 200 201 Raises: 202 PumpWoodMicroserviceUnavailableError if request does not 203 return 200 code. 204 """ 205 # Service url begging are related to service general functions 206 url = 'service/{service}/health-check/'.format(service=service) 207 get_url = urljoin(self.server_url, url) 208 response = requests.get(get_url, timeout=5) 209 210 # Raise a Pumpwood error if raise_if_not_healthy informing that the 211 # service health-check did not return a OK status 212 if not response.ok and raise_error: 213 msg = ( 214 "Call on service [{service}] health-check end-point did not " 215 "return a OK status.") 216 raise PumpWoodMicroserviceUnavailableError( 217 msg, payload={'service': service}) 218 219 return response.ok
ABC class to define system associated requests.
System associated request involve list of routes, services that were registed on Pumpwood, but also some test usefull features such as cache invalidation and health check calls.
21 def list_registered_routes(self, auth_header: dict = None): 22 """List routes that have been registed at Kong.""" 23 list_url = 'rest/pumpwood/routes/' 24 routes = self.request_get( 25 url=list_url, auth_header=auth_header) 26 for key, item in routes.items(): 27 item.sort() 28 return routes
List routes that have been registed at Kong.
30 def is_microservice_registered(self, microservice: str, 31 auth_header: dict = None) -> bool: 32 """Check if a microservice (kong service) is registered at Kong. 33 34 Args: 35 microservice (str): 36 Service associated with microservice registered on 37 Pumpwood Kong. 38 auth_header (dict): 39 Auth header to substitute the microservice original 40 at the request (user impersonation). 41 42 Returns: 43 Return true if microservice is registered. 44 """ 45 routes = self.list_registered_routes(auth_header=auth_header) 46 return microservice in routes.keys()
Check if a microservice (kong service) is registered at Kong.
Arguments:
- microservice (str): Service associated with microservice registered on Pumpwood Kong.
- auth_header (dict): Auth header to substitute the microservice original at the request (user impersonation).
Returns:
Return true if microservice is registered.
48 def list_registered_endpoints(self, auth_header: dict = None, 49 availability: str = 'front_avaiable' 50 ) -> list: 51 """List all routes and services that have been registed at Kong. 52 53 It is possible to restrict the return to end-points that should be 54 avaiable at the frontend. Using this feature it is possibel to 'hide' 55 services from GUI keeping them avaiable for programatic calls. 56 57 Args: 58 auth_header: 59 Auth header to substitute the microservice original 60 at the request (user impersonation). 61 availability: 62 Set the availability that is associated with the service. 63 So far it is implemented 'front_avaiable' and 'all'. 64 65 Returns: 66 Return a list of serialized services objects containing the 67 routes associated with at `route_set`. 68 69 Service and routes have `notes__verbose` and `description__verbose` 70 that are the repective strings associated with note and 71 description but translated using Pumpwood's I8s, 72 73 Raises: 74 PumpWoodWrongParameters: 75 Raise PumpWoodWrongParameters if availability passed as 76 paraemter is not implemented. 77 """ 78 list_url = 'rest/pumpwood/endpoints/' 79 routes = self.request_get( 80 url=list_url, parameters={'availability': availability}, 81 auth_header=auth_header) 82 return routes
List all routes and services that have been registed at Kong.
It is possible to restrict the return to end-points that should be avaiable at the frontend. Using this feature it is possibel to 'hide' services from GUI keeping them avaiable for programatic calls.
Arguments:
- auth_header: Auth header to substitute the microservice original at the request (user impersonation).
- availability: Set the availability that is associated with the service. So far it is implemented 'front_avaiable' and 'all'.
Returns:
Return a list of serialized services objects containing the routes associated with at
route_set.Service and routes have
notes__verboseanddescription__verbosethat are the repective strings associated with note and description but translated using Pumpwood's I8s,
Raises:
- PumpWoodWrongParameters: Raise PumpWoodWrongParameters if availability passed as paraemter is not implemented.
84 def dummy_call(self, payload: dict = None, 85 auth_header: dict = None) -> dict: 86 """Return a dummy call to ensure headers and payload reaching app. 87 88 The request just bounce on the server and return the headers and 89 payload that reached the application. It is usefull for probing 90 proxy servers, API gateways and other security and load balance 91 tools. 92 93 Args: 94 payload: 95 Payload to be returned by the dummy call end-point. 96 auth_header: 97 Auth header to substitute the microservice original 98 at the request (user impersonation). 99 100 Returns: 101 Return a dictonary with: 102 - **full_path**: Full path of the request. 103 - **method**: Method used at the call 104 - **headers**: Headers at the request. 105 - **data**: Post payload sent at the request. 106 """ 107 list_url = 'rest/pumpwood/dummy-call/' 108 if payload is None: 109 return self.request_get( 110 url=list_url, auth_header=auth_header) 111 else: 112 return self.request_post( 113 url=list_url, data=payload, 114 auth_header=auth_header)
Return a dummy call to ensure headers and payload reaching app.
The request just bounce on the server and return the headers and payload that reached the application. It is usefull for probing proxy servers, API gateways and other security and load balance tools.
Arguments:
- payload: Payload to be returned by the dummy call end-point.
- auth_header: Auth header to substitute the microservice original at the request (user impersonation).
Returns:
Return a dictonary with:
- full_path: Full path of the request.
- method: Method used at the call
- headers: Headers at the request.
- data: Post payload sent at the request.
116 def dummy_raise(self, exception_class: str, exception_deep: int, 117 payload: dict = {}, auth_header: dict = None) -> None: 118 """Raise an Pumpwood error with the payload. 119 120 This and point raises an Arbitrary PumpWoodException error, it can be 121 used for debuging error treatment. 122 123 Args: 124 exception_class: 125 Class of the exception to be raised. 126 exception_deep: 127 Deep of the exception in microservice calls. This arg will 128 make error recusive, calling the end-point it self for 129 `exception_deep` time before raising the error. 130 payload: 131 Payload that will be returned with error. 132 auth_header: 133 Auth header to substitute the microservice original 134 at the request (user impersonation). 135 136 Returns: 137 Should not return any results, all possible call should result 138 in raising the correspondent error. 139 140 Raises: 141 Should raise the correspondent error passed on exception_class 142 arg, with payload. 143 """ 144 url = 'rest/pumpwood/dummy-raise/' 145 payload["exception_class"] = exception_class 146 payload["exception_deep"] = exception_deep 147 self.request_post(url=url, data=payload, auth_header=auth_header)
Raise an Pumpwood error with the payload.
This and point raises an Arbitrary PumpWoodException error, it can be used for debuging error treatment.
Arguments:
- exception_class: Class of the exception to be raised.
- exception_deep: Deep of the exception in microservice calls. This arg will
make error recusive, calling the end-point it self for
exception_deeptime before raising the error. - payload: Payload that will be returned with error.
- auth_header: Auth header to substitute the microservice original at the request (user impersonation).
Returns:
Should not return any results, all possible call should result in raising the correspondent error.
Raises:
- Should raise the correspondent error passed on exception_class
- arg, with payload.
149 def clear_service_cache(self, service: str, 150 auth_header: dict = None) -> bool: 151 """Clear disk cache associated with service container. 152 153 This request will ask service to clear disk cache. If there are 154 replicas at same service it is possible that the cache might be 155 invalidated on just one of the replicas leading to inconsistent 156 cache behavior. 157 158 Args: 159 service (str): 160 Pumpwood service to have its cache invalidated, it is expected 161 that the service implement the invalidation end-point 162 `/rest/[service-name]/clear-diskcache`. 163 auth_header (dict): 164 Auth header to substitute the microservice original 165 at the request (user impersonation). 166 167 Returns: 168 True if cache was invalidated. 169 """ 170 # Service url begging are related to service general functions 171 url = 'service/{service}/clear-diskcache/'\ 172 .format(service=service) 173 response = self.request_post( 174 url=url, data={}, auth_header=auth_header) 175 return response
Clear disk cache associated with service container.
This request will ask service to clear disk cache. If there are replicas at same service it is possible that the cache might be invalidated on just one of the replicas leading to inconsistent cache behavior.
Arguments:
- service (str): Pumpwood service to have its cache invalidated, it is expected
that the service implement the invalidation end-point
/rest/[service-name]/clear-diskcache. - auth_header (dict): Auth header to substitute the microservice original at the request (user impersonation).
Returns:
True if cache was invalidated.
177 def health_check_service(self, service: str, 178 raise_error: bool = False) -> bool: 179 """Hits the health_check, it is not necessary authentication. 180 181 Helth check end-points are simple and does not involve connection 182 with database or storage. It will just check if container is up 183 and receiving requests. It is usefull for tests and for inter-service 184 calls. 185 186 If `raise_error` is set as True, function will raise 187 PumpWoodMicroserviceUnavailableError. 188 189 Args: 190 service (str): 191 Pumpwood service to have its cache invalidated, it is expected 192 that the service implement the invalidation end-point 193 `/rest/[service-name]/clear-diskcache`. 194 raise_error (bool): 195 If request does not return 200 status, it will raise a 196 PumpWoodMicroserviceUnavailableError. 197 198 Returns: 199 True if service is healthy. 200 201 Raises: 202 PumpWoodMicroserviceUnavailableError if request does not 203 return 200 code. 204 """ 205 # Service url begging are related to service general functions 206 url = 'service/{service}/health-check/'.format(service=service) 207 get_url = urljoin(self.server_url, url) 208 response = requests.get(get_url, timeout=5) 209 210 # Raise a Pumpwood error if raise_if_not_healthy informing that the 211 # service health-check did not return a OK status 212 if not response.ok and raise_error: 213 msg = ( 214 "Call on service [{service}] health-check end-point did not " 215 "return a OK status.") 216 raise PumpWoodMicroserviceUnavailableError( 217 msg, payload={'service': service}) 218 219 return response.ok
Hits the health_check, it is not necessary authentication.
Helth check end-points are simple and does not involve connection with database or storage. It will just check if container is up and receiving requests. It is usefull for tests and for inter-service calls.
If raise_error is set as True, function will raise
PumpWoodMicroserviceUnavailableError.
Arguments:
- service (str): Pumpwood service to have its cache invalidated, it is expected
that the service implement the invalidation end-point
/rest/[service-name]/clear-diskcache. - raise_error (bool): If request does not return 200 status, it will raise a PumpWoodMicroserviceUnavailableError.
Returns:
True if service is healthy.
Raises:
- PumpWoodMicroserviceUnavailableError if request does not
- return 200 code.
12class ABCPermissionMicroservice(ABC, PumpWoodMicroServiceBase): 13 """Abstract class for permission checking at pumpwood.""" 14 15 def check_if_logged(self, auth_header: dict = None) -> bool: 16 """Check if user is logged. 17 18 Args: 19 auth_header (dict): = None 20 AuthHeader to substitute the microservice original at 21 request. If not passed, microservice object auth_header 22 will be used. 23 24 Returns: 25 Return True if auth_header is looged and False if not 26 """ 27 try: 28 check = self.request_get( 29 url="rest/registration/check/", 30 auth_header=auth_header) 31 except PumpWoodUnauthorized: 32 return False 33 return check 34 35 def get_user_info(self, auth_header: dict = None, 36 use_disk_cache: bool = False, 37 disk_cache_expire: int = None) -> dict: 38 """Get user info. 39 40 Args: 41 auth_header (dict): = None 42 AuthHeader to substitute the microservice original at 43 request. If not passed, microservice object auth_header 44 will be used. 45 use_disk_cache (bool): 46 It possible use disk cache. 47 disk_cache_expire (int): 48 Set a time to expire the cache. If not passed env variable 49 ``PUMPWOOD_COMMUNICATION__AUTHORIZATION_CACHE_TIMEOUT`` 50 env variable (legacy spelling supported), default 60 s. 51 52 Returns: 53 A serialized user object with information of the logged user. 54 """ 55 url = "rest/registration/retrieveauthenticateduser/" 56 temp_disk_cache_expire = ( 57 disk_cache_expire 58 if disk_cache_expire is not None else 59 AUTHORIZATION_CACHE_TIMEOUT) 60 61 user_info = self.request_get( 62 url=url, auth_header=auth_header, use_disk_cache=use_disk_cache, 63 disk_cache_expire=temp_disk_cache_expire) 64 return user_info 65 66 def check_permission(self, model_class: str, end_point: str, 67 extra_arg: str = None, allow_service_user: str = None, 68 allow_external: str = None, auth_header: dict = None, 69 ) -> dict: 70 """Get user info. 71 72 Args: 73 model_class (str): 74 Model class associated to be checked for access. 75 end_point (str): 76 Name of the end-point that will be checked for permission. Ex.: 77 retrieve, save, list, list-without-pag, ... 78 extra_arg (str): 79 Used on some end-points. On action end-point it is reponsible 80 for setting the action associated with the call. 81 allow_service_user: str = None: 82 83 allow_external: str = None: 84 85 auth_header (dict): 86 AuthHeader to substitute the microservice original at 87 request. If not passed, microservice object auth_header 88 will be used. 89 90 Returns: 91 A serialized user object with information of the logged user. 92 """ 93 # user_info = self.request_post( 94 # url="rest/registration/check/", 95 # payload={ 96 # 'end_point': end_point, 97 # 'first_arg': first_arg, 98 # 'second_arg': second_arg, 99 # 'api_config_allow': api_config_allow, 100 # 'api_config_deny': api_config_deny}, 101 # auth_header=auth_header, timeout=self.default_timeout) 102 # return user_info 103 return True 104 105 def get_user_row_permission(self, auth_header: dict = None, 106 use_disk_cache: bool = False, 107 disk_cache_expire: int = None, 108 return_ids: bool = False 109 ) -> list[dict] | list[int]: 110 """Get all user associated row pemission. 111 112 It will include row permissions associated directly to user or by 113 row permission associated with user groups that they are into. 114 115 Args: 116 auth_header (dict): = None 117 AuthHeader to substitute the microservice original at 118 request. If not passed, microservice object auth_header 119 will be used. 120 use_disk_cache (bool): 121 It possible use disk cache. 122 disk_cache_expire (int): 123 Set a time to expire the cache. If not passed env variable 124 ``PUMPWOOD_COMMUNICATION__AUTHORIZATION_CACHE_TIMEOUT`` 125 env variable (legacy spelling supported), default 60 s. 126 return_ids (bool): 127 Return only the ids of the row permissions. 128 129 Returns: 130 Return a list Row permissions associatef with user directly or 131 by a user group. 132 """ 133 url = '/rest/userprofile/actions/self_row_permissions/' 134 temp_disk_cache_expire = ( 135 disk_cache_expire 136 if disk_cache_expire is not None else 137 AUTHORIZATION_CACHE_TIMEOUT) 138 139 # Try to retrieve disk cache using authorization header to create 140 # the cache hash 141 request_header = self._check_auth_header(auth_header) 142 hash_dict = None 143 results = None 144 is_on_cache = False 145 if use_disk_cache: 146 hash_dict = { 147 'context': 'pumpwood_communication-get_user_row_permission', 148 'authorization': request_header['Authorization']} 149 results = default_cache.get(hash_dict=hash_dict) 150 if results is not None: 151 msg = "row_permission from cache url[{url}]".format(url=url) 152 logger.info(msg) 153 is_on_cache = True 154 155 if results is None: 156 results = self.request_post( 157 url=url, auth_header=auth_header, data={})['result'] 158 159 if use_disk_cache and not is_on_cache: 160 default_cache.set( 161 hash_dict=hash_dict, value=results, 162 expire=temp_disk_cache_expire) 163 164 if return_ids: 165 return [x['pk'] for x in results] 166 else: 167 return results
Abstract class for permission checking at pumpwood.
15 def check_if_logged(self, auth_header: dict = None) -> bool: 16 """Check if user is logged. 17 18 Args: 19 auth_header (dict): = None 20 AuthHeader to substitute the microservice original at 21 request. If not passed, microservice object auth_header 22 will be used. 23 24 Returns: 25 Return True if auth_header is looged and False if not 26 """ 27 try: 28 check = self.request_get( 29 url="rest/registration/check/", 30 auth_header=auth_header) 31 except PumpWoodUnauthorized: 32 return False 33 return check
Check if user is logged.
Arguments:
- auth_header (dict): = None AuthHeader to substitute the microservice original at request. If not passed, microservice object auth_header will be used.
Returns:
Return True if auth_header is looged and False if not
35 def get_user_info(self, auth_header: dict = None, 36 use_disk_cache: bool = False, 37 disk_cache_expire: int = None) -> dict: 38 """Get user info. 39 40 Args: 41 auth_header (dict): = None 42 AuthHeader to substitute the microservice original at 43 request. If not passed, microservice object auth_header 44 will be used. 45 use_disk_cache (bool): 46 It possible use disk cache. 47 disk_cache_expire (int): 48 Set a time to expire the cache. If not passed env variable 49 ``PUMPWOOD_COMMUNICATION__AUTHORIZATION_CACHE_TIMEOUT`` 50 env variable (legacy spelling supported), default 60 s. 51 52 Returns: 53 A serialized user object with information of the logged user. 54 """ 55 url = "rest/registration/retrieveauthenticateduser/" 56 temp_disk_cache_expire = ( 57 disk_cache_expire 58 if disk_cache_expire is not None else 59 AUTHORIZATION_CACHE_TIMEOUT) 60 61 user_info = self.request_get( 62 url=url, auth_header=auth_header, use_disk_cache=use_disk_cache, 63 disk_cache_expire=temp_disk_cache_expire) 64 return user_info
Get user info.
Arguments:
- auth_header (dict): = None AuthHeader to substitute the microservice original at request. If not passed, microservice object auth_header will be used.
- use_disk_cache (bool): It possible use disk cache.
- disk_cache_expire (int): Set a time to expire the cache. If not passed env variable
PUMPWOOD_COMMUNICATION__AUTHORIZATION_CACHE_TIMEOUTenv variable (legacy spelling supported), default 60 s.
Returns:
A serialized user object with information of the logged user.
66 def check_permission(self, model_class: str, end_point: str, 67 extra_arg: str = None, allow_service_user: str = None, 68 allow_external: str = None, auth_header: dict = None, 69 ) -> dict: 70 """Get user info. 71 72 Args: 73 model_class (str): 74 Model class associated to be checked for access. 75 end_point (str): 76 Name of the end-point that will be checked for permission. Ex.: 77 retrieve, save, list, list-without-pag, ... 78 extra_arg (str): 79 Used on some end-points. On action end-point it is reponsible 80 for setting the action associated with the call. 81 allow_service_user: str = None: 82 83 allow_external: str = None: 84 85 auth_header (dict): 86 AuthHeader to substitute the microservice original at 87 request. If not passed, microservice object auth_header 88 will be used. 89 90 Returns: 91 A serialized user object with information of the logged user. 92 """ 93 # user_info = self.request_post( 94 # url="rest/registration/check/", 95 # payload={ 96 # 'end_point': end_point, 97 # 'first_arg': first_arg, 98 # 'second_arg': second_arg, 99 # 'api_config_allow': api_config_allow, 100 # 'api_config_deny': api_config_deny}, 101 # auth_header=auth_header, timeout=self.default_timeout) 102 # return user_info 103 return True
Get user info.
Arguments:
- model_class (str): Model class associated to be checked for access.
- end_point (str): Name of the end-point that will be checked for permission. Ex.: retrieve, save, list, list-without-pag, ...
- extra_arg (str): Used on some end-points. On action end-point it is reponsible for setting the action associated with the call.
- allow_service_user: str = None:
- allow_external: str = None:
- auth_header (dict): AuthHeader to substitute the microservice original at request. If not passed, microservice object auth_header will be used.
Returns:
A serialized user object with information of the logged user.
105 def get_user_row_permission(self, auth_header: dict = None, 106 use_disk_cache: bool = False, 107 disk_cache_expire: int = None, 108 return_ids: bool = False 109 ) -> list[dict] | list[int]: 110 """Get all user associated row pemission. 111 112 It will include row permissions associated directly to user or by 113 row permission associated with user groups that they are into. 114 115 Args: 116 auth_header (dict): = None 117 AuthHeader to substitute the microservice original at 118 request. If not passed, microservice object auth_header 119 will be used. 120 use_disk_cache (bool): 121 It possible use disk cache. 122 disk_cache_expire (int): 123 Set a time to expire the cache. If not passed env variable 124 ``PUMPWOOD_COMMUNICATION__AUTHORIZATION_CACHE_TIMEOUT`` 125 env variable (legacy spelling supported), default 60 s. 126 return_ids (bool): 127 Return only the ids of the row permissions. 128 129 Returns: 130 Return a list Row permissions associatef with user directly or 131 by a user group. 132 """ 133 url = '/rest/userprofile/actions/self_row_permissions/' 134 temp_disk_cache_expire = ( 135 disk_cache_expire 136 if disk_cache_expire is not None else 137 AUTHORIZATION_CACHE_TIMEOUT) 138 139 # Try to retrieve disk cache using authorization header to create 140 # the cache hash 141 request_header = self._check_auth_header(auth_header) 142 hash_dict = None 143 results = None 144 is_on_cache = False 145 if use_disk_cache: 146 hash_dict = { 147 'context': 'pumpwood_communication-get_user_row_permission', 148 'authorization': request_header['Authorization']} 149 results = default_cache.get(hash_dict=hash_dict) 150 if results is not None: 151 msg = "row_permission from cache url[{url}]".format(url=url) 152 logger.info(msg) 153 is_on_cache = True 154 155 if results is None: 156 results = self.request_post( 157 url=url, auth_header=auth_header, data={})['result'] 158 159 if use_disk_cache and not is_on_cache: 160 default_cache.set( 161 hash_dict=hash_dict, value=results, 162 expire=temp_disk_cache_expire) 163 164 if return_ids: 165 return [x['pk'] for x in results] 166 else: 167 return results
Get all user associated row pemission.
It will include row permissions associated directly to user or by row permission associated with user groups that they are into.
Arguments:
- auth_header (dict): = None AuthHeader to substitute the microservice original at request. If not passed, microservice object auth_header will be used.
- use_disk_cache (bool): It possible use disk cache.
- disk_cache_expire (int): Set a time to expire the cache. If not passed env variable
PUMPWOOD_COMMUNICATION__AUTHORIZATION_CACHE_TIMEOUTenv variable (legacy spelling supported), default 60 s. - return_ids (bool): Return only the ids of the row permissions.
Returns:
Return a list Row permissions associatef with user directly or by a user group.
9class ABCETLMicroservice(ABC, PumpWoodMicroServiceBase): 10 """ABC class to define ETL associated requests. 11 12 ETL associated request involve trigger an ETL process. 13 """ 14 15 def trigger_etl_process(self, model_class: str, 16 trigger_type: Literal[ 17 'create', 'update', 'delete', 'action', 18 'process_finish'], 19 object_id: int | None = None, 20 action_name: str | None = None, 21 parameters: dict | None = None, 22 process_name: str | None = None, 23 send_to_rabbitmq: bool = True, 24 auth_header: dict | None = None 25 ) -> bool | None: 26 """Trigger an ETL process via the ETLTrigger action. 27 28 If ``pumpwood-etl-app`` is not registered at Kong, no request is 29 sent and the method returns ``None``. 30 31 Args: 32 model_class (str): 33 Model class name that originated the trigger. 34 trigger_type (Literal): 35 Kind of event: ``create``, ``update``, ``delete``, 36 ``action``, or ``process_finish``. 37 object_id (int | None): 38 Primary key of the affected object, when applicable. 39 action_name (str | None): 40 Name of the action when ``trigger_type`` is ``action``. 41 parameters (dict | None): 42 Extra parameters forwarded to matching ETL triggers. 43 process_name (str | None): 44 Optional ETL process name to restrict matching triggers. 45 send_to_rabbitmq (bool): 46 If True, enqueue work on RabbitMQ. Defaults to True. 47 auth_header (dict | None): 48 Auth header to substitute the microservice original at 49 the request (user impersonation). 50 51 Returns: 52 bool | None: 53 The ``result`` field from ``process_matching_triggers`` 54 when the ETL app is registered; ``None`` when it is not. 55 56 Raises: 57 PumpWoodException: 58 If ``process_matching_triggers`` is not available on 59 ``ETLTrigger``. 60 PumpWoodActionArgsException: 61 If action arguments are invalid for 62 ``process_matching_triggers``. 63 PumpWoodObjectDoesNotExist: 64 If a requested object referenced by the action is not 65 found. 66 """ 67 is_etl_app_registered = self.is_microservice_registered( 68 microservice="pumpwood-etl-app", 69 auth_header=auth_header) 70 if not is_etl_app_registered: 71 return None 72 73 # Trigger the ETL process 74 action_result = self.execute_action( 75 model_class="ETLTrigger", 76 action="process_matching_triggers", 77 parameters={ 78 'model_class': model_class, 79 'trigger_type': trigger_type, 80 'object_id': object_id, 81 'action_name': action_name, 82 'parameters': parameters, 83 'process_name': process_name, 84 'send_to_rabbitmq': send_to_rabbitmq}, 85 auth_header=auth_header)['result'] 86 return action_result
ABC class to define ETL associated requests.
ETL associated request involve trigger an ETL process.
15 def trigger_etl_process(self, model_class: str, 16 trigger_type: Literal[ 17 'create', 'update', 'delete', 'action', 18 'process_finish'], 19 object_id: int | None = None, 20 action_name: str | None = None, 21 parameters: dict | None = None, 22 process_name: str | None = None, 23 send_to_rabbitmq: bool = True, 24 auth_header: dict | None = None 25 ) -> bool | None: 26 """Trigger an ETL process via the ETLTrigger action. 27 28 If ``pumpwood-etl-app`` is not registered at Kong, no request is 29 sent and the method returns ``None``. 30 31 Args: 32 model_class (str): 33 Model class name that originated the trigger. 34 trigger_type (Literal): 35 Kind of event: ``create``, ``update``, ``delete``, 36 ``action``, or ``process_finish``. 37 object_id (int | None): 38 Primary key of the affected object, when applicable. 39 action_name (str | None): 40 Name of the action when ``trigger_type`` is ``action``. 41 parameters (dict | None): 42 Extra parameters forwarded to matching ETL triggers. 43 process_name (str | None): 44 Optional ETL process name to restrict matching triggers. 45 send_to_rabbitmq (bool): 46 If True, enqueue work on RabbitMQ. Defaults to True. 47 auth_header (dict | None): 48 Auth header to substitute the microservice original at 49 the request (user impersonation). 50 51 Returns: 52 bool | None: 53 The ``result`` field from ``process_matching_triggers`` 54 when the ETL app is registered; ``None`` when it is not. 55 56 Raises: 57 PumpWoodException: 58 If ``process_matching_triggers`` is not available on 59 ``ETLTrigger``. 60 PumpWoodActionArgsException: 61 If action arguments are invalid for 62 ``process_matching_triggers``. 63 PumpWoodObjectDoesNotExist: 64 If a requested object referenced by the action is not 65 found. 66 """ 67 is_etl_app_registered = self.is_microservice_registered( 68 microservice="pumpwood-etl-app", 69 auth_header=auth_header) 70 if not is_etl_app_registered: 71 return None 72 73 # Trigger the ETL process 74 action_result = self.execute_action( 75 model_class="ETLTrigger", 76 action="process_matching_triggers", 77 parameters={ 78 'model_class': model_class, 79 'trigger_type': trigger_type, 80 'object_id': object_id, 81 'action_name': action_name, 82 'parameters': parameters, 83 'process_name': process_name, 84 'send_to_rabbitmq': send_to_rabbitmq}, 85 auth_header=auth_header)['result'] 86 return action_result
Trigger an ETL process via the ETLTrigger action.
If pumpwood-etl-app is not registered at Kong, no request is
sent and the method returns None.
Arguments:
- model_class (str): Model class name that originated the trigger.
- trigger_type (Literal): Kind of event:
create,update,delete,action, orprocess_finish. - object_id (int | None): Primary key of the affected object, when applicable.
- action_name (str | None): Name of the action when
trigger_typeisaction. - parameters (dict | None): Extra parameters forwarded to matching ETL triggers.
- process_name (str | None): Optional ETL process name to restrict matching triggers.
- send_to_rabbitmq (bool): If True, enqueue work on RabbitMQ. Defaults to True.
- auth_header (dict | None): Auth header to substitute the microservice original at the request (user impersonation).
Returns:
bool | None: The
resultfield fromprocess_matching_triggerswhen the ETL app is registered;Nonewhen it is not.
Raises:
- PumpWoodException: If
process_matching_triggersis not available onETLTrigger. - PumpWoodActionArgsException: If action arguments are invalid for
process_matching_triggers. - PumpWoodObjectDoesNotExist: If a requested object referenced by the action is not found.