Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if LoadBalancerName is set #468

Merged
merged 8 commits into from
Jun 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion senza/components/elastic_load_balancer_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from senza.components.elastic_load_balancer import (ALLOWED_LOADBALANCER_SCHEMES,
get_load_balancer_name,
get_ssl_cert)
from senza.utils import generate_valid_cloud_name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you import this here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I change it, this function should be used in line 77. thanks

from senza.definitions import AccountArguments

from ..cli import TemplateArguments
Expand Down Expand Up @@ -73,7 +74,9 @@ def component_elastic_load_balancer_v2(definition,
health_check_path = configuration.get("HealthCheckPath") or '/health'
health_check_port = configuration.get("HealthCheckPort") or configuration["HTTPPort"]

if configuration.get('NameSuffix'):
if configuration.get('LoadBalancerName'):
loadbalancer_name = generate_valid_cloud_name(configuration["LoadBalancerName"], 32)
elif configuration.get('NameSuffix'):
version = '{}-{}'.format(info["StackVersion"],
configuration['NameSuffix'])
loadbalancer_name = get_load_balancer_name(info["StackName"], version)
Expand Down
10 changes: 9 additions & 1 deletion senza/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,19 @@ def pystache_render(*args, **kwargs):
return render.render(*args, **kwargs)


def generate_valid_cloud_name(name: str, length: int):
"""
Generate a name with that length and remove double - signs
remove a starting or trailing -
"""
return re.sub(r'(-(?=-{1,})|^-|-$)', '', name[:length])


def get_load_balancer_name(stack_name: str, stack_version: str):
"""
Returns the name of the load balancer for the stack name and version,
truncating the name if necessary.
"""
# Loadbalancer name cannot exceed 32 characters, try to shorten
l = 32 - len(stack_version) - 1
return '{}-{}'.format(stack_name[:l], stack_version)
return '{}-{}'.format(generate_valid_cloud_name(stack_name, l), stack_version)
2 changes: 1 addition & 1 deletion tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -1215,4 +1215,4 @@ def test_component_autoscaling_group_multiple_load_balancer_v2(monkeypatch):

result = component_auto_scaling_group(definition, configuration, args, info, False, MagicMock())

assert [{'Ref': 'LB1TargetGroup'},{'Ref': 'LB2TargetGroup'}] == result['Resources']['Test']['Properties']['TargetGroupARNs']
assert [{'Ref': 'LB1TargetGroup'},{'Ref': 'LB2TargetGroup'}] == result['Resources']['Test']['Properties']['TargetGroupARNs']
10 changes: 9 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from senza.utils import camel_case_to_underscore, get_load_balancer_name
from senza.utils import camel_case_to_underscore, get_load_balancer_name, generate_valid_cloud_name


def test_camel_case_to_underscore():
Expand All @@ -11,3 +11,11 @@ def test_get_load_balancer_name():
assert get_load_balancer_name(stack_name='really-long-application-name',
stack_version='cd871c54') == 'really-long-application-cd871c54'
assert get_load_balancer_name(stack_name='app-name', stack_version='1') == 'app-name-1'


def test_generate_valid_cloud_name():
assert generate_valid_cloud_name(name='invalid-aws--cloud-name', length=32) == 'invalid-aws-cloud-name'
assert generate_valid_cloud_name(name='-invalid-aws-cloud-name', length=32) == 'invalid-aws-cloud-name'
assert generate_valid_cloud_name(name='invalid-aws-cloud-name-', length=32) == 'invalid-aws-cloud-name'
assert generate_valid_cloud_name(name='invalid-aws--cloud-name-', length=32) == 'invalid-aws-cloud-name'
assert generate_valid_cloud_name(name='invalid-aws-cloud-name-long-replaced', length=27) == 'invalid-aws-cloud-name-long'