mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-27 10:58:37 -06:00
Update APIs (ipam & dns)
This commit is contained in:
parent
ec36cba029
commit
033a54aa0a
@ -9,18 +9,10 @@ from dns.models import Zone, Record
|
||||
|
||||
class ZoneSerializer(serializers.ModelSerializer):
|
||||
|
||||
bind_export = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model=Zone
|
||||
fields = ['id', 'name', 'ttl', 'soa_name', 'soa_contact', 'soa_serial', 'soa_refresh', 'soa_retry', 'soa_expire', 'soa_minimum', 'description']
|
||||
|
||||
def get_bind_export(self, obj):
|
||||
records = Record.objects.filter(zone=obj)
|
||||
return {
|
||||
'export': obj.to_bind(records),
|
||||
}
|
||||
|
||||
class ZoneNestedSerializer(ZoneSerializer):
|
||||
|
||||
class Meta(ZoneSerializer.Meta):
|
||||
@ -35,18 +27,12 @@ class RecordSerializer(serializers.ModelSerializer):
|
||||
|
||||
zone = ZoneNestedSerializer()
|
||||
address = IPAddressNestedSerializer()
|
||||
bind_export = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model=Record
|
||||
fields = ['id', 'name', 'category', 'record_type', 'priority', 'zone', 'address', 'value', 'description']
|
||||
|
||||
def get_bind_export(self, obj):
|
||||
return {
|
||||
'export': obj.to_bind(),
|
||||
}
|
||||
fields = ['id', 'name', 'record_type', 'priority', 'zone', 'address', 'value', 'description']
|
||||
|
||||
class RecordNestedSerializer(RecordSerializer):
|
||||
|
||||
class Meta(RecordSerializer.Meta):
|
||||
fields = ['id', 'name', 'record_type', 'zone']
|
||||
fields = ['id', 'name', 'record_type', 'zone']
|
||||
|
@ -12,4 +12,8 @@ urlpatterns = [
|
||||
url(r'^records/$', RecordListView.as_view(), name='record_list'),
|
||||
url(r'^records/(?P<pk>\d+)/$', RecordDetailView.as_view(), name='record_detail'),
|
||||
|
||||
# BIND Exports
|
||||
url(r'^bind/forward/$', bind_forward, name='bind_forward'),
|
||||
url(r'^bind/reverse/$', bind_reverse, name='bind_reverse'),
|
||||
|
||||
]
|
||||
|
@ -1,7 +1,11 @@
|
||||
from rest_framework import generics
|
||||
from django.http import HttpResponse
|
||||
|
||||
from rest_framework.decorators import api_view
|
||||
from rest_framework.response import Response
|
||||
|
||||
from ipam.models import IPAddress
|
||||
from dns.models import Zone, Record
|
||||
from dns.models import Zone, Record, export_bind_forward, export_bind_reverse
|
||||
from dns import filters
|
||||
|
||||
from . import serializers
|
||||
@ -42,3 +46,24 @@ class RecordDetailView(generics.RetrieveAPIView):
|
||||
"""
|
||||
queryset = Record.objects.all()
|
||||
serializer_class = serializers.RecordSerializer
|
||||
|
||||
#
|
||||
# BIND Exports
|
||||
#
|
||||
|
||||
@api_view(['GET'])
|
||||
def bind_forward(request):
|
||||
"""
|
||||
Full export of forward zones in BIND format
|
||||
"""
|
||||
zones_list = export_bind_forward()
|
||||
return Response(zones_list)
|
||||
|
||||
@api_view(['GET'])
|
||||
def bind_reverse(request):
|
||||
"""
|
||||
Full export of reverse zones in BIND format
|
||||
"""
|
||||
zones_list = export_bind_reverse()
|
||||
return Response(zones_list)
|
||||
|
||||
|
@ -12,6 +12,8 @@ import time
|
||||
from django.db.models.signals import pre_delete
|
||||
from django.dispatch import receiver
|
||||
|
||||
import ipam.models
|
||||
|
||||
class Zone(CreatedUpdatedModel):
|
||||
"""
|
||||
A Zone represents a DNS zone. It contains SOA data but no records, records are represented as Record objects.
|
||||
@ -169,4 +171,44 @@ class Record(CreatedUpdatedModel):
|
||||
def on_record_delete(sender, **kwargs):
|
||||
kwargs['instance'].zone.save()
|
||||
|
||||
#
|
||||
# BIND Exports
|
||||
#
|
||||
|
||||
def export_bind_forward():
|
||||
zones = Zone.objects.all()
|
||||
|
||||
zones_list = []
|
||||
for z in zones:
|
||||
records = Record.objects.filter(zone=z)
|
||||
zones_list.append({
|
||||
'num': len(zones_list),
|
||||
'id': z.name,
|
||||
'content': z.to_bind(records)
|
||||
})
|
||||
|
||||
return zones_list
|
||||
|
||||
def export_bind_reverse():
|
||||
zones = {}
|
||||
|
||||
prefixes = ipam.models.Prefix.objects.all()
|
||||
|
||||
for p in prefixes:
|
||||
child_ip = ipam.models.IPAddress.objects.filter(address__net_contained_or_equal=str(p.prefix))
|
||||
z = p.to_bind(child_ip)
|
||||
for zz in z:
|
||||
if not zz['id'] in zones:
|
||||
zones[zz['id']] = zz['content']
|
||||
|
||||
zones_list = []
|
||||
for zid,zc in zones.items():
|
||||
zones_list.append({
|
||||
'num': len(zones_list),
|
||||
'id': zid,
|
||||
'content': zc,
|
||||
})
|
||||
|
||||
return zones_list
|
||||
|
||||
|
||||
|
@ -12,7 +12,7 @@ from utilities.views import (
|
||||
)
|
||||
|
||||
from . import filters, forms, tables
|
||||
from .models import Zone, Record
|
||||
from .models import Zone, Record, export_bind_forward, export_bind_reverse
|
||||
from .tables import RecordZoneTable
|
||||
|
||||
import StringIO, zipfile, time
|
||||
@ -194,40 +194,8 @@ def bind_export(request, zones_list, context):
|
||||
})
|
||||
|
||||
def full_forward(request):
|
||||
|
||||
zones = Zone.objects.all()
|
||||
|
||||
zones_list = []
|
||||
for z in zones:
|
||||
records = Record.objects.filter(zone=z)
|
||||
zones_list.append({
|
||||
'num': len(zones_list),
|
||||
'id': z.name,
|
||||
'content': z.to_bind(records)
|
||||
})
|
||||
|
||||
return bind_export(request, zones_list, 'forward')
|
||||
return bind_export(request, export_bind_forward(), 'forward')
|
||||
|
||||
def full_reverse(request):
|
||||
|
||||
zones = {}
|
||||
|
||||
prefixes = Prefix.objects.all()
|
||||
|
||||
for p in prefixes:
|
||||
child_ip = IPAddress.objects.filter(address__net_contained_or_equal=str(p.prefix))
|
||||
z = p.to_bind(child_ip)
|
||||
for zz in z:
|
||||
if not zz['id'] in zones:
|
||||
zones[zz['id']] = zz['content']
|
||||
|
||||
zones_list = []
|
||||
for zid,zc in zones.items():
|
||||
zones_list.append({
|
||||
'num': len(zones_list),
|
||||
'id': zid,
|
||||
'content': zc,
|
||||
})
|
||||
|
||||
return bind_export(request, zones_list, 'reverse')
|
||||
return bind_export(request, export_bind_reverse(), 'reverse')
|
||||
|
||||
|
@ -123,7 +123,7 @@ class PrefixSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = Prefix
|
||||
fields = ['id', 'family', 'prefix', 'site', 'vrf', 'vlan', 'status', 'role', 'description']
|
||||
fields = ['id', 'family', 'prefix', 'site', 'vrf', 'vlan', 'status', 'role', 'description', 'ttl', 'soa_name', 'soa_contact', 'soa_serial', 'soa_refresh', 'soa_retry', 'soa_expire', 'soa_minimum']
|
||||
|
||||
|
||||
class PrefixNestedSerializer(PrefixSerializer):
|
||||
@ -142,7 +142,7 @@ class IPAddressSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = IPAddress
|
||||
fields = ['id', 'family', 'address', 'vrf', 'hostname', 'interface', 'description', 'nat_inside', 'nat_outside']
|
||||
fields = ['id', 'family', 'address', 'vrf', 'ptr', 'interface', 'description', 'nat_inside', 'nat_outside']
|
||||
|
||||
|
||||
class IPAddressNestedSerializer(IPAddressSerializer):
|
||||
|
@ -29,9 +29,9 @@
|
||||
<a id="bind_export_select_{{ z.num }}" href="#">Select</a>
|
||||
</span>
|
||||
</div>
|
||||
<table class="table table-hover panel-body">
|
||||
<tr><td>
|
||||
<pre id="bind_export_{{ z.num }}" style="overflow: auto;">{{ z.content }}</pre>
|
||||
<table class="table table-hover panel-body" style="max-width: 100%; overflow: scroll;">
|
||||
<tr style="max-width: 100%; overflow: scroll;"><td style="max-width: 100%; overflow: scroll;">
|
||||
<pre id="bind_export_{{ z.num }}" style="overflow: auto; overflow-x: auto; overflow-y: auto; word-wrap: break-word; white-space: pre;">{{ z.content }}</pre>
|
||||
</td></tr>
|
||||
</table>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user