netbox3


Namenetbox3 JSON
Version 0.1.14 PyPI version JSON
download
home_pagehttps://github.com/vladimirs-git/netbox3
SummaryPython package designed to assist in working with the Netbox REST API. The filter parameters are identical to those in the Web UI filter form. It replaces brief data with full information, and Netbox objects are represented as a recursive multidimensional dictionary.
upload_time2023-12-16 21:49:27
maintainer
docs_urlNone
authorVladimirs Prusakovs
requires_python>=3.8,<4.0
licenseApache-2.0
keywords netbox
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            netbox3
=======


Overview
========

Python package designed to help work with the `Netbox`_ REST API.

- `NbApi`_ Request data from Netbox using filter parameters identical to those in the Web UI filter form. Filter parameters use the ``OR`` operator.
- `NbForager`_ The REST API returns objects that contain a brief representation of related objects. NbForager replaces brief data with full and objects look like a recursive multidimensional dictionary.
- `NbBranch`_ Extract typed values from a Netbox object dictionary by using a chain of keys.

Checked with Python >= 3.8, Netbox >= v3.6.
Fully documented on `Read the Docs`_.


----------------------------------------------------------------------------------------

Quickstart
==========

Install the package from pypi.org

.. code:: bash

    pip install netbox3

or from github.com repository

.. code:: bash

    pip install git+https://github.com/vladimirs-git/netbox3


NbForager demonstration.
Assemble Netbox objects within self as a multidimensional dictionary.

Request the main object. All nested objects also are requested.
Assemble multidimensional dictionary.

.. code:: python

    from pprint import pprint

    from netbox3 import NbForager

    HOST = "demo.netbox.dev"
    TOKEN = "1234567890123456789012345678901234567890"
    nbf = NbForager(host=HOST, token=TOKEN, threads=10)

    # Request devices with all nested object: device-roles, tenants, tags, etc.
    nbf.dcim.devices.get(nested=True)
    print(f"{len(nbf.root.dcim.devices)=}")
    print(f"{len(nbf.root.dcim.device_roles)=}")
    print(f"{len(nbf.root.tenancy.tenants)=}")
    print(f"{len(nbf.root.extras.tags)=}")
    # len(nbf.root.dcim.devices)=78
    # len(nbf.root.dcim.device_roles)=10
    # len(nbf.root.tenancy.tenants)=5
    # len(nbf.root.extras.tags)=2


    # Assemble objects within self as multidimensional dictionary.
    tree = nbf.join_tree()
    pprint(list(tree.dcim.devices.values())[0])
    # {"id": 1,
    #  "name": "dmi01-akron-rtr01",
    #  "rack": {"id": 1,
    #           "site": {"id": 2,
    #                    "tenant": {"id": 5,
    #                               "group": {"id": 1,
    #                                         "name": "Customers",
    #                                         ...
    #           "tenant": {"id": 5,
    #                      "group": {"id": 1,
    #                                "name": "Customers",
    #                                ...
    # ...

Request objects using filtering parameters. Assemble multidimensional dictionary.

.. code:: python

    from pprint import pprint

    from netbox3 import NbForager, NbBranch

    HOST = "demo.netbox.dev"
    TOKEN = "1234567890123456789012345678901234567890"
    nbf = NbForager(host=HOST, token=TOKEN)

    # Request specific devices and all sites from Netbox.
    # Note that the site in the device only contains basic data and
    # does not include tags, region and other extended data.
    nbf.dcim.devices.get(q="PP:B")
    nbf.dcim.sites.get()
    device = nbf.root.dcim.devices[88]
    pprint(device)
    # {"id": 88,
    #  "name": "PP:B117",
    #  "site": {"display": "MDF",
    #           "id": 21,
    #           "name": "MDF",
    #           "slug": "ncsu-065",
    #           "url": "https://demo.netbox.dev/api/dcim/sites/21/"},
    #  ...

    # Assemble objects within self as multidimensional dictionary.
    # Note that the device now includes site region and all other data.
    tree = nbf.join_tree()
    device = tree.dcim.devices[88]
    pprint(device)
    # {"id": 88,
    #  "name": "PP:B117",
    #  "site": {"display": "MDF",
    #           "id": 21,
    #           "name": "MDF",
    #           "slug": "ncsu-065",
    #           "url": "https://demo.netbox.dev/api/dcim/sites/21/"
    #           "region": {"_depth": 2,
    #                      "display": "North Carolina",
    #                      "id": 40,
    #                      "name": "North Carolina",
    #                      "slug": "us-nc",
    #                      "url": "https://demo.netbox.dev/api/dcim/regions/40/"},
    #           "tenant": {"display": "NC State University",
    #                      "id": 13,
    #                      "name": "NC State University",
    #                      "slug": "nc-state",
    #                      "url": "https://demo.netbox.dev/api/tenancy/tenants/13/"},
    #           ...
    # ...

    # Access site attribute through a device.
    region = device["site"]["region"]["name"]
    print(f"{region=}")  # region="North Carolina"

    # Use NbBranch to ensure the data type if any dictionary in the chain is missing.
    region = NbBranch(device).str("site", "region", "name")
    print(f"{region=}")  # region="North Carolina"


NbApi demonstration.
Create, get, update and delete ip-addresses.

.. code:: python

    from netbox3 import NbApi

    HOST = "demo.netbox.dev"
    TOKEN = "1234567890123456789012345678901234567890"
    nb = NbApi(host=HOST, token=TOKEN)

    # Create 2 addresses with different methods (different outputs)
    response = nb.ipam.ip_addresses.create(address="1.2.3.4/24", tags=[2], status="active")
    print(response)  # <Response [201]>
    data = nb.ipam.ip_addresses.create_d(address="1.2.3.4/24", tags=[3], status="reserved")
    print(data)  # {"id": 183, "display": "1.2.3.4/24", ...

    # Get all addresses
    addresses = nb.ipam.ip_addresses.get()
    print(len(addresses))  # 181

    # Get all ip-addresses in global routing
    addresses = nb.ipam.ip_addresses.get(vrf="null")
    print(len(addresses))  # 30

    # Get newly created ip-addresses by complex filter
    # Note, you can use parameters similarly to the ``OR`` operator.
    # Filter addresses in the global routing AND
    # (have either the tag "bravo" OR "charlie") AND
    # (have a status of either active OR reserved).
    addresses = nb.ipam.ip_addresses.get(or_q=["1.2.3", "4.5.6"],
                                         vrf="null",
                                         or_tag=["bravo", "charlie"],
                                         status=["active", "reserved"])
    print(len(addresses))  # 2

    addresses = nb.ipam.ip_addresses.get(address="1.2.3.4/24")
    for address in addresses:
        # Update
        id_ = address["id"]
        response = nb.ipam.ip_addresses.update(id=id_, description="text")
        print(response)  # <Response [200]>
        print(nb.ipam.ip_addresses.get(id=id_)[0]["description"])  # text

        # Delete
        response = nb.ipam.ip_addresses.delete(id=id_)
        print(response)  # <Response [204]>


----------------------------------------------------------------------------------------

.. _`Netbox`: https://github.com/netbox-community/netbox
.. _`Read the Docs`: https://netbox3.readthedocs.io/en/latest/
.. _`NbApi`: https://netbox3.readthedocs.io/en/latest/api/nb_api.html#nbapi
.. _`NbForager`: https://netbox3.readthedocs.io/en/latest/foragers/nb_forager.html#nbforager
.. _`NbBranch`: https://netbox3.readthedocs.io/en/latest/branch/nb_branch.html#nbbranch


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/vladimirs-git/netbox3",
    "name": "netbox3",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "netbox",
    "author": "Vladimirs Prusakovs",
    "author_email": "vladimir.prusakovs@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/a7/c1/dbef0f7733afeecadfefdb6422eaa008cc18f1e7d74756a380ce3531908e/netbox3-0.1.14.tar.gz",
    "platform": null,
    "description": "netbox3\n=======\n\n\nOverview\n========\n\nPython package designed to help work with the `Netbox`_ REST API.\n\n- `NbApi`_ Request data from Netbox using filter parameters identical to those in the Web UI filter form. Filter parameters use the ``OR`` operator.\n- `NbForager`_ The REST API returns objects that contain a brief representation of related objects. NbForager replaces brief data with full and objects look like a recursive multidimensional dictionary.\n- `NbBranch`_ Extract typed values from a Netbox object dictionary by using a chain of keys.\n\nChecked with Python >= 3.8, Netbox >= v3.6.\nFully documented on `Read the Docs`_.\n\n\n----------------------------------------------------------------------------------------\n\nQuickstart\n==========\n\nInstall the package from pypi.org\n\n.. code:: bash\n\n    pip install netbox3\n\nor from github.com repository\n\n.. code:: bash\n\n    pip install git+https://github.com/vladimirs-git/netbox3\n\n\nNbForager demonstration.\nAssemble Netbox objects within self as a multidimensional dictionary.\n\nRequest the main object. All nested objects also are requested.\nAssemble multidimensional dictionary.\n\n.. code:: python\n\n    from pprint import pprint\n\n    from netbox3 import NbForager\n\n    HOST = \"demo.netbox.dev\"\n    TOKEN = \"1234567890123456789012345678901234567890\"\n    nbf = NbForager(host=HOST, token=TOKEN, threads=10)\n\n    # Request devices with all nested object: device-roles, tenants, tags, etc.\n    nbf.dcim.devices.get(nested=True)\n    print(f\"{len(nbf.root.dcim.devices)=}\")\n    print(f\"{len(nbf.root.dcim.device_roles)=}\")\n    print(f\"{len(nbf.root.tenancy.tenants)=}\")\n    print(f\"{len(nbf.root.extras.tags)=}\")\n    # len(nbf.root.dcim.devices)=78\n    # len(nbf.root.dcim.device_roles)=10\n    # len(nbf.root.tenancy.tenants)=5\n    # len(nbf.root.extras.tags)=2\n\n\n    # Assemble objects within self as multidimensional dictionary.\n    tree = nbf.join_tree()\n    pprint(list(tree.dcim.devices.values())[0])\n    # {\"id\": 1,\n    #  \"name\": \"dmi01-akron-rtr01\",\n    #  \"rack\": {\"id\": 1,\n    #           \"site\": {\"id\": 2,\n    #                    \"tenant\": {\"id\": 5,\n    #                               \"group\": {\"id\": 1,\n    #                                         \"name\": \"Customers\",\n    #                                         ...\n    #           \"tenant\": {\"id\": 5,\n    #                      \"group\": {\"id\": 1,\n    #                                \"name\": \"Customers\",\n    #                                ...\n    # ...\n\nRequest objects using filtering parameters. Assemble multidimensional dictionary.\n\n.. code:: python\n\n    from pprint import pprint\n\n    from netbox3 import NbForager, NbBranch\n\n    HOST = \"demo.netbox.dev\"\n    TOKEN = \"1234567890123456789012345678901234567890\"\n    nbf = NbForager(host=HOST, token=TOKEN)\n\n    # Request specific devices and all sites from Netbox.\n    # Note that the site in the device only contains basic data and\n    # does not include tags, region and other extended data.\n    nbf.dcim.devices.get(q=\"PP:B\")\n    nbf.dcim.sites.get()\n    device = nbf.root.dcim.devices[88]\n    pprint(device)\n    # {\"id\": 88,\n    #  \"name\": \"PP:B117\",\n    #  \"site\": {\"display\": \"MDF\",\n    #           \"id\": 21,\n    #           \"name\": \"MDF\",\n    #           \"slug\": \"ncsu-065\",\n    #           \"url\": \"https://demo.netbox.dev/api/dcim/sites/21/\"},\n    #  ...\n\n    # Assemble objects within self as multidimensional dictionary.\n    # Note that the device now includes site region and all other data.\n    tree = nbf.join_tree()\n    device = tree.dcim.devices[88]\n    pprint(device)\n    # {\"id\": 88,\n    #  \"name\": \"PP:B117\",\n    #  \"site\": {\"display\": \"MDF\",\n    #           \"id\": 21,\n    #           \"name\": \"MDF\",\n    #           \"slug\": \"ncsu-065\",\n    #           \"url\": \"https://demo.netbox.dev/api/dcim/sites/21/\"\n    #           \"region\": {\"_depth\": 2,\n    #                      \"display\": \"North Carolina\",\n    #                      \"id\": 40,\n    #                      \"name\": \"North Carolina\",\n    #                      \"slug\": \"us-nc\",\n    #                      \"url\": \"https://demo.netbox.dev/api/dcim/regions/40/\"},\n    #           \"tenant\": {\"display\": \"NC State University\",\n    #                      \"id\": 13,\n    #                      \"name\": \"NC State University\",\n    #                      \"slug\": \"nc-state\",\n    #                      \"url\": \"https://demo.netbox.dev/api/tenancy/tenants/13/\"},\n    #           ...\n    # ...\n\n    # Access site attribute through a device.\n    region = device[\"site\"][\"region\"][\"name\"]\n    print(f\"{region=}\")  # region=\"North Carolina\"\n\n    # Use NbBranch to ensure the data type if any dictionary in the chain is missing.\n    region = NbBranch(device).str(\"site\", \"region\", \"name\")\n    print(f\"{region=}\")  # region=\"North Carolina\"\n\n\nNbApi demonstration.\nCreate, get, update and delete ip-addresses.\n\n.. code:: python\n\n    from netbox3 import NbApi\n\n    HOST = \"demo.netbox.dev\"\n    TOKEN = \"1234567890123456789012345678901234567890\"\n    nb = NbApi(host=HOST, token=TOKEN)\n\n    # Create 2 addresses with different methods (different outputs)\n    response = nb.ipam.ip_addresses.create(address=\"1.2.3.4/24\", tags=[2], status=\"active\")\n    print(response)  # <Response [201]>\n    data = nb.ipam.ip_addresses.create_d(address=\"1.2.3.4/24\", tags=[3], status=\"reserved\")\n    print(data)  # {\"id\": 183, \"display\": \"1.2.3.4/24\", ...\n\n    # Get all addresses\n    addresses = nb.ipam.ip_addresses.get()\n    print(len(addresses))  # 181\n\n    # Get all ip-addresses in global routing\n    addresses = nb.ipam.ip_addresses.get(vrf=\"null\")\n    print(len(addresses))  # 30\n\n    # Get newly created ip-addresses by complex filter\n    # Note, you can use parameters similarly to the ``OR`` operator.\n    # Filter addresses in the global routing AND\n    # (have either the tag \"bravo\" OR \"charlie\") AND\n    # (have a status of either active OR reserved).\n    addresses = nb.ipam.ip_addresses.get(or_q=[\"1.2.3\", \"4.5.6\"],\n                                         vrf=\"null\",\n                                         or_tag=[\"bravo\", \"charlie\"],\n                                         status=[\"active\", \"reserved\"])\n    print(len(addresses))  # 2\n\n    addresses = nb.ipam.ip_addresses.get(address=\"1.2.3.4/24\")\n    for address in addresses:\n        # Update\n        id_ = address[\"id\"]\n        response = nb.ipam.ip_addresses.update(id=id_, description=\"text\")\n        print(response)  # <Response [200]>\n        print(nb.ipam.ip_addresses.get(id=id_)[0][\"description\"])  # text\n\n        # Delete\n        response = nb.ipam.ip_addresses.delete(id=id_)\n        print(response)  # <Response [204]>\n\n\n----------------------------------------------------------------------------------------\n\n.. _`Netbox`: https://github.com/netbox-community/netbox\n.. _`Read the Docs`: https://netbox3.readthedocs.io/en/latest/\n.. _`NbApi`: https://netbox3.readthedocs.io/en/latest/api/nb_api.html#nbapi\n.. _`NbForager`: https://netbox3.readthedocs.io/en/latest/foragers/nb_forager.html#nbforager\n.. _`NbBranch`: https://netbox3.readthedocs.io/en/latest/branch/nb_branch.html#nbbranch\n\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Python package designed to assist in working with the Netbox REST API. The filter parameters are identical to those in the Web UI filter form. It replaces brief data with full information, and Netbox objects are represented as a recursive multidimensional dictionary.",
    "version": "0.1.14",
    "project_urls": {
        "Bug Tracker": "https://github.com/vladimirs-git/netbox3/issues",
        "Download URL": "https://github.com/vladimirs-git/netbox3/archive/refs/tags/0.1.14.tar.gz",
        "Homepage": "https://github.com/vladimirs-git/netbox3",
        "Repository": "https://github.com/vladimirs-git/netbox3"
    },
    "split_keywords": [
        "netbox"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ce37752af8911b05084dd66d7f5845db20e0d30ca373742f4577ec249ffcac76",
                "md5": "cece8cb88be0b35c08d2c5c14f5a5000",
                "sha256": "74bc080910f681d2b44a8aa5e8c639453cd469c605af1fc7ad348cd46a744fd4"
            },
            "downloads": -1,
            "filename": "netbox3-0.1.14-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "cece8cb88be0b35c08d2c5c14f5a5000",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 59329,
            "upload_time": "2023-12-16T21:49:25",
            "upload_time_iso_8601": "2023-12-16T21:49:25.146609Z",
            "url": "https://files.pythonhosted.org/packages/ce/37/752af8911b05084dd66d7f5845db20e0d30ca373742f4577ec249ffcac76/netbox3-0.1.14-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a7c1dbef0f7733afeecadfefdb6422eaa008cc18f1e7d74756a380ce3531908e",
                "md5": "634da564a27803f9616e0364986b0727",
                "sha256": "5f75dfc7347297ded2872256c6435f5a902f73e9d502a71b2f5b058abccf4b5f"
            },
            "downloads": -1,
            "filename": "netbox3-0.1.14.tar.gz",
            "has_sig": false,
            "md5_digest": "634da564a27803f9616e0364986b0727",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 43589,
            "upload_time": "2023-12-16T21:49:27",
            "upload_time_iso_8601": "2023-12-16T21:49:27.106679Z",
            "url": "https://files.pythonhosted.org/packages/a7/c1/dbef0f7733afeecadfefdb6422eaa008cc18f1e7d74756a380ce3531908e/netbox3-0.1.14.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-16 21:49:27",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "vladimirs-git",
    "github_project": "netbox3",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "netbox3"
}
        
Elapsed time: 0.19356s