Pre-generate the OpenAPI schema
See original GitHub issueNetBox version
v2.11.3
Feature type
Change to existing functionality
Proposed functionality
A feature like “collectstatic” which builds the openapi schema once in ./upgrade.sh
, then serves it as a static file.
Currently it’s served dynamically and is very slow, taking consistently 7-8 seconds on my system:
# time curl -sS localhost:8001/api/docs/?format=openapi | wc
0 78785 799797
real 0m7.805s
user 0m0.016s
sys 0m0.005s
Alternatively, cache it in redis (it will take nearly a megabyte of RAM, but worth it)
Use case
ansible netbox inventory
The ansible netbox inventory fetches the whole openapi schema each time it runs. When combined with the actual data queries it does for devices, vms etc, this gives around a 10-second lead time to any playbook which is hitting the netbox API.
Whilst this inventory module has a feature to cache the results of the inventory (if you configure a suitable cache plugin), I want to get the “real-time” up-to-the-minute data from Netbox. The plugin can’t be configured to cache the openapi schema only, whilst still making data queries to Netbox.
pynetbox
Any code which uses pynetbox, and calls nb.openapi()
, will also benefit.
import pynetbox
nb = pynetbox.api(API_URL, token=API_TOKEN)
nb.openapi()
Database changes
None
External dependencies
None
Issue Analytics
- State:
- Created 2 years ago
- Reactions:11
- Comments:10 (9 by maintainers)
Top GitHub Comments
There are two ways to bite the cherry.
Whilst both are possible, (1) gives the maximum benefit:
Ideally, Netbox would pregenerate the spec and give an ETag - then smart clients can check and don’t have to refetch unless it has changed.
Given that the solution turns out to be just a case of adding
cache_timeout=86400
, that seems like a very reasonable cost/benefit tradeoff 😃