aboutsummaryrefslogtreecommitdiffstats
path: root/src/resource_inventory/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/resource_inventory/models.py')
-rw-r--r--src/resource_inventory/models.py90
1 files changed, 80 insertions, 10 deletions
diff --git a/src/resource_inventory/models.py b/src/resource_inventory/models.py
index 031ccce..d152698 100644
--- a/src/resource_inventory/models.py
+++ b/src/resource_inventory/models.py
@@ -105,6 +105,33 @@ class RamProfile(models.Model):
return str(self.amount) + "G for " + str(self.host)
+class Resource(models.Model):
+ class Meta:
+ abstract = True
+
+ def get_configuration(self, state):
+ """
+ Returns the desired configuration for this host as a
+ JSON object as defined in the rest api spec.
+ state is a ConfigState
+ TODO: single method, or different methods for hw, network, snapshot, etc?
+ """
+ raise NotImplementedError("Must implement in concrete Resource classes")
+
+ def reserve(self):
+ """
+ Reserves this resource for its currently
+ assigned booking.
+ """
+ raise NotImplementedError("Must implement in concrete Resource classes")
+
+ def release(self):
+ """
+ Makes this resource available again for new boookings
+ """
+ raise NotImplementedError("Must implement in concrete Resource classes")
+
+
# Generic resource templates
class GenericResourceBundle(models.Model):
id = models.AutoField(primary_key=True)
@@ -116,12 +143,12 @@ class GenericResourceBundle(models.Model):
public = models.BooleanField(default=False)
hidden = models.BooleanField(default=False)
- def getHosts(self):
- return_hosts = []
+ def getResources(self):
+ my_resources = []
for genericResource in self.generic_resources.all():
- return_hosts.append(genericResource.getHost())
+ my_resources.append(genericResource.getResource())
- return return_hosts
+ return my_resources
def __str__(self):
return self.name
@@ -137,6 +164,29 @@ class Network(models.Model):
return self.name
+class PhysicalNetwork(Resource):
+ vlan_id = models.IntegerField()
+ generic_network = models.ForeignKey(Network, on_delete=models.CASCADE)
+
+ def get_configuration(self, state):
+ """
+ Returns the network configuration
+ Collects info about each attached network interface and vlan, etc
+ """
+ return {}
+
+ def reserve(self):
+ """
+ Reserves vlan(s) associated with this network
+ """
+ # vlan_manager = self.bundle.lab.vlan_manager
+ return False
+
+ def release(self):
+ # vlan_manager = self.bundle.lab.vlan_manager
+ return False
+
+
class NetworkConnection(models.Model):
network = models.ForeignKey(Network, on_delete=models.CASCADE)
vlan_is_tagged = models.BooleanField()
@@ -147,18 +197,25 @@ class Vlan(models.Model):
vlan_id = models.IntegerField()
tagged = models.BooleanField()
public = models.BooleanField(default=False)
- network = models.ForeignKey(Network, on_delete=models.DO_NOTHING, null=True)
+ network = models.ForeignKey(PhysicalNetwork, on_delete=models.DO_NOTHING, null=True)
def __str__(self):
return str(self.vlan_id) + ("_T" if self.tagged else "")
+class ConfigState:
+ NEW = 0
+ RESET = 100
+ CLEAN = 200
+
+
class GenericResource(models.Model):
bundle = models.ForeignKey(GenericResourceBundle, related_name='generic_resources', on_delete=models.CASCADE)
hostname_validchars = RegexValidator(regex=r'(?=^.{1,253}$)(?=(^([A-Za-z0-9\-\_]{1,62}\.)*[A-Za-z0-9\-\_]{1,63}$))', message="Enter a valid hostname. Full domain name may be 1-253 characters, each hostname 1-63 characters (including suffixed dot), and valid characters for hostnames are A-Z, a-z, 0-9, hyphen (-), and underscore (_)")
name = models.CharField(max_length=200, validators=[hostname_validchars])
- def getHost(self):
+ def getResource(self):
+ # TODO: This will have to be dealt with
return self.generic_host
def __str__(self):
@@ -183,8 +240,7 @@ class GenericHost(models.Model):
# Physical, actual resources
-class ResourceBundle(models.Model):
- id = models.AutoField(primary_key=True)
+class ResourceBundle(Resource):
template = models.ForeignKey(GenericResourceBundle, on_delete=models.SET_NULL, null=True)
def __str__(self):
@@ -289,6 +345,9 @@ class Image(models.Model):
def __str__(self):
return self.name
+ def in_use(self):
+ return Host.objects.filter(booked=True, config__image=self).exists()
+
def get_sentinal_opnfv_role():
return OPNFVRole.objects.get_or_create(name="deleted", description="Role was deleted.")
@@ -336,8 +395,7 @@ def get_default_remote_info():
# Concrete host, actual machine in a lab
-class Host(models.Model):
- id = models.AutoField(primary_key=True)
+class Host(Resource):
template = models.ForeignKey(GenericHost, on_delete=models.SET_NULL, null=True)
booked = models.BooleanField(default=False)
name = models.CharField(max_length=200, unique=True)
@@ -354,6 +412,18 @@ class Host(models.Model):
def __str__(self):
return self.name
+ def get_configuration(self, state):
+ ipmi = state == ConfigState.NEW
+ power = "off" if state == ConfigState.CLEAN else "on"
+
+ return {
+ "id": self.labid,
+ "image": self.config.image.lab_id,
+ "hostname": self.template.resource.name,
+ "power": power,
+ "ipmi_create": str(ipmi)
+ }
+
class Interface(models.Model):
id = models.AutoField(primary_key=True)