Add support for redirect actions on ELBv2 listeners
This commit is contained in:
parent
850496f29a
commit
a86ec26e46
@ -131,7 +131,7 @@ class InvalidActionTypeError(ELBClientError):
|
|||||||
def __init__(self, invalid_name, index):
|
def __init__(self, invalid_name, index):
|
||||||
super(InvalidActionTypeError, self).__init__(
|
super(InvalidActionTypeError, self).__init__(
|
||||||
"ValidationError",
|
"ValidationError",
|
||||||
"1 validation error detected: Value '%s' at 'actions.%s.member.type' failed to satisfy constraint: Member must satisfy enum value set: [forward]" % (invalid_name, index)
|
"1 validation error detected: Value '%s' at 'actions.%s.member.type' failed to satisfy constraint: Member must satisfy enum value set: [forward, redirect]" % (invalid_name, index)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -204,8 +204,20 @@ class FakeListener(BaseModel):
|
|||||||
# transform default actions to confirm with the rest of the code and XML templates
|
# transform default actions to confirm with the rest of the code and XML templates
|
||||||
if "DefaultActions" in properties:
|
if "DefaultActions" in properties:
|
||||||
default_actions = []
|
default_actions = []
|
||||||
for action in properties['DefaultActions']:
|
for i, action in enumerate(properties['DefaultActions']):
|
||||||
default_actions.append({'type': action['Type'], 'target_group_arn': action['TargetGroupArn']})
|
action_type = action['Type']
|
||||||
|
if action_type == 'forward':
|
||||||
|
default_actions.append({'type': action_type, 'target_group_arn': action['TargetGroupArn']})
|
||||||
|
elif action_type == 'redirect':
|
||||||
|
redirect_action = {'type': action_type, }
|
||||||
|
for redirect_config_key, redirect_config_value in action['RedirectConfig'].items():
|
||||||
|
# need to match the output of _get_list_prefix
|
||||||
|
if redirect_config_key == 'StatusCode':
|
||||||
|
redirect_config_key = 'status_code'
|
||||||
|
redirect_action['redirect_config._' + redirect_config_key.lower()] = redirect_config_value
|
||||||
|
default_actions.append(redirect_action)
|
||||||
|
else:
|
||||||
|
raise InvalidActionTypeError(action_type, i + 1)
|
||||||
else:
|
else:
|
||||||
default_actions = None
|
default_actions = None
|
||||||
|
|
||||||
@ -417,11 +429,15 @@ class ELBv2Backend(BaseBackend):
|
|||||||
for i, action in enumerate(actions):
|
for i, action in enumerate(actions):
|
||||||
index = i + 1
|
index = i + 1
|
||||||
action_type = action['type']
|
action_type = action['type']
|
||||||
if action_type not in ['forward']:
|
if action_type == 'forward':
|
||||||
raise InvalidActionTypeError(action_type, index)
|
|
||||||
action_target_group_arn = action['target_group_arn']
|
action_target_group_arn = action['target_group_arn']
|
||||||
if action_target_group_arn not in target_group_arns:
|
if action_target_group_arn not in target_group_arns:
|
||||||
raise ActionTargetGroupNotFoundError(action_target_group_arn)
|
raise ActionTargetGroupNotFoundError(action_target_group_arn)
|
||||||
|
elif action_type == 'redirect':
|
||||||
|
# nothing to do
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
raise InvalidActionTypeError(action_type, index)
|
||||||
|
|
||||||
# TODO: check for error 'TooManyRegistrationsForTargetId'
|
# TODO: check for error 'TooManyRegistrationsForTargetId'
|
||||||
# TODO: check for error 'TooManyRules'
|
# TODO: check for error 'TooManyRules'
|
||||||
@ -483,10 +499,18 @@ class ELBv2Backend(BaseBackend):
|
|||||||
arn = load_balancer_arn.replace(':loadbalancer/', ':listener/') + "/%s%s" % (port, id(self))
|
arn = load_balancer_arn.replace(':loadbalancer/', ':listener/') + "/%s%s" % (port, id(self))
|
||||||
listener = FakeListener(load_balancer_arn, arn, protocol, port, ssl_policy, certificate, default_actions)
|
listener = FakeListener(load_balancer_arn, arn, protocol, port, ssl_policy, certificate, default_actions)
|
||||||
balancer.listeners[listener.arn] = listener
|
balancer.listeners[listener.arn] = listener
|
||||||
for action in default_actions:
|
for i, action in enumerate(default_actions):
|
||||||
|
action_type = action['type']
|
||||||
|
if action_type == 'forward':
|
||||||
if action['target_group_arn'] in self.target_groups.keys():
|
if action['target_group_arn'] in self.target_groups.keys():
|
||||||
target_group = self.target_groups[action['target_group_arn']]
|
target_group = self.target_groups[action['target_group_arn']]
|
||||||
target_group.load_balancer_arns.append(load_balancer_arn)
|
target_group.load_balancer_arns.append(load_balancer_arn)
|
||||||
|
elif action_type == 'redirect':
|
||||||
|
# nothing to do
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
raise InvalidActionTypeError(action_type, i + 1)
|
||||||
|
|
||||||
return listener
|
return listener
|
||||||
|
|
||||||
def describe_load_balancers(self, arns, names):
|
def describe_load_balancers(self, arns, names):
|
||||||
@ -649,11 +673,15 @@ class ELBv2Backend(BaseBackend):
|
|||||||
for i, action in enumerate(actions):
|
for i, action in enumerate(actions):
|
||||||
index = i + 1
|
index = i + 1
|
||||||
action_type = action['type']
|
action_type = action['type']
|
||||||
if action_type not in ['forward']:
|
if action_type == 'forward':
|
||||||
raise InvalidActionTypeError(action_type, index)
|
|
||||||
action_target_group_arn = action['target_group_arn']
|
action_target_group_arn = action['target_group_arn']
|
||||||
if action_target_group_arn not in target_group_arns:
|
if action_target_group_arn not in target_group_arns:
|
||||||
raise ActionTargetGroupNotFoundError(action_target_group_arn)
|
raise ActionTargetGroupNotFoundError(action_target_group_arn)
|
||||||
|
elif action_type == 'redirect':
|
||||||
|
# nothing to do
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
raise InvalidActionTypeError(action_type, index)
|
||||||
|
|
||||||
# TODO: check for error 'TooManyRegistrationsForTargetId'
|
# TODO: check for error 'TooManyRegistrationsForTargetId'
|
||||||
# TODO: check for error 'TooManyRules'
|
# TODO: check for error 'TooManyRules'
|
||||||
@ -873,7 +901,7 @@ class ELBv2Backend(BaseBackend):
|
|||||||
# Its already validated in responses.py
|
# Its already validated in responses.py
|
||||||
listener.ssl_policy = ssl_policy
|
listener.ssl_policy = ssl_policy
|
||||||
|
|
||||||
if default_actions is not None:
|
if default_actions is not None and default_actions != []:
|
||||||
# Is currently not validated
|
# Is currently not validated
|
||||||
listener.default_actions = default_actions
|
listener.default_actions = default_actions
|
||||||
|
|
||||||
|
@ -704,7 +704,11 @@ CREATE_RULE_TEMPLATE = """<CreateRuleResponse xmlns="http://elasticloadbalancing
|
|||||||
{% for action in rule.actions %}
|
{% for action in rule.actions %}
|
||||||
<member>
|
<member>
|
||||||
<Type>{{ action["type"] }}</Type>
|
<Type>{{ action["type"] }}</Type>
|
||||||
|
{% if action["type"] == "forward" %}
|
||||||
<TargetGroupArn>{{ action["target_group_arn"] }}</TargetGroupArn>
|
<TargetGroupArn>{{ action["target_group_arn"] }}</TargetGroupArn>
|
||||||
|
{% elif action["type"] == "redirect" %}
|
||||||
|
<RedirectConfig>{{ action["redirect_config"] }}</RedirectConfig>
|
||||||
|
{% endif %}
|
||||||
</member>
|
</member>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</Actions>
|
</Actions>
|
||||||
@ -772,7 +776,15 @@ CREATE_LISTENER_TEMPLATE = """<CreateListenerResponse xmlns="http://elasticloadb
|
|||||||
{% for action in listener.default_actions %}
|
{% for action in listener.default_actions %}
|
||||||
<member>
|
<member>
|
||||||
<Type>{{ action.type }}</Type>
|
<Type>{{ action.type }}</Type>
|
||||||
<TargetGroupArn>{{ action.target_group_arn }}</TargetGroupArn>
|
{% if action["type"] == "forward" %}
|
||||||
|
<TargetGroupArn>{{ action["target_group_arn"] }}</TargetGroupArn>
|
||||||
|
{% elif action["type"] == "redirect" %}
|
||||||
|
<RedirectConfig>
|
||||||
|
<Protocol>{{ action["redirect_config._protocol"] }}</Protocol>
|
||||||
|
<Port>{{ action["redirect_config._port"] }}</Port>
|
||||||
|
<StatusCode>{{ action["redirect_config._status_code"] }}</StatusCode>
|
||||||
|
</RedirectConfig>
|
||||||
|
{% endif %}
|
||||||
</member>
|
</member>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</DefaultActions>
|
</DefaultActions>
|
||||||
@ -877,7 +889,15 @@ DESCRIBE_RULES_TEMPLATE = """<DescribeRulesResponse xmlns="http://elasticloadbal
|
|||||||
{% for action in rule.actions %}
|
{% for action in rule.actions %}
|
||||||
<member>
|
<member>
|
||||||
<Type>{{ action["type"] }}</Type>
|
<Type>{{ action["type"] }}</Type>
|
||||||
|
{% if action["type"] == "forward" %}
|
||||||
<TargetGroupArn>{{ action["target_group_arn"] }}</TargetGroupArn>
|
<TargetGroupArn>{{ action["target_group_arn"] }}</TargetGroupArn>
|
||||||
|
{% elif action["type"] == "redirect" %}
|
||||||
|
<RedirectConfig>
|
||||||
|
<Protocol>{{ action["redirect_config._protocol"] }}</Protocol>
|
||||||
|
<Port>{{ action["redirect_config._port"] }}</Port>
|
||||||
|
<StatusCode>{{ action["redirect_config._status_code"] }}</StatusCode>
|
||||||
|
</RedirectConfig>
|
||||||
|
{% endif %}
|
||||||
</member>
|
</member>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</Actions>
|
</Actions>
|
||||||
@ -970,7 +990,15 @@ DESCRIBE_LISTENERS_TEMPLATE = """<DescribeLoadBalancersResponse xmlns="http://el
|
|||||||
{% for action in listener.default_actions %}
|
{% for action in listener.default_actions %}
|
||||||
<member>
|
<member>
|
||||||
<Type>{{ action.type }}</Type>
|
<Type>{{ action.type }}</Type>
|
||||||
<TargetGroupArn>{{ action.target_group_arn }}</TargetGroupArn>
|
{% if action["type"] == "forward" %}
|
||||||
|
<TargetGroupArn>{{ action["target_group_arn"] }}</TargetGroupArn>m
|
||||||
|
{% elif action["type"] == "redirect" %}
|
||||||
|
<RedirectConfig>
|
||||||
|
<Protocol>{{ action["redirect_config._protocol"] }}</Protocol>
|
||||||
|
<Port>{{ action["redirect_config._port"] }}</Port>
|
||||||
|
<StatusCode>{{ action["redirect_config._status_code"] }}</StatusCode>
|
||||||
|
</RedirectConfig>
|
||||||
|
{% endif %}
|
||||||
</member>
|
</member>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</DefaultActions>
|
</DefaultActions>
|
||||||
@ -1399,7 +1427,15 @@ MODIFY_LISTENER_TEMPLATE = """<ModifyListenerResponse xmlns="http://elasticloadb
|
|||||||
{% for action in listener.default_actions %}
|
{% for action in listener.default_actions %}
|
||||||
<member>
|
<member>
|
||||||
<Type>{{ action.type }}</Type>
|
<Type>{{ action.type }}</Type>
|
||||||
<TargetGroupArn>{{ action.target_group_arn }}</TargetGroupArn>
|
{% if action["type"] == "forward" %}
|
||||||
|
<TargetGroupArn>{{ action["target_group_arn"] }}</TargetGroupArn>
|
||||||
|
{% elif action["type"] == "redirect" %}
|
||||||
|
<RedirectConfig>
|
||||||
|
<Protocol>{{ action["redirect_config._protocol"] }}</Protocol>
|
||||||
|
<Port>{{ action["redirect_config._port"] }}</Port>
|
||||||
|
<StatusCode>{{ action["redirect_config._status_code"] }}</StatusCode>
|
||||||
|
</RedirectConfig>
|
||||||
|
{% endif %}
|
||||||
</member>
|
</member>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</DefaultActions>
|
</DefaultActions>
|
||||||
|
@ -1586,3 +1586,143 @@ def test_create_target_groups_through_cloudformation():
|
|||||||
assert len(
|
assert len(
|
||||||
[tg for tg in target_group_dicts if tg['TargetGroupName'].startswith('test-stack')]
|
[tg for tg in target_group_dicts if tg['TargetGroupName'].startswith('test-stack')]
|
||||||
) == 2
|
) == 2
|
||||||
|
|
||||||
|
|
||||||
|
@mock_elbv2
|
||||||
|
@mock_ec2
|
||||||
|
def test_redirect_action_listener_rule():
|
||||||
|
conn = boto3.client('elbv2', region_name='us-east-1')
|
||||||
|
ec2 = boto3.resource('ec2', region_name='us-east-1')
|
||||||
|
|
||||||
|
security_group = ec2.create_security_group(
|
||||||
|
GroupName='a-security-group', Description='First One')
|
||||||
|
vpc = ec2.create_vpc(CidrBlock='172.28.7.0/24', InstanceTenancy='default')
|
||||||
|
subnet1 = ec2.create_subnet(
|
||||||
|
VpcId=vpc.id,
|
||||||
|
CidrBlock='172.28.7.192/26',
|
||||||
|
AvailabilityZone='us-east-1a')
|
||||||
|
subnet2 = ec2.create_subnet(
|
||||||
|
VpcId=vpc.id,
|
||||||
|
CidrBlock='172.28.7.192/26',
|
||||||
|
AvailabilityZone='us-east-1b')
|
||||||
|
|
||||||
|
response = conn.create_load_balancer(
|
||||||
|
Name='my-lb',
|
||||||
|
Subnets=[subnet1.id, subnet2.id],
|
||||||
|
SecurityGroups=[security_group.id],
|
||||||
|
Scheme='internal',
|
||||||
|
Tags=[{'Key': 'key_name', 'Value': 'a_value'}])
|
||||||
|
|
||||||
|
load_balancer_arn = response.get('LoadBalancers')[0].get('LoadBalancerArn')
|
||||||
|
|
||||||
|
response = conn.create_listener(LoadBalancerArn=load_balancer_arn,
|
||||||
|
Protocol='HTTP',
|
||||||
|
Port=80,
|
||||||
|
DefaultActions=[
|
||||||
|
{'Type': 'redirect',
|
||||||
|
'RedirectConfig': {
|
||||||
|
'Protocol': 'HTTPS',
|
||||||
|
'Port': '443',
|
||||||
|
'StatusCode': 'HTTP_301'
|
||||||
|
}}])
|
||||||
|
|
||||||
|
listener = response.get('Listeners')[0]
|
||||||
|
expected_default_actions = [{
|
||||||
|
'Type': 'redirect',
|
||||||
|
'RedirectConfig': {
|
||||||
|
'Protocol': 'HTTPS',
|
||||||
|
'Port': '443',
|
||||||
|
'StatusCode': 'HTTP_301'
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
listener.get('DefaultActions').should.equal(expected_default_actions)
|
||||||
|
listener_arn = listener.get('ListenerArn')
|
||||||
|
|
||||||
|
describe_rules_response = conn.describe_rules(ListenerArn=listener_arn)
|
||||||
|
describe_rules_response['Rules'][0]['Actions'].should.equal(expected_default_actions)
|
||||||
|
|
||||||
|
describe_listener_response = conn.describe_listeners(ListenerArns=[listener_arn, ])
|
||||||
|
describe_listener_actions = describe_listener_response['Listeners'][0]['DefaultActions']
|
||||||
|
describe_listener_actions.should.equal(expected_default_actions)
|
||||||
|
|
||||||
|
modify_listener_response = conn.modify_listener(ListenerArn=listener_arn, Port=81)
|
||||||
|
modify_listener_actions = modify_listener_response['Listeners'][0]['DefaultActions']
|
||||||
|
modify_listener_actions.should.equal(expected_default_actions)
|
||||||
|
|
||||||
|
|
||||||
|
@mock_elbv2
|
||||||
|
@mock_cloudformation
|
||||||
|
def test_redirect_action_listener_rule_cloudformation():
|
||||||
|
cnf_conn = boto3.client('cloudformation', region_name='us-east-1')
|
||||||
|
elbv2_client = boto3.client('elbv2', region_name='us-east-1')
|
||||||
|
|
||||||
|
template = {
|
||||||
|
"AWSTemplateFormatVersion": "2010-09-09",
|
||||||
|
"Description": "ECS Cluster Test CloudFormation",
|
||||||
|
"Resources": {
|
||||||
|
"testVPC": {
|
||||||
|
"Type": "AWS::EC2::VPC",
|
||||||
|
"Properties": {
|
||||||
|
"CidrBlock": "10.0.0.0/16",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"subnet1": {
|
||||||
|
"Type": "AWS::EC2::Subnet",
|
||||||
|
"Properties": {
|
||||||
|
"CidrBlock": "10.0.0.0/24",
|
||||||
|
"VpcId": {"Ref": "testVPC"},
|
||||||
|
"AvalabilityZone": "us-east-1b",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"subnet2": {
|
||||||
|
"Type": "AWS::EC2::Subnet",
|
||||||
|
"Properties": {
|
||||||
|
"CidrBlock": "10.0.1.0/24",
|
||||||
|
"VpcId": {"Ref": "testVPC"},
|
||||||
|
"AvalabilityZone": "us-east-1b",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"testLb": {
|
||||||
|
"Type": "AWS::ElasticLoadBalancingV2::LoadBalancer",
|
||||||
|
"Properties": {
|
||||||
|
"Name": "my-lb",
|
||||||
|
"Subnets": [{"Ref": "subnet1"}, {"Ref": "subnet2"}],
|
||||||
|
"Type": "application",
|
||||||
|
"SecurityGroups": [],
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"testListener": {
|
||||||
|
"Type": "AWS::ElasticLoadBalancingV2::Listener",
|
||||||
|
"Properties": {
|
||||||
|
"LoadBalancerArn": {"Ref": "testLb"},
|
||||||
|
"Port": 80,
|
||||||
|
"Protocol": "HTTP",
|
||||||
|
"DefaultActions": [{
|
||||||
|
"Type": "redirect",
|
||||||
|
"RedirectConfig": {
|
||||||
|
"Port": "443",
|
||||||
|
"Protocol": "HTTPS",
|
||||||
|
"StatusCode": "HTTP_301",
|
||||||
|
}
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template_json = json.dumps(template)
|
||||||
|
cnf_conn.create_stack(StackName="test-stack", TemplateBody=template_json)
|
||||||
|
|
||||||
|
describe_load_balancers_response = elbv2_client.describe_load_balancers(Names=['my-lb',])
|
||||||
|
describe_load_balancers_response['LoadBalancers'].should.have.length_of(1)
|
||||||
|
load_balancer_arn = describe_load_balancers_response['LoadBalancers'][0]['LoadBalancerArn']
|
||||||
|
|
||||||
|
describe_listeners_response = elbv2_client.describe_listeners(LoadBalancerArn=load_balancer_arn)
|
||||||
|
|
||||||
|
describe_listeners_response['Listeners'].should.have.length_of(1)
|
||||||
|
describe_listeners_response['Listeners'][0]['DefaultActions'].should.equal([{
|
||||||
|
'Type': 'redirect',
|
||||||
|
'RedirectConfig': {
|
||||||
|
'Port': '443', 'Protocol': 'HTTPS', 'StatusCode': 'HTTP_301',
|
||||||
|
}
|
||||||
|
},])
|
||||||
|
Loading…
Reference in New Issue
Block a user