Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/import_cloudformation'
Browse files Browse the repository at this point in the history
  • Loading branch information
jonapich committed Apr 19, 2016
2 parents 89caac9 + ae20dce commit e64c26a
Show file tree
Hide file tree
Showing 6 changed files with 412 additions and 20 deletions.
2 changes: 1 addition & 1 deletion examples/Autoscaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
accessKeyId=Ref(DeployUserAccessKey),
secretKey=Ref(DeployUserSecretKey)
)
})
})
),
UserData=Base64(Join('', [
"#!/bin/bash\n",
Expand Down
67 changes: 67 additions & 0 deletions tests/test_examples_template_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import os
import re
import unittest
import json

from troposphere.template_generator import TemplateGenerator

try:
import StringIO as io
except ImportError:
import io

try:
u = unicode
except NameError:
u = str


class TestTemplateGenerator(unittest.TestCase):
maxDiff = None

# those are set by create_test_class
filename = None
expected_output = None

def test_template_generator(self):
"""
Ensures that all example outputs can be loaded into the
template generator and back to JSON with no difference.
"""
# we first get both outputs as JSON strings
template = self.expected_output
generated = TemplateGenerator(json.loads(template)).to_json()

# then we make them into a dict for comparison
template = json.loads(template)
generated = json.loads(generated)

self.assertDictEqual(template, generated)


def create_test_class(testname, **kwargs):
klass = type(testname, (TestTemplateGenerator,), kwargs)
return klass


def load_tests(loader, tests, pattern):
# Filter out all *.py files from the examples directory
examples = 'examples'
regex = re.compile(r'.py$', re.I)
example_filesnames = filter(regex.search, os.listdir(examples))

suite = unittest.TestSuite()

for f in example_filesnames:
testname = 'test_' + f[:-3]
expected_output = open('tests/examples_output/%s.template' %
f[:-3]).read()
test_class = create_test_class(testname, filename=examples + '/' + f,
expected_output=expected_output)
tests = loader.loadTestsFromTestCase(test_class)
suite.addTests(tests)

return suite

if __name__ == '__main__':
unittest.main()
9 changes: 8 additions & 1 deletion troposphere/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ def getdata(self, data):
else:
return data

class GenericHelperFn(AWSHelperFn):
""" Used as a fallback for the template generator """
def __init__(self, data):
self.data = self.getdata(data)

def JSONrepr(self):
return self.data


class Base64(AWSHelperFn):
def __init__(self, data):
Expand Down Expand Up @@ -336,7 +344,6 @@ def __init__(self, data):
def JSONrepr(self):
return self.data


class Select(AWSHelperFn):
def __init__(self, indx, objects):
self.data = {'Fn::Select': [indx, objects]}
Expand Down
24 changes: 7 additions & 17 deletions troposphere/emr.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,15 @@ def properties_validator(xs):

return xs


def configurations_validator(xs):
if not isinstance(xs, list):
raise ValueError("Configurations must be a list of "
"Configuration objects.")
for x in xs:
if not isinstance(x, Configuration):
raise ValueError("Configuration '%s' must be of "
"Configuration type" % x)
return xs


class Configuration(AWSProperty):
props = {
'Classification': (basestring, False),
'ConfigurationProperties': (properties_validator, False),
'Configurations': (configurations_validator, False)
'ConfigurationProperties': (properties_validator, False)
}

# we must define this one afterwards since Configuration does not exist
# before Configuration is done initializing
Configuration.props['Configurations'] = ([Configuration], False)

def market_validator(x):
valid_values = ['ON_DEMAND', 'SPOT']
Expand All @@ -94,7 +84,7 @@ def market_validator(x):
class InstanceGroupConfigProperty(AWSProperty):
props = {
'BidPrice': (basestring, False),
'Configurations': (configurations_validator, False),
'Configurations': ([Configuration], False),
'InstanceCount': (positive_integer, True),
'InstanceType': (basestring, True),
'Market': (market_validator, False),
Expand Down Expand Up @@ -162,7 +152,7 @@ class Cluster(AWSObject):
'AdditionalInfo': (dict, False),
'Applications': ([Application], False),
'BootstrapActions': ([BootstrapActionConfig], False),
'Configurations': (configurations_validator, False),
'Configurations': ([Configuration], False),
'EbsConfiguration': (EbsConfiguration, False),
'Instances': (JobFlowInstancesConfig, True),
'JobFlowRole': (basestring, True),
Expand All @@ -180,7 +170,7 @@ class InstanceGroupConfig(AWSObject):

props = {
'BidPrice': (basestring, False),
'Configurations': (configurations_validator, False),
'Configurations': ([Configuration], False),
'EbsConfiguration': (EbsConfiguration, False),
'InstanceCount': (integer, True),
'InstanceRole': (basestring, True),
Expand Down
6 changes: 5 additions & 1 deletion troposphere/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@ class Bucket(AWSObject):
LogDeliveryWrite,
]

def __init__(self, name, **kwargs):
def __init__(self, name=None, **kwargs):
if not name and 'title' in kwargs:
name = kwargs.pop('title')
if not name:
raise TypeError("You must provide a bucket name")
super(Bucket, self).__init__(name, **kwargs)

if 'AccessControl' in kwargs:
Expand Down
Loading

0 comments on commit e64c26a

Please sign in to comment.