Skip to content

Commit 050aba6

Browse files
codewithzubair07codewithzubair07leandrodamascena
authored
fix(parameters): key appconfig default provider by environment and ap… (#8635)
fix(parameters): key appconfig default provider by environment and application Co-authored-by: codewithzubair07 <juberquaishi553@gmail.com> Co-authored-by: Leandro Damascena <lcdama@amazon.pt>
1 parent 46eb971 commit 050aba6

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

‎aws_lambda_powertools/utilities/parameters/appconfig.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,12 @@ def get_app_config(
214214
# If max_age is not set, resolve it from the environment variable, defaulting to DEFAULT_MAX_AGE_SECS
215215
max_age = resolve_max_age(env=os.getenv(constants.PARAMETERS_MAX_AGE_ENV, DEFAULT_MAX_AGE_SECS), choice=max_age)
216216

217-
# Only create the provider if this function is called at least once
218-
if "appconfig" not in DEFAULT_PROVIDERS:
219-
DEFAULT_PROVIDERS["appconfig"] = AppConfigProvider(environment=environment, application=application)
217+
# One provider per environment/application pair, created on first use
218+
provider_key = f"appconfig:{environment}:{application}"
219+
if provider_key not in DEFAULT_PROVIDERS:
220+
DEFAULT_PROVIDERS[provider_key] = AppConfigProvider(environment=environment, application=application)
220221

221-
return DEFAULT_PROVIDERS["appconfig"].get(
222+
return DEFAULT_PROVIDERS[provider_key].get(
222223
name,
223224
max_age=max_age,
224225
transform=transform,

‎tests/functional/parameters/_boto3/test_utilities_parameters.py‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ def _get_multiple(self, path: str, **kwargs) -> dict[str, str]: ...
10331033

10341034
monkeypatch.setitem(parameters.base.DEFAULT_PROVIDERS, "ssm", TestProvider())
10351035
monkeypatch.setitem(parameters.base.DEFAULT_PROVIDERS, "secrets", TestProvider())
1036-
monkeypatch.setitem(parameters.base.DEFAULT_PROVIDERS, "appconfig", TestProvider())
1036+
monkeypatch.setitem(parameters.base.DEFAULT_PROVIDERS, "appconfig:test:test", TestProvider())
10371037

10381038
parameters.get_parameter(mock_name)
10391039
parameters.get_secret(mock_name)
@@ -3103,7 +3103,7 @@ def _get(self, name: str, **kwargs) -> bytes:
31033103
def _get_multiple(self, path: str, **kwargs) -> dict[str, str]:
31043104
raise NotImplementedError()
31053105

3106-
monkeypatch.setitem(parameters.base.DEFAULT_PROVIDERS, "appconfig", TestProvider())
3106+
monkeypatch.setitem(parameters.base.DEFAULT_PROVIDERS, "appconfig:dev:myapp", TestProvider())
31073107

31083108
environment = "dev"
31093109
application = "myapp"
@@ -3128,7 +3128,7 @@ def _get(self, name: str, **kwargs) -> str:
31283128
def _get_multiple(self, path: str, **kwargs) -> dict[str, str]:
31293129
raise NotImplementedError()
31303130

3131-
monkeypatch.setitem(parameters.base.DEFAULT_PROVIDERS, "appconfig", TestProvider())
3131+
monkeypatch.setitem(parameters.base.DEFAULT_PROVIDERS, "appconfig:dev:myapp", TestProvider())
31323132

31333133
environment = "dev"
31343134
application = "myapp"
@@ -3158,10 +3158,36 @@ def _get_multiple(self, path: str, **kwargs) -> dict[str, str]:
31583158
value = parameters.get_app_config(mock_name, environment="dev", application="myapp")
31593159

31603160
# THEN
3161-
assert parameters.appconfig.DEFAULT_PROVIDERS["appconfig"] is not None
3161+
assert parameters.appconfig.DEFAULT_PROVIDERS["appconfig:dev:myapp"] is not None
31623162
assert value == mock_value
31633163

31643164

3165+
def test_appconf_get_app_config_different_environment_and_application(monkeypatch, mock_name):
3166+
# GIVEN a provider that reports which environment/application it was built for
3167+
class TestProvider(BaseProvider):
3168+
def __init__(self, environment: str, application: str):
3169+
super().__init__()
3170+
self.environment = environment
3171+
self.application = application
3172+
3173+
def _get(self, name: str, **kwargs) -> str:
3174+
return f"{self.application}/{self.environment}"
3175+
3176+
def _get_multiple(self, path: str, **kwargs) -> dict[str, str]:
3177+
raise NotImplementedError()
3178+
3179+
monkeypatch.setattr(parameters.appconfig, "DEFAULT_PROVIDERS", {})
3180+
monkeypatch.setattr(parameters.appconfig, "AppConfigProvider", TestProvider)
3181+
3182+
# WHEN get_app_config is called with different applications and environments
3183+
first = parameters.get_app_config(mock_name, environment="dev", application="app-a", max_age=0)
3184+
second = parameters.get_app_config(mock_name, environment="prod", application="app-b", max_age=0)
3185+
3186+
# THEN each call uses its own environment and application
3187+
assert first == "app-a/dev"
3188+
assert second == "app-b/prod"
3189+
3190+
31653191
def test_transform_value_auto(mock_value: str):
31663192
# GIVEN
31673193
json_data = json.dumps({"A": mock_value})

0 commit comments

Comments
 (0)