From dd85f35f5a3e54fddffbd16c0953a83440f91d2e Mon Sep 17 00:00:00 2001 From: dominictootell Date: Wed, 14 Sep 2016 10:04:51 +0100 Subject: [PATCH] don't set cacheClusterSize if cache not enabled --- moto/apigateway/models.py | 28 ++++-- moto/apigateway/responses.py | 7 +- tests/test_apigateway/test_apigateway.py | 105 ++++++++++++++++++++--- 3 files changed, 119 insertions(+), 21 deletions(-) diff --git a/moto/apigateway/models.py b/moto/apigateway/models.py index 9166519f6..90e5b0bd8 100644 --- a/moto/apigateway/models.py +++ b/moto/apigateway/models.py @@ -155,7 +155,8 @@ class Resource(object): class Stage(dict): - def __init__(self, name=None, deployment_id=None, variables=None): + def __init__(self, name=None, deployment_id=None, variables=None, + description='',cacheClusterEnabled=False,cacheClusterSize=None): super(Stage, self).__init__() if variables is None: variables = {} @@ -163,9 +164,13 @@ class Stage(dict): self['deploymentId'] = deployment_id self['methodSettings'] = {} self['variables'] = variables - self['description'] = '' - self['cacheClusterEnabled'] = False - self['cacheClusterSize'] = 0.5 + self['description'] = description + self['cacheClusterEnabled'] = cacheClusterEnabled + if self['cacheClusterEnabled']: + self['cacheClusterSize'] = str(0.5) + + if cacheClusterSize is not None: + self['cacheClusterSize'] = str(cacheClusterSize) def apply_operations(self, patch_operations): for op in patch_operations: @@ -173,8 +178,10 @@ class Stage(dict): self._apply_operation_to_variables(op) elif '/cacheClusterEnabled' in op['path']: self['cacheClusterEnabled'] = self._str2bool(op['value']) + if 'cacheClusterSize' not in self and self['cacheClusterEnabled']: + self['cacheClusterSize'] = str(0.5) elif '/cacheClusterSize' in op['path']: - self['cacheClusterSize'] = float(op['value']) + self['cacheClusterSize'] = str(float(op['value'])) elif '/description' in op['path']: self['description'] = op['value'] elif '/deploymentId' in op['path']: @@ -324,10 +331,11 @@ class RestAPI(object): for method in httpretty.httpretty.METHODS: httpretty.register_uri(method, stage_url, body=self.resource_callback) - def create_stage(self, name, deployment_id,variables=None): + def create_stage(self, name, deployment_id,variables=None,description='',cacheClusterEnabled=None,cacheClusterSize=None): if variables is None: variables = {} - stage = Stage(name=name, deployment_id=deployment_id,variables=variables) + stage = Stage(name=name, deployment_id=deployment_id,variables=variables, + description=description,cacheClusterSize=cacheClusterSize,cacheClusterEnabled=cacheClusterEnabled) self.stages[name] = stage self.update_integration_mocks(name) return stage @@ -429,11 +437,13 @@ class APIGatewayBackend(BaseBackend): return api.get_stages() - def create_stage(self, function_id, stage_name, deploymentId,variables=None): + def create_stage(self, function_id, stage_name, deploymentId, + variables=None,description='',cacheClusterEnabled=None,cacheClusterSize=None): if variables is None: variables = {} api = self.get_rest_api(function_id) - api.create_stage(stage_name,deploymentId,variables) + api.create_stage(stage_name,deploymentId,variables=variables, + description=description,cacheClusterEnabled=cacheClusterEnabled,cacheClusterSize=cacheClusterSize) return api.stages.get(stage_name) def update_stage(self, function_id, stage_name, patch_operations): diff --git a/moto/apigateway/responses.py b/moto/apigateway/responses.py index 76183a5d0..e95d2887d 100644 --- a/moto/apigateway/responses.py +++ b/moto/apigateway/responses.py @@ -114,8 +114,13 @@ class APIGatewayResponse(BaseResponse): stage_name = self._get_param("stageName") deployment_id = self._get_param("deploymentId") stage_variables = self._get_param_with_default_value('variables',{}) + description = self._get_param_with_default_value('description','') + cacheClusterEnabled = self._get_param_with_default_value('cacheClusterEnabled',False) + cacheClusterSize = self._get_param_with_default_value('cacheClusterSize',None) - stage_response = self.backend.create_stage(function_id, stage_name, deployment_id,variables=stage_variables) + stage_response = self.backend.create_stage(function_id, stage_name, deployment_id, + variables=stage_variables, description=description, + cacheClusterEnabled=cacheClusterEnabled, cacheClusterSize=cacheClusterSize) elif self.method == 'GET': stages = self.backend.get_stages(function_id) return 200, headers, json.dumps({"item": stages}) diff --git a/tests/test_apigateway/test_apigateway.py b/tests/test_apigateway/test_apigateway.py index 200cf5ff4..b60f41e49 100644 --- a/tests/test_apigateway/test_apigateway.py +++ b/tests/test_apigateway/test_apigateway.py @@ -515,6 +515,40 @@ def test_update_stage_configuration(): ) stage['stageName'].should.equal(stage_name) stage['deploymentId'].should.equal(deployment_id2) + stage.shouldnt.have.key('cacheClusterSize') + + client.update_stage(restApiId=api_id,stageName=stage_name, + patchOperations=[ + { + "op" : "replace", + "path" : "/cacheClusterEnabled", + "value": "True" + } + ]) + + stage = client.get_stage( + restApiId=api_id, + stageName=stage_name + ) + + stage.should.have.key('cacheClusterSize').which.should.equal("0.5") + + client.update_stage(restApiId=api_id,stageName=stage_name, + patchOperations=[ + { + "op" : "replace", + "path" : "/cacheClusterSize", + "value": "1.6" + } + ]) + + stage = client.get_stage( + restApiId=api_id, + stageName=stage_name + ) + + stage.should.have.key('cacheClusterSize').which.should.equal("1.6") + client.update_stage(restApiId=api_id,stageName=stage_name, patchOperations=[ @@ -567,7 +601,7 @@ def test_update_stage_configuration(): stage = client.get_stage(restApiId=api_id,stageName=stage_name) stage['description'].should.match('stage description update') - stage['cacheClusterSize'].should.equal(1.6) + stage['cacheClusterSize'].should.equal("1.6") stage['variables']['environment'].should.match('dev') stage['variables'].should_not.have.key('region') stage['cacheClusterEnabled'].should.be.true @@ -662,10 +696,16 @@ def test_create_stage(): 'variables':{}, 'ResponseMetadata': {'HTTPStatusCode': 200}, 'description':'', - 'cacheClusterSize':0.5, 'cacheClusterEnabled':False }) + stage = client.get_stage( + restApiId=api_id, + stageName=new_stage_name + ) + stage['stageName'].should.equal(new_stage_name) + stage['deploymentId'].should.equal(deployment_id2) + new_stage_name_with_vars = 'stage_with_vars' response = client.create_stage(restApiId=api_id,stageName=new_stage_name_with_vars,deploymentId=deployment_id2,variables={ "env" : "dev" @@ -680,17 +720,9 @@ def test_create_stage(): 'variables':{ "env" : "dev" }, 'ResponseMetadata': {'HTTPStatusCode': 200}, 'description':'', - 'cacheClusterSize':0.5, - 'cacheClusterEnabled':False + 'cacheClusterEnabled': False }) - stage = client.get_stage( - restApiId=api_id, - stageName=new_stage_name - ) - stage['stageName'].should.equal(new_stage_name) - stage['deploymentId'].should.equal(deployment_id2) - stage = client.get_stage( restApiId=api_id, stageName=new_stage_name_with_vars @@ -699,6 +731,57 @@ def test_create_stage(): stage['deploymentId'].should.equal(deployment_id2) stage['variables'].should.have.key('env').which.should.match("dev") + new_stage_name = 'stage_with_vars_and_cache_settings' + response = client.create_stage(restApiId=api_id,stageName=new_stage_name,deploymentId=deployment_id2,variables={ + "env" : "dev" + }, cacheClusterEnabled=True,description="hello moto") + + response['ResponseMetadata'].pop('HTTPHeaders', None) # this is hard to match against, so remove it + + response.should.equal({ + 'stageName':new_stage_name, + 'deploymentId':deployment_id2, + 'methodSettings':{}, + 'variables':{ "env" : "dev" }, + 'ResponseMetadata': {'HTTPStatusCode': 200}, + 'description':'hello moto', + 'cacheClusterEnabled': True, + 'cacheClusterSize' : "0.5" + }) + + stage = client.get_stage( + restApiId=api_id, + stageName=new_stage_name + ) + + stage['cacheClusterSize'].should.equal("0.5") + + new_stage_name = 'stage_with_vars_and_cache_settings_and_size' + response = client.create_stage(restApiId=api_id,stageName=new_stage_name,deploymentId=deployment_id2,variables={ + "env" : "dev" + }, cacheClusterEnabled=True,cacheClusterSize="1.6",description="hello moto") + + response['ResponseMetadata'].pop('HTTPHeaders', None) # this is hard to match against, so remove it + + response.should.equal({ + 'stageName':new_stage_name, + 'deploymentId':deployment_id2, + 'methodSettings':{}, + 'variables':{ "env" : "dev" }, + 'ResponseMetadata': {'HTTPStatusCode': 200}, + 'description':'hello moto', + 'cacheClusterEnabled': True, + 'cacheClusterSize' : "1.6" + }) + + stage = client.get_stage( + restApiId=api_id, + stageName=new_stage_name + ) + stage['stageName'].should.equal(new_stage_name) + stage['deploymentId'].should.equal(deployment_id2) + stage['variables'].should.have.key('env').which.should.match("dev") + stage['cacheClusterSize'].should.equal("1.6")