Skip to content

Commit 2422cd6

Browse files
author
Jim Robinson
committed
Use pagination and cache results
1 parent 0524eb6 commit 2422cd6

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

src/croudtech_python_aws_app_config/cli.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,14 @@ def cli(ctx, endpoint_url, put_metrics):
6060
default="json",
6161
type=click.Choice(["json", "yaml", "environment", "environment-export"]),
6262
)
63+
@click.option(
64+
"--parse-redis-param/--ignore-redis-param",
65+
default=True,
66+
is_flag=True,
67+
help="Parse redis host and allocate a redis database number"
68+
)
6369
def get_parameters(
64-
ctx, environment_name, app_name, ssm_prefix, region, include_common, output_format
70+
ctx, environment_name, app_name, ssm_prefix, region, include_common, output_format, parse_redis_param
6571
):
6672
ssm_config = SsmConfig(
6773
environment_name=environment_name,
@@ -72,6 +78,7 @@ def get_parameters(
7278
click=click,
7379
endpoint_url=ctx.obj["AWS_ENDPOINT_URL"],
7480
put_metrics=ctx.obj["PUT_METRICS"],
81+
parse_redis=parse_redis_param
7582
)
7683
output = "Invalid output format"
7784

src/croudtech_python_aws_app_config/ssm_config.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def __init__(
5858
use_sns=True,
5959
endpoint_url=os.getenv("AWS_ENDPOINT_URL", None),
6060
put_metrics=True,
61+
parse_redis=True
6162
):
6263
self.environment_name = environment_name
6364
self.app_name = app_name
@@ -69,6 +70,7 @@ def __init__(
6970
self.use_sns = use_sns
7071
self.endpoint_url = endpoint_url
7172
self.put_metrics = put_metrics
73+
self.parse_redis = parse_redis
7274

7375
@property
7476
def ssm_client(self):
@@ -100,7 +102,7 @@ def get_parameters(self):
100102
return parameters
101103

102104
def parse_parameters(self, parameters):
103-
if "/REDIS_DB" not in parameters or parameters["/REDIS_DB"] == "auto":
105+
if self.parse_redis and ("/REDIS_DB" not in parameters or parameters["/REDIS_DB"] == "auto"):
104106
redis_host = (
105107
parameters["/REDIS_HOST"] if "/REDIS_HOST" in parameters else False
106108
)
@@ -210,8 +212,7 @@ def fetch_parameters(self, path, absolute_path=False):
210212
parameter_name = parameter["Name"]
211213
else:
212214
parameter_name = parameter["Name"].replace(path, "")
213-
parameters[parameter_name] = parameter["Value"]
214-
logger.debug("Fetched parameters from AWS SSM.")
215+
parameters[parameter_name] = parameter["Value"]
215216
except botocore.exceptions.ClientError as err:
216217
logger.debug("Failed to fetch parameters. Invalid token")
217218
return {}
@@ -221,21 +222,22 @@ def fetch_parameters(self, path, absolute_path=False):
221222
return parameters
222223

223224
def fetch_paginated_parameters(self, path):
224-
parameters = []
225-
fetch_next_page = True
226-
api_parameters = {"Path": path, "Recursive": True, "WithDecryption": True}
227-
while fetch_next_page:
228-
response = self.ssm_client.get_parameters_by_path(**api_parameters)
229-
230-
if "Parameters" in response:
231-
parameters = parameters + response["Parameters"]
232-
if "NextToken" in response:
233-
api_parameters["NextToken"] = response["NextToken"]
234-
fetch_next_page = True
235-
else:
236-
fetch_next_page = False
237-
238-
return parameters
225+
if not hasattr(self, "fetched_parameters"):
226+
self.fetched_parameters = {}
227+
if path not in self.fetched_parameters:
228+
paginator = self.ssm_client.get_paginator('get_parameters_by_path')
229+
parameters = paginator.paginate(
230+
Path=path,
231+
Recursive=True,
232+
WithDecryption=True,
233+
)
234+
235+
self.fetched_parameters["path"] = parameters.build_full_result()["Parameters"]
236+
237+
for item in self.fetched_parameters["path"]:
238+
logger.debug("Found Parameter %s." % item["Name"])
239+
logger.debug("Fetched parameters from AWS SSM for path %s." % path)
240+
return self.fetched_parameters["path"]
239241

240242
def parameter_name_to_underscore(self, name):
241243
return name[1 : len(name)].replace("/", "_")

0 commit comments

Comments
 (0)