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

Add a validator for ELB names #615

Merged
merged 3 commits into from
Nov 15, 2016
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
11 changes: 10 additions & 1 deletion tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from troposphere.validators import positive_integer, network_port
from troposphere.validators import s3_bucket_name, encoding, status
from troposphere.validators import iam_path, iam_names, iam_role_name
from troposphere.validators import iam_group_name, iam_user_name
from troposphere.validators import iam_group_name, iam_user_name, elb_name


class TestValidators(unittest.TestCase):
Expand Down Expand Up @@ -73,6 +73,15 @@ def test_s3_bucket_name(self):
with self.assertRaises(ValueError):
s3_bucket_name(b)

def test_elb_name(self):
for b in ['a', 'a-a', 'aaa', 'a'*32,
'wick3d-elb-name', 'Wick3d-ELB-Name']:
elb_name(b)
for b in ['a'*33, 'invalid_elb', '-invalid-elb',
'invalid-elb-', '-elb-', '-a', 'a-']:
with self.assertRaises(ValueError):
elb_name(b)

def test_encoding(self):
for e in ['plain', 'base64']:
encoding(e)
Expand Down
4 changes: 2 additions & 2 deletions troposphere/elasticloadbalancing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from . import AWSObject, AWSProperty
from .validators import (
boolean, integer_range, positive_integer, network_port, integer)
boolean, elb_name, integer_range, positive_integer, network_port, integer)


class AppCookieStickinessPolicy(AWSProperty):
Expand Down Expand Up @@ -88,7 +88,7 @@ class LoadBalancer(AWSObject):
'HealthCheck': (HealthCheck, False),
'Instances': (list, False),
'LBCookieStickinessPolicy': (list, False),
'LoadBalancerName': (basestring, False),
'LoadBalancerName': (elb_name, False),
'Listeners': (list, True),
'Policies': (list, False),
'Scheme': (basestring, False),
Expand Down
4 changes: 2 additions & 2 deletions troposphere/elasticloadbalancingv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from . import AWSObject, AWSProperty
from .validators import (
network_port, integer)
elb_name, network_port, integer)


class LoadBalancerAttributes(AWSProperty):
Expand Down Expand Up @@ -106,7 +106,7 @@ class LoadBalancer(AWSObject):

props = {
'LoadBalancerAttributes': ([LoadBalancerAttributes], False),
'Name': (basestring, False),
'Name': (elb_name, False),
'Scheme': (basestring, False),
'SecurityGroups': (list, False),
'Subnets': (list, True),
Expand Down
8 changes: 8 additions & 0 deletions troposphere/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ def s3_bucket_name(b):
raise ValueError("%s is not a valid s3 bucket name" % b)


def elb_name(b):
elb_name_re = compile(r'^[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,30}[a-zA-Z0-9]{1})?$') # noqa
if elb_name_re.match(b):
return b
else:
raise ValueError("%s is not a valid elb name" % b)


def encoding(encoding):
valid_encodings = ['plain', 'base64']
if encoding not in valid_encodings:
Expand Down