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 2 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
10 changes: 9 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,14 @@ def test_s3_bucket_name(self):
with self.assertRaises(ValueError):
s3_bucket_name(b)

def test_elb_name(self):
for b in ['a', 'a'*32, 'wick3d-elb-name', 'Wick3d-ELB-Name']:
Copy link
Member

Choose a reason for hiding this comment

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

Given the regex you might want to add:

a-a
aa
aaa

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added in 9b55342

elb_name(b)
for b in ['a'*63, 'invalid_elb', '-invalid-elb',
Copy link
Member

Choose a reason for hiding this comment

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

Probably want 'a'*33 instead of 63. Add 'a-' as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Copy pasta. Fixed and expanded in 9b55342

'invalid-elb-', '-elb-']:
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}$')
Copy link
Member

Choose a reason for hiding this comment

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

Try something like this:

^[a-zA-Z0-9](?:[a-zA-Z0-9\-]{0,30}[a-zA-Z0-9]{1})?$

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This worked! Nice call. I've added it here: 9b55342

I tested manually a bit on http://pythex.org/ just for context.

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