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

Commit

Permalink
Merge b731278 into e8614fd
Browse files Browse the repository at this point in the history
  • Loading branch information
niallcreech committed Jul 23, 2015
2 parents e8614fd + b731278 commit 9ab6a41
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
45 changes: 29 additions & 16 deletions bootstrap_cfn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import logging
import os
import sys

import textwrap
import uuid

from troposphere import Base64, FindInMap, GetAZs, GetAtt, Join, Output, Ref, Tags, Template
from troposphere.autoscaling import AutoScalingGroup, BlockDeviceMapping, \
Expand Down Expand Up @@ -74,18 +74,7 @@ def process(self):
template = self._attach_elbs(template)

if 'rds' in self.data:
rds = self.rds()
map(template.add_resource, rds)
template.add_output(Output(
"dbhost",
Description="RDS Hostname",
Value=GetAtt("RDSInstance", "Endpoint.Address")
))
template.add_output(Output(
"dbport",
Description="RDS Port",
Value=GetAtt("RDSInstance", "Endpoint.Port")
))
self.rds(template)

if 's3' in self.data:
self.s3(template)
Expand Down Expand Up @@ -320,7 +309,16 @@ def s3(self, template):
def ssl(self):
return self.data['ssl']

def rds(self):
def rds(self, template):
"""
Create an RDS resource configuration from the config file data
and add it to the troposphere template. Outputs for the RDS name,
host and port are created.
Args:
template:
The cloudformation template file
"""
# REQUIRED FIELDS MAPPING
required_fields = {
'db-name': 'DBName',
Expand All @@ -329,7 +327,6 @@ def rds(self):
'backup-retention-period': 'BackupRetentionPeriod',
'db-master-username': 'MasterUsername',
'db-master-password': 'MasterUserPassword',
'identifier': 'DBInstanceIdentifier',
'db-engine': 'Engine',
'db-engine-version': 'EngineVersion',
'instance-class': 'DBInstanceClass',
Expand Down Expand Up @@ -366,13 +363,18 @@ def rds(self):
)
resources.append(database_sg)

identifier = ("RDS%s%s"
% ((self.stack_name).replace('-', '').replace('.', '').replace('_', ''),
uuid.uuid4().__str__()[-8:]))

rds_instance = DBInstance(
"RDSInstance",
PubliclyAccessible=False,
AllowMajorVersionUpgrade=False,
AutoMinorVersionUpgrade=False,
VPCSecurityGroups=[GetAtt(database_sg, "GroupId")],
DBSubnetGroupName=Ref(rds_subnet_group),
DBInstanceIdentifier=identifier,
StorageEncrypted=False,
DependsOn=database_sg.title
)
Expand All @@ -390,7 +392,18 @@ def rds(self):
if yaml_key in self.data['rds']:
rds_instance.__setattr__(rds_prop, self.data['rds'][yaml_key])

return resources
# Add resources and outputs
map(template.add_resource, resources)
template.add_output(Output(
"dbhost",
Description="RDS Hostname",
Value=GetAtt(rds_instance, "Endpoint.Address")
))
template.add_output(Output(
"dbport",
Description="RDS Port",
Value=GetAtt(rds_instance, "Endpoint.Port")
))

def elb(self):
# REQUIRED FIELDS AND MAPPING
Expand Down
1 change: 0 additions & 1 deletion docs/sample-project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ dev:
storage: 5
storage-type: gp2
backup-retention-period: 1
identifier: test-dev
db-name: test
db-master-username: testuser
instance-class: db.t2.micro
Expand Down
1 change: 0 additions & 1 deletion tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def setUp(self):
'db-master-password': 'testpassword',
'db-master-username': 'testuser',
'db-name': 'test',
'identifier': 'test-dev',
'instance-class': 'db.t2.micro',
'multi-az': False,
'storage': 5,
Expand Down
36 changes: 32 additions & 4 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ def test_rds(self):
db_instance.AutoMinorVersionUpgrade = False
db_instance.BackupRetentionPeriod = 1
db_instance.DBInstanceClass = 'db.t2.micro'
db_instance.DBInstanceIdentifier = 'test-dev'
db_instance.Engine = 'postgres'
db_instance.EngineVersion = '9.3.5'
db_instance.VPCSecurityGroups = [GetAtt(db_sg, 'GroupId')]
Expand All @@ -293,10 +292,39 @@ def test_rds(self):
'tests/sample-project.yaml',
'dev',
'tests/sample-project-passwords.yaml').config, 'my-stack-name')
rds_dict = self._resources_to_dict(config.rds())

template = Template()
config.rds(template)
resources = template.resources.values()
rds_dict = self._resources_to_dict(resources)
# RDS dict will contain DBIdentifier, which is random.
# So we check it seperately here then remove it
self.assertTrue("DBInstanceIdentifier" in rds_dict["RDSInstance"]["Properties"],
"test_rds: template does not contain DBInstanceIdentifier")
identifier = rds_dict["RDSInstance"]["Properties"]["DBInstanceIdentifier"]
# identifier starts RDS and contains an 8 char uuid plus stack name
# so this should always be true
self.assertTrue(len(identifier) >= 12)
rds_dict["RDSInstance"]["Properties"].pop("DBInstanceIdentifier")
known = self._resources_to_dict(known)
compare(known, rds_dict)

# Test for outputs
expected_outputs = {
"dbhost": {
"Description": "RDS Hostname",
"Value": {"Fn::GetAtt": ["RDSInstance", "Endpoint.Address"]}
},
"dbport": {
"Description": "RDS Port",
"Value": {
"Fn::GetAtt": ["RDSInstance", "Endpoint.Port"]
}
}
}
actual_outputs = self._resources_to_dict(template.outputs.values())
compare(expected_outputs, actual_outputs)

def test_elb(self):
known = []
lb = LoadBalancer(
Expand Down Expand Up @@ -593,7 +621,7 @@ def test_process(self):
wanted = ["StaticBucketName", "dbhost", "dbport"]
output_names = cfn_template['Outputs'].keys()
output_names.sort()
compare(output_names, wanted)
compare(wanted, output_names)

mappings = cfn_template['Mappings']
expected = {
Expand Down Expand Up @@ -649,7 +677,7 @@ def test_process_with_vpc_config(self):
wanted = ["StaticBucketName", "dbhost", "dbport"]
output_names = cfn_template['Outputs'].keys()
output_names.sort()
compare(output_names, wanted)
compare(wanted, output_names)

mappings = cfn_template['Mappings']
expected = {
Expand Down

0 comments on commit 9ab6a41

Please sign in to comment.