def test_callable_generate_no_template(self, error): # Make a pyplate with no CloudFormationTemplate in it pyplate_contents = '' pyplate = NamedTemporaryFile() pyplate.write(pyplate_contents) pyplate.flush() error.return_value = Exception('I am a test mock, this traceback is expected.') core.generate_pyplate(pyplate.name) error.assert_called_once_with('No CloudFormationTemplate found in pyplate')
def test_callable_generate(self): # Make a pyplate that uses the options mapping pyplate_contents = dedent(u'''\ cft = CloudFormationTemplate('This is a test') cft.parameters.update({ 'Exists': options['ThisKeyExists'] })''') pyplate = NamedTemporaryFile() pyplate.write(pyplate_contents) pyplate.flush() # call generate directly with an options mapping, no CLI output = json.loads(core.generate_pyplate(pyplate.name, {'ThisKeyExists': True})) self.assertTrue(output['Parameters']['Exists'])
def test_callable_generate(self): # Make a pyplate that uses the options mapping pyplate_contents = dedent(u'''\ cft = CloudFormationTemplate('This is a test') cft.parameters.update({ 'Exists': options['ThisKeyExists'] })''') pyplate = NamedTemporaryFile() pyplate.write(pyplate_contents) pyplate.flush() # call generate directly with an options mapping, no CLI output = json.loads( core.generate_pyplate(pyplate.name, {'ThisKeyExists': True})) self.assertTrue(output['Parameters']['Exists'])
def load_template(self, stack_name, template_path): stack_config = self.config['stacks'][stack_name] log.debug("converting template %r to JSON", template_path) # see https://github.com/seandst/cfn-pyplates/issues/27 for the # solution to using these private functions sys.path.insert(0, os.path.dirname(template_path)) options_mapping = OptionsMapping(stack_config.get('options', {})) # add external resources to options self.load_external_resources(options_mapping, stack_name) # make 'options' available for import, so that flake8 can stay # happy when parsing the templates cfn_pyplates.core.options = options_mapping # load the template and convert the result to JSON return generate_pyplate(template_path, options_mapping)
# This isn't included in cfn_pyplates like most pyplate components; # It must be imported explicitly from cfn_pyplates.core import generate_pyplate # Given the "project.py" example above, generate the pyplate # directly in python generate_pyplate('/path/to/project.py') # In addition, if you already have a reference to a # CloudFormationTemplate, generating its JSON template is as easy as # casting it as a string (or unicode) object: print str(my_cloud_formation_template_instance)
def generate(): """Generate CloudFormation JSON Template based on a Pyplate Usage: cfn_py_generate <pyplate> [<outfile>] [-o/--options=<options_mapping>] cfn_py_generate (-h|--help) cfn_py_generate --version Arguments: pyplate Input pyplate file name outfile File in which to place the compiled JSON template (if omitted or '-', outputs to stdout) Options: -o --options=<options_mapping> Input JSON or YAML file for options mapping exposed in the pyplate as "options_mapping" (if '-', accepts input from stdin) -h --help This usage information WARNING! Do not use pyplates that you haven't personally examined! A pyplate is a crazy hybrid of JSON-looking python. exec is used to read the pyplate, so any code in there is going to run, even potentailly harmful things. Be careful. """ from pkg_resources import require version = require("cfn-pyplates")[0].version args = docopt(generate.__doc__, version=version) scheme = Schema({ '<pyplate>': Use(open), '<outfile>': Or(None, Use(_open_outfile)), '--options': Or(None, Use(_open_optionfile)), '--help': Or(True, False), '--version': Or(True, False), }) args = scheme.validate(args) options_file = args['--options'] if options_file: options = yaml.load(options_file) else: options = {} output = core.generate_pyplate(args['<pyplate>'], OptionsMapping(options)) if not output: return 1 if not args['<outfile>'] or args['<outfile>'] == '-': print output else: args['<outfile>'].write(output) # Explicitly return a posixy "EVERYTHING IS OKAY" 0 return 0