Skip to content

Commit a4b95b8

Browse files
committed
add wrapper method for yaml_parse
1 parent 25cbce6 commit a4b95b8

4 files changed

Lines changed: 57 additions & 5 deletions

File tree

stacker/actions/diff.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .base import plan, build_walker
88
from . import build
99
from .. import exceptions
10-
from ..awscli_yamlhelper import yaml_parse
10+
from ..util import parse_cloudformation_template
1111
from ..status import NotSubmittedStatus, NotUpdatedStatus, COMPLETE
1212

1313
logger = logging.getLogger(__name__)
@@ -227,13 +227,13 @@ def _diff_stack(self, stack, **kwargs):
227227
self._print_new_stack(new_stack, parameters)
228228
else:
229229
# Diff our old & new stack/parameters
230-
old_template = yaml_parse(old_template)
230+
old_template = parse_cloudformation_template(old_template)
231231
if isinstance(old_template, (str, unicode)):
232232
# YAML templates returned from CFN need parsing again
233233
# "AWSTemplateFormatVersion: \"2010-09-09\"\nParam..."
234234
# ->
235235
# AWSTemplateFormatVersion: "2010-09-09"
236-
old_template = yaml_parse(old_template)
236+
old_template = parse_cloudformation_template(old_template)
237237
old_stack = self._normalize_json(
238238
json.dumps(old_template,
239239
sort_keys=True,

stacker/blueprints/raw.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import hashlib
44
import json
55

6-
from ..awscli_yamlhelper import yaml_parse
6+
from ..util import parse_cloudformation_template
77
from ..exceptions import MissingVariable, UnresolvedVariable
88

99

@@ -100,7 +100,7 @@ def to_dict(self):
100100
dict: the loaded template as a python dictionary
101101
102102
"""
103-
return yaml_parse(self.rendered)
103+
return parse_cloudformation_template(self.rendered)
104104

105105
def render_template(self):
106106
"""Load template and generate its md5 hash."""

stacker/tests/test_util.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
get_client_region,
2020
get_s3_endpoint,
2121
s3_bucket_location_constraint,
22+
parse_cloudformation_template,
2223
Extractor,
2324
TarExtractor,
2425
TarGzipExtractor,
@@ -144,6 +145,45 @@ def test_s3_bucket_location_constraint(self):
144145
result
145146
)
146147

148+
def test_parse_cloudformation_template(self):
149+
template = """AWSTemplateFormatVersion: "2010-09-09"
150+
Parameters:
151+
Param1:
152+
Type: String
153+
Resources:
154+
Bucket:
155+
Type: AWS::S3::Bucket
156+
Properties:
157+
BucketName:
158+
!Join
159+
- "-"
160+
- - !Ref "AWS::StackName"
161+
- !Ref "AWS::Region"
162+
Outputs:
163+
DummyId:
164+
Value: dummy-1234"""
165+
parsed_template = {
166+
'AWSTemplateFormatVersion': '2010-09-09',
167+
'Outputs': {'DummyId': {'Value': 'dummy-1234'}},
168+
'Parameters': {'Param1': {'Type': 'String'}},
169+
'Resources': {
170+
'Bucket': {'Type': 'AWS::S3::Bucket',
171+
'Properties': {
172+
'BucketName': {
173+
u'Fn::Join': [
174+
'-',
175+
[{u'Ref': u'AWS::StackName'},
176+
{u'Ref': u'AWS::Region'}]
177+
]
178+
}
179+
}}
180+
}
181+
}
182+
self.assertEqual(
183+
parse_cloudformation_template(template),
184+
parsed_template
185+
)
186+
147187
def test_extractors(self):
148188
self.assertEqual(Extractor('test.zip').archive, 'test.zip')
149189
self.assertEqual(TarExtractor().extension(), '.tar')

stacker/util.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from yaml.constructor import ConstructorError
2323
from yaml.nodes import MappingNode
2424

25+
from .awscli_yamlhelper import yaml_parse
2526
from stacker.session_cache import get_session
2627

2728
logger = logging.getLogger(__name__)
@@ -497,6 +498,17 @@ def ensure_s3_bucket(s3_client, bucket_name, bucket_region):
497498
raise
498499

499500

501+
def parse_cloudformation_template(template):
502+
"""Parse CFN template string.
503+
504+
Leverages the vendored aws-cli yamlhelper to handle JSON or YAML templates.
505+
506+
Args:
507+
template (str): The template body.
508+
"""
509+
return yaml_parse(template)
510+
511+
500512
class Extractor(object):
501513
"""Base class for extractors."""
502514

0 commit comments

Comments
 (0)