-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ran Isenberg
committed
Jul 8, 2021
1 parent
b0c9daa
commit 2679cb9
Showing
7 changed files
with
98 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,12 +1,16 @@ | ||
"""Advanced feature toggles utility | ||
""" | ||
from .appconfig_fetcher import AppConfigFetcher | ||
from .configuration_store import ConfigurationStore | ||
from .exceptions import ConfigurationException | ||
from .schema import ACTION, SchemaValidator | ||
from .schema_fetcher import SchemaFetcher | ||
|
||
__all__ = [ | ||
"ConfigurationException", | ||
"ConfigurationStore", | ||
"ACTION", | ||
"SchemaValidator", | ||
"AppConfigFetcher", | ||
"SchemaFetcher", | ||
] |
57 changes: 57 additions & 0 deletions
57
aws_lambda_powertools/utilities/feature_toggles/appconfig_fetcher.py
This file contains 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,57 @@ | ||
import logging | ||
from typing import Any, Dict, Optional | ||
|
||
from botocore.config import Config | ||
|
||
from aws_lambda_powertools.utilities.parameters import AppConfigProvider, GetParameterError, TransformParameterError | ||
|
||
from .exceptions import ConfigurationException | ||
from .schema_fetcher import SchemaFetcher | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
TRANSFORM_TYPE = "json" | ||
|
||
|
||
class AppConfigFetcher(SchemaFetcher): | ||
def __init__( | ||
self, | ||
environment: str, | ||
service: str, | ||
configuration_name: str, | ||
cache_seconds: int, | ||
config: Optional[Config] = None, | ||
): | ||
"""This class fetches JSON schemas from AWS AppConfig | ||
Args: | ||
environment (str): what appconfig environment to use 'dev/test' etc. | ||
service (str): what service name to use from the supplied environment | ||
configuration_name (str): what configuration to take from the environment & service combination | ||
cache_seconds (int): cache expiration time, how often to call AppConfig to fetch latest configuration | ||
config (Optional[Config]): boto3 client configuration | ||
""" | ||
super().__init__(configuration_name, cache_seconds) | ||
self._logger = logger | ||
self._conf_store = AppConfigProvider(environment=environment, application=service, config=config) | ||
|
||
def get_json_configuration(self) -> Dict[str, Any]: | ||
"""Get configuration string from AWs AppConfig and return the parsed JSON dictionary | ||
Raises: | ||
ConfigurationException: Any validation error or appconfig error that can occur | ||
Returns: | ||
Dict[str, Any]: parsed JSON dictionary | ||
""" | ||
try: | ||
return self._conf_store.get( | ||
name=self.configuration_name, | ||
transform=TRANSFORM_TYPE, | ||
max_age=self._cache_seconds, | ||
) # parse result conf as JSON, keep in cache for self.max_age seconds | ||
except (GetParameterError, TransformParameterError) as exc: | ||
error_str = f"unable to get AWS AppConfig configuration file, exception={str(exc)}" | ||
self._logger.error(error_str) | ||
raise ConfigurationException(error_str) |
This file contains 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 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
class ConfigurationException(Exception): | ||
"""When a a configuration store raises an exception on schema retrieval or parsing""" | ||
"""When a a configuration store raises an exception on config retrieval or parsing""" |
20 changes: 20 additions & 0 deletions
20
aws_lambda_powertools/utilities/feature_toggles/schema_fetcher.py
This file contains 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,20 @@ | ||
from abc import ABC, abstractclassmethod | ||
from typing import Any, Dict | ||
|
||
|
||
class SchemaFetcher(ABC): | ||
def __init__(self, configuration_name: str, cache_seconds: int): | ||
self.configuration_name = configuration_name | ||
self._cache_seconds = cache_seconds | ||
|
||
@abstractclassmethod | ||
def get_json_configuration(self) -> Dict[str, Any]: | ||
"""Get configuration string from any configuration storing service and return the parsed JSON dictionary | ||
Raises: | ||
ConfigurationException: Any error that can occur during schema fetch or JSON parse | ||
Returns: | ||
Dict[str, Any]: parsed JSON dictionary | ||
""" | ||
return None |
This file contains 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 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