1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
11 from django.contrib.auth.models import User
12 from django.db import models
17 class LabStatus(object):
23 def upload_to(object, filename):
24 return object.user.username + '/' + filename
26 class UserProfile(models.Model):
27 user = models.OneToOneField(User, on_delete=models.CASCADE)
28 timezone = models.CharField(max_length=100, blank=False, default='UTC')
29 ssh_public_key = models.FileField(upload_to=upload_to, null=True, blank=True)
30 pgp_public_key = models.FileField(upload_to=upload_to, null=True, blank=True)
31 email_addr = models.CharField(max_length=300, blank=False, default='email@mail.com')
32 company = models.CharField(max_length=200, blank=False)
34 oauth_token = models.CharField(max_length=1024, blank=False)
35 oauth_secret = models.CharField(max_length=1024, blank=False)
37 jira_url = models.CharField(max_length=100, default='')
38 full_name = models.CharField(max_length=100, default='')
39 booking_privledge = models.BooleanField(default=False)
42 db_table = 'user_profile'
45 return self.user.username
48 class VlanManager(models.Model):
49 # list of length 4096 containing either 0 (not available) or 1 (available)
50 vlans = models.TextField()
51 block_size = models.IntegerField()
52 allow_overlapping = models.BooleanField()
53 # list of length 4096 containing either 0 (not rexerved) or 1 (reserved)
54 reserved_vlans = models.TextField()
56 def get_vlan(self, count=1):
58 vlans = json.loads(self.vlans)
59 for i in range(count):
60 new_vlan = vlans.index(1) # will throw if none available
62 allocated.append(new_vlan)
67 def get_public_vlan(self):
68 return PublicNetwork.objects.filter(lab=self.lab_set.first(), in_use=False).first()
70 def reserve_public_vlan(self, vlan):
71 net = PublicNetwork.objects.get(lab=self.lab_set.first(), vlan=vlan, in_use=False)
75 def release_public_vlan(self, vlan):
76 net = PublicNetwork.objects.get(lab=self.lab_set.first(), vlan=vlan, in_use=True)
80 def public_vlan_is_available(self, vlan):
81 net = PublicNetwork.objects.get(lab=self.lab_set.first(), vlan=vlan)
85 def is_available(self, vlans):
87 'vlans' is either a single vlan id integer or a list of integers
88 will return true (available) or false
90 if self.allow_overlapping:
93 reserved = json.loads(self.reserved_vlans)
94 vlan_master_list = json.loads(self.vlans)
101 if not vlan_master_list[vlan] or reserved[vlan]:
105 def release_vlans(self, vlans):
107 'vlans' is either a single vlan id integer or a list of integers
108 will make the vlans available
109 doesnt return a value
111 my_vlans = json.loads(self.vlans)
120 self.vlans = json.dumps(my_vlans)
123 def reserve_vlans(self, vlans):
124 my_vlans = json.loads(self.vlans)
134 if my_vlans[vlan] is 0:
135 raise ValueError("vlan " + str(vlan) + " is not available")
138 self.vlans = json.dumps(my_vlans)
143 class Lab(models.Model):
144 lab_user = models.OneToOneField(User, on_delete=models.CASCADE)
145 name = models.CharField(max_length=200, primary_key=True, unique=True, null=False, blank=False)
146 contact_email = models.EmailField(max_length=200, null=True, blank=True)
147 contact_phone = models.CharField(max_length=20, null=True, blank=True)
148 status = models.IntegerField(default=LabStatus.UP)
149 vlan_manager = models.ForeignKey(VlanManager, on_delete=models.CASCADE, null=True)
150 location = models.TextField(default="unknown")
151 api_token = models.CharField(max_length=50)
152 description = models.CharField(max_length=240)
155 def make_api_token():
156 alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
159 key += random.choice(alphabet)
167 class PublicNetwork(models.Model):
168 vlan = models.IntegerField()
169 lab = models.ForeignKey(Lab, on_delete=models.CASCADE)
170 in_use = models.BooleanField(default=False)
171 cidr = models.CharField(max_length=50, default="0.0.0.0/0")
172 gateway = models.CharField(max_length=50, default="0.0.0.0")