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)
This commit is contained in:
Hugo Lopes Tavares 2017-11-15 14:36:53 -05:00 committed by Terry Cain
parent 7b1ec157b8
commit aa6a0765c1
3 changed files with 69 additions and 36 deletions

View File

@ -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

View File

@ -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)

View File

@ -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'])