Skip to content

Commit

Permalink
allow raw cfn templates to be loaded from remote package_sources (#638)
Browse files Browse the repository at this point in the history
* allow raw cfn templates to be loaded from remote package_sources

Fixes #628

* add test for new get_template_path function
  • Loading branch information
troyready authored and phobologic committed Aug 19, 2018
1 parent dd92170 commit e563352
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
6 changes: 5 additions & 1 deletion docs/config.rst
Expand Up @@ -340,7 +340,11 @@ A stack has the following keys:
``template_path`` for the stack.
**template_path:**
Path to raw CloudFormation template (JSON or YAML). Specify this or
``class_path`` for the stack.
``class_path`` for the stack. Path can be specified relative to the current
working directory (e.g. templates stored alongside the Config), or relative
to a directory in the python ``sys.path`` (i.e. for loading templates
retrieved via ``packages_sources``).

**description:**
A short description to apply to the stack. This overwrites any description
provided in the Blueprint. See: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-description-structure.html
Expand Down
37 changes: 34 additions & 3 deletions stacker/blueprints/raw.py
Expand Up @@ -5,12 +5,36 @@

import hashlib
import json
import os
import sys

from ..util import parse_cloudformation_template
from ..exceptions import UnresolvedVariable
from ..exceptions import InvalidConfig, UnresolvedVariable
from .base import Blueprint


def get_template_path(filename):
"""Find raw template in working directory or in sys.path.
template_path from config may refer to templates colocated with the Stacker
config, or files in remote package_sources. Here, we emulate python module
loading to find the path to the template.
Args:
filename (str): Template filename.
Returns:
Optional[str]: Path to file, or None if no file found
"""
if os.path.isfile(filename):
return filename
for i in sys.path:
if os.path.isfile(os.path.join(i, filename)):
return os.path.join(i, filename)
return None


def get_template_params(template):
"""Parse a CFN template for defined parameters.
Expand Down Expand Up @@ -159,8 +183,15 @@ def requires_change_set(self):
def rendered(self):
"""Return (generating first if needed) rendered template."""
if not self._rendered:
with open(self.raw_template_path, 'r') as template:
self._rendered = template.read()
template_path = get_template_path(self.raw_template_path)
if template_path:
with open(template_path, 'r') as template:
self._rendered = template.read()
else:
raise InvalidConfig(
'Could not find template %s' % self.raw_template_path
)

return self._rendered

@property
Expand Down
32 changes: 31 additions & 1 deletion stacker/tests/blueprints/test_raw.py
Expand Up @@ -3,11 +3,15 @@
from __future__ import division
from __future__ import absolute_import
import json
import os
import sys
import unittest

from mock import MagicMock

from stacker.blueprints.raw import get_template_params, RawTemplateBlueprint
from stacker.blueprints.raw import (
get_template_params, get_template_path, RawTemplateBlueprint
)
from ..factories import mock_context

RAW_JSON_TEMPLATE_PATH = 'stacker/tests/fixtures/cfn_template.json'
Expand All @@ -17,6 +21,32 @@
class TestRawBluePrintHelpers(unittest.TestCase):
"""Test class for functions in module."""

def test_get_template_path_local_file(self): # noqa pylint: disable=invalid-name
"""Verify get_template_path finding a file relative to CWD."""
self.assertEqual(get_template_path(RAW_YAML_TEMPLATE_PATH),
RAW_YAML_TEMPLATE_PATH)

def test_get_template_path_invalid_file(self): # noqa pylint: disable=invalid-name
"""Verify get_template_path with an invalid filename."""
self.assertEqual(get_template_path('afilenamethatdoesnotexist.txt'),
None)

def test_get_template_path_file_in_syspath(self): # noqa pylint: disable=invalid-name
"""Verify get_template_path with a file in sys.path.
This ensures templates are able to be retreived from remote packages.
"""
stacker_tests_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) # noqa
old_sys_path = list(sys.path)
sys.path.append(stacker_tests_dir)
try:
self.assertEqual(get_template_path('fixtures/cfn_template.yaml'),
os.path.join(stacker_tests_dir,
'fixtures/cfn_template.yaml'))
finally:
sys.path = old_sys_path

def test_get_template_params(self):
"""Verify get_template_params function operation."""
template_dict = {
Expand Down

0 comments on commit e563352

Please sign in to comment.