question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pre-generate the OpenAPI schema

See original GitHub issue

NetBox 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:closed
  • Created 2 years ago
  • Reactions:11
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
candlerbcommented, Jun 2, 2021

There are two ways to bite the cherry.

  1. At the server end: pre-generate the spec and serve it statically
  2. At the client end: read the spec once and cache it

Whilst both are possible, (1) gives the maximum benefit:

  • It only has to be implemented once
  • It benefits all clients, even dumb ones
  • Clients don’t have to worry about running with a stale copy of the spec when Netbox changes
  • TBH, transferring 1MB over the network is cheap these days

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.

1reaction
candlerbcommented, Oct 1, 2021

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 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Demo: Generate an OpenAPI Schema From a ytt Data Values ...
Carvel maintainer, Cari, demos how to generate an OpenAPI Schema from a ytt data values schema (and put it in your kapp-controller ...
Read more >
How To Generate Angular & Spring Code From OpenAPI ...
The OpenAPI code generator needs a yaml schema definition file which includes all relevant information about the API code that should be generated....
Read more >
Documenting RESTful APIs using MicroProfile OpenAPI
Add OpenAPI @Schema annotations to the InventoryList class and the systems ... you can provide a valid pregenerated OpenAPI document to describe your...
Read more >
Usage - OpenAPI Generator
Options for OpenAPI Generator are the same whether you're using the CLI, Maven/Gradle Plugins, or Online generation options.
Read more >
Apigentools Docs
spec/v1/shared.yaml - A file containing any OpenAPI objects that are referenced from more than one "section"— components (which includes schemas and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found