-# model validity exceptions
-class IncompatibleInstallerForOS(Exception):
- pass
-
-
-class IncompatibleScenarioForInstaller(Exception):
- pass
-
-
-class IncompatibleImageForHost(Exception):
- pass
-
-
-class ImageOwnershipInvalid(Exception):
- pass
-
-
-class ImageNotAvailableAtLab(Exception):
- pass
-
-
-class LabDNE(Exception):
- pass
-
-
-class HostProfileDNE(Exception):
- pass
-
-
-class HostNotAvailable(Exception):
- pass
-
-
-class NoLabSelectedError(Exception):
- pass
-
-
-class OPNFVRoleDNE(Exception):
- pass
-
-
-class NoRemainingPublicNetwork(Exception):
- pass
-
-
-class BookingPermissionException(Exception):
- pass
-
-
-def parse_host_field(host_field_contents):
- host_json = json.loads(host_field_contents)
- lab_dict = host_json['labs'][0]
- lab_id = list(lab_dict.keys())[0]
- lab_user_id = int(lab_id.split("_")[-1])
- lab = Lab.objects.get(lab_user__id=lab_user_id)
-
- host_dict = host_json['hosts'][0]
- profile_id = list(host_dict.keys())[0]
- profile_id = int(profile_id.split("_")[-1])
- profile = HostProfile.objects.get(id=profile_id)
-
- # check validity of field data before trying to apply to models
- if len(host_json['labs']) != 1:
- raise NoLabSelectedError("No lab was selected")
- if not lab:
- raise LabDNE("Lab with provided ID does not exist")
- if not profile:
- raise HostProfileDNE("Host type with provided ID does not exist")
-
- return lab, profile
-
-
-def check_available_matching_host(lab, hostprofile):
- available_host_types = ResourceManager.getInstance().getAvailableHostTypes(lab)
- if hostprofile not in available_host_types:
- # TODO: handle deleting generic resource in this instance along with grb
- raise HostNotAvailable('Requested host type is not available. Please try again later. Host availability can be viewed in the "Hosts" tab to the left.')
-
- hostset = Host.objects.filter(lab=lab, profile=hostprofile).filter(booked=False).filter(working=True)
- if not hostset.exists():
- raise HostNotAvailable("Couldn't find any matching unbooked hosts")
-
- return True
-
-
-def generate_grb(owner, lab, common_id):
- grbundle = GenericResourceBundle(owner=owner)
- grbundle.lab = lab
- grbundle.name = "grbundle for quick booking with uid " + common_id
- grbundle.description = "grbundle created for quick-deploy booking"
- grbundle.save()
-
- return grbundle
-
-
-def generate_gresource(bundle, hostname):
- if not re.match(r"(?=^.{1,253}$)(^([A-Za-z0-9-_]{1,62}\.)*[A-Za-z0-9-_]{1,63})$", hostname):
- raise InvalidHostnameException("Hostname must comply to RFC 952 and all extensions to it until this point")
- gresource = GenericResource(bundle=bundle, name=hostname)
- gresource.save()
-
- return gresource
-
-
-def generate_ghost(generic_resource, host_profile):
- ghost = GenericHost()
- ghost.resource = generic_resource
- ghost.profile = host_profile
- ghost.save()
-
- return ghost
-
-
-def generate_config_bundle(owner, common_id, grbundle):
- cbundle = ConfigBundle()
- cbundle.owner = owner
- cbundle.name = "configbundle for quick booking with uid " + common_id
- cbundle.description = "configbundle created for quick-deploy booking"
- cbundle.bundle = grbundle
- cbundle.save()
-
- return cbundle
-
-
-def generate_opnfvconfig(scenario, installer, config_bundle):
- opnfvconfig = OPNFVConfig()
- opnfvconfig.scenario = scenario
- opnfvconfig.installer = installer
- opnfvconfig.bundle = config_bundle
- opnfvconfig.save()
-
- return opnfvconfig
-
+def parse_resource_field(resource_json):
+ """
+ Parse the json from the frontend.
+
+ returns a reference to the selected Lab and ResourceTemplate objects
+ """
+ lab, template = (None, None)
+ lab_dict = resource_json['lab']
+ for lab_info in lab_dict.values():
+ if lab_info['selected']:
+ lab = Lab.objects.get(lab_user__id=lab_info['id'])
+
+ resource_dict = resource_json['resource']
+ for resource_info in resource_dict.values():
+ if resource_info['selected']:
+ template = ResourceTemplate.objects.get(pk=resource_info['id'])
+
+ if lab is None:
+ raise ValidationError("No lab was selected")
+ if template is None:
+ raise ValidationError("No Host was selected")
+
+ return lab, template
+
+
+def update_template(old_template, image, hostname, user, global_cloud_config=None):
+ """
+ Duplicate a template to the users account and update configured fields.
+
+ The dashboard presents users with preconfigured resource templates,
+ but the user might want to make small modifications, e.g hostname and
+ linux distro. So we copy the public template and create a private version
+ to the user's profile, and mark it temporary. When the booking ends the
+ new template is deleted
+ """
+ name = user.username + "'s Copy of '" + old_template.name + "'"
+ num_copies = ResourceTemplate.objects.filter(name__startswith=name).count()
+ template = ResourceTemplate.objects.create(
+ name=name if num_copies == 0 else name + " (" + str(num_copies) + ")",
+ xml=old_template.xml,
+ owner=user,
+ lab=old_template.lab,
+ description=old_template.description,
+ public=False,
+ temporary=True,
+ private_vlan_pool=old_template.private_vlan_pool,
+ public_vlan_pool=old_template.public_vlan_pool,
+ copy_of=old_template
+ )