2020-03-21 10:59:04 +00:00
|
|
|
from django.db import models
|
2020-10-11 20:32:08 +00:00
|
|
|
from django.db.models import JSONField, Q
|
|
|
|
from django.utils import timezone
|
2020-03-22 19:55:11 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-10-25 20:00:30 +00:00
|
|
|
from django.core.validators import MinValueValidator, MaxValueValidator
|
2020-03-21 10:59:04 +00:00
|
|
|
|
2020-10-11 20:32:08 +00:00
|
|
|
from uncloud import COUNTRIES
|
|
|
|
|
2020-03-21 10:59:04 +00:00
|
|
|
class UncloudModel(models.Model):
|
|
|
|
"""
|
|
|
|
This class extends the standard model with an
|
|
|
|
extra_data field that can be used to include public,
|
|
|
|
but internal information.
|
|
|
|
|
|
|
|
For instance if you migrate from an existing virtualisation
|
|
|
|
framework to uncloud.
|
|
|
|
|
|
|
|
The extra_data attribute should be considered a hack and whenever
|
|
|
|
data is necessary for running uncloud, it should **not** be stored
|
|
|
|
in there.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
extra_data = JSONField(editable=False, blank=True, null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2020-03-22 19:55:11 +00:00
|
|
|
|
|
|
|
# See https://docs.djangoproject.com/en/dev/ref/models/fields/#field-choices-enum-types
|
|
|
|
class UncloudStatus(models.TextChoices):
|
|
|
|
PENDING = 'PENDING', _('Pending')
|
|
|
|
AWAITING_PAYMENT = 'AWAITING_PAYMENT', _('Awaiting payment')
|
|
|
|
BEING_CREATED = 'BEING_CREATED', _('Being created')
|
|
|
|
SCHEDULED = 'SCHEDULED', _('Scheduled') # resource selected, waiting for dispatching
|
|
|
|
ACTIVE = 'ACTIVE', _('Active')
|
|
|
|
MODIFYING = 'MODIFYING', _('Modifying') # Resource is being changed
|
|
|
|
DELETED = 'DELETED', _('Deleted') # Resource has been deleted
|
|
|
|
DISABLED = 'DISABLED', _('Disabled') # Is usable, but cannot be used for new things
|
|
|
|
UNUSABLE = 'UNUSABLE', _('Unusable'), # Has some kind of error
|
2020-10-11 20:32:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
###
|
|
|
|
# General address handling
|
|
|
|
class CountryField(models.CharField):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs.setdefault('choices', COUNTRIES)
|
|
|
|
kwargs.setdefault('default', 'CH')
|
|
|
|
kwargs.setdefault('max_length', 2)
|
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def get_internal_type(self):
|
|
|
|
return "CharField"
|
|
|
|
|
|
|
|
|
|
|
|
class UncloudAddress(models.Model):
|
|
|
|
full_name = models.CharField(max_length=256)
|
|
|
|
organization = models.CharField(max_length=256, blank=True, null=True)
|
|
|
|
street = models.CharField(max_length=256)
|
|
|
|
city = models.CharField(max_length=256)
|
|
|
|
postal_code = models.CharField(max_length=64)
|
|
|
|
country = CountryField(blank=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2020-10-25 20:00:30 +00:00
|
|
|
|
|
|
|
###
|
|
|
|
# UncloudNetworks are used as identifiers - such they are a base of uncloud
|
|
|
|
|
|
|
|
class UncloudNetwork(models.Model):
|
|
|
|
"""
|
|
|
|
Storing IP networks
|
|
|
|
"""
|
|
|
|
|
|
|
|
network_address = models.GenericIPAddressField(null=False, unique=True)
|
|
|
|
network_mask = models.IntegerField(null=False,
|
|
|
|
validators=[MinValueValidator(0),
|
|
|
|
MaxValueValidator(128)]
|
|
|
|
)
|
|
|
|
|
|
|
|
description = models.CharField(max_length=256)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def populate_db_defaults(cls):
|
|
|
|
for net, desc in [
|
|
|
|
( "2a0a:e5c0:11::", "uncloud Billing" ),
|
|
|
|
( "2a0a:e5c0:11:1::", "uncloud Referral" ),
|
|
|
|
( "2a0a:e5c0:11:2::", "uncloud Coupon" )
|
|
|
|
]:
|
|
|
|
obj, created = cls.objects.get_or_create(network_address=net,
|
|
|
|
defaults= {
|
|
|
|
'network_mask': 64,
|
|
|
|
'description': desc
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if not ':' in self.network_address and self.network_mask > 32:
|
|
|
|
raise FieldError("Mask cannot exceed 32 for IPv4")
|
|
|
|
|
|
|
|
super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"{self.network_address}/{self.network_mask} {self.description}"
|
|
|
|
|
2020-10-11 20:32:08 +00:00
|
|
|
###
|
|
|
|
# Who is running / providing this instance of uncloud?
|
|
|
|
|
|
|
|
class UncloudProvider(UncloudAddress):
|
|
|
|
"""
|
|
|
|
A class resembling who is running this uncloud instance.
|
|
|
|
This might change over time so we allow starting/ending dates
|
|
|
|
|
|
|
|
This also defines the taxation rules.
|
|
|
|
|
|
|
|
starting/ending date define from when to when this is valid. This way
|
|
|
|
we can model address changes and have it correct in the bills.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Meta:
|
|
|
|
# FIXMe: only allow non overlapping time frames -- how to define this as a constraint?
|
|
|
|
starting_date = models.DateField()
|
|
|
|
ending_date = models.DateField(blank=True, null=True)
|
|
|
|
|
|
|
|
billing_network = models.ForeignKey(UncloudNetwork, related_name="uncloudproviderbill", on_delete=models.CASCADE)
|
|
|
|
referral_network = models.ForeignKey(UncloudNetwork, related_name="uncloudproviderreferral", on_delete=models.CASCADE)
|
2020-10-25 12:52:36 +00:00
|
|
|
coupon_network = models.ForeignKey(UncloudNetwork, related_name="uncloudprovidercoupon", on_delete=models.CASCADE)
|
2020-10-11 20:32:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_provider(cls, when=None):
|
|
|
|
"""
|
|
|
|
Find active provide at a certain time - if there was any
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not when:
|
|
|
|
when = timezone.now()
|
|
|
|
|
|
|
|
|
|
|
|
return cls.objects.get(Q(starting_date__gte=when, ending_date__lte=when) |
|
|
|
|
Q(starting_date__gte=when, ending_date__isnull=True))
|
|
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def populate_db_defaults(cls):
|
2020-10-11 20:36:01 +00:00
|
|
|
obj, created = cls.objects.get_or_create(full_name="ungleich glarus ag",
|
|
|
|
street="Bahnhofstrasse 1",
|
|
|
|
postal_code="8783",
|
|
|
|
city="Linthal",
|
|
|
|
country="CH",
|
2020-10-11 20:32:08 +00:00
|
|
|
starting_date=timezone.now(),
|
|
|
|
billing_network=UncloudNetwork.objects.get(description="uncloud Billing"),
|
2020-10-25 12:52:36 +00:00
|
|
|
referral_network=UncloudNetwork.objects.get(description="uncloud Referral"),
|
|
|
|
coupon_network=UncloudNetwork.objects.get(description="uncloud Coupon")
|
2020-10-11 20:32:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
2020-10-11 20:36:01 +00:00
|
|
|
return f"{self.full_name} {self.country}"
|