Commit b927b8e6 by Simon János

diff returns added and removed resources separately

parent 4aa2bf9e
Pipeline #290 passed with stage
in 33 seconds
...@@ -142,10 +142,16 @@ class ResourceGroup(Resource): ...@@ -142,10 +142,16 @@ class ResourceGroup(Resource):
return list(self.__resources.values()) return list(self.__resources.values())
def diff(self, other, flat_leaves=True): def diff(self, other, flat_leaves=True):
diff = {'added': {}, 'removed': {}}
flat_self = self.flatten(flat_leaves=flat_leaves) flat_self = self.flatten(flat_leaves=flat_leaves)
flat_other = other.flatten(flat_leaves=flat_leaves) flat_other = other.flatten(flat_leaves=flat_leaves)
diff_keys = set(flat_self.keys()).symmetric_difference(flat_other.keys()) diff_keys = set(flat_self.keys()).symmetric_difference(flat_other.keys())
return {key: flat_self.get(key, flat_other.get(key)) for key in diff_keys} for key in diff_keys:
if key in flat_self.keys():
diff['removed'][key] = flat_self[key]
if key in flat_other.keys():
diff['added'][key] = flat_other[key]
return diff
def flatten(self, fqn=None, flat_leaves=True): def flatten(self, fqn=None, flat_leaves=True):
flat = dict() flat = dict()
......
...@@ -342,18 +342,27 @@ class ResourceGroupTest(TestCase): ...@@ -342,18 +342,27 @@ class ResourceGroupTest(TestCase):
del tree_dict2['resources'][0] del tree_dict2['resources'][0]
group2 = ResourceGroup(tree_dict2) group2 = ResourceGroup(tree_dict2)
expected_diff = { expected_diff_values = {
'root.instance-1.id': 'instance-1', 'root.instance-1.id': 'instance-1',
'root.instance-1.type': 'instance', 'root.instance-1.type': 'instance',
} }
expected_diff_added = {
'added': expected_diff_values,
'removed': {}
}
expected_diff_removed = {
'added': {},
'removed': expected_diff_values
}
# when # when
diff1 = group1.diff(group2, flat_leaves=True) diff1 = group1.diff(group2, flat_leaves=True)
diff2 = group2.diff(group1, flat_leaves=True) diff2 = group2.diff(group1, flat_leaves=True)
# then # then
self.assertEqual(diff1, diff2) self.assertNotEqual(diff1, diff2)
self.assertEqual(expected_diff, diff1) self.assertEqual(expected_diff_removed, diff1)
self.assertEqual(expected_diff_added, diff2)
def test_resource_group_diff_not_flat_leaves(self): def test_resource_group_diff_not_flat_leaves(self):
# given # given
...@@ -364,14 +373,23 @@ class ResourceGroupTest(TestCase): ...@@ -364,14 +373,23 @@ class ResourceGroupTest(TestCase):
del tree_dict2['resources'][0] del tree_dict2['resources'][0]
group2 = ResourceGroup(tree_dict2) group2 = ResourceGroup(tree_dict2)
expected_diff = { expected_diff_values = {
'root.instance-1': Instance(id=removed_resource['id']) 'root.instance-1': Instance(id=removed_resource['id'])
} }
expected_diff_added = {
'added': expected_diff_values,
'removed': {}
}
expected_diff_removed = {
'added': {},
'removed': expected_diff_values
}
# when # when
diff1 = group1.diff(group2, flat_leaves=False) diff1 = group1.diff(group2, flat_leaves=False)
diff2 = group2.diff(group1, flat_leaves=False) diff2 = group2.diff(group1, flat_leaves=False)
# then # then
self.assertEqual(diff1, diff2) self.assertNotEqual(diff1, diff2)
self.assertEqual(expected_diff, diff1) self.assertEqual(expected_diff_removed, diff1)
self.assertEqual(expected_diff_added, diff2)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment