From ecc9e6f73432efa6ca2309423f61571b9767808d Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sun, 25 Oct 2020 22:43:34 +0100 Subject: [PATCH] [reverseDNS] add basic logic --- doc/uncloud-manual-2020-08-01.org | 3 ++- uncloud_net/admin.py | 5 +++-- uncloud_net/models.py | 30 +++++++++++++++++++++++++++++- uncloud_pay/models.py | 20 +++++++++++++++++++- 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/doc/uncloud-manual-2020-08-01.org b/doc/uncloud-manual-2020-08-01.org index 78dd900..6dd8fb2 100644 --- a/doc/uncloud-manual-2020-08-01.org +++ b/doc/uncloud-manual-2020-08-01.org @@ -236,7 +236,8 @@ VPNNetworks can be managed by all authenticated users. ** Milestones :uncloud: *** 1.1 (cleanup 1) -**** +**** TODO Unify ValidationError, FieldError - define proper Exception + - What do we use for model errors *** 1.0 (initial release) **** TODO Initial Generic product support - Product diff --git a/uncloud_net/admin.py b/uncloud_net/admin.py index dec357a..5dad27b 100644 --- a/uncloud_net/admin.py +++ b/uncloud_net/admin.py @@ -1,5 +1,6 @@ from django.contrib import admin +from .models import ReverseDNSEntry -# for m in [ UncloudNetwork ]: -# admin.site.register(m) +for m in [ ReverseDNSEntry ]: + admin.site.register(m) diff --git a/uncloud_net/models.py b/uncloud_net/models.py index afd5004..c9c8bc3 100644 --- a/uncloud_net/models.py +++ b/uncloud_net/models.py @@ -4,8 +4,9 @@ import ipaddress from django.db import models from django.contrib.auth import get_user_model from django.core.validators import MinValueValidator, MaxValueValidator -from django.core.exceptions import FieldError +from django.core.exceptions import FieldError, ValidationError +from uncloud_pay.models import Order class MACAdress(models.Model): default_prefix = 0x420000000000 @@ -194,3 +195,30 @@ class ReverseDNSEntry(models.Model): ip_address = models.GenericIPAddressField(null=False, unique=True) name = models.CharField(max_length=253, null=False) + + def save(self, *args, **kwargs): + # Product.objects.filter(config__parameters__contains='reverse_dns_network') + # FIXME: check if order is still active / not replaced + + allowed = False + + for order in Order.objects.filter(config__parameters__reverse_dns_network__isnull=False, + owner=self.owner): + network = order.config['parameters']['reverse_dns_network'] + + net = ipaddress.ip_network(network) + addr = ipaddress.ip_address(self.ip_address) + + if addr in net: + allowed = True + break + + + if not allowed: + raise ValidationError(f"User {self.owner} does not have the right to create reverse DNS entry for {self.ip_address}") + + super().save(*args, **kwargs) + + + def __str__(self): + return f"{self.ip_address} - {self.name}" diff --git a/uncloud_pay/models.py b/uncloud_pay/models.py index 00163b6..353ab94 100644 --- a/uncloud_pay/models.py +++ b/uncloud_pay/models.py @@ -454,6 +454,16 @@ class Product(models.Model): obj.recurring_periods.add(recurring_period, through_defaults= { 'is_default': True }) + obj, created = cls.objects.get_or_create(name="reverse DNS", + description="Reverse DNS network", + currency=Currency.CHF, + config={ + 'parameters': [ + 'network' + ] + }) + obj.recurring_periods.add(recurring_period, through_defaults= { 'is_default': True }) + def __str__(self): return f"{self.name} - {self.description}" @@ -968,6 +978,13 @@ class Order(models.Model): return (one_time_price, recurring_price, config) + def check_parameters(self): + if 'parameters' in self.product.config: + for parameter in self.product.config['parameters']: + if not parameter in self.config['parameters']: + raise ValidationError(f"Required parameter '{parameter}' is missing.") + + def save(self, *args, **kwargs): # Calculate the price of the order when we create it # IMMUTABLE fields -- need to create new order to modify them @@ -983,11 +1000,12 @@ class Order(models.Model): except ObjectDoesNotExist: raise ValidationError(f"Recurring Period {self.recurring_period} not allowed for product {self.product}") + self.check_parameters() + if self.ending_date and self.ending_date < self.starting_date: raise ValidationError("End date cannot be before starting date") - super().save(*args, **kwargs)