Commit c70a8f28 by Simon János

flat_leaves switch on flatten; checking resource equality using flat resource model

parent ce7daa24
Pipeline #288 passed with stage
in 31 seconds
...@@ -64,11 +64,7 @@ class Resource(object): ...@@ -64,11 +64,7 @@ class Resource(object):
def __eq__(self, other): def __eq__(self, other):
try: try:
for attr, other_value in other.items(): return self.flatten(flat_leaves=True) == other.flatten(flat_leaves=True)
value = self._attributes.get(attr)
if value is None or value != other_value:
return False
return True
except AttributeError: except AttributeError:
return False return False
...@@ -93,9 +89,11 @@ class Resource(object): ...@@ -93,9 +89,11 @@ class Resource(object):
def __add__(self, other): def __add__(self, other):
return ResourceGroup(resources=[self, other]) return ResourceGroup(resources=[self, other])
def flatten(self, fqn=None): def flatten(self, fqn=None, flat_leaves=True):
fqn = '%s.%s' % (fqn, self.id) if fqn else self.id fqn = '%s.%s' % (fqn, self.id) if fqn else self.id
return {'%s.%s' % (fqn, key): str(value) for key, value in self.items()} if flat_leaves:
return {'%s.%s' % (fqn, key): str(value) for key, value in self.items()}
return {fqn: self}
@staticmethod @staticmethod
def json_encoder(data): def json_encoder(data):
...@@ -116,6 +114,12 @@ class ResourceGroup(Resource): ...@@ -116,6 +114,12 @@ class ResourceGroup(Resource):
self.add(resource) self.add(resource)
self._attributes['resources'] = self.resources self._attributes['resources'] = self.resources
def __eq__(self, other):
try:
return self.id == other.id and super(ResourceGroup, self).__eq__(other)
except AttributeError:
return False
def add(self, resource): def add(self, resource):
if not isinstance(resource, Resource): if not isinstance(resource, Resource):
try: try:
...@@ -143,10 +147,10 @@ class ResourceGroup(Resource): ...@@ -143,10 +147,10 @@ class ResourceGroup(Resource):
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}
def flatten(self, fqn=None): def flatten(self, fqn=None, flat_leaves=True):
flat = dict() flat = dict()
for resource in self.resources: for resource in self.resources:
flat.update(resource.flatten('%s.%s' % (fqn, self.id) if fqn else self.id)) flat.update(resource.flatten('%s.%s' % (fqn, self.id) if fqn else self.id, flat_leaves))
return flat return flat
......
...@@ -276,32 +276,33 @@ class ResourceGroupTest(TestCase): ...@@ -276,32 +276,33 @@ class ResourceGroupTest(TestCase):
self.assertNotEqual(ResourceGroup(id='some_group', resources=[resource1]), self.assertNotEqual(ResourceGroup(id='some_group', resources=[resource1]),
ResourceGroup(id='some_group', resources=[resource2])) ResourceGroup(id='some_group', resources=[resource2]))
def test_resource_group_flatten(self): TEST_TREE_DICT = {
'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'
}
]
}
]
}
def test_resource_group_flatten_flat_leaves(self):
# given # given
tree_dict = {
'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'
}
]
}
]
}
expected_flat_dict = { expected_flat_dict = {
'root.instance-1.id': 'instance-1', 'root.instance-1.id': 'instance-1',
'root.instance-1.type': 'instance', 'root.instance-1.type': 'instance',
...@@ -312,41 +313,32 @@ class ResourceGroupTest(TestCase): ...@@ -312,41 +313,32 @@ class ResourceGroupTest(TestCase):
} }
# when # when
tree_resource = Resource(tree_dict) tree_resource = Resource(self.TEST_TREE_DICT)
flat_dict = tree_resource.flatten() flat_dict = tree_resource.flatten()
# 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_flatten_not_flat_leaves(self):
# given # given
tree_dict1 = { expected_flat_dict = {
'id': 'root', 'root.instance-1': Instance(id='instance-1'),
'type': 'group', 'root.level-1.instance-2': Instance(id='instance-2'),
'resources': [ 'root.level-1.instance-3': Instance(id='instance-3')
{
'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) # when
tree_resource = Resource(self.TEST_TREE_DICT)
flat_dict = tree_resource.flatten(flat_leaves=False)
# then
self.assertEqual(expected_flat_dict, flat_dict)
def test_resource_group_diff(self):
# given
group1 = ResourceGroup(self.TEST_TREE_DICT)
tree_dict2 = copy.deepcopy(self.TEST_TREE_DICT)
del tree_dict2['resources'][0] del tree_dict2['resources'][0]
group2 = ResourceGroup(tree_dict2) group2 = ResourceGroup(tree_dict2)
......
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