From 913e992a4809f72806093a96638782c04a5e157f Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Mon, 6 Apr 2020 22:30:01 +0200 Subject: [PATCH] [vpn] fix urls --- uncloud_django_based/uncloud/uncloud/urls.py | 4 ++- .../uncloud/uncloud_net/models.py | 26 ++++++++++--------- .../uncloud/uncloud_net/serializers.py | 9 +++++-- .../uncloud/uncloud_net/views.py | 14 +++++++--- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/uncloud_django_based/uncloud/uncloud/urls.py b/uncloud_django_based/uncloud/uncloud/urls.py index d7550db..54f4d36 100644 --- a/uncloud_django_based/uncloud/uncloud/urls.py +++ b/uncloud_django_based/uncloud/uncloud/urls.py @@ -46,7 +46,7 @@ router.register(r'vm/vm', vmviews.VMProductViewSet, basename='vmproduct') router.register(r'service/matrix', serviceviews.MatrixServiceProductViewSet, basename='matrixserviceproduct') # Net -router.register(r'net/vpn', netviews.VPNProductViewSet, basename='vpnproduct') +router.register(r'net/vpn', netviews.VPNNetworkViewSet, basename='vpnnet') # Pay @@ -63,6 +63,8 @@ router.register(r'admin/payment', payviews.AdminPaymentViewSet, basename='admin/ router.register(r'admin/order', payviews.AdminOrderViewSet, basename='admin/order') router.register(r'admin/vmhost', vmviews.VMHostViewSet) router.register(r'admin/vmcluster', vmviews.VMClusterViewSet) +router.register(r'admin/vpnpool', netviews.VPNPoolViewSet) + router.register(r'admin/opennebula', oneviews.VMViewSet, basename='opennebula') # User/Account diff --git a/uncloud_django_based/uncloud/uncloud_net/models.py b/uncloud_django_based/uncloud/uncloud_net/models.py index 934eeb5..d811902 100644 --- a/uncloud_django_based/uncloud/uncloud_net/models.py +++ b/uncloud_django_based/uncloud/uncloud_net/models.py @@ -6,31 +6,33 @@ from django.core.validators import MinValueValidator, MaxValueValidator from uncloud_pay.models import Product, RecurringPeriod from uncloud.models import UncloudModel, UncloudStatus + +class MACAdress(models.Model): + default_prefix = 0x420000000000 + class VPNPool(UncloudModel): """ Network address pools from which VPNs can be created """ - network = models.GenericIPAddressField(primary_key=True, - editable=False) + network = models.GenericIPAddressField(primary_key=True) network_size = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(128)]) -class VPNNetwork(UncloudModel): + vpn_hostname = models.CharField(max_length=256) + + wireguard_private_key = models.CharField(max_length=48) + + +class VPNNetwork(Product): """ A selected network. Used for tracking reservations / used networks """ vpnpool = models.ForeignKey(VPNPool, on_delete=models.CASCADE) - network = models.GenericIPAddressField(primary_key=True, - editable=False) + network = models.GenericIPAddressField(editable=False, + unique=True) -class VPNProduct(Product): - network = models.ForeignKey(VPNPool, - on_delete=models.CASCADE) - - -class MACAdress(models.Model): - default_prefix = 0x420000000000 + wireguard_public_key = models.CharField(max_length=48) diff --git a/uncloud_django_based/uncloud/uncloud_net/serializers.py b/uncloud_django_based/uncloud/uncloud_net/serializers.py index 856688b..7f3ab8e 100644 --- a/uncloud_django_based/uncloud/uncloud_net/serializers.py +++ b/uncloud_django_based/uncloud/uncloud_net/serializers.py @@ -3,11 +3,16 @@ from rest_framework import serializers from .models import * -class VPNProductSerializer(serializers.ModelSerializer): +class VPNPoolSerializer(serializers.ModelSerializer): + class Meta: + model = VPNPool + fields = '__all__' + +class VPNNetworkSerializer(serializers.ModelSerializer): network_size = serializers.IntegerField(min_value=0, max_value=128) class Meta: - model = VPNProduct + model = VPNNetwork fields = '__all__' diff --git a/uncloud_django_based/uncloud/uncloud_net/views.py b/uncloud_django_based/uncloud/uncloud_net/views.py index f22da2f..7afc99d 100644 --- a/uncloud_django_based/uncloud/uncloud_net/views.py +++ b/uncloud_django_based/uncloud/uncloud_net/views.py @@ -6,14 +6,20 @@ from .models import * from .serializers import * -class VPNProductViewSet(viewsets.ModelViewSet): - serializer_class = VPNProductSerializer +class VPNPoolViewSet(viewsets.ModelViewSet): + serializer_class = VPNPoolSerializer + permission_classes = [permissions.IsAdminUser] + queryset = VPNPool.objects.all() + + +class VPNNetworkViewSet(viewsets.ModelViewSet): + serializer_class = VPNNetworkSerializer permission_classes = [permissions.IsAdminUser] def get_queryset(self): if self.request.user.is_superuser: - obj = VPNProduct.objects.all() + obj = VPNNetwork.objects.all() else: - obj = VPNProduct.objects.filter(owner=self.request.user) + obj = VPNNetwork.objects.filter(owner=self.request.user) return obj