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):
def __eq__(self, other):
try:
for attr, other_value in other.items():
value = self._attributes.get(attr)
if value is None or value != other_value:
return False
return True
return self.flatten(flat_leaves=True) == other.flatten(flat_leaves=True)
except AttributeError:
return False
......@@ -93,9 +89,11 @@ class Resource(object):
def __add__(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
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
def json_encoder(data):
......@@ -116,6 +114,12 @@ class ResourceGroup(Resource):
self.add(resource)
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):
if not isinstance(resource, Resource):
try:
......@@ -143,10 +147,10 @@ class ResourceGroup(Resource):
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):
def flatten(self, fqn=None, flat_leaves=True):
flat = dict()
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
......
......@@ -276,32 +276,33 @@ class ResourceGroupTest(TestCase):
self.assertNotEqual(ResourceGroup(id='some_group', resources=[resource1]),
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
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 = {
'root.instance-1.id': 'instance-1',
'root.instance-1.type': 'instance',
......@@ -312,41 +313,32 @@ class ResourceGroupTest(TestCase):
}
# when
tree_resource = Resource(tree_dict)
tree_resource = Resource(self.TEST_TREE_DICT)
flat_dict = tree_resource.flatten()
# then
self.assertEqual(expected_flat_dict, flat_dict)
def test_resource_group_diff(self):
def test_resource_group_flatten_not_flat_leaves(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'
}
]
}
]
expected_flat_dict = {
'root.instance-1': Instance(id='instance-1'),
'root.level-1.instance-2': Instance(id='instance-2'),
'root.level-1.instance-3': Instance(id='instance-3')
}
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]
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