diff options
Diffstat (limited to 'src/resource_inventory/resource_manager.py')
-rw-r--r-- | src/resource_inventory/resource_manager.py | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/src/resource_inventory/resource_manager.py b/src/resource_inventory/resource_manager.py index 9406977..52af824 100644 --- a/src/resource_inventory/resource_manager.py +++ b/src/resource_inventory/resource_manager.py @@ -6,20 +6,29 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## + +from __future__ import annotations # noqa: F407 + import re +from typing import Optional from django.db.models import Q from dashboard.exceptions import ResourceAvailabilityException from resource_inventory.models import ( + Resource, ResourceBundle, ResourceTemplate, + ResourceConfiguration, Network, Vlan, PhysicalNetwork, InterfaceConfiguration, ) +from account.models import Lab +from django.contrib.auth.models import User + class ResourceManager: @@ -29,19 +38,19 @@ class ResourceManager: pass @staticmethod - def getInstance(): + def getInstance() -> ResourceManager: if ResourceManager.instance is None: ResourceManager.instance = ResourceManager() return ResourceManager.instance - def getAvailableResourceTemplates(self, lab, user=None): + def getAvailableResourceTemplates(self, lab: Lab, user: Optional[User] = None) -> list[ResourceTemplate]: filter = Q(public=True) if user: filter = filter | Q(owner=user) filter = filter & Q(temporary=False) & Q(lab=lab) return ResourceTemplate.objects.filter(filter) - def templateIsReservable(self, resource_template): + def templateIsReservable(self, resource_template: ResourceTemplate): """ Check if the required resources to reserve this template is available. @@ -63,28 +72,32 @@ class ResourceManager: return True # public interface - def deleteResourceBundle(self, resourceBundle): + def deleteResourceBundle(self, resourceBundle: ResourceBundle): raise NotImplementedError("Resource Bundle Deletion Not Implemented") - def releaseResourceBundle(self, resourceBundle): + def releaseResourceBundle(self, resourceBundle: ResourceBundle): resourceBundle.release() - def get_vlans(self, resourceTemplate): + def get_vlans(self, resourceTemplate: ResourceTemplate) -> dict[str, int]: + """ + returns: dict from network name to the associated vlan number (backend vlan id) + """ networks = {} vlan_manager = resourceTemplate.lab.vlan_manager for network in resourceTemplate.networks.all(): if network.is_public: - public_net = vlan_manager.get_public_vlan() + # already throws if can't get requested count, so can always expect public_net to be Some + public_net = vlan_manager.get_public_vlan(within=resourceTemplate.public_vlan_pool_set()) vlan_manager.reserve_public_vlan(public_net.vlan) networks[network.name] = public_net.vlan else: # already throws if can't get requested count, so can always index in @ 0 - vlans = vlan_manager.get_vlans(count=1) + vlans = vlan_manager.get_vlans(count=1, within=resourceTemplate.private_vlan_pool_set()) vlan_manager.reserve_vlans(vlans[0]) networks[network.name] = vlans[0] return networks - def instantiateTemplate(self, resource_template): + def instantiateTemplate(self, resource_template: ResourceTemplate): """ Convert a ResourceTemplate into a ResourceBundle. @@ -113,16 +126,18 @@ class ResourceManager: return resource_bundle - def configureNetworking(self, resource_bundle, resource, vlan_map): + def configureNetworking(self, resource_bundle: ResourceBundle, resource: Resource, vlan_map: dict[str, int]): + """ + @vlan_map: dict from network name to the associated vlan number (backend vlan id) + """ for physical_interface in resource.interfaces.all(): - # assign interface configs - iface_configs = InterfaceConfiguration.objects.filter( + # assign interface configs + iface_config = InterfaceConfiguration.objects.get( profile=physical_interface.profile, resource_config=resource.config ) - iface_config = iface_configs.first() physical_interface.acts_as = iface_config physical_interface.acts_as.save() @@ -143,7 +158,7 @@ class ResourceManager: ) # private interface - def acquireHost(self, resource_config): + def acquireHost(self, resource_config: ResourceConfiguration) -> Resource: resources = resource_config.profile.get_resources( lab=resource_config.template.lab, unreserved=True |