Skip to content

Commit 1083a58

Browse files
committed
move diff normalize_json to function; fix diff params not displaying
1 parent 40b8afe commit 1083a58

2 files changed

Lines changed: 29 additions & 31 deletions

File tree

stacker/actions/diff.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,27 @@ def diff_parameters(old_params, new_params):
143143
return diff
144144

145145

146+
def normalize_json(template):
147+
"""Normalize our template for diffing.
148+
149+
Args:
150+
template(str): string representing the template
151+
152+
Returns:
153+
list: json representation of the parameters
154+
"""
155+
obj = parse_cloudformation_template(template)
156+
json_str = json.dumps(obj, sort_keys=True, indent=4)
157+
result = []
158+
lines = json_str.split("\n")
159+
for line in lines:
160+
result.append(line + "\n")
161+
return result
162+
163+
146164
def print_stack_changes(stack_name, new_stack, old_stack, new_params,
147165
old_params):
148-
"""Prints out the paramters (if changed) and stack diff"""
166+
"""Prints out the parameters (if changed) and stack diff"""
149167
from_file = "old_%s" % (stack_name,)
150168
to_file = "new_%s" % (stack_name,)
151169
lines = difflib.context_diff(
@@ -156,9 +174,10 @@ def print_stack_changes(stack_name, new_stack, old_stack, new_params,
156174
template_changes = list(lines)
157175
if not template_changes:
158176
print "*** No changes to template ***"
159-
else:
160-
param_diffs = diff_parameters(old_params, new_params)
177+
param_diffs = diff_parameters(old_params, new_params)
178+
if param_diffs:
161179
print format_params_diff(param_diffs)
180+
if template_changes:
162181
print "".join(template_changes)
163182

164183

@@ -174,23 +193,6 @@ class Action(build.Action):
174193
config.
175194
"""
176195

177-
def _normalize_json(self, template):
178-
"""Normalizes our template for diffing
179-
180-
Args:
181-
template(str): string representing the template
182-
183-
Returns:
184-
list: json representation of the parameters
185-
"""
186-
obj = parse_cloudformation_template(template)
187-
json_str = json.dumps(obj, sort_keys=True, indent=4)
188-
result = []
189-
lines = json_str.split("\n")
190-
for line in lines:
191-
result.append(line + "\n")
192-
return result
193-
194196
def _print_new_stack(self, stack, parameters):
195197
"""Prints out the parameters & stack contents of a new stack"""
196198
print "New template parameters:"
@@ -229,7 +231,7 @@ def _diff_stack(self, stack, **kwargs):
229231
for p in parameters:
230232
new_params[p['ParameterKey']] = p['ParameterValue']
231233
new_template = stack.blueprint.rendered
232-
new_stack = self._normalize_json(new_template)
234+
new_stack = normalize_json(new_template)
233235

234236
print "============== Stack: %s ==============" % (stack.name,)
235237
# If this is a completely new template dump our params & stack
@@ -244,7 +246,7 @@ def _diff_stack(self, stack, **kwargs):
244246
# ->
245247
# AWSTemplateFormatVersion: "2010-09-09"
246248
old_template = parse_cloudformation_template(old_template)
247-
old_stack = self._normalize_json(
249+
old_stack = normalize_json(
248250
json.dumps(old_template,
249251
sort_keys=True,
250252
indent=4)

stacker/tests/actions/test_diff.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
from stacker.actions.diff import (
66
diff_dictionaries,
77
diff_parameters,
8-
Action,
8+
normalize_json,
99
DictValue
1010
)
11-
from stacker.context import Config, Context
1211

1312

1413
class TestDictValueFormat(unittest.TestCase):
@@ -86,14 +85,11 @@ def test_diff_parameters_no_changes(self):
8685
self.assertEquals(param_diffs, [])
8786

8887

89-
class TestDiffAction(unittest.TestCase):
90-
"""Test diff Action class."""
88+
class TestDiffFunctions(unittest.TestCase):
89+
"""Test functions in diff."""
9190

9291
def test_normalize_json(self):
93-
"""Ensure _normalize_json parses yaml correctly."""
94-
diff = Action(
95-
context=Context(config=Config({"namespace": "namespace"}))
96-
)
92+
"""Ensure normalize_json parses yaml correctly."""
9793
with open(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), # noqa
9894
'fixtures',
9995
'cfn_template.yaml'), 'r') as yamlfile:
@@ -141,4 +137,4 @@ def test_normalize_json(self):
141137
' }\n',
142138
'}\n'
143139
]
144-
self.assertEquals(normalized_template, diff._normalize_json(template))
140+
self.assertEquals(normalized_template, normalize_json(template))

0 commit comments

Comments
 (0)