Skip to content

Commit

Permalink
Merge pull request #13 from fictivekin/fix-single-parameter-loading
Browse files Browse the repository at this point in the history
Fix single-key parameter loading
  • Loading branch information
jperras authored Jun 28, 2023
2 parents 547c4b6 + ad1d2e3 commit f6adba8
Show file tree
Hide file tree
Showing 4 changed files with 605 additions and 251 deletions.
33 changes: 27 additions & 6 deletions dynaconf_aws_loader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def load(
project_prefix=project_prefix,
env_name=env_name,
namespace_prefix=namespace_prefix,
key=key,
silent=silent,
)
if value:
Expand All @@ -164,6 +165,7 @@ def load(
namespace_prefix=None,
silent=silent,
)

if normal_results:
filter_strategy = obj.get("AWS_SSM_NAMESPACE_FILTER_STRATEGY")
if filter_strategy:
Expand Down Expand Up @@ -196,7 +198,8 @@ def _fetch_single_parameter(
client,
project_prefix: str,
env_name: str,
namespace_prefix: str | None,
key: str,
namespace_prefix: str | None = None,
silent: bool = True,
):
"""
Expand All @@ -207,12 +210,22 @@ def _fetch_single_parameter(
if namespace_prefix is not None:
path = f"{path}/{namespace_prefix}"

path = f"{path}/{key}"

logger.debug("Attempting to load a single parameter %s from AWS SSM" % path)

try:
value = client.get_parameter(Name=path, WithDecryption=True)
except (ClientError, BotoCoreError) as exc:
logger.warn("Could not connect to AWS SSM.", exc_info=exc)
except ClientError as exc:
if exc.response.get("Error", {}).get("Code") == "ParameterNotFound":
logger.warn("Parameter with path %s does not exist in AWS SSM." % path)
if silent:
return
raise
except BotoCoreError:
logger.warn(
"Could not connect to AWS SSM at endpoint %s." % client.meta.endpoint_url
)
if silent:
return
raise
Expand All @@ -227,7 +240,7 @@ def _fetch_all_parameters(
client,
project_prefix: str,
env_name: str,
namespace_prefix: str | None,
namespace_prefix: str | None = None,
silent: bool = True,
):
"""
Expand All @@ -248,8 +261,16 @@ def _fetch_all_parameters(
for parameter in page["Parameters"]:
data.append({parameter["Name"]: parameter["Value"]})

except (ClientError, BotoCoreError) as exc:
logger.warn("Could not connect to AWS SSM.", exc_info=exc)
except ClientError as exc:
if exc.response.get("Error", {}).get("Code") == "ParameterNotFound":
logger.warn("Parameter with path %s does not exist in AWS SSM." % path)
if silent:
return
raise
except BotoCoreError:
logger.warn(
"Could not connect to AWS SSM at endpoint %s." % client.meta.endpoint_url
)
if silent:
return
raise
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ boto3-stubs = {extras = ["ssm"], version = "^1.26"}
localstack-client = "^2.1"
pytest-env = "^0.8.1"


[tool.poetry.group.test.dependencies]
pytest-mock = "^3.11.1"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Expand Down
Loading

0 comments on commit f6adba8

Please sign in to comment.