This repository has been archived by the owner on May 19, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api] RESTful api for Subnet and IpAddress
- Loading branch information
Showing
8 changed files
with
294 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from django.shortcuts import get_object_or_404 | ||
from rest_framework import status | ||
from rest_framework.authentication import BasicAuthentication, SessionAuthentication | ||
from rest_framework.generics import CreateAPIView, ListCreateAPIView, RetrieveUpdateDestroyAPIView | ||
from rest_framework.permissions import DjangoModelPermissions | ||
from rest_framework.response import Response | ||
|
||
from .serializers import IpAddressSerializer, IpRequestSerializer, SubnetSerializer | ||
|
||
|
||
class BaseIpAddressListCreateView(ListCreateAPIView): | ||
serializer_class = IpAddressSerializer | ||
authentication_classes = (SessionAuthentication, BasicAuthentication) | ||
permission_classes = (DjangoModelPermissions,) | ||
|
||
def get_queryset(self): | ||
subnet = get_object_or_404(self.subnet_model, pk=self.kwargs["subnet_id"]) | ||
return subnet.ipaddress_set.all() | ||
|
||
|
||
class BaseSubnetListCreateView(ListCreateAPIView): | ||
serializer_class = SubnetSerializer | ||
authentication_classes = (SessionAuthentication, BasicAuthentication) | ||
permission_classes = (DjangoModelPermissions,) | ||
|
||
|
||
class BaseSubnetView(RetrieveUpdateDestroyAPIView): | ||
serializer_class = SubnetSerializer | ||
authentication_classes = (SessionAuthentication, BasicAuthentication) | ||
permission_classes = (DjangoModelPermissions,) | ||
|
||
|
||
class BaseIpAddressView(RetrieveUpdateDestroyAPIView): | ||
serializer_class = IpAddressSerializer | ||
authentication_classes = (SessionAuthentication, BasicAuthentication) | ||
permission_classes = (DjangoModelPermissions,) | ||
|
||
|
||
class BaseRequestIPView(CreateAPIView): | ||
serializer_class = IpRequestSerializer | ||
authentication_classes = (SessionAuthentication, BasicAuthentication) | ||
permission_classes = (DjangoModelPermissions,) | ||
|
||
def post(self, request, *args, **kwargs): | ||
subnet = get_object_or_404(self.subnet_model, pk=kwargs["subnet_id"]) | ||
ip_address = subnet.request_ip(dict(description=request.data.get("description"))) | ||
if ip_address: | ||
serializer = IpAddressSerializer(ip_address) | ||
headers = self.get_success_headers(serializer.data) | ||
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) | ||
return Response(None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,26 @@ | ||
import swapper | ||
from rest_framework import serializers | ||
|
||
IpAddress = swapper.load_model("django_ipam", "IpAddress") | ||
IpAddress = swapper.load_model('django_ipam', 'IpAddress') | ||
Subnet = swapper.load_model('django_ipam', 'Subnet') | ||
|
||
|
||
class IpRequestSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = IpAddress | ||
fields = ('subnet', 'description') | ||
read_only_fields = ('created', 'modified') | ||
|
||
|
||
class IpAddressSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = IpAddress | ||
fields = ('ip_address', 'subnet', 'description') | ||
fields = '__all__' | ||
read_only_fields = ('created', 'modified') | ||
|
||
|
||
class SubnetSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Subnet | ||
fields = '__all__' | ||
read_only_fields = ('created', 'modified') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.