[reverseDNS] add basic logic

This commit is contained in:
Nico Schottelius 2020-10-25 22:43:34 +01:00
parent 20c7c86703
commit ecc9e6f734
4 changed files with 53 additions and 5 deletions

View file

@ -236,7 +236,8 @@ VPNNetworks can be managed by all authenticated users.
** Milestones :uncloud: ** Milestones :uncloud:
*** 1.1 (cleanup 1) *** 1.1 (cleanup 1)
**** **** TODO Unify ValidationError, FieldError - define proper Exception
- What do we use for model errors
*** 1.0 (initial release) *** 1.0 (initial release)
**** TODO Initial Generic product support **** TODO Initial Generic product support
- Product - Product

View file

@ -1,5 +1,6 @@
from django.contrib import admin from django.contrib import admin
from .models import ReverseDNSEntry
# for m in [ UncloudNetwork ]: for m in [ ReverseDNSEntry ]:
# admin.site.register(m) admin.site.register(m)

View file

@ -4,8 +4,9 @@ import ipaddress
from django.db import models from django.db import models
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.core.validators import MinValueValidator, MaxValueValidator 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): class MACAdress(models.Model):
default_prefix = 0x420000000000 default_prefix = 0x420000000000
@ -194,3 +195,30 @@ class ReverseDNSEntry(models.Model):
ip_address = models.GenericIPAddressField(null=False, unique=True) ip_address = models.GenericIPAddressField(null=False, unique=True)
name = models.CharField(max_length=253, null=False) 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}"

View file

@ -454,6 +454,16 @@ class Product(models.Model):
obj.recurring_periods.add(recurring_period, through_defaults= { 'is_default': True }) 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): def __str__(self):
return f"{self.name} - {self.description}" return f"{self.name} - {self.description}"
@ -968,6 +978,13 @@ class Order(models.Model):
return (one_time_price, recurring_price, config) 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): def save(self, *args, **kwargs):
# Calculate the price of the order when we create it # Calculate the price of the order when we create it
# IMMUTABLE fields -- need to create new order to modify them # IMMUTABLE fields -- need to create new order to modify them
@ -983,11 +1000,12 @@ class Order(models.Model):
except ObjectDoesNotExist: except ObjectDoesNotExist:
raise ValidationError(f"Recurring Period {self.recurring_period} not allowed for product {self.product}") 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: if self.ending_date and self.ending_date < self.starting_date:
raise ValidationError("End date cannot be before starting date") raise ValidationError("End date cannot be before starting date")
super().save(*args, **kwargs) super().save(*args, **kwargs)