Skip to content

Instantly share code, notes, and snippets.

@hughsaunders
Last active December 25, 2015 21:09
Show Gist options
  • Save hughsaunders/7040823 to your computer and use it in GitHub Desktop.
Save hughsaunders/7040823 to your computer and use it in GitHub Desktop.
import json
buckets='[{"hash": "rjenkins1", "name": "default", "weight": 11796, "type_id": 6, "alg": "straw", "type_name": "root", "items": [{"id": -2, "weight": 3932, "pos": 0}, {"id": -3, "weight": 3932, "pos": 1}, {"id": -4, "weight": 3932, "pos": 2}], "id": -1}, {"hash": "rjenkins1", "name": "ceph-server1", "weight": 3932, "type_id": 1, "alg": "straw", "type_name": "host", "items": [{"id": 0, "weight": 3932, "pos": 0}], "id": -2}, {"hash": "rjenkins1", "name": "ceph-server2", "weight": 3932, "type_id": 1, "alg": "straw", "type_name": "host", "items": [{"id": 1, "weight": 3932, "pos": 0}], "id": -3}, {"hash": "rjenkins1", "name": "ceph-server3", "weight": 3932, "type_id": 1, "alg": "straw", "type_name": "host", "items": [{"id": 2, "weight": 3932, "pos": 0}], "id": -4}]'
devices='[{"id": 0, "name": "osd.0"}, {"id": 1, "name": "osd.1"}, {"id": 2, "name": "osd.2"}]'
# Read json and combine into single list
combined=json.loads(buckets)+json.loads(devices)
# Create dictionary indexed by id
items=dict(zip([x['id'] for x in combined],combined))
def print_recursive(node,path):
#get 'full' node object from parent reference
node = items[node['id']]
if 'type_name' not in node:
node['type_name']= 'osd'
print "%s%s %s" %(" "*len(path)*3, node['type_name'], node['name'])
if 'items' in node:
for child in node['items']:
print_recursive(child,path+[node])
path=[]
roots = [x for x in combined if 'type_name' in x and x['type_name'] == 'root']
for root in roots:
print_recursive(root,path)
$ python /private/tmp/tree.py
root default
host ceph-server1
osd osd.0
host ceph-server2
osd osd.1
host ceph-server3
osd osd.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment