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