Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a built-in function to create and activate a Branch #670

Open
jchambers2012 opened this issue Feb 21, 2025 · 0 comments
Open

Add a built-in function to create and activate a Branch #670

jchambers2012 opened this issue Feb 21, 2025 · 0 comments
Labels
type: feature Introduction of new functionality to the application

Comments

@jchambers2012
Copy link

jchambers2012 commented Feb 21, 2025

pynetbox version

v7.4.1

NetBox version

v4.1.8

Feature type

New functionality

Proposed functionality

I know this is creating the niche carve out for a single plugin but this will help simplify switching or activating a branch within the module and #642 shows how the module can use branching today.

I propose adding new functions to create and activate a branch so the user using this module doesn’t have to duplicate code between scripts to create/activate and wait for a bunch to become ready/active. Below is an example of a single function that can create and validation that it’s active. However this could also be split up into separate functions so user code can do other processing while a branch is bring created and not ready for activation. Such as:

  • 1 to create a branch (non-blocking)
  • 1 to activate a branch (blocking or non-rdy status return)
  • 1 to verify that your in a branch (true/false)

Example of what we use today pulled from a class that wrap the module with extra functionality:

def set_branch(self, branch_name, create=True):
    # Add check here for plug-in support otherwise raise exception 

    # https://github.com/netbox-community/pynetbox/issues/642#issuecomment-2350317544
    branch = self._connection.plugins.branching.branches.get(name=branch_name)
    if not branch and create:
        branch = self._connection.plugins.branching.branches.create(
            name=branch_name, status="new" #status required via api - https://github.com/netboxlabs/netbox-branching/discussions/173
        )
        i = 0
        rdy = False
        time.sleep(1)
        while not rdy and i < 10:
            try:
                branch_test = self._connection.plugins.branching.branches.get(
                    id=branch.id
                )
                if branch_test.status.value == "ready":
                    rdy = True
            except Exception as e:
                logger.debug(f"{e}")
            i += 1
            time.sleep(10)
        if not rdy:
            raise Exception("Branch not ready")
    else:
        raise Exception("Branch not found")
    #Branch is ready so set the header
    self._connection.http_session.headers["X-NetBox-Branch"] = branch.schema_id

Use case

I have several scripts that are being converted over to utilizing the branching functionality via this module and the functionality to create and verify that a branch is active and ready to receive API calls is being duplicated across code bases purposes.

External dependencies

The external dependency will be the NetBox Labs Branching Plugin, and the functionality to activate a branch can check that the plugin is installed before executing on the api or raising exception

@jchambers2012 jchambers2012 added the type: feature Introduction of new functionality to the application label Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature Introduction of new functionality to the application
Projects
None yet
Development

No branches or pull requests

1 participant