|
| 1 | +import logging |
| 2 | +import traceback |
| 3 | +from typing import Any, Dict, Optional, cast |
| 4 | + |
| 5 | +from botocore.config import Config |
| 6 | + |
| 7 | +from aws_lambda_powertools.utilities.parameters import AppConfigProvider, GetParameterError, TransformParameterError |
| 8 | + |
| 9 | +from ...shared import jmespath_utils |
| 10 | +from .base import StoreProvider |
| 11 | +from .exceptions import ConfigurationStoreError, StoreClientError |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | +TRANSFORM_TYPE = "json" |
| 16 | + |
| 17 | + |
| 18 | +class AppConfigStore(StoreProvider): |
| 19 | + def __init__( |
| 20 | + self, |
| 21 | + environment: str, |
| 22 | + application: str, |
| 23 | + name: str, |
| 24 | + cache_seconds: int, |
| 25 | + sdk_config: Optional[Config] = None, |
| 26 | + envelope: str = "", |
| 27 | + jmespath_options: Optional[Dict] = None, |
| 28 | + ): |
| 29 | + """This class fetches JSON schemas from AWS AppConfig |
| 30 | +
|
| 31 | + Parameters |
| 32 | + ---------- |
| 33 | + environment: str |
| 34 | + Appconfig environment, e.g. 'dev/test' etc. |
| 35 | + application: str |
| 36 | + AppConfig application name, e.g. 'powertools' |
| 37 | + name: str |
| 38 | + AppConfig configuration name e.g. `my_conf` |
| 39 | + cache_seconds: int |
| 40 | + cache expiration time, how often to call AppConfig to fetch latest configuration |
| 41 | + sdk_config: Optional[Config] |
| 42 | + Botocore Config object to pass during client initialization |
| 43 | + envelope : str |
| 44 | + JMESPath expression to pluck feature flags data from config |
| 45 | + jmespath_options : Dict |
| 46 | + Alternative JMESPath options to be included when filtering expr |
| 47 | + """ |
| 48 | + super().__init__() |
| 49 | + self.environment = environment |
| 50 | + self.application = application |
| 51 | + self.name = name |
| 52 | + self.cache_seconds = cache_seconds |
| 53 | + self.config = sdk_config |
| 54 | + self.envelope = envelope |
| 55 | + self.jmespath_options = jmespath_options |
| 56 | + self._conf_store = AppConfigProvider(environment=environment, application=application, config=sdk_config) |
| 57 | + |
| 58 | + def get_configuration(self) -> Dict[str, Any]: |
| 59 | + """Fetch feature schema configuration from AWS AppConfig |
| 60 | +
|
| 61 | + Raises |
| 62 | + ------ |
| 63 | + ConfigurationStoreError |
| 64 | + Any validation error or AppConfig error that can occur |
| 65 | +
|
| 66 | + Returns |
| 67 | + ------- |
| 68 | + Dict[str, Any] |
| 69 | + parsed JSON dictionary |
| 70 | + """ |
| 71 | + try: |
| 72 | + # parse result conf as JSON, keep in cache for self.max_age seconds |
| 73 | + config = cast( |
| 74 | + dict, |
| 75 | + self._conf_store.get( |
| 76 | + name=self.name, |
| 77 | + transform=TRANSFORM_TYPE, |
| 78 | + max_age=self.cache_seconds, |
| 79 | + ), |
| 80 | + ) |
| 81 | + |
| 82 | + if self.envelope: |
| 83 | + config = jmespath_utils.unwrap_event_from_envelope( |
| 84 | + data=config, envelope=self.envelope, jmespath_options=self.jmespath_options |
| 85 | + ) |
| 86 | + |
| 87 | + return config |
| 88 | + except (GetParameterError, TransformParameterError) as exc: |
| 89 | + err_msg = traceback.format_exc() |
| 90 | + if "AccessDenied" in err_msg: |
| 91 | + raise StoreClientError(err_msg) from exc |
| 92 | + raise ConfigurationStoreError("Unable to get AWS AppConfig configuration file") from exc |
0 commit comments