I have below dictionary (generated from a report, so the structure can change).
I need to go to the depth of the dictionary, find the id
which in this case is 'id': u'ef3c8cf1-0987-4e56-a6d5-763c42be1f75'
, (there can be more than 1), delete that id and then move to one level up and repeat the same till I get to the top id
which I finally delete. Since there is a dependency, I need to delete the orphan id
first and then move to the top.
Any help is appreciable. If any other file/information is needed, please let me know.
{
'id': u'4c31d813-a989-47dd-b01b-9a27b8db2dfc',
'snapshots':
[
{
'id': u'3ddc7ddd-02ca-4669-a0cb-fb0d56a4a6f5',
'volumes':
[
{
'id': u'5488de90-50dc-4d72-a6aa-c995422fa179',
'snapshots': [],
'snapshot_id': u'3ddc7ddd-02ca-4669-a0cb-fb0d56a4a6f5'
},
{
'id': u'e566645f-4fb3-4778-be67-447a5bdd678d',
'snapshots':
[
{
'id': u'd637f6ea-4a41-448c-874f-ffe624ddc597',
'volumes':
[
{
'id': u'ef3c8cf1-0987-4e56-a6d5-763c42be1f75',
'snapshots': [],
'snapshot_id': u'd637f6ea-4a41-448c-874f-ffe624ddc597'
}
]
}
],
'snapshot_id': u'3ddc7ddd-02ca-4669-a0cb-fb0d56a4a6f5'},
{
'id': u'196483ee-4f21-4d83-8e15-8caea532b2ab',
'snapshots': [],
'snapshot_id': u'3ddc7ddd-02ca-4669-a0cb-fb0d56a4a6f5'
}
]
}
],
'snapshot_id': None
}
Python code
oh=openstack_helper.OpenstackHelper()
def get_objects(item):
items=None
if item == 'stacks':
items=oh.get_stacks()
if item == 'volumes':
items=oh.get_volumes()
if item == 'snapshots':
items=oh.get_snapshots()
return items
def dep_graph(volumes,snapshots,snapshot_id=None):
vol_list=[]
for volume in volumes:
if volume.snapshot_id == snapshot_id:
info={'id':volume.id,'snapshot_id':volume.snapshot_id,'snapshots':[],
}
vol_list.append(info)
for snapshot in snapshots:
for volume in vol_list:
snap_list=[]
if snapshot.volume_id == volume['id']:
info={'id':snapshot.id, 'volumes':[]}
info['volumes'].extend(dep_graph(volumes,snapshots,snapshot.id))
volume['snapshots'].append(info)
return vol_list
if __name__ == '__main__':
volumes = get_objects('volumes')
snapshots = get_objects('snapshots')
output = dep_graph(volumes, snapshots)
print output