Fix vlan deallocation and ownership 92/70692/2
authorSawyer Bergeron <sbergeron@iol.unh.edu>
Fri, 7 Aug 2020 18:49:11 +0000 (14:49 -0400)
committerSawyer Bergeron <sbergeron@iol.unh.edu>
Fri, 7 Aug 2020 19:11:53 +0000 (15:11 -0400)
Change-Id: I74bd2d062611ba1ca997cfb50a91d2396e70526b
Signed-off-by: Sawyer Bergeron <sbergeron@iol.unh.edu>
src/dashboard/tasks.py
src/resource_inventory/models.py
src/resource_inventory/resource_manager.py

index b980799..50e64c8 100644 (file)
@@ -14,7 +14,6 @@ from django.utils import timezone
 from booking.models import Booking
 from notifier.manager import NotificationHandler
 from api.models import Job, JobStatus, SoftwareRelation, HostHardwareRelation, HostNetworkRelation, AccessRelation
-from resource_inventory.resource_manager import ResourceManager
 from resource_inventory.models import ConfigState
 
 
@@ -75,4 +74,4 @@ def free_hosts():
         resource__isnull=False
     )
     for booking in bookings:
-        ResourceManager.getInstance().deleteResourceBundle(booking.resource)
+        booking.resource.release()
index 7a1f259..c918513 100644 (file)
@@ -199,6 +199,13 @@ class ResourceBundle(models.Model):
         # TODO
         pass
 
+    def release(self):
+        for pn in PhysicalNetwork.objects.filter(bundle=self).all():
+            pn.release()
+
+        for resource in self.get_resources():
+            resource.release()
+
     def get_template_name(self):
         if not self.template:
             return ""
@@ -401,6 +408,7 @@ class Network(models.Model):
 class PhysicalNetwork(models.Model):
     vlan_id = models.IntegerField()
     generic_network = models.ForeignKey(Network, on_delete=models.CASCADE)
+    bundle = models.ForeignKey(ResourceBundle, null=True, on_delete=models.CASCADE)
 
     def get_configuration(self, state):
         """
@@ -412,11 +420,19 @@ class PhysicalNetwork(models.Model):
 
     def reserve(self):
         """Reserve 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
+        from booking.models import Booking
+
+        booking = Booking.objects.get(resource=self.bundle)
+        lab = booking.lab
+        vlan_manager = lab.vlan_manager
+
+        if self.generic_network.is_public:
+            vlan_manager.release_public_vlan(self.vlan_id)
+        else:
+            vlan_manager.release_vlans([self.vlan_id])
         return False
 
     def __str__(self):
index 9300040..81f4747 100644 (file)
@@ -102,7 +102,7 @@ class ResourceManager:
                 phys_res.config = config
                 resources.append(phys_res)
 
-                self.configureNetworking(phys_res, vlan_map)
+                self.configureNetworking(resource_bundle, phys_res, vlan_map)
                 phys_res.save()
 
             except Exception as e:
@@ -111,7 +111,7 @@ class ResourceManager:
 
         return resource_bundle
 
-    def configureNetworking(self, resource, vlan_map):
+    def configureNetworking(self, resource_bundle, resource, vlan_map):
         for physical_interface in resource.interfaces.all():
             # assign interface configs
 
@@ -128,7 +128,8 @@ class ResourceManager:
             for connection in iface_config.connections.all():
                 physicalNetwork = PhysicalNetwork.objects.create(
                     vlan_id=vlan_map[connection.network.name],
-                    generic_network=connection.network
+                    generic_network=connection.network,
+                    bundle=resource_bundle,
                 )
                 physical_interface.config.add(
                     Vlan.objects.create(