Commit 4aa2bf9e by Simon János

flat_leaves option on ResourceGroup.diff()

parent c70a8f28
Pipeline #289 passed with stage
in 33 seconds
...@@ -141,9 +141,9 @@ class ResourceGroup(Resource): ...@@ -141,9 +141,9 @@ class ResourceGroup(Resource):
def resources(self): def resources(self):
return list(self.__resources.values()) return list(self.__resources.values())
def diff(self, other): def diff(self, other, flat_leaves=True):
flat_self = self.flatten() flat_self = self.flatten(flat_leaves=flat_leaves)
flat_other = other.flatten() 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} return {key: flat_self.get(key, flat_other.get(key)) for key in diff_keys}
......
...@@ -334,7 +334,7 @@ class ResourceGroupTest(TestCase): ...@@ -334,7 +334,7 @@ class ResourceGroupTest(TestCase):
# then # then
self.assertEqual(expected_flat_dict, flat_dict) self.assertEqual(expected_flat_dict, flat_dict)
def test_resource_group_diff(self): def test_resource_group_diff_flat_leaves(self):
# given # given
group1 = ResourceGroup(self.TEST_TREE_DICT) group1 = ResourceGroup(self.TEST_TREE_DICT)
...@@ -348,8 +348,29 @@ class ResourceGroupTest(TestCase): ...@@ -348,8 +348,29 @@ class ResourceGroupTest(TestCase):
} }
# when # when
diff1 = group1.diff(group2) diff1 = group1.diff(group2, flat_leaves=True)
diff2 = group2.diff(group1) diff2 = group2.diff(group1, flat_leaves=True)
# then
self.assertEqual(diff1, diff2)
self.assertEqual(expected_diff, diff1)
def test_resource_group_diff_not_flat_leaves(self):
# given
group1 = ResourceGroup(self.TEST_TREE_DICT)
tree_dict2 = copy.deepcopy(self.TEST_TREE_DICT)
removed_resource = tree_dict2['resources'][0]
del tree_dict2['resources'][0]
group2 = ResourceGroup(tree_dict2)
expected_diff = {
'root.instance-1': Instance(id=removed_resource['id'])
}
# when
diff1 = group1.diff(group2, flat_leaves=False)
diff2 = group2.diff(group1, flat_leaves=False)
# then # then
self.assertEqual(diff1, diff2) self.assertEqual(diff1, 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