Skip to content

Commit

Permalink
Refactored get_load_balancer_name to be used when getting stack health
Browse files Browse the repository at this point in the history
  • Loading branch information
Arjun Naik committed Mar 22, 2017
1 parent b362a33 commit 157a2a7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
4 changes: 2 additions & 2 deletions senza/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from .traffic import (change_version_traffic, get_records,
print_version_traffic, resolve_to_ip_addresses)
from .utils import (camel_case_to_underscore, ensure_keys, named_value,
pystache_render)
pystache_render, get_load_balancer_name)

STYLES = {
'RUNNING': {'fg': 'green'},
Expand Down Expand Up @@ -532,7 +532,7 @@ def health(region, stack_ref, all, output, w, watch):
alb_id = ''.join([d['Value'] for d in metric['Dimensions'] if d['Name'] == 'LoadBalancer'])
alb_ids[alb_id.split('/')[1]] = alb_id
for stack in get_stacks(stack_refs, region, all=all):
lb_name = stack.StackName
lb_name = get_load_balancer_name(stack_name=stack.name, stack_version=stack.version)
instance_health = get_instance_health(lb_name, region)
row = {
'stack_name': stack.name,
Expand Down
11 changes: 1 addition & 10 deletions senza/components/elastic_load_balancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from clickclick import fatal_error
from senza.aws import resolve_security_groups
from senza.definitions import AccountArguments
from senza.utils import get_load_balancer_name

from ..cli import TemplateArguments
from ..manaus import ClientError
Expand All @@ -15,16 +16,6 @@
ALLOWED_LOADBALANCER_SCHEMES = frozenset(["internet-facing", "internal"])


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)


def get_ssl_cert(subdomain, main_zone, configuration, account_info: AccountArguments):
ssl_cert = configuration.get('SSLCertificateId')

Expand Down
10 changes: 10 additions & 0 deletions senza/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,13 @@ def pystache_render(*args, **kwargs):
"""
render = pystache.Renderer(missing_tags='strict')
return render.render(*args, **kwargs)


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)
8 changes: 7 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from senza.utils import camel_case_to_underscore
from senza.utils import camel_case_to_underscore, get_load_balancer_name


def test_camel_case_to_underscore():
assert camel_case_to_underscore('CamelCaseToUnderscore') == 'camel_case_to_underscore'
assert camel_case_to_underscore('ThisIsABook') == 'this_is_a_book'
assert camel_case_to_underscore('InstanceID') == 'instance_id'


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'

0 comments on commit 157a2a7

Please sign in to comment.