From feb9048d70e341d3bf144786b8846d7ebec775c6 Mon Sep 17 00:00:00 2001 From: Janos Tolgyesi Date: Fri, 31 Mar 2023 01:10:27 +0200 Subject: [PATCH] Auto identity is more robust to AWS service response --- backpack/__init__.py | 2 +- backpack/autoidentity.py | 23 +++++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/backpack/__init__.py b/backpack/__init__.py index dc7b6084..27e3598f 100644 --- a/backpack/__init__.py +++ b/backpack/__init__.py @@ -1,6 +1,6 @@ ''' Utilities for AWS Panorama application development. ''' -__version__ = '0.3.1' +__version__ = '0.3.2' __author__ = 'Janos Tolgyesi' import functools diff --git a/backpack/autoidentity.py b/backpack/autoidentity.py index 4c5b0a60..c04d5249 100644 --- a/backpack/autoidentity.py +++ b/backpack/autoidentity.py @@ -34,7 +34,7 @@ class AutoIdentityData(BaseModel): application_status: str = Field(alias='HealthStatus') ''' Health status of this application. ''' - application_description: str = Field(alias='Description') + application_description: Optional[str] = Field(alias='Description') ''' The description of this application. ''' @classmethod @@ -52,6 +52,10 @@ def for_test_environment(cls, application_instance_id: str, application_name: st ) +class AutoIdentityError(RuntimeError): + ''' AutoIdentity specific error. ''' + + class AutoIdentityFetcher: ''' AutoIdentity instance queries metadata of the current application instance. @@ -87,7 +91,7 @@ def __init__(self, application_instance_id or os.environ.get('AppGraph_Uid') ) if not self.application_instance_id: - raise RuntimeError( + raise AutoIdentityError( 'Could not find application instance id in environment variable "AppGraph_Uid"' ) self.device_region = device_region @@ -104,17 +108,18 @@ def get_data(self, "NOT_AVAILABLE". If set to None, will not retry. Raises: - RuntimeError: if could not fetch the auto identity information, and retry_freq is set + AutoIdentityError: if could not fetch the auto identity information, and retry_freq is set to None. ''' def fetch() -> Dict[str, Any]: app_instance_data = self._app_instance_data(self.application_instance_id) + self._logger.info('Fetched data: %s', app_instance_data) if not app_instance_data: - raise RuntimeError( + raise AutoIdentityError( 'Could not find application instance in service response. ' - 'Check if application_instance_id=%s ' - 'and device_region=%s parameters are correct.' + 'Check if application_instance_id={} ' + 'and device_region={} parameters are correct.' .format(self.application_instance_id, self.device_region) ) else: @@ -127,7 +132,7 @@ def fetch() -> Dict[str, Any]: self._logger.info('Application HealthStatus=%s', status) if status in ('NOT_AVAILABLE'): if retry_freq is None: - raise RuntimeError( + raise AutoIdentityError( f'Application HealthStatus is "{status}" and retry is disabled.' ) else: @@ -138,9 +143,7 @@ def fetch() -> Dict[str, Any]: retry_freq = min(retry_freq, max_retry_freq) retries += 1 if max_retry_num is not None and retries > max_retry_num: - raise RuntimeError( - 'Maximum number of retries reached.' - ) + raise AutoIdentityError('Maximum number of retries reached.') else: continue else: