Skip to content

Commit

Permalink
Auto identity is more robust to AWS service response
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtj committed Mar 30, 2023
1 parent 8260c48 commit feb9048
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion backpack/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
''' Utilities for AWS Panorama application development. '''

__version__ = '0.3.1'
__version__ = '0.3.2'
__author__ = 'Janos Tolgyesi'

import functools
Expand Down
23 changes: 13 additions & 10 deletions backpack/autoidentity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit feb9048

Please sign in to comment.