Merge "Update actions to free hosts and vlans"
authorSawyer Bergeron <sbergeron@iol.unh.edu>
Tue, 30 Mar 2021 17:54:21 +0000 (17:54 +0000)
committerGerrit Code Review <gerrit@opnfv.org>
Tue, 30 Mar 2021 17:54:21 +0000 (17:54 +0000)
1  2 
src/dashboard/admin_utils.py

@@@ -306,13 -223,22 +309,28 @@@ def force_release_booking(booking_id: i
          task.save()
  
  
+ def free_leaked_public_vlans(safety_buffer_days=2):
+     for lab in Lab.objects.all():
+         current_booking_set = Booking.objects.filter(end__gte=timezone.now() + timedelta(days=safety_buffer_days))
+         marked_nets = set()
+         for booking in current_booking_set:
+             for network in get_network_metadata(booking.id):
+                 marked_nets.add(network["vlan_id"])
+         for net in PublicNetwork.objects.filter(lab=lab).filter(in_use=True):
+             if net.vlan not in marked_nets:
+                 lab.vlan_manager.release_public_vlan(net.vlan)
  def get_network_metadata(booking_id: int):
 +    """
 +    Takes a booking id and prints all (known) networks that are owned by it.
 +    Returns an object of the form {<network name>: {"vlan_id": int, "netname": str <network name>, "public": bool <whether network is public/routable}}
 +
 +    @booking_id: the id of the Booking object to be queried
 +    """
      booking = Booking.objects.get(id=booking_id)
      bundle = booking.resource
      pnets = PhysicalNetwork.objects.filter(bundle=bundle).all()