|
1 |
| -# pylint: disable=no-name-in-module,line-too-long |
2 | 1 | import logging
|
3 | 2 | from typing import Any, Dict, List, Optional
|
4 | 3 |
|
5 |
| -from botocore.config import Config |
6 |
| - |
7 |
| -from aws_lambda_powertools.utilities.parameters import AppConfigProvider, GetParameterError, TransformParameterError |
8 |
| - |
9 | 4 | from . import schema
|
10 | 5 | from .exceptions import ConfigurationException
|
11 |
| - |
12 |
| -TRANSFORM_TYPE = "json" |
| 6 | +from .schema_fetcher import SchemaFetcher |
13 | 7 |
|
14 | 8 | logger = logging.getLogger(__name__)
|
15 | 9 |
|
16 | 10 |
|
17 | 11 | class ConfigurationStore:
|
18 |
| - def __init__( |
19 |
| - self, environment: str, service: str, conf_name: str, cache_seconds: int, config: Optional[Config] = None |
20 |
| - ): |
| 12 | + def __init__(self, schema_fetcher: SchemaFetcher): |
21 | 13 | """constructor
|
22 | 14 |
|
23 | 15 | Args:
|
24 |
| - environment (str): what appconfig environment to use 'dev/test' etc. |
25 |
| - service (str): what service name to use from the supplied environment |
26 |
| - conf_name (str): what configuration to take from the environment & service combination |
27 |
| - cache_seconds (int): cache expiration time, how often to call AppConfig to fetch latest configuration |
| 16 | + schema_fetcher (SchemaFetcher): A schema JSON fetcher, can be AWS AppConfig, Hashicorp Consul etc. |
28 | 17 | """
|
29 |
| - self._cache_seconds = cache_seconds |
30 | 18 | self._logger = logger
|
31 |
| - self._conf_name = conf_name |
| 19 | + self._schema_fetcher = schema_fetcher |
32 | 20 | self._schema_validator = schema.SchemaValidator(self._logger)
|
33 |
| - self._conf_store = AppConfigProvider(environment=environment, application=service, config=config) |
34 | 21 |
|
35 | 22 | def _match_by_action(self, action: str, condition_value: Any, context_value: Any) -> bool:
|
36 | 23 | if not context_value:
|
@@ -99,17 +86,11 @@ def get_configuration(self) -> Dict[str, Any]:
|
99 | 86 | Returns:
|
100 | 87 | Dict[str, Any]: parsed JSON dictionary
|
101 | 88 | """
|
102 |
| - try: |
103 |
| - schema = self._conf_store.get( |
104 |
| - name=self._conf_name, |
105 |
| - transform=TRANSFORM_TYPE, |
106 |
| - max_age=self._cache_seconds, |
107 |
| - ) # parse result conf as JSON, keep in cache for self.max_age seconds |
108 |
| - except (GetParameterError, TransformParameterError) as exc: |
109 |
| - error_str = f"unable to get AWS AppConfig configuration file, exception={str(exc)}" |
110 |
| - self._logger.error(error_str) |
111 |
| - raise ConfigurationException(error_str) |
112 |
| - |
| 89 | + schema: Dict[ |
| 90 | + str, Any |
| 91 | + ] = ( |
| 92 | + self._schema_fetcher.get_json_configuration() |
| 93 | + ) # parse result conf as JSON, keep in cache for self.max_age seconds |
113 | 94 | # validate schema
|
114 | 95 | self._schema_validator.validate_json_schema(schema)
|
115 | 96 | return schema
|
|
0 commit comments