Commit 87cc9250 by Simon János

resource group diff method based on flat model keys

parent 9b554083
Pipeline #284 passed with stage
in 32 seconds
......@@ -134,6 +134,12 @@ class ResourceGroup(Resource):
def resources(self):
return list(self.__resources.values())
def diff(self, other):
flat_self = self.flatten()
flat_other = other.flatten()
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}
def flatten(self, fqn=None):
flat = dict()
for resource in self.resources:
......
import copy
import json
from unittest.case import TestCase
......@@ -251,6 +252,7 @@ class ResourceGroupTest(TestCase):
ResourceGroup(id='some_group', resources=[resource2]))
def test_resource_group_flatten(self):
# given
tree_dict = {
'id': 'root',
'type': 'group',
......@@ -284,7 +286,54 @@ class ResourceGroupTest(TestCase):
'root.level-1.instance-3.type': 'instance'
}
# when
tree_resource = Resource(tree_dict)
flat_dict = tree_resource.flatten()
# then
self.assertEqual(expected_flat_dict, flat_dict)
def test_resource_group_diff(self):
# given
tree_dict1 = {
'id': 'root',
'type': 'group',
'resources': [
{
'id': 'instance-1',
'type': 'instance'
},
{
'id': 'level-1',
'type': 'group',
'resources': [
{
'id': 'instance-2',
'type': 'instance'
},
{
'id': 'instance-3',
'type': 'instance'
}
]
}
]
}
group1 = ResourceGroup(tree_dict1)
tree_dict2 = copy.deepcopy(tree_dict1)
del tree_dict2['resources'][0]
group2 = ResourceGroup(tree_dict2)
expected_diff = {
'root.instance-1.id': 'instance-1',
'root.instance-1.type': 'instance',
}
# when
diff1 = group1.diff(group2)
diff2 = group2.diff(group1)
# then
self.assertEqual(diff1, diff2)
self.assertEqual(expected_diff, diff1)
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