diff --git a/moto/ssm/models.py b/moto/ssm/models.py index 65b4d6743..ae1d19047 100644 --- a/moto/ssm/models.py +++ b/moto/ssm/models.py @@ -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: diff --git a/moto/ssm/responses.py b/moto/ssm/responses.py index 29fd8820c..f64c25e23 100644 --- a/moto/ssm/responses.py +++ b/moto/ssm/responses.py @@ -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) diff --git a/tests/test_ssm/test_ssm_boto3.py b/tests/test_ssm/test_ssm_boto3.py index 1b02536a1..10f40ab89 100644 --- a/tests/test_ssm/test_ssm_boto3.py +++ b/tests/test_ssm/test_ssm_boto3.py @@ -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():