Skip to content

Commit

Permalink
Paginate SSM Get Parameters by Path request (#7)
Browse files Browse the repository at this point in the history
* use pagination

* Update source.py

* Update source.py

Co-authored-by: Anthony Lukach <anthonylukach@gmail.com>
  • Loading branch information
jscaria and alukach authored Jun 9, 2022
1 parent f8f7c57 commit e5fca16
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions pydantic_ssm_settings/source.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import logging
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, Optional, Union
from typing import TYPE_CHECKING, Any, Dict, Optional

from botocore.exceptions import ClientError
from botocore.client import Config
Expand All @@ -19,8 +19,8 @@
class AwsSsmSettingsSource:
__slots__ = ("ssm_prefix",)

def __init__(self, ssm_prefix: Union[typing.StrPath, None]):
self.ssm_prefix: Union[typing.StrPath, None] = ssm_prefix
def __init__(self, ssm_prefix: Optional[typing.StrPath]):
self.ssm_prefix: Optional[typing.StrPath] = ssm_prefix

@property
def client(self) -> "SSMClient":
Expand Down Expand Up @@ -48,17 +48,18 @@ def __call__(self, settings: BaseSettings) -> Dict[str, Any]:
logger.debug(f"Building SSM settings with prefix of {secrets_path=}")

try:
params = self.client.get_parameters_by_path(
Path=str(secrets_path), WithDecryption=True
)["Parameters"]
paginator = self.client.get_paginator('get_parameters_by_path')
response_iterator = paginator.paginate(Path=str(secrets_path), WithDecryption=True)

return {
str(Path(parameter["Name"]).relative_to(secrets_path)): parameter["Value"]
for page in response_iterator
for parameter in page['Parameters']
}

except ClientError:
logger.exception("Failed to get parameters from %s", secrets_path)
return {}

return {
str(Path(param["Name"]).relative_to(secrets_path)): param["Value"]
for param in params
}

def __repr__(self) -> str:
return f"AwsSsmSettingsSource(ssm_prefix={self.ssm_prefix!r})"

0 comments on commit e5fca16

Please sign in to comment.