Skip to content

Commit

Permalink
Merge pull request #15 from fictivekin/dynaconf-loader-identifier-cha…
Browse files Browse the repository at this point in the history
…nges

Handle loader identifier changes between Dynaconf 3.1->3.2
  • Loading branch information
jperras authored Jul 20, 2023
2 parents 6fd7975 + 100c6e7 commit e035374
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ jobs:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10"]
dynaconf-version: ["3.1.12", "3.2.0"]
steps:

- uses: actions/checkout@v3
Expand Down Expand Up @@ -43,5 +44,8 @@ jobs:
- name: Install dependencies
run: poetry install

- name: Override with specific Dynaconf version(s)
run: poetry add dynaconf==${{ matrix.dynaconf-version }}

- name: Run Tests with PyTest for Python ${{ matrix.python-version }}
run: poetry run pytest --verbose -rsxX --color=auto
27 changes: 24 additions & 3 deletions dynaconf_aws_loader/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
"""
Dynaconf Loader for AWS Secrets Manager
"""
import typing as t

from importlib.metadata import version

# Pull version from the package data; canonical source is pyproject.toml
__version__ = version(__package__)

# The loader identifier, which is used to identify the provenance of compiled
# configuration key/value pairs in the resulting ``Settings`` object.
IDENTIFIER = "aws-ssm"

def generate_loader_identifier(path: str, env: str):
"""
The loader identifier, which is used to identify the provenance of compiled
configuration key/value pairs in the resulting ``Settings`` object.
:param path: String-based path to identify where the value has been sourced from
:param env: String-based env name
"""

# Dynamic import here, due to the changes between Dynaconf 3.1 and 3.2 with
# respect to loader identifiers.
try:
# Use the new-style source metadata loading for better introspection
# of where values are sourced from.
from dynaconf.loaders.base import SourceMetadata

loader_identifier = SourceMetadata(loader="aws-ssm", identifier=path, env=env)
except ImportError: # Dynaconf<=3.1, simply return a string.
# Use the old-style string identifiers.
loader_identifier = "aws-ssm"

return loader_identifier
8 changes: 5 additions & 3 deletions dynaconf_aws_loader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

from dynaconf.utils.parse_conf import parse_conf_data

from . import IDENTIFIER
from .util import slashes_to_dict, pull_from_env_or_obj
from . import generate_loader_identifier

if t.TYPE_CHECKING:
from mypy_boto3_ssm.client import SSMClient
Expand Down Expand Up @@ -141,6 +141,8 @@ def load(
if namespace_prefix is not None:
path = f"{path}/{namespace_prefix}"

loader_identifier = generate_loader_identifier(path, env)

if key is not None:
value = _fetch_single_parameter(
client,
Expand Down Expand Up @@ -173,7 +175,7 @@ def load(

obj.update(
normal_results,
loader_identifier=IDENTIFIER,
loader_identifier=loader_identifier,
validate=validate,
)

Expand All @@ -189,7 +191,7 @@ def load(
if namespaced_results:
obj.update(
namespaced_results,
loader_identifier=IDENTIFIER,
loader_identifier=loader_identifier,
validate=validate,
)

Expand Down

0 comments on commit e035374

Please sign in to comment.