|
| 1 | +Branching Plugin |
| 2 | +================ |
| 3 | + |
| 4 | +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. |
| 5 | + |
| 6 | +Activating Branches |
| 7 | +----------------- |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +.. code-block:: python |
| 12 | +
|
| 13 | + import pynetbox |
| 14 | + |
| 15 | + # Initialize the API |
| 16 | + nb = pynetbox.api( |
| 17 | + "http://localhost:8000", |
| 18 | + token="your-token-here" |
| 19 | + ) |
| 20 | + |
| 21 | + # Get an existing branch |
| 22 | + branch = nb.plugins.branching.branches.get(id=1) |
| 23 | + |
| 24 | + # Activate the branch for operations |
| 25 | + with nb.activate_branch(branch): |
| 26 | + # All operations within this block will use the branch's schema |
| 27 | + sites = nb.dcim.sites.all() |
| 28 | + # Make changes to objects... |
| 29 | + # These changes will only exist in this branch |
| 30 | +
|
| 31 | +Waiting for Branch Status |
| 32 | +----------------------- |
| 33 | + |
| 34 | +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`_ library provides a robust way to handle these waiting scenarios. |
| 35 | + |
| 36 | +First, install tenacity: |
| 37 | + |
| 38 | +.. code-block:: bash |
| 39 | +
|
| 40 | + pip install tenacity |
| 41 | +
|
| 42 | +Here's how to create a reusable function to wait for branch status changes: |
| 43 | + |
| 44 | +.. code-block:: python |
| 45 | +
|
| 46 | + from tenacity import retry, retry_if_result, stop_after_attempt, wait_exponential |
| 47 | + import pynetbox |
| 48 | +
|
| 49 | + @retry( |
| 50 | + stop=stop_after_attempt(30), # Try for up to 30 attempts |
| 51 | + wait=wait_exponential( |
| 52 | + multiplier=1, min=4, max=60 |
| 53 | + ), # Wait between 4-60 seconds, increasing exponentially |
| 54 | + retry=retry_if_result(lambda x: not x), # Retry if the status check returns False |
| 55 | + ) |
| 56 | + def wait_for_branch_status(branch, target_status): |
| 57 | + """Wait for branch to reach a specific status, with exponential backoff.""" |
| 58 | + branch = nb.plugins.branching.branches.get(branch.id) |
| 59 | + return str(branch.status) == target_status |
| 60 | +
|
| 61 | + # Example usage: |
| 62 | + branch = nb.plugins.branching.branches.create(name="my-branch") |
| 63 | + |
| 64 | + # Wait for branch to be ready |
| 65 | + wait_for_branch_status(branch, "Ready") |
| 66 | + |
| 67 | + # Get the latest branch status |
| 68 | + branch = nb.plugins.branching.branches.get(branch.id) |
| 69 | + print(f"Branch is now ready! Status: {branch.status}") |
| 70 | +
|
| 71 | +The function will: |
| 72 | +1. Check the current status of the branch |
| 73 | +2. If the status doesn't match the target status, it will retry with exponential backoff |
| 74 | +3. Continue retrying until either: |
| 75 | + - The branch reaches the target status |
| 76 | + - The maximum number of attempts (30) is reached |
| 77 | + - The maximum wait time (60 seconds) is exceeded |
| 78 | + |
| 79 | +The exponential backoff ensures that we don't overwhelm the server with requests while still checking frequently enough to catch status changes quickly. |
| 80 | + |
| 81 | +.. _tenacity: https://github.com/jd/tenacity |
| 82 | + |
0 commit comments