diff options
author | Sean Smith <ssmith@iol.unh.edu> | 2021-03-19 11:48:00 -0400 |
---|---|---|
committer | Sean Smith <ssmith@iol.unh.edu> | 2021-05-28 11:06:08 -0400 |
commit | ced6bf516bb08073288d4ad0a47d136a504a95c8 (patch) | |
tree | acc9520f5175c06eaad3b701f539de255de56d1f /src/booking | |
parent | 8086a7aa9aa95d5af341b67cba85b1377a168b98 (diff) |
API V1
Change-Id: I38bc35eeecc0cc7d2334d1fb4b9f215a291e31f6
Signed-off-by: Sean Smith <ssmith@iol.unh.edu>
Diffstat (limited to 'src/booking')
-rw-r--r-- | src/booking/quick_deployer.py | 88 |
1 files changed, 50 insertions, 38 deletions
diff --git a/src/booking/quick_deployer.py b/src/booking/quick_deployer.py index 0a3bfc6..65dd9b2 100644 --- a/src/booking/quick_deployer.py +++ b/src/booking/quick_deployer.py @@ -10,10 +10,11 @@ import json from django.db.models import Q +from django.db import transaction from datetime import timedelta from django.utils import timezone from django.core.exceptions import ValidationError -from account.models import Lab +from account.models import Lab, UserProfile from resource_inventory.models import ( ResourceTemplate, @@ -167,7 +168,7 @@ def generate_resource_bundle(template): return resource_bundle -def check_invariants(request, **kwargs): +def check_invariants(**kwargs): # TODO: This should really happen in the BookingForm validation methods installer = kwargs['installer'] image = kwargs['image'] @@ -188,7 +189,7 @@ def check_invariants(request, **kwargs): # TODO # if image.host_type != host_profile: # raise ValidationError("The chosen image is not available for the chosen host type") - if not image.public and image.owner != request.user: + if not image.public and image.owner != kwargs['owner']: raise ValidationError("You are not the owner of the chosen private image") if length < 1 or length > 21: raise BookingLengthException("Booking must be between 1 and 21 days long") @@ -196,62 +197,73 @@ def check_invariants(request, **kwargs): def create_from_form(form, request): """ - Create a Booking from the user's form. - - Large, nasty method to create a booking or return a useful error - based on the form from the frontend + Parse data from QuickBookingForm to create booking """ resource_field = form.cleaned_data['filter_field'] - purpose_field = form.cleaned_data['purpose'] - project_field = form.cleaned_data['project'] - users_field = form.cleaned_data['users'] - hostname = 'opnfv_host' if not form.cleaned_data['hostname'] else form.cleaned_data['hostname'] - length = form.cleaned_data['length'] - - image = form.cleaned_data['image'] - scenario = form.cleaned_data['scenario'] - installer = form.cleaned_data['installer'] lab, resource_template = parse_resource_field(resource_field) data = form.cleaned_data data['lab'] = lab data['resource_template'] = resource_template - check_invariants(request, **data) + data['owner'] = request.user + + return _create_booking(data) + + +def create_from_API(body, user): + """ + Parse data from Automation API to create booking + """ + booking_info = json.loads(body.decode('utf-8')) + + data = {} + data['purpose'] = booking_info['purpose'] + data['project'] = booking_info['project'] + data['users'] = [UserProfile.objects.get(pk=user_id) + for user_id in booking_info['collaborators']] + data['hostname'] = booking_info['hostname'] + data['length'] = booking_info['length'] + data['installer'] = None + data['scenario'] = None + + data['image'] = Image.objects.get(pk=booking_info['imageLabID']) + + data['resource_template'] = ResourceTemplate.objects.get(pk=booking_info['templateID']) + data['lab'] = data['resource_template'].lab + data['owner'] = user + + return _create_booking(data) + + +@transaction.atomic +def _create_booking(data): + check_invariants(**data) # check booking privileges # TODO: use the canonical booking_allowed method because now template might have multiple # machines - if Booking.objects.filter(owner=request.user, end__gt=timezone.now()).count() >= 3 and not request.user.userprofile.booking_privledge: + if Booking.objects.filter(owner=data['owner'], end__gt=timezone.now()).count() >= 3 and not data['owner'].userprofile.booking_privledge: raise PermissionError("You do not have permission to have more than 3 bookings at a time.") - ResourceManager.getInstance().templateIsReservable(resource_template) - - resource_template = update_template(resource_template, image, hostname, request.user) - - # if no installer provided, just create blank host - opnfv_config = None - if installer: - hconf = resource_template.getConfigs()[0] - opnfv_config = generate_opnfvconfig(scenario, installer, resource_template) - generate_hostopnfv(hconf, opnfv_config) - - # generate resource bundle - resource_bundle = generate_resource_bundle(resource_template) + ResourceManager.getInstance().templateIsReservable(data['resource_template']) + data['resource_template'] = update_template(data['resource_template'], data['image'], 'opnfv_host' if not data['hostname'] else data['hostname'], data['owner']) + resource_bundle = generate_resource_bundle(data['resource_template']) # generate booking booking = Booking.objects.create( - purpose=purpose_field, - project=project_field, - lab=lab, - owner=request.user, + purpose=data['purpose'], + project=data['project'], + lab=data['lab'], + owner=data['owner'], start=timezone.now(), - end=timezone.now() + timedelta(days=int(length)), + end=timezone.now() + timedelta(days=int(data['length'])), resource=resource_bundle, - opnfv_config=opnfv_config + opnfv_config=None ) + booking.pdf = PDFTemplater.makePDF(booking) - for collaborator in users_field: # list of UserProfiles + for collaborator in data['users']: # list of UserProfiles booking.collaborators.add(collaborator.user) booking.save() |