Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
ELB Automatic resource naming
Browse files Browse the repository at this point in the history
This change removes settings LoadBalancerName manually and
uses the AWS auto naming instead.

* Do not set LoadBalancerName manually, use AWS auto naming
* Add elb dns names as outputs
* Pass troposphere template to elb function so that resources
and outputs can be set up internal to the function
* Update tests for elb method requiring template and resultant
need to seperate out resource componenents for comparison
* PEP8 fixes for test.py and test_iam.py

(Closes #136)
  • Loading branch information
Niall Creech committed Jul 30, 2015
1 parent 527c5dd commit a2ca850
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 50 deletions.
49 changes: 35 additions & 14 deletions bootstrap_cfn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ def process(self):
map(template.add_resource, ec2)

if 'elb' in self.data:
elbs, sgs = self.elb()
map(template.add_resource, elbs)
map(template.add_resource, sgs)
template = self._attach_elbs(template)
self.elb(template)

if 'rds' in self.data:
self.rds(template)
Expand Down Expand Up @@ -400,12 +397,23 @@ def rds(self, template):
Value=GetAtt(rds_instance, "Endpoint.Port")
))

def elb(self):
def elb(self, template):
"""
Create an ELB resource configuration from the config file data
and add them to the troposphere template. Outputs for each ELB's
DNSName are created.
Args:
template:
The cloudformation template file
"""
# REQUIRED FIELDS AND MAPPING
# Note, 'name' field is used internally to help label
# logical ids, and as part of the DNS record name.
required_fields = {
'listeners': 'Listeners',
'scheme': 'Scheme',
'name': 'LoadBalancerName',
'name': None,
'hosted_zone': 'HostedZoneName'
}

Expand All @@ -425,7 +433,6 @@ def elb(self):
Subnets=[Ref("SubnetA"), Ref("SubnetB"), Ref("SubnetC")],
Listeners=elb['listeners'],
Scheme=elb['scheme'],
LoadBalancerName=self._get_elb_canonical_name(elb['name']),
ConnectionDrainingPolicy=ConnectionDrainingPolicy(
Enabled=True,
Timeout=120,
Expand Down Expand Up @@ -502,7 +509,8 @@ def elb(self):
Ref("AWS::Region"),
":",
Ref("AWS::AccountId"),
':loadbalancer/%s' % load_balancer.LoadBalancerName
':loadbalancer/',
Ref(load_balancer)
])
],
"Effect": "Allow"}
Expand Down Expand Up @@ -544,7 +552,20 @@ def elb(self):
)
load_balancer.SecurityGroups = [Ref(sg)]
elb_sgs.append(sg)
return elb_list, elb_sgs

# Add outputs
output_name = "ELB" + safe_name
logging.debug("config:elb:Adding output to ELB '%s'" % (output_name))
template.add_output(Output(
output_name,
Description="ELB DNSName",
Value=GetAtt(load_balancer, "DNSName")
))

# Update template with ELB resources
map(template.add_resource, elb_list)
map(template.add_resource, elb_sgs)
template = self._attach_elbs(template)

def _convert_ref_dict_to_objects(self, o):
"""
Expand Down Expand Up @@ -725,10 +746,10 @@ def _attach_elbs(self, template):
return template
asgs = self._find_resources(template,
'AWS::AutoScaling::AutoScalingGroup')
elbs = self._find_resources(template,
'AWS::ElasticLoadBalancing::LoadBalancer')

asgs[0].LoadBalancerNames = [x.LoadBalancerName for x in elbs]
template.resources[asgs[0].title] = asgs[0]
if len(asgs) > 0:
elbs = self._find_resources(template,
'AWS::ElasticLoadBalancing::LoadBalancer')
asgs[0].LoadBalancerNames = [Ref(x) for x in elbs]
template.resources[asgs[0].title] = asgs[0]

return template
2 changes: 1 addition & 1 deletion tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def test_stack_wait_for_stack_not_done(self):
with self.assertRaises(errors.CfnTimeoutError):
print cloudformation.Cloudformation(
self.env.aws_profile).wait_for_stack_done(self.stack_name, 1, 1)

def test_wait_for_stack_done(self):
stack_evt_mock = mock.Mock()
rt = mock.PropertyMock(return_value='AWS::CloudFormation::Stack')
Expand Down
26 changes: 16 additions & 10 deletions tests/test_iam.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,25 @@ class TestIAM(unittest.TestCase):
{
"server_certificate":
{
"certificate_body": ("-----BEGIN CERTIFICATE-----"
"CERT1CERT1CERT1CERT1CERT1CER"
"-----END CERTIFICATE-----"),
"certificate_chain": ("-----BEGIN CERTIFICATE-----"
"CHAIN1CHAIN1CHAIN1CHAIN1CHA"
"-----END CERTIFICATE-----"),
"certificate_key": ("-----BEGIN PRIVATE KEY-----"
"KEY1KEY1KEY1KEY1KEY1KEY1KEY"
"-----END PRIVATE KEY-----"),
}
"certificate_body": (
"-----BEGIN CERTIFICATE-----"
"CERT1CERT1CERT1CERT1CERT1CER"
"-----END CERTIFICATE-----"
),
"certificate_chain": (
"-----BEGIN CERTIFICATE-----"
"CHAIN1CHAIN1CHAIN1CHAIN1CHA"
"-----END CERTIFICATE-----"
),
"certificate_key": (
"-----BEGIN PRIVATE KEY-----"
"KEY1KEY1KEY1KEY1KEY1KEY1KEY"
"-----END PRIVATE KEY-----"
),
}
}
}
}
successful_response = \
{
"status": 200,
Expand Down

0 comments on commit a2ca850

Please sign in to comment.