All Projects → scaleway → python-netboxapi

scaleway / python-netboxapi

Licence: other
Python client API for Netbox

Programming Languages

python
139335 projects - #7 most used programming language

Labels

Projects that are alternatives of or similar to python-netboxapi

netbox-bgp
NetBox plugin for BGP related objects documentation
Stars: ✭ 135 (+350%)
Mutual labels:  netbox
django-netbox-graphql
Django module which provides a GraphQL API for Netbox
Stars: ✭ 16 (-46.67%)
Mutual labels:  netbox
ntc-netbox-plugin-metrics-ext
NetBox Plugin to improve the instrumentation of NetBox and expose additional metrics (Application Metrics, RQ Worker).
Stars: ✭ 32 (+6.67%)
Mutual labels:  netbox
netbox-sync
Sync objects from VMware or redfish sources to NetBox
Stars: ✭ 172 (+473.33%)
Mutual labels:  netbox
netbox automation
Manage netbox configuration with automation. Netbox as a the source of truth: generate an ansible inventory file from Netbox with automation, generate yaml files for ansible playbooks or jinja templates from Netbox with automation
Stars: ✭ 28 (-6.67%)
Mutual labels:  netbox
netbox-chart
A Helm chart for NetBox
Stars: ✭ 141 (+370%)
Mutual labels:  netbox
Netbox
Infrastructure resource modeling for network automation. Open source under Apache 2. Public demo: https://demo.netbox.dev
Stars: ✭ 9,310 (+30933.33%)
Mutual labels:  netbox
netbox-agent
Netbox agent to run on your infrastructure's servers
Stars: ✭ 99 (+230%)
Mutual labels:  netbox
ttl255.com
ttl255.com - Resources
Stars: ✭ 31 (+3.33%)
Mutual labels:  netbox
netbox-plugin-skeleton
Skeleton for starting NetBox plugins
Stars: ✭ 15 (-50%)
Mutual labels:  netbox
netbox-vcenter
vCenter integration plugin for NetBox
Stars: ✭ 36 (+20%)
Mutual labels:  netbox
netbox-paloalto
NetBox plugin for listing firewall rules (from a Palo Alto Networks firewall/Panorama) associated with a NetBox object.
Stars: ✭ 27 (-10%)
Mutual labels:  netbox
network-importer
The network importer is a tool/library to analyze and/or synchronize an existing network with a Network Source of Truth (SOT), it's designed to be idempotent and by default it's only showing the difference between the running network and the remote SOT.
Stars: ✭ 120 (+300%)
Mutual labels:  netbox
ntmap
Network topology map using Netbox as a data source
Stars: ✭ 74 (+146.67%)
Mutual labels:  netbox
hphr
Halophile Router (a VyOS-based, SaltStack-automated, NetBox-configured router for small provider networks)
Stars: ✭ 39 (+30%)
Mutual labels:  netbox
coredns-netbox-plugin
A coredns plugin to get dns records from Netbox
Stars: ✭ 35 (+16.67%)
Mutual labels:  netbox
netbox-joined-inventory
Netbox_joined_inventory is a python script that gathers data from a Netbox source-of-truth and stores them as Ansible inventory, group_vars and host_vars files.
Stars: ✭ 21 (-30%)
Mutual labels:  netbox
yaani
Yet another Ansible Netbox inventory
Stars: ✭ 12 (-60%)
Mutual labels:  netbox

Python Netbox API

Build Status Coverage Status

Python client API for Netbox, using requests.

Usage

Netbox API

Import NetboxAPI:

from netboxapi import NetboxAPI

Initialize a new NetboxAPI object:

netbox_api = NetboxAPI(url="netbox.example.com/api")

# or if you enabled the authentication
netbox_api = NetboxAPI(
    url="netbox.example.com/api", username="user", password="password"
)

# or if you have generated a token
netbox_api = NetboxAPI(
    url="netbox.example.com/api", token="token"
)

# but the following is useless, as the token will not be used
netbox_api = NetboxAPI(
    url="netbox.example.com/api", username="user", password="password",
    token="token"
)

Then use multiple available methods to interact with the api:

>>> netbox_api.get("dcim/sites/1/racks/")
{
    "id": 1,
    "name": "Some rack",
    …
}

>>> netbox_api.post("dcim/device-roles/", json={"name": "test", …},)
{
    "id": 1,
    "name": "test",
    …
}

>>> netbox_api.patch("dcim/device-roles/", json={"slug": "test"},)
{
    "id": 1,
    "name": "test",
    "slug": "test",
    …
}

>>> netbox_api.put("dcim/device-roles/1/", json={"name": "test", …},)
{
    "id": 1,
    "name": "test",
    "slug": "test",
    …
}

>>> netbox_api.delete("dcim/sites/1/")
<<Response [204]>>

Netbox Mapper

NetboxMapper is available to interact with Netbox objects. Received json from the netbox API is converted into mapper objects, by setting its attributes accordingly to the dict. To use it, first import NetboxMapper:

from netboxapi import NetboxAPI, NetboxMapper

Initialize a new NetboxMapper object:

netbox_api = NetboxAPI(
    url="netbox.example.com/api", username="user", password="password"
)
netbox_mapper = NetboxMapper(netbox_api, app_name="dcim", model="sites")

GET

Then get all objects of the model:

>>> sites = list(netbox_mapper.get())
[<NetboxMapper>, <NetboxMapper>, …]

>>> print(sites[0].id)
1
>>> print(sites[0].name)
"Some site"

Or get a specific site by its id:

>>> netbox_mapper.get(1)

It is possible to get a subresourses of an object, and/or specify a query:

>>> netbox_mapper.get("1", "racks", q="name_to_filter")

Any kwargs (here q=) is used as a GET parameter for the request.

Pagination is transparently handled, but it is possible to specify how many items are wanted per page by setting the GET parameter limit, to limit the number of requests done to Netbox in case of long iterations.

Foreign keys

Foreign keys are handle automatically by the mapper.

>>> site = next(netbox_mapper.get())
>>> print(site.region.name)
"Some region"

When accessing to site.region, a query will be done to fetch the foreign object. It will then be saved in cache to avoid unnecessary queries for next accesses.

To refresh an object and its foreign keys, just do:

>>> site = next(site.get())

POST

Use the kwargs of a mapper to send a post request and create a new object:

>>> netbox_mapper.post(name="A site", slug="a_site", region="Some region")
<NetboxMapper>  # corresponding to the new created object

If a mapper is sent as parameter, post() will automatically take its id. However, it will not update the foreign object.

PUT

Use put() in a child mapper to update the resource upstream by reflecting the changes made in the object attributes:

>>> child_mapper = netbox_mapper.get(1)
>>> child_mapper.name = "another name"
>>> child_mapper.put()
<requests>  # requests object containing the netbox response

PATCH

PATCH is not supported in mappers, as it does not make really sense (to me) with the mapper logic.

DELETE

Delete an object upstream by calling delete():

>>> netbox_mapper.delete(1)
<requests>  # requests object containing the netbox response

# OR

>>> child_mapper = netbox_mapper.get(1)
>>> child_mapper.delete()
<requests>  # requests object containing the netbox response

But trying to delete another object of the same model from a child mapper is not possible:

>>> child_mapper = netbox_mapper.get(1)
>>> child_mapper.delete(2)
Exception ForbiddenAsChildError

Dependencies

  • python 3.4 (it certainly works with prior versions, just not tested)

License

Tool under the BSD license. Do not hesitate to report bugs, ask me some questions or do some pull request if you want to!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].