Skip to content

Commit c159a76

Browse files
authored
689 Update documentation and add add release checklist (#691)
1 parent 0f244d9 commit c159a76

33 files changed

+1269
-1149
lines changed

.github/workflows/build-mkdocs.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Build MkDocs
2+
on:
3+
push:
4+
branches:
5+
- master
6+
- main
7+
permissions:
8+
contents: write
9+
jobs:
10+
deploy:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: actions/setup-python@v4
15+
with:
16+
python-version: 3.x
17+
- run: pip install mkdocs-material mkdocs-autorefs mkdocs-material-extensions mkdocstrings mkdocstrings-python-legacy mkdocs-include-markdown-plugin pymdown-extensions markdown-include
18+
- run: mkdocs gh-deploy --force

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Alternatively, you can clone the repo and run `python setup.py install`.
2828

2929
## Quick Start
3030

31-
The full pynetbox API is documented on [Read the Docs](http://pynetbox.readthedocs.io/en/latest/), but the following should be enough to get started using it.
31+
The full pynetbox API is documented on [GitHub Pages](https://netbox-community.github.io/pynetbox/), but the following should be enough to get started using it.
3232

3333
To begin, import pynetbox and instantiate the API.
3434

docs/IPAM.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# IPAM
2+
3+
::: pynetbox.models.ipam.Prefixes
4+
handler: python
5+
options:
6+
members: true
7+
8+
::: pynetbox.models.ipam.VlanGroups
9+
handler: python
10+
options:
11+
members: true

docs/IPAM.rst

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/Makefile

Lines changed: 0 additions & 20 deletions
This file was deleted.

docs/advanced.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Custom Sessions
2+
3+
Custom sessions can be used to modify the default HTTP behavior. Below are a few examples, most of them from [here](https://hodovi.ch/blog/advanced-usage-python-requests-timeouts-retries-hooks/).
4+
5+
## Headers
6+
7+
To set a custom header on all requests. These headers are automatically merged with headers pynetbox sets itself.
8+
9+
Example:
10+
11+
```python
12+
import pynetbox
13+
import requests
14+
session = requests.Session()
15+
session.headers = {"mycustomheader": "test"}
16+
nb = pynetbox.api(
17+
'http://localhost:8000',
18+
token='d6f4e314a5b5fefd164995169f28ae32d987704f'
19+
)
20+
nb.http_session = session
21+
```
22+
23+
## SSL Verification
24+
25+
To disable SSL verification. See [the docs](https://requests.readthedocs.io/en/stable/user/advanced/#ssl-cert-verification).
26+
27+
Example:
28+
29+
```python
30+
import pynetbox
31+
import requests
32+
session = requests.Session()
33+
session.verify = False
34+
nb = pynetbox.api(
35+
'http://localhost:8000',
36+
token='d6f4e314a5b5fefd164995169f28ae32d987704f'
37+
)
38+
nb.http_session = session
39+
```
40+
41+
## Timeouts
42+
43+
Setting timeouts requires the use of Adapters.
44+
45+
Example:
46+
47+
```python
48+
from requests.adapters import HTTPAdapter
49+
50+
class TimeoutHTTPAdapter(HTTPAdapter):
51+
def __init__(self, *args, **kwargs):
52+
self.timeout = kwargs.get("timeout", 5)
53+
super().__init__(*args, **kwargs)
54+
55+
def send(self, request, **kwargs):
56+
kwargs['timeout'] = self.timeout
57+
return super().send(request, **kwargs)
58+
59+
adapter = TimeoutHTTPAdapter()
60+
session = requests.Session()
61+
session.mount("http://", adapter)
62+
session.mount("https://", adapter)
63+
64+
nb = pynetbox.api(
65+
'http://localhost:8000',
66+
token='d6f4e314a5b5fefd164995169f28ae32d987704f'
67+
)
68+
nb.http_session = session
69+
```

docs/advanced.rst

Lines changed: 0 additions & 72 deletions
This file was deleted.

docs/branching.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Branching Plugin
2+
3+
The NetBox branching plugin allows you to create and work with branches in NetBox, similar to version control systems. This enables you to make changes in isolation and merge them back to the main branch when ready.
4+
5+
## Activating Branches
6+
7+
The `activate_branch` context manager allows you to perform operations within a specific branch's schema. All operations performed within the context manager will use that branch's schema.
8+
9+
```python
10+
import pynetbox
11+
12+
# Initialize the API
13+
nb = pynetbox.api(
14+
"http://localhost:8000",
15+
token="your-token-here"
16+
)
17+
18+
# Get an existing branch
19+
branch = nb.plugins.branching.branches.get(id=1)
20+
21+
# Activate the branch for operations
22+
with nb.activate_branch(branch):
23+
# All operations within this block will use the branch's schema
24+
sites = nb.dcim.sites.all()
25+
# Make changes to objects...
26+
# These changes will only exist in this branch
27+
```
28+
29+
## Waiting for Branch Status
30+
31+
When working with branches, you often need to wait for certain status changes, such as when a branch becomes ready after creation or when a merge operation completes. The [tenacity](https://github.com/jd/tenacity) library provides a robust way to handle these waiting scenarios.
32+
33+
First, install tenacity:
34+
35+
```bash
36+
pip install tenacity
37+
```
38+
39+
Here's how to create a reusable function to wait for branch status changes:
40+
41+
```python
42+
from tenacity import retry, retry_if_result, stop_after_attempt, wait_exponential
43+
import pynetbox
44+
45+
@retry(
46+
stop=stop_after_attempt(30), # Try for up to 30 attempts
47+
wait=wait_exponential(
48+
multiplier=1, min=4, max=60
49+
), # Wait between 4-60 seconds, increasing exponentially
50+
retry=retry_if_result(lambda x: not x), # Retry if the status check returns False
51+
)
52+
def wait_for_branch_status(branch, target_status):
53+
"""Wait for branch to reach a specific status, with exponential backoff."""
54+
branch = nb.plugins.branching.branches.get(branch.id)
55+
return str(branch.status) == target_status
56+
57+
# Example usage:
58+
branch = nb.plugins.branching.branches.create(name="my-branch")
59+
60+
# Wait for branch to be ready
61+
wait_for_branch_status(branch, "Ready")
62+
63+
# Get the latest branch status
64+
branch = nb.plugins.branching.branches.get(branch.id)
65+
print(f"Branch is now ready! Status: {branch.status}")
66+
```
67+
68+
The function will:
69+
70+
1. Check the current status of the branch
71+
2. If the status doesn't match the target status, it will retry with exponential backoff
72+
3. Continue retrying until either:
73+
- The branch reaches the target status
74+
- The maximum number of attempts (30) is reached
75+
- The maximum wait time (60 seconds) is exceeded
76+
77+
The exponential backoff ensures that we don't overwhelm the server with requests while still checking frequently enough to catch status changes quickly.

docs/branching.rst

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)