Skip to content

Commit

Permalink
Move target group default values to model FakeTargetGroup
Browse files Browse the repository at this point in the history
Before this commit everything that needs to create target groups
had to handle the default values (i.e., cloudformation call & ELBV2Response call)
  • Loading branch information
hltbra committed Nov 15, 2017
1 parent 7b1ec15 commit 88b3894
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 36 deletions.
35 changes: 14 additions & 21 deletions moto/elbv2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ def __init__(self,
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
Expand All @@ -75,7 +75,10 @@ def __init__(self,
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 = {
Expand Down Expand Up @@ -454,28 +457,18 @@ def create_target_group(self, name, **kwargs):
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
Expand Down
18 changes: 9 additions & 9 deletions moto/elbv2/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,14 @@ def create_target_group(self):
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,
Expand All @@ -201,7 +201,7 @@ def create_target_group(self):
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)
Expand Down
52 changes: 46 additions & 6 deletions tests/test_cloudformation/test_cloudformation_stack_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2149,7 +2149,7 @@ def test_stack_elbv2_resources_integration():
"IpAddressType": "ipv4",
}
},
"mytargetgroup": {
"mytargetgroup1": {
"Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
"Properties": {
"HealthCheckIntervalSeconds": 30,
Expand All @@ -2162,7 +2162,7 @@ def test_stack_elbv2_resources_integration():
"Matcher": {
"HttpCode": "200,201"
},
"Name": "mytargetgroup",
"Name": "mytargetgroup1",
"Port": 80,
"Protocol": "HTTP",
"TargetType": "instance",
Expand All @@ -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",
Expand Down Expand Up @@ -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')
Expand All @@ -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'])
Expand Down

0 comments on commit 88b3894

Please sign in to comment.