项目作者: scaleway

项目描述 :
Python client API for Netbox
高级语言: Python
项目地址: git://github.com/scaleway/python-netboxapi.git
创建时间: 2017-03-27T13:02:29Z
项目社区:https://github.com/scaleway/python-netboxapi

开源协议:Other

下载


Python Netbox API

Build Status Coverage Status

Python client API for Netbox, using requests.

Usage

Netbox API

Import NetboxAPI:

  1. from netboxapi import NetboxAPI

Initialize a new NetboxAPI object:

  1. netbox_api = NetboxAPI(url="netbox.example.com/api")
  2. # or if you enabled the authentication
  3. netbox_api = NetboxAPI(
  4. url="netbox.example.com/api", username="user", password="password"
  5. )
  6. # or if you have generated a token
  7. netbox_api = NetboxAPI(
  8. url="netbox.example.com/api", token="token"
  9. )
  10. # but the following is useless, as the token will not be used
  11. netbox_api = NetboxAPI(
  12. url="netbox.example.com/api", username="user", password="password",
  13. token="token"
  14. )

Then use multiple available methods to interact with the api:

  1. >>> netbox_api.get("dcim/sites/1/racks/")
  2. {
  3. "id": 1,
  4. "name": "Some rack",
  5. }
  6. >>> netbox_api.post("dcim/device-roles/", json={"name": "test", …},)
  7. {
  8. "id": 1,
  9. "name": "test",
  10. }
  11. >>> netbox_api.patch("dcim/device-roles/", json={"slug": "test"},)
  12. {
  13. "id": 1,
  14. "name": "test",
  15. "slug": "test",
  16. }
  17. >>> netbox_api.put("dcim/device-roles/1/", json={"name": "test", …},)
  18. {
  19. "id": 1,
  20. "name": "test",
  21. "slug": "test",
  22. }
  23. >>> netbox_api.delete("dcim/sites/1/")
  24. <<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:

  1. from netboxapi import NetboxAPI, NetboxMapper

Initialize a new NetboxMapper object:

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

GET

Then get all objects of the model:

  1. >>> sites = list(netbox_mapper.get())
  2. [<NetboxMapper>, <NetboxMapper>, …]
  3. >>> print(sites[0].id)
  4. 1
  5. >>> print(sites[0].name)
  6. "Some site"

Or get a specific site by its id:

  1. >>> netbox_mapper.get(1)

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

  1. >>> 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.

  1. >>> site = next(netbox_mapper.get())
  2. >>> print(site.region.name)
  3. "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:

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

POST

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

  1. >>> netbox_mapper.post(name="A site", slug="a_site", region="Some region")
  2. <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:

  1. >>> child_mapper = netbox_mapper.get(1)
  2. >>> child_mapper.name = "another name"
  3. >>> child_mapper.put()
  4. <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():

  1. >>> netbox_mapper.delete(1)
  2. <requests> # requests object containing the netbox response
  3. # OR
  4. >>> child_mapper = netbox_mapper.get(1)
  5. >>> child_mapper.delete()
  6. <requests> # requests object containing the netbox response

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

  1. >>> child_mapper = netbox_mapper.get(1)
  2. >>> child_mapper.delete(2)
  3. 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!