aboutsummaryrefslogtreecommitdiffstats
path: root/src/account/models.py
diff options
context:
space:
mode:
authorJustin Choquette <jchoquette@iol.unh.edu>2023-06-08 12:46:53 -0400
committerJustin Choquette <jchoquette@iol.unh.edu>2023-07-21 13:17:51 -0400
commita09db9f287a02873c0226759f8ea444bb304cd59 (patch)
tree59e744e4b998973a808abbae2d21fbdd6201d829 /src/account/models.py
parent8ddc7e820e120f1dde4e901d3cb6f1dd3f281e65 (diff)
LaaS 3.0 Almost MVP
Change-Id: Ided9a43cf3088bb58a233dc459711c03f43e11b8 Signed-off-by: Justin Choquette <jchoquette@iol.unh.edu>
Diffstat (limited to 'src/account/models.py')
-rw-r--r--src/account/models.py178
1 files changed, 0 insertions, 178 deletions
diff --git a/src/account/models.py b/src/account/models.py
index 32229b1..f1deca7 100644
--- a/src/account/models.py
+++ b/src/account/models.py
@@ -16,9 +16,6 @@ import random
from collections import Counter
-from dashboard.exceptions import ResourceAvailabilityException
-
-
class LabStatus(object):
"""
A Poor man's enum for the status of a lab.
@@ -63,163 +60,6 @@ class UserProfile(models.Model):
def __str__(self):
return self.user.username
-
-class VlanManager(models.Model):
- """
- Keeps track of the vlans for a lab.
-
- Vlans are represented as indexes into a 4096 element list.
- This list is serialized to JSON for storing in the DB.
- """
-
- # list of length 4096 containing either 0 (not available) or 1 (available)
- vlans = models.TextField()
- # list of length 4096 containing either 0 (not reserved) or 1 (reserved)
- reserved_vlans = models.TextField()
-
- block_size = models.IntegerField()
-
- # True if the lab allows two different users to have the same private vlans
- # if they use QinQ or a vxlan overlay, for example
- allow_overlapping = models.BooleanField()
-
- def get_vlans(self, count=1, within=None):
- """
- Return the IDs of available vlans as a list[int], but does not reserve them.
-
- Will throw index exception if not enough vlans are available.
- Always returns a list of ints
-
- If `within` is not none, will filter against that as a set, requiring that any vlans returned are within that set
- """
- allocated = []
- vlans = json.loads(self.vlans)
- reserved = json.loads(self.reserved_vlans)
-
- for i in range(0, len(vlans) - 1):
- if len(allocated) >= count:
- break
-
- if vlans[i] == 0 and self.allow_overlapping is False:
- continue
-
- if reserved[i] == 1:
- continue
-
- # vlan is available and not reserved, so safe to add
- if within is not None:
- if i in within:
- allocated.append(i)
- else:
- allocated.append(i)
- continue
-
- if len(allocated) != count:
- raise ResourceAvailabilityException("There were not enough available private vlans for the allocation. Please contact the administrators.")
-
- return allocated
-
- def get_public_vlan(self, within=None):
- """Return reference to an available public network without reserving it."""
- r = PublicNetwork.objects.filter(lab=self.lab_set.first(), in_use=False)
- if within is not None:
- r = r.filter(vlan__in=within)
-
- if r.count() < 1:
- raise ResourceAvailabilityException("There were not enough available public vlans for the allocation. Please contact the administrators.")
-
- return r.first()
-
- def reserve_public_vlan(self, vlan):
- """Reserves the Public Network that has the given vlan."""
- net = PublicNetwork.objects.get(lab=self.lab_set.first(), vlan=vlan, in_use=False)
- net.in_use = True
- net.save()
-
- def release_public_vlan(self, vlan):
- """Un-reserves a public network with the given vlan."""
- net = PublicNetwork.objects.get(lab=self.lab_set.first(), vlan=vlan, in_use=True)
- net.in_use = False
- net.save()
-
- def public_vlan_is_available(self, vlan):
- """
- Whether the public vlan is available.
-
- returns true if the network with the given vlan is free to use,
- False otherwise
- """
- net = PublicNetwork.objects.get(lab=self.lab_set.first(), vlan=vlan)
- return not net.in_use
-
- def is_available(self, vlans):
- """
- If the vlans are available.
-
- 'vlans' is either a single vlan id integer or a list of integers
- will return true (available) or false
- """
- if self.allow_overlapping:
- return True
-
- reserved = json.loads(self.reserved_vlans)
- vlan_master_list = json.loads(self.vlans)
- try:
- iter(vlans)
- except Exception:
- vlans = [vlans]
-
- for vlan in vlans:
- if not vlan_master_list[vlan] or reserved[vlan]:
- return False
- return True
-
- def release_vlans(self, vlans):
- """
- Make the vlans available for another booking.
-
- 'vlans' is either a single vlan id integer or a list of integers
- will make the vlans available
- doesnt return a value
- """
- my_vlans = json.loads(self.vlans)
-
- try:
- iter(vlans)
- except Exception:
- vlans = [vlans]
-
- for vlan in vlans:
- my_vlans[vlan] = 1
- self.vlans = json.dumps(my_vlans)
- self.save()
-
- def reserve_vlans(self, vlans):
- """
- Reserves all given vlans or throws a ValueError.
-
- vlans can be an integer or a list of integers.
- """
- my_vlans = json.loads(self.vlans)
-
- reserved = json.loads(self.reserved_vlans)
-
- try:
- iter(vlans)
- except Exception:
- vlans = [vlans]
-
- vlans = set(vlans)
-
- for vlan in vlans:
- if my_vlans[vlan] == 0 or reserved[vlan] == 1:
- raise ValueError("vlan " + str(vlan) + " is not available")
-
- my_vlans[vlan] = 0
- self.vlans = json.dumps(my_vlans)
- self.save()
-
-
class Lab(models.Model):
"""
Model representing a Hosting Lab.
@@ -233,7 +73,6 @@ class Lab(models.Model):
contact_email = models.EmailField(max_length=200, null=True, blank=True)
contact_phone = models.CharField(max_length=20, null=True, blank=True)
status = models.IntegerField(default=LabStatus.UP)
- vlan_manager = models.ForeignKey(VlanManager, on_delete=models.CASCADE, null=True)
location = models.TextField(default="unknown")
# This token must apear in API requests from this lab
api_token = models.CharField(max_length=50)
@@ -250,26 +89,9 @@ class Lab(models.Model):
key += random.choice(alphabet)
return key
- def get_available_resources(self):
- # Cannot import model normally due to ciruclar import
- Server = apps.get_model('resource_inventory', 'Server') # TODO: Find way to import ResourceQuery
- resources = [str(resource.profile) for resource in Server.objects.filter(lab=self, working=True, booked=False)]
- return dict(Counter(resources))
-
def __str__(self):
return self.name
-
-class PublicNetwork(models.Model):
- """L2/L3 network that can reach the internet."""
-
- vlan = models.IntegerField()
- lab = models.ForeignKey(Lab, on_delete=models.CASCADE)
- in_use = models.BooleanField(default=False)
- cidr = models.CharField(max_length=50, default="0.0.0.0/0")
- gateway = models.CharField(max_length=50, default="0.0.0.0")
-
-
class Downtime(models.Model):
"""
A Downtime event.