mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-21 04:42:22 -06:00
Initial push to public repo
This commit is contained in:
0
netbox/extras/api/__init__.py
Normal file
0
netbox/extras/api/__init__.py
Normal file
31
netbox/extras/api/renderers.py
Normal file
31
netbox/extras/api/renderers.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from rest_framework import renderers
|
||||
|
||||
|
||||
# IP address family designations
|
||||
AF = {
|
||||
4: 'A',
|
||||
6: 'AAAA',
|
||||
}
|
||||
|
||||
|
||||
class BINDZoneRenderer(renderers.BaseRenderer):
|
||||
"""
|
||||
Generate a BIND zone file from a list of DNS records.
|
||||
Required fields: `name`, `primary_ip`
|
||||
"""
|
||||
media_type = 'text/plain'
|
||||
format = 'bind-zone'
|
||||
|
||||
def render(self, data, media_type=None, renderer_context=None):
|
||||
records = []
|
||||
for record in data:
|
||||
if record.get('name') and record.get('primary_ip'):
|
||||
try:
|
||||
records.append("{} IN {} {}".format(
|
||||
record['name'],
|
||||
AF[record['primary_ip']['family']],
|
||||
record['primary_ip']['address'].split('/')[0],
|
||||
))
|
||||
except KeyError:
|
||||
pass
|
||||
return '\n'.join(records)
|
||||
14
netbox/extras/api/serializers.py
Normal file
14
netbox/extras/api/serializers.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from extras.models import Graph
|
||||
|
||||
|
||||
class GraphSerializer(serializers.ModelSerializer):
|
||||
embed_url = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Graph
|
||||
fields = ['name', 'embed_url', 'link']
|
||||
|
||||
def get_embed_url(self, obj):
|
||||
return obj.embed_url(self.context['graphed_object'])
|
||||
33
netbox/extras/api/views.py
Normal file
33
netbox/extras/api/views.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from rest_framework import generics
|
||||
|
||||
from django.http import Http404
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from circuits.models import Provider
|
||||
from dcim.models import Site, Interface
|
||||
from extras.models import Graph, GRAPH_TYPE_INTERFACE, GRAPH_TYPE_PROVIDER, GRAPH_TYPE_SITE
|
||||
from .serializers import GraphSerializer
|
||||
|
||||
|
||||
class GraphListView(generics.ListAPIView):
|
||||
"""
|
||||
Returns a list of relevant graphs
|
||||
"""
|
||||
serializer_class = GraphSerializer
|
||||
|
||||
def get_serializer_context(self):
|
||||
cls = {
|
||||
GRAPH_TYPE_INTERFACE: Interface,
|
||||
GRAPH_TYPE_PROVIDER: Provider,
|
||||
GRAPH_TYPE_SITE: Site,
|
||||
}
|
||||
context = super(GraphListView, self).get_serializer_context()
|
||||
context.update({'graphed_object': get_object_or_404(cls[self.kwargs.get('type')], pk=self.kwargs['pk'])})
|
||||
return context
|
||||
|
||||
def get_queryset(self):
|
||||
graph_type = self.kwargs.get('type', None)
|
||||
if not graph_type:
|
||||
raise Http404()
|
||||
queryset = Graph.objects.filter(type=graph_type)
|
||||
return queryset
|
||||
Reference in New Issue
Block a user