add describe_rules to elbv2
This commit is contained in:
parent
3ac10945c1
commit
a73fa64043
@ -142,3 +142,11 @@ class ActionTargetGroupNotFoundError(ELBClientError):
|
|||||||
"TargetGroupNotFound",
|
"TargetGroupNotFound",
|
||||||
"Target group '%s' not found" % arn
|
"Target group '%s' not found" % arn
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidDescribeRulesRequest(ELBClientError):
|
||||||
|
|
||||||
|
def __init__(self, msg):
|
||||||
|
super(InvalidDescribeRulesRequest, self).__init__(
|
||||||
|
"ValidationError", msg
|
||||||
|
)
|
||||||
|
@ -19,6 +19,7 @@ from .exceptions import (
|
|||||||
InvalidConditionValueError,
|
InvalidConditionValueError,
|
||||||
InvalidActionTypeError,
|
InvalidActionTypeError,
|
||||||
ActionTargetGroupNotFoundError,
|
ActionTargetGroupNotFoundError,
|
||||||
|
InvalidDescribeRulesRequest
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -313,6 +314,29 @@ class ELBv2Backend(BaseBackend):
|
|||||||
|
|
||||||
return matched_balancers
|
return matched_balancers
|
||||||
|
|
||||||
|
def describe_rules(self, listener_arn, rule_arns):
|
||||||
|
if listener_arn is None and not rule_arns:
|
||||||
|
raise InvalidDescribeRulesRequest(
|
||||||
|
"You must specify either listener rule ARNs or a listener ARN"
|
||||||
|
)
|
||||||
|
if listener_arn is not None and rule_arns is not None:
|
||||||
|
raise InvalidDescribeRulesRequest(
|
||||||
|
'Listener rule ARNs and a listener ARN cannot be specified at the same time'
|
||||||
|
)
|
||||||
|
if listener_arn:
|
||||||
|
listener = self.describe_listeners(None, [listener_arn])[0]
|
||||||
|
return listener.rules
|
||||||
|
|
||||||
|
# search for rule arns
|
||||||
|
matched_rules = []
|
||||||
|
for load_balancer_arn in self.load_balancers:
|
||||||
|
listeners = self.load_balancers.get(load_balancer_arn).listeners.values()
|
||||||
|
for listener in listeners:
|
||||||
|
for rule in listener.rules:
|
||||||
|
if rule.arn in rule_arns:
|
||||||
|
matched_rules.append(rule)
|
||||||
|
return matched_rules
|
||||||
|
|
||||||
def describe_target_groups(self, load_balancer_arn, target_group_arns, names):
|
def describe_target_groups(self, load_balancer_arn, target_group_arns, names):
|
||||||
if load_balancer_arn:
|
if load_balancer_arn:
|
||||||
if load_balancer_arn not in self.load_balancers:
|
if load_balancer_arn not in self.load_balancers:
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
import base64
|
||||||
from moto.core.responses import BaseResponse
|
from moto.core.responses import BaseResponse
|
||||||
from .models import elbv2_backends
|
from .models import elbv2_backends
|
||||||
from .exceptions import DuplicateTagKeysError
|
from .exceptions import DuplicateTagKeysError
|
||||||
@ -124,6 +125,26 @@ class ELBV2Response(BaseResponse):
|
|||||||
template = self.response_template(DESCRIBE_LOAD_BALANCERS_TEMPLATE)
|
template = self.response_template(DESCRIBE_LOAD_BALANCERS_TEMPLATE)
|
||||||
return template.render(load_balancers=load_balancers_resp, marker=next_marker)
|
return template.render(load_balancers=load_balancers_resp, marker=next_marker)
|
||||||
|
|
||||||
|
def describe_rules(self):
|
||||||
|
listener_arn = self._get_param('ListenerArn')
|
||||||
|
rule_arns = self._get_multi_param('RuleArns.member') if any(k for k in list(self.querystring.keys()) if k.startswith('RuleArns.member')) else None
|
||||||
|
all_rules = self.elbv2_backend.describe_rules(listener_arn, rule_arns)
|
||||||
|
all_arns = [rule.arn for rule in all_rules]
|
||||||
|
all_arns = [base64.urlsafe_b64encode(bytes(rule.arn, 'UTF-8')) for rule in all_rules]
|
||||||
|
page_size = self._get_int_param('PageSize', 50) # set 50 for temporary
|
||||||
|
|
||||||
|
marker = self._get_param('Marker')
|
||||||
|
if marker:
|
||||||
|
start = all_arns.index(marker) + 1
|
||||||
|
else:
|
||||||
|
start = 0
|
||||||
|
rules_resp = all_rules[start:start + page_size]
|
||||||
|
next_marker = None
|
||||||
|
if len(all_rules) > start + page_size:
|
||||||
|
next_marker = base64.urlsafe_b64encode(bytes(rules_resp[-1].arn, 'UTF-8'))
|
||||||
|
template = self.response_template(DESCRIBE_RULES_TEMPLATE)
|
||||||
|
return template.render(rules=rules_resp, marker=next_marker)
|
||||||
|
|
||||||
def describe_target_groups(self):
|
def describe_target_groups(self):
|
||||||
load_balancer_arn = self._get_param('LoadBalancerArn')
|
load_balancer_arn = self._get_param('LoadBalancerArn')
|
||||||
target_group_arns = self._get_multi_param('TargetGroupArns.member')
|
target_group_arns = self._get_multi_param('TargetGroupArns.member')
|
||||||
@ -516,6 +537,45 @@ DESCRIBE_LOAD_BALANCERS_TEMPLATE = """<DescribeLoadBalancersResponse xmlns="http
|
|||||||
</ResponseMetadata>
|
</ResponseMetadata>
|
||||||
</DescribeLoadBalancersResponse>"""
|
</DescribeLoadBalancersResponse>"""
|
||||||
|
|
||||||
|
DESCRIBE_RULES_TEMPLATE = """<DescribeRulesResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2015-12-01/">
|
||||||
|
<DescribeRulesResult>
|
||||||
|
<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>
|
||||||
|
{% if marker %}
|
||||||
|
<NextMarker>{{ marker }}</NextMarker>
|
||||||
|
{% endif %}
|
||||||
|
</DescribeRulesResult>
|
||||||
|
<ResponseMetadata>
|
||||||
|
<RequestId>74926cf3-f3a3-11e5-b543-9f2c3fbb9bee</RequestId>
|
||||||
|
</ResponseMetadata>
|
||||||
|
</DescribeRulesResponse>"""
|
||||||
|
|
||||||
DESCRIBE_TARGET_GROUPS_TEMPLATE = """<DescribeTargetGroupsResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2015-12-01/">
|
DESCRIBE_TARGET_GROUPS_TEMPLATE = """<DescribeTargetGroupsResponse xmlns="http://elasticloadbalancing.amazonaws.com/doc/2015-12-01/">
|
||||||
<DescribeTargetGroupsResult>
|
<DescribeTargetGroupsResult>
|
||||||
|
@ -614,7 +614,32 @@ def test_create_listener_rules():
|
|||||||
priorities = [rule['Priority'] for rule in rules['Rules']]
|
priorities = [rule['Priority'] for rule in rules['Rules']]
|
||||||
priorities.should.equal(['50', '100', 'default'])
|
priorities.should.equal(['50', '100', 'default'])
|
||||||
|
|
||||||
arn = rules['Rules'][0]['RuleArn']
|
# 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'])
|
||||||
|
|
||||||
|
obtained_rules = conn.describe_rules(RuleArns=[first_rule['RuleArn']])
|
||||||
|
obtained_rules['Rules'].should.equal([first_rule])
|
||||||
|
|
||||||
|
# test for pagination
|
||||||
|
obtained_rules = conn.describe_rules(ListenerArn=http_listener_arn, PageSize=1)
|
||||||
|
len(obtained_rules['Rules']).should.equal(1)
|
||||||
|
obtained_rules.should.have.key('NextMarker')
|
||||||
|
|
||||||
|
# test for invalid describe rule request
|
||||||
|
with assert_raises(ClientError):
|
||||||
|
conn.describe_rules()
|
||||||
|
with assert_raises(ClientError):
|
||||||
|
conn.describe_rules(RuleArns=[])
|
||||||
|
with assert_raises(ClientError):
|
||||||
|
conn.describe_rules(
|
||||||
|
ListenerArn=http_listener_arn,
|
||||||
|
RuleArns=[first_rule['RuleArn']]
|
||||||
|
)
|
||||||
|
|
||||||
|
# delete
|
||||||
|
arn = first_rule['RuleArn']
|
||||||
conn.delete_rule(RuleArn=arn)
|
conn.delete_rule(RuleArn=arn)
|
||||||
# TODO: describe rule and ensure rule is removed
|
# TODO: describe rule and ensure rule is removed
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user