From aa6a0765c1202fc0a375a422f2f9637a8f7f12b3 Mon Sep 17 00:00:00 2001 From: Hugo Lopes Tavares Date: Wed, 15 Nov 2017 14:36:53 -0500 Subject: [PATCH] Move target group default values to model FakeTargetGroup (#1343) Before this commit everything that needs to create target groups had to handle the default values (i.e., cloudformation call & ELBV2Response call) --- moto/elbv2/models.py | 35 +++++-------- moto/elbv2/responses.py | 18 +++---- .../test_cloudformation_stack_integration.py | 52 ++++++++++++++++--- 3 files changed, 69 insertions(+), 36 deletions(-) diff --git a/moto/elbv2/models.py b/moto/elbv2/models.py index c9e77b10..e78cf348 100644 --- a/moto/elbv2/models.py +++ b/moto/elbv2/models.py @@ -52,13 +52,13 @@ class FakeTargetGroup(BaseModel): vpc_id, protocol, port, - healthcheck_protocol, - healthcheck_port, - healthcheck_path, - healthcheck_interval_seconds, - healthcheck_timeout_seconds, - healthy_threshold_count, - unhealthy_threshold_count, + healthcheck_protocol='HTTP', + healthcheck_port='traffic-port', + healthcheck_path='/', + healthcheck_interval_seconds='30', + healthcheck_timeout_seconds='5', + healthy_threshold_count='5', + unhealthy_threshold_count='2', matcher=None, target_type=None): self.name = name @@ -75,7 +75,10 @@ class FakeTargetGroup(BaseModel): self.unhealthy_threshold_count = unhealthy_threshold_count self.load_balancer_arns = [] self.tags = {} - self.matcher = matcher + if matcher is None: + self.matcher = {'HttpCode': '200'} + else: + self.matcher = matcher self.target_type = target_type self.attributes = { @@ -454,28 +457,18 @@ class ELBv2Backend(BaseBackend): raise DuplicateTargetGroupName() valid_protocols = ['HTTPS', 'HTTP', 'TCP'] - if kwargs['healthcheck_protocol'] not in valid_protocols: + if kwargs.get('healthcheck_protocol') and kwargs['healthcheck_protocol'] not in valid_protocols: raise InvalidConditionValueError( "Value {} at 'healthCheckProtocol' failed to satisfy constraint: " "Member must satisfy enum value set: {}".format(kwargs['healthcheck_protocol'], valid_protocols)) - if kwargs['protocol'] not in valid_protocols: + if kwargs.get('protocol') and kwargs['protocol'] not in valid_protocols: raise InvalidConditionValueError( "Value {} at 'protocol' failed to satisfy constraint: " "Member must satisfy enum value set: {}".format(kwargs['protocol'], valid_protocols)) - if FakeTargetGroup.HTTP_CODE_REGEX.match(kwargs['matcher']['HttpCode']) is None: + if kwargs.get('matcher') and FakeTargetGroup.HTTP_CODE_REGEX.match(kwargs['matcher']['HttpCode']) is None: raise RESTError('InvalidParameterValue', 'HttpCode must be like 200 | 200-399 | 200,201 ...') - valid_protocols = ['HTTPS', 'HTTP', 'TCP'] - if kwargs['healthcheck_protocol'] not in valid_protocols: - raise InvalidConditionValueError( - "Value {} at 'healthCheckProtocol' failed to satisfy constraint: " - "Member must satisfy enum value set: {}".format(kwargs['healthcheck_protocol'], valid_protocols)) - if kwargs['protocol'] not in valid_protocols: - raise InvalidConditionValueError( - "Value {} at 'protocol' failed to satisfy constraint: " - "Member must satisfy enum value set: {}".format(kwargs['protocol'], valid_protocols)) - arn = make_arn_for_target_group(account_id=1, name=name, region_name=self.region_name) target_group = FakeTargetGroup(name, arn, **kwargs) self.target_groups[target_group.arn] = target_group diff --git a/moto/elbv2/responses.py b/moto/elbv2/responses.py index aa855b43..7c71ce78 100644 --- a/moto/elbv2/responses.py +++ b/moto/elbv2/responses.py @@ -180,14 +180,14 @@ class ELBV2Response(BaseResponse): vpc_id = self._get_param('VpcId') protocol = self._get_param('Protocol') port = self._get_param('Port') - healthcheck_protocol = self._get_param('HealthCheckProtocol', 'HTTP') - healthcheck_port = self._get_param('HealthCheckPort', 'traffic-port') - healthcheck_path = self._get_param('HealthCheckPath', '/') - healthcheck_interval_seconds = self._get_param('HealthCheckIntervalSeconds', '30') - healthcheck_timeout_seconds = self._get_param('HealthCheckTimeoutSeconds', '5') - healthy_threshold_count = self._get_param('HealthyThresholdCount', '5') - unhealthy_threshold_count = self._get_param('UnhealthyThresholdCount', '2') - http_codes = self._get_param('Matcher.HttpCode', '200') + healthcheck_protocol = self._get_param('HealthCheckProtocol') + healthcheck_port = self._get_param('HealthCheckPort') + healthcheck_path = self._get_param('HealthCheckPath') + healthcheck_interval_seconds = self._get_param('HealthCheckIntervalSeconds') + healthcheck_timeout_seconds = self._get_param('HealthCheckTimeoutSeconds') + healthy_threshold_count = self._get_param('HealthyThresholdCount') + unhealthy_threshold_count = self._get_param('UnhealthyThresholdCount') + matcher = self._get_param('Matcher') target_group = self.elbv2_backend.create_target_group( name, @@ -201,7 +201,7 @@ class ELBV2Response(BaseResponse): healthcheck_timeout_seconds=healthcheck_timeout_seconds, healthy_threshold_count=healthy_threshold_count, unhealthy_threshold_count=unhealthy_threshold_count, - matcher={'HttpCode': http_codes} + matcher=matcher, ) template = self.response_template(CREATE_TARGET_GROUP_TEMPLATE) diff --git a/tests/test_cloudformation/test_cloudformation_stack_integration.py b/tests/test_cloudformation/test_cloudformation_stack_integration.py index 6216e40d..c4a138de 100644 --- a/tests/test_cloudformation/test_cloudformation_stack_integration.py +++ b/tests/test_cloudformation/test_cloudformation_stack_integration.py @@ -2149,7 +2149,7 @@ def test_stack_elbv2_resources_integration(): "IpAddressType": "ipv4", } }, - "mytargetgroup": { + "mytargetgroup1": { "Type": "AWS::ElasticLoadBalancingV2::TargetGroup", "Properties": { "HealthCheckIntervalSeconds": 30, @@ -2162,7 +2162,7 @@ def test_stack_elbv2_resources_integration(): "Matcher": { "HttpCode": "200,201" }, - "Name": "mytargetgroup", + "Name": "mytargetgroup1", "Port": 80, "Protocol": "HTTP", "TargetType": "instance", @@ -2177,12 +2177,37 @@ def test_stack_elbv2_resources_integration(): } } }, + "mytargetgroup2": { + "Type": "AWS::ElasticLoadBalancingV2::TargetGroup", + "Properties": { + "HealthCheckIntervalSeconds": 30, + "HealthCheckPath": "/status", + "HealthCheckPort": 8080, + "HealthCheckProtocol": "HTTP", + "HealthCheckTimeoutSeconds": 5, + "HealthyThresholdCount": 30, + "UnhealthyThresholdCount": 5, + "Name": "mytargetgroup2", + "Port": 8080, + "Protocol": "HTTP", + "TargetType": "instance", + "Targets": [{ + "Id": { + "Ref": "ec2instance", + "Port": 8080, + }, + }], + "VpcId": { + "Ref": "myvpc", + } + } + }, "listener": { "Type": "AWS::ElasticLoadBalancingV2::Listener", "Properties": { "DefaultActions": [{ "Type": "forward", - "TargetGroupArn": {"Ref": "mytargetgroup"} + "TargetGroupArn": {"Ref": "mytargetgroup1"} }], "LoadBalancerArn": {"Ref": "alb"}, "Port": "80", @@ -2236,8 +2261,10 @@ def test_stack_elbv2_resources_integration(): load_balancers[0]['Type'].should.equal('application') load_balancers[0]['IpAddressType'].should.equal('ipv4') - target_groups = elbv2_conn.describe_target_groups()['TargetGroups'] - len(target_groups).should.equal(1) + target_groups = sorted( + elbv2_conn.describe_target_groups()['TargetGroups'], + key=lambda tg: tg['TargetGroupName']) # sort to do comparison with indexes + len(target_groups).should.equal(2) target_groups[0]['HealthCheckIntervalSeconds'].should.equal(30) target_groups[0]['HealthCheckPath'].should.equal('/status') target_groups[0]['HealthCheckPort'].should.equal('80') @@ -2246,11 +2273,24 @@ def test_stack_elbv2_resources_integration(): target_groups[0]['HealthyThresholdCount'].should.equal(30) target_groups[0]['UnhealthyThresholdCount'].should.equal(5) target_groups[0]['Matcher'].should.equal({'HttpCode': '200,201'}) - target_groups[0]['TargetGroupName'].should.equal('mytargetgroup') + target_groups[0]['TargetGroupName'].should.equal('mytargetgroup1') target_groups[0]['Port'].should.equal(80) target_groups[0]['Protocol'].should.equal('HTTP') target_groups[0]['TargetType'].should.equal('instance') + target_groups[1]['HealthCheckIntervalSeconds'].should.equal(30) + target_groups[1]['HealthCheckPath'].should.equal('/status') + target_groups[1]['HealthCheckPort'].should.equal('8080') + target_groups[1]['HealthCheckProtocol'].should.equal('HTTP') + target_groups[1]['HealthCheckTimeoutSeconds'].should.equal(5) + target_groups[1]['HealthyThresholdCount'].should.equal(30) + target_groups[1]['UnhealthyThresholdCount'].should.equal(5) + target_groups[1]['Matcher'].should.equal({'HttpCode': '200'}) + target_groups[1]['TargetGroupName'].should.equal('mytargetgroup2') + target_groups[1]['Port'].should.equal(8080) + target_groups[1]['Protocol'].should.equal('HTTP') + target_groups[1]['TargetType'].should.equal('instance') + listeners = elbv2_conn.describe_listeners(LoadBalancerArn=load_balancers[0]['LoadBalancerArn'])['Listeners'] len(listeners).should.equal(1) listeners[0]['LoadBalancerArn'].should.equal(load_balancers[0]['LoadBalancerArn'])