From 508b392697d4d166204a90aff51896031be5ba57 Mon Sep 17 00:00:00 2001 From: ramox- Date: Wed, 21 Mar 2018 16:56:57 +0100 Subject: [PATCH] Make putparameter behave more like the real endpoint does, respond with Version or ParameterAlreadyExists (#1464) --- moto/ssm/models.py | 1 + moto/ssm/responses.py | 13 +++++++++++-- tests/test_ssm/test_ssm_boto3.py | 23 ++++++++++++++++------- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/moto/ssm/models.py b/moto/ssm/models.py index 0f75599c3..af450e39e 100644 --- a/moto/ssm/models.py +++ b/moto/ssm/models.py @@ -126,6 +126,7 @@ class SimpleSystemManagerBackend(BaseBackend): last_modified_date = time.time() self._parameters[name] = Parameter( name, value, type, description, keyid, last_modified_date, version) + return version def add_tags_to_resource(self, resource_type, resource_id, tags): for key, value in tags.items(): diff --git a/moto/ssm/responses.py b/moto/ssm/responses.py index 757bf0317..d9906a82e 100644 --- a/moto/ssm/responses.py +++ b/moto/ssm/responses.py @@ -162,9 +162,18 @@ class SimpleSystemManagerResponse(BaseResponse): keyid = self._get_param('KeyId') overwrite = self._get_param('Overwrite', False) - self.ssm_backend.put_parameter( + result = self.ssm_backend.put_parameter( name, description, value, type_, keyid, overwrite) - return json.dumps({}) + + if result is None: + error = { + '__type': 'ParameterAlreadyExists', + 'message': 'Parameter {0} already exists.'.format(name) + } + return json.dumps(error), dict(status=400) + + response = {'Version': result} + return json.dumps(response) def add_tags_to_resource(self): resource_id = self._get_param('ResourceId') diff --git a/tests/test_ssm/test_ssm_boto3.py b/tests/test_ssm/test_ssm_boto3.py index 0e8a770b3..97801e0b9 100644 --- a/tests/test_ssm/test_ssm_boto3.py +++ b/tests/test_ssm/test_ssm_boto3.py @@ -97,12 +97,14 @@ def test_get_parameters_by_path(): def test_put_parameter(): client = boto3.client('ssm', region_name='us-east-1') - client.put_parameter( + response = client.put_parameter( Name='test', Description='A test parameter', Value='value', Type='String') + response['Version'].should.equal(1) + response = client.get_parameters( Names=[ 'test' @@ -115,11 +117,16 @@ def test_put_parameter(): response['Parameters'][0]['Type'].should.equal('String') response['Parameters'][0]['Version'].should.equal(1) - client.put_parameter( - Name='test', - Description='desc 2', - Value='value 2', - Type='String') + try: + client.put_parameter( + Name='test', + Description='desc 2', + Value='value 2', + Type='String') + raise RuntimeError('Should fail') + except botocore.exceptions.ClientError as err: + err.operation_name.should.equal('PutParameter') + err.response['Error']['Message'].should.equal('Parameter test already exists.') response = client.get_parameters( Names=[ @@ -134,13 +141,15 @@ def test_put_parameter(): response['Parameters'][0]['Type'].should.equal('String') response['Parameters'][0]['Version'].should.equal(1) - client.put_parameter( + response = client.put_parameter( Name='test', Description='desc 3', Value='value 3', Type='String', Overwrite=True) + response['Version'].should.equal(2) + response = client.get_parameters( Names=[ 'test'