Add Pagination to ssm:get_parameters_by_path. Closes #1864

This commit is contained in:
Adam Smith 2019-11-27 22:12:31 +00:00
parent 502957f1f9
commit 051193e1bf
3 changed files with 46 additions and 5 deletions

View File

@ -510,7 +510,15 @@ class SimpleSystemManagerBackend(BaseBackend):
result.append(self.get_parameter(name, with_decryption))
return result
def get_parameters_by_path(self, path, with_decryption, recursive, filters=None):
def get_parameters_by_path(
self,
path,
with_decryption,
recursive,
filters=None,
next_token=None,
max_results=10,
):
"""Implement the get-parameters-by-path-API in the backend."""
result = []
# path could be with or without a trailing /. we handle this
@ -527,7 +535,19 @@ class SimpleSystemManagerBackend(BaseBackend):
continue
result.append(self.get_parameter(param_name, with_decryption))
return result
return self._get_values_nexttoken(result, max_results, next_token)
def _get_values_nexttoken(self, values_list, max_results, next_token=None):
if next_token is None:
next_token = 0
next_token = int(next_token)
max_results = int(max_results)
values = values_list[next_token : next_token + max_results]
if len(values) == max_results:
next_token = str(next_token + max_results)
else:
next_token = None
return values, next_token
def get_parameter_history(self, name, with_decryption):
if name in self._parameters:

View File

@ -77,12 +77,19 @@ class SimpleSystemManagerResponse(BaseResponse):
with_decryption = self._get_param("WithDecryption")
recursive = self._get_param("Recursive", False)
filters = self._get_param("ParameterFilters")
token = self._get_param("NextToken")
max_results = self._get_param("MaxResults", 10)
result = self.ssm_backend.get_parameters_by_path(
path, with_decryption, recursive, filters
result, next_token = self.ssm_backend.get_parameters_by_path(
path,
with_decryption,
recursive,
filters,
next_token=token,
max_results=max_results,
)
response = {"Parameters": []}
response = {"Parameters": [], "NextToken": next_token}
for parameter in result:
param_data = parameter.response_object(with_decryption)

View File

@ -158,6 +158,20 @@ def test_get_parameters_by_path():
len(response["Parameters"]).should.equal(1)
{p["Name"] for p in response["Parameters"]}.should.equal(set(["/baz/pwd"]))
response = client.get_parameters_by_path(Path="/", Recursive=True, MaxResults=4)
len(response["Parameters"]).should.equal(4)
response["NextToken"].should.equal("4")
response = client.get_parameters_by_path(
Path="/", Recursive=True, MaxResults=4, NextToken=response["NextToken"]
)
len(response["Parameters"]).should.equal(4)
response["NextToken"].should.equal("8")
response = client.get_parameters_by_path(
Path="/", Recursive=True, MaxResults=4, NextToken=response["NextToken"]
)
len(response["Parameters"]).should.equal(1)
response.should_not.have.key("NextToken")
@mock_ssm
def test_put_parameter():