add modify_rules to elbv2

This commit is contained in:
Toshiya Kawasaki 2017-08-17 02:25:39 +09:00
parent a73fa64043
commit 9bc6779485
4 changed files with 135 additions and 2 deletions

View File

@ -150,3 +150,11 @@ class InvalidDescribeRulesRequest(ELBClientError):
super(InvalidDescribeRulesRequest, self).__init__(
"ValidationError", msg
)
class RuleNotFoundError(ELBClientError):
def __init__(self):
super(RuleNotFoundError, self).__init__(
"RuleNotFound",
"The specified rule does not exist.")

View File

@ -19,7 +19,8 @@ from .exceptions import (
InvalidConditionValueError,
InvalidActionTypeError,
ActionTargetGroupNotFoundError,
InvalidDescribeRulesRequest
InvalidDescribeRulesRequest,
RuleNotFoundError
)
@ -406,6 +407,49 @@ class ELBv2Backend(BaseBackend):
return listener
raise ListenerNotFoundError()
def modify_rule(self, rule_arn, conditions, actions):
rules = self.describe_rules(listener_arn=None, rule_arns=[rule_arn])
if not rules:
raise RuleNotFoundError()
rule = rules[0]
# validate conditions
for condition in conditions:
field = condition['field']
if field not in ['path-pattern', 'host-header']:
raise InvalidConditionFieldError(field)
values = condition['values']
if len(values) == 0:
raise InvalidConditionValueError('A condition value must be specified')
if len(values) > 1:
raise InvalidConditionValueError(
"The '%s' field contains too many values; the limit is '1'" % field
)
# TODO: check pattern of value for 'host-header'
# TODO: check pattern of value for 'path-pattern'
# validate Actions
target_group_arns = [target_group.arn for target_group in self.target_groups.values()]
for i, action in enumerate(actions):
index = i + 1
action_type = action['type']
if action_type not in ['forward']:
raise InvalidActionTypeError(action_type, index)
action_target_group_arn = action['target_group_arn']
if action_target_group_arn not in target_group_arns:
raise ActionTargetGroupNotFoundError(action_target_group_arn)
# TODO: check for error 'TooManyRegistrationsForTargetId'
# TODO: check for error 'TooManyRules'
# modify rule
rule.conditions = conditions
rule.actions = actions
return [rule]
def register_targets(self, target_group_arn, instances):
target_group = self.target_groups.get(target_group_arn)
if target_group is None:

View File

@ -196,6 +196,28 @@ class ELBV2Response(BaseResponse):
template = self.response_template(DELETE_LISTENER_TEMPLATE)
return template.render()
def modify_rule(self):
rule_arn = self._get_param('RuleArn')
_conditions = self._get_list_prefix('Conditions.member')
conditions = []
for _condition in _conditions:
condition = {}
condition['field'] = _condition['field']
values = sorted(
[e for e in _condition.items() if e[0].startswith('values.member')],
key=lambda x: x[0]
)
condition['values'] = [e[1] for e in values]
conditions.append(condition)
actions = self._get_list_prefix('Actions.member')
rules = self.elbv2_backend.modify_rule(
rule_arn=rule_arn,
conditions=conditions,
actions=actions
)
template = self.response_template(MODIFY_RULE_TEMPLATE)
return template.render(rules=rules)
def modify_target_group_attributes(self):
target_group_arn = self._get_param('TargetGroupArn')
target_group = self.elbv2_backend.target_groups.get(target_group_arn)
@ -678,6 +700,43 @@ CONFIGURE_HEALTH_CHECK_TEMPLATE = """<ConfigureHealthCheckResponse xmlns="http:/
</ResponseMetadata>
</ConfigureHealthCheckResponse>"""
MODIFY_RULE_TEMPLATE = """<ModifyRuleResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2015-12-01/">
<ModifyRuleResult>
<Rules>
{% for rule in rules %}
<member>
<IsDefault>{{ "true" if rule.is_default else "false" }}</IsDefault>
<Conditions>
{% for condition in rule.conditions %}
<member>
<Field>{{ condition["field"] }}</Field>
<Values>
{% for value in condition["values"] %}
<member>{{ value }}</member>
{% endfor %}
</Values>
</member>
{% endfor %}
</Conditions>
<Priority>{{ rule.priority }}</Priority>
<Actions>
{% for action in rule.actions %}
<member>
<Type>{{ action["type"] }}</Type>
<TargetGroupArn>{{ action["target_group_arn"] }}</TargetGroupArn>
</member>
{% endfor %}
</Actions>
<RuleArn>{{ rule.arn }}</RuleArn>
</member>
{% endfor %}
</Rules>
</ModifyRuleResult>
<ResponseMetadata>
<RequestId>c5478c83-f397-11e5-bb98-57195a6eb84a</RequestId>
</ResponseMetadata>
</ModifyRuleResponse>"""
MODIFY_TARGET_GROUP_ATTRIBUTES_TEMPLATE = """<ModifyTargetGroupAttributesResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2015-12-01/">
<ModifyTargetGroupAttributesResult>
<Attributes>

View File

@ -615,10 +615,10 @@ def test_create_listener_rules():
priorities.should.equal(['50', '100', 'default'])
# test for describe listeners
first_rule = rules['Rules'][0]
obtained_rules = conn.describe_rules(ListenerArn=http_listener_arn)
obtained_rules['Rules'].should.equal(rules['Rules'])
first_rule = obtained_rules['Rules'][0]
obtained_rules = conn.describe_rules(RuleArns=[first_rule['RuleArn']])
obtained_rules['Rules'].should.equal([first_rule])
@ -638,6 +638,28 @@ def test_create_listener_rules():
RuleArns=[first_rule['RuleArn']]
)
# modify
new_host = 'new.example.com'
new_path_pattern = 'new_path'
modified_rule = conn.modify_rule(
RuleArn=first_rule['RuleArn'],
Conditions=[{
'Field': 'host-header',
'Values': [ new_host ]
},
{
'Field': 'path-pattern',
'Values': [ new_path_pattern ]
}],
Actions=[{
'TargetGroupArn': target_group.get('TargetGroupArn'),
'Type': 'forward'
}]
)['Rules'][0]
rules = conn.describe_rules(ListenerArn=http_listener_arn)
modified_rule.should.equal(rules['Rules'][0])
# delete
arn = first_rule['RuleArn']
conn.delete_rule(RuleArn=arn)