This is going to loop over all of the ports used anywhere in the tenant for every router. Again, the number of routers is small, but the number of ports may be very large, so this loop may run many times. It would be more efficient to build a dictionary mapping the port ids of the router_interface ports for the routers to the router id in one loop, and then in another loop update the ports that appear in that mapping. Something like:

  router_ports = {}
  for router in data['routers']:
    router_details = quantumclient.show_router(router['id'])
    for port in router_details['router'].get('ports', []):
      router_ports[port['id']] = router['id']
  for port in data['ports']:
    port['device_id'] = router_ports.get(port['id'], port['device_id'])

That loops over each set of data one time.