diff options
author | Sawyer Bergeron <sbergeron@iol.unh.edu> | 2019-08-29 16:35:53 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2019-08-29 16:35:53 +0000 |
commit | 01bdb95d22840393a20378559cf3cf59c4bdce78 (patch) | |
tree | 7880970eb7f55c2e9a6af7ada2ee5900cac9e2be /src/api | |
parent | 99f96e86bdf78e9d93ad1d259fc18e3fff772720 (diff) | |
parent | d78fcef6ec55dbaa225c6607bdac430539bf3f0b (diff) |
Merge "Adds Downtime Awareness"
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/forms.py | 16 | ||||
-rw-r--r-- | src/api/models.py | 34 | ||||
-rw-r--r-- | src/api/urls.py | 2 | ||||
-rw-r--r-- | src/api/views.py | 36 |
4 files changed, 88 insertions, 0 deletions
diff --git a/src/api/forms.py b/src/api/forms.py new file mode 100644 index 0000000..1b74a9b --- /dev/null +++ b/src/api/forms.py @@ -0,0 +1,16 @@ +############################################################################## +# Copyright (c) 2019 Parker Berberian and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################# + +import django.forms as forms + + +class DowntimeForm(forms.Form): + start = forms.DateTimeField() + end = forms.DateTimeField() + description = forms.CharField(max_length=1000, required=False) diff --git a/src/api/models.py b/src/api/models.py index 1f708ae..682785b 100644 --- a/src/api/models.py +++ b/src/api/models.py @@ -13,6 +13,7 @@ from django.db import models from django.core.exceptions import PermissionDenied from django.shortcuts import get_object_or_404 from django.urls import reverse +from django.utils import timezone import json import uuid @@ -30,6 +31,7 @@ from resource_inventory.models import ( ) from resource_inventory.idf_templater import IDFTemplater from resource_inventory.pdf_templater import PDFTemplater +from account.models import Downtime class JobStatus(object): @@ -67,6 +69,38 @@ class LabManager(object): def __init__(self, lab): self.lab = lab + def get_downtime(self): + return Downtime.objects.filter(start__lt=timezone.now(), end__gt=timezone.now(), lab=self.lab) + + def get_downtime_json(self): + downtime = self.get_downtime().first() # should only be one item in queryset + if downtime: + return { + "is_down": True, + "start": downtime.start, + "end": downtime.end, + "description": downtime.description + } + return {"is_down": False} + + def create_downtime(self, form): + """ + takes in a dictionary that describes the model. + { + "start": utc timestamp + "end": utc timestamp + "description": human text (optional) + } + For timestamp structure, https://docs.djangoproject.com/en/2.2/ref/forms/fields/#datetimefield + """ + Downtime.objects.create( + start=form.cleaned_data['start'], + end=form.cleaned_data['end'], + description=form.cleaned_data['description'], + lab=self.lab + ) + return self.get_downtime_json() + def update_host_remote_info(self, data, host_id): host = get_object_or_404(Host, labid=host_id, lab=self.lab) info = {} diff --git a/src/api/urls.py b/src/api/urls.py index 778f6eb..b8b9cff 100644 --- a/src/api/urls.py +++ b/src/api/urls.py @@ -31,6 +31,7 @@ from api.views import ( lab_profile, lab_status, lab_inventory, + lab_downtime, specific_job, specific_task, new_jobs, @@ -47,6 +48,7 @@ urlpatterns = [ path('labs/<slug:lab_name>/profile', lab_profile), path('labs/<slug:lab_name>/status', lab_status), path('labs/<slug:lab_name>/inventory', lab_inventory), + path('labs/<slug:lab_name>/downtime', lab_downtime), path('labs/<slug:lab_name>/hosts/<slug:host_id>', lab_host), path('labs/<slug:lab_name>/hosts/<slug:host_id>/bmc', update_host_bmc), path('labs/<slug:lab_name>/booking/<int:booking_id>/pdf', get_pdf, name="get-pdf"), diff --git a/src/api/views.py b/src/api/views.py index fb28958..a5153d7 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -12,6 +12,7 @@ from django.contrib.auth.decorators import login_required from django.shortcuts import redirect from django.utils.decorators import method_decorator +from django.utils import timezone from django.views import View from django.http.response import JsonResponse, HttpResponse from rest_framework import viewsets @@ -20,6 +21,7 @@ from django.views.decorators.csrf import csrf_exempt from api.serializers.booking_serializer import BookingSerializer from api.serializers.old_serializers import UserSerializer +from api.forms import DowntimeForm from account.models import UserProfile from booking.models import Booking from api.models import LabManagerTracker, get_task @@ -150,6 +152,40 @@ def current_jobs(request, lab_name=""): return JsonResponse(lab_manager.get_current_jobs(), safe=False) +def lab_downtime(request, lab_name=""): + lab_token = request.META.get('HTTP_AUTH_TOKEN') + lab_manager = LabManagerTracker.get(lab_name, lab_token) + if request.method == "GET": + return JsonResponse(lab_manager.get_downtime_json()) + if request.method == "POST": + return post_lab_downtime(request, lab_manager) + if request.method == "DELETE": + return delete_lab_downtime(lab_manager) + return HttpResponse(status=405) + + +def post_lab_downtime(request, lab_manager): + current_downtime = lab_manager.get_downtime() + if current_downtime.exists(): + return JsonResponse({"error": "Lab is already in downtime"}, status=422) + form = DowntimeForm(request.POST) + if form.is_valid(): + return JsonResponse(lab_manager.create_downtime(form)) + else: + return JsonResponse(form.errors.get_json_data(), status=400) + + +def delete_lab_downtime(lab_manager): + current_downtime = lab_manager.get_downtime() + if current_downtime.exists(): + dt = current_downtime.first() + dt.end = timezone.now() + dt.save() + return JsonResponse(lab_manager.get_downtime_json(), safe=False) + else: + return JsonResponse({"error": "Lab is not in downtime"}, status=422) + + def done_jobs(request, lab_name=""): lab_token = request.META.get('HTTP_AUTH_TOKEN') lab_manager = LabManagerTracker.get(lab_name, lab_token) |