Netbox-Graphql is a simple Django app which provides a GraphQL API for Netbox. This module is dockerized and development of it is still in progress. Netbox's circuit module with all it's models are covered. For this part of app you can CRUD operations.
- CIRCUITS
- Provider
- Circuit
- CircuitType
- CircuitTermination
- DCIM
- Region
- Site
- TENANCY
- TenantGroup
- Tenant
- IPAM
- Role
- VlanGroup
- VLAN
- VRF
- RIR
- Aggregate
- Prefix
- VIRTUALIZATION
- ClusterType
- ClusterGroup
- Cluster
- VirtualMachine
Install module:
pip install django-netbox-graphql #from external storage, not yet deployed or pip install dist/django-netbox-graphql-0.0.x.tar.gz
Add below modules to your INSTALLED_APPS setting like this:
INSTALLED_APPS = [ ... 'graphene_django', 'netbox_graphql', ]
Add graphene settings to netbox/setting.py:
GRAPHIQL_ENABLED = True # optional, by default True, set False to disable it GRAPHENE = { 'SCHEMA' : 'netbox_graphql.schema.schema', #points to the netbox-graphql schema variable in schema.py 'SCHEMA_INDENT': 2, #defines the indentation space in the output }
Include the polls URLconf in your project netbox/urls.py like this:
url(r'^graphql', include('netbox_graphql.urls')),
Visit http://127.0.0.1:8000/graphql to fetch records with graphql:
curl -H 'Content-Type: application/json'\ -H "Authorization: Token <netbox_token>"\ -XPOST -d '{"query":"{ circuitTypes { edges { node { id name slug } } } }"}' http://localhost:8000/graphql
Visit http://localhost:8000/user/api-tokens/ to generate token
Create:
curl -H 'Content-Type: application/json'\ -H "Authorization: Token <netbox_token>"\ -XPOST -d '{"query":"mutation { newCircuitType(input: {name: \"Type1\", slug: \"type1\"}) { circuitType { id name slug } } }"}' http://localhost:8000/graphql {"data":{"newCircuitType":{"circuitType":{"id":"Q2lyY3VpdFR5cGVOb2RlOjI1","name":"Type1","slug":"type1"}}}}
Read:
curl -H 'Content-Type: application/json'\ -H "Authorization: Token <netbox_token>"\ -XPOST -d '{"query":"{ circuitTypes(id: \"<circuit-type-id>\") { edges { node { id name slug } } } }"}' http://localhost:8000/graphql {"data":{"circuitTypes":{"edges":[{"node":{"id":"Q2lyY3VpdFR5cGVOb2RlOjI0","name":"Type","slug":"type"}}]}}}
Update:
curl -H 'Content-Type: application/json'\ -H "Authorization: Token <netbox_token>"\ -XPOST -d '{"query":"mutation { updateCircuitType(input: {id:\"<circuit-type-id>\", name: \"TypeX\", slug: \"typeX\"}) { circuitType { slug name slug } } }"}' http://localhost:8000/graphql {"data":{"updateCircuitType":{"circuitType":{"id":"Q2lyY3VpdFR5cGVOb2RlOjI0","name":"TypeX","slug":"typeX"}}}}
Delete:
curl -H 'Content-Type: application/json'\ -H "Authorization: Token <netbox_token>"\ -XPOST -d '{"query":"mutation { deleteCircuitType(input: {id:\"<circuit-type-id>\"}) { circuitType { name slug } } }"}' http://localhost:8000/graphql {"data":{"deleteCircuitType":{"circuitType":{"name":"TypeX","slug":"typeX"}}}}
You should have installed graphene_django:
INSTALLED_APPS = [ ... 'graphene_django', ]
Create url for graphql client with adding new link in urls.py
url(r'^graphql/client', GraphQLView.as_view(graphiql=True)),
All examples are available here
The simplest way to make package and deploy it is with using twine:
pip install twine # install twine python setup.py sdist # build django-netbox-graphql-X.X.X.tar.gz twine upload dist/django-netbox-graphql-X.X.X.tar.gz
Run unit tests:
docker-compose run netbox ./manage.py test # runs all tests docker-compose run netbox ./manage.py test netbox_graphql/ # runs only netbox_graphql module tests # At the end, you can stop any running service and cleanup as follows: docker-compose down
Can be found at https://pypi.python.org/pypi?:action=display&name=django-netbox-graphql
This module was developed and funded by nine