import json from dcim.models import Site, Location, Rack, Manufacturer, DeviceType, DeviceRole, Device, Cable, FrontPort, Interface, PowerPanel, PowerFeed from tenancy.models import Tenant from virtualization.models import Cluster, VirtualMachine from ipam.models import VRF, Prefix, VLAN, IPAddress out = {} out['sites'] = [{'name': s.name, 'slug': s.slug, 'status': s.status} for s in Site.objects.all()] out['locations'] = [{'name': l.name, 'slug': l.slug, 'site': l.site.name, 'status': l.status} for l in Location.objects.all()] out['racks'] = [{'name': r.name, 'site': r.site.name, 'loc': r.location.name if r.location else None, 'u_height': r.u_height, 'status': r.status} for r in Rack.objects.all()] out['manufacturers'] = [{'name': m.name, 'slug': m.slug} for m in Manufacturer.objects.all()] out['device_types'] = [{'model': dt.model, 'slug': dt.slug, 'manuf': dt.manufacturer.name, 'u_height': dt.u_height, 'count': getattr(dt, 'device_count', None)} for dt in DeviceType.objects.all()] out['device_roles'] = [{'name': r.name, 'slug': r.slug, 'color': r.color} for r in DeviceRole.objects.all()] out['devices_by_type'] = {} for dt in DeviceType.objects.all(): devs = Device.objects.filter(device_type=dt) out['devices_by_type'][dt.model] = [{'name': d.name, 'rack': d.rack.name if d.rack else None, 'status': d.status} for d in devs][:40] out['cable_types'] = [dict([(f.name, getattr(c, f.name)) for f in Cable._meta.get_fields() if f.name in ('type','label','status','length')]) for c in Cable.objects.all()] out['tenants'] = [{'name': t.name} for t in Tenant.objects.all()] out['vrfs'] = [{'name': v.name} for v in VRF.objects.all()] out['prefixes'] = [{'prefix': str(p.prefix), 'vrf': p.vrf.name if p.vrf else None} for p in Prefix.objects.all()] out['vlans'] = [{'vid': v.vid, 'name': v.name} for v in VLAN.objects.all()] print(json.dumps(out, ensure_ascii=False, indent=1, default=str))