diff options
author | Adam Hassick <ahassick@iol.unh.edu> | 2021-06-29 16:49:27 -0400 |
---|---|---|
committer | Adam Hassick <ahassick@iol.unh.edu> | 2021-07-23 16:22:54 +0000 |
commit | 6ffb1fdf6ce7825770148bada5a4c54899e4ed36 (patch) | |
tree | da5d8390a4d46a898840083a761809af47bd7f52 /src/api | |
parent | 49e2b407003b69551ddafa851639e83ec42a5b09 (diff) |
Cobbler model changes, new endpoints
Signed-off-by: Adam Hassick <ahassick@iol.unh.edu>
Change-Id: If0a94730e92747127cef121ec4930a4c8bae6c92
Signed-off-by: Sawyer Bergeron <sbergeron@iol.unh.edu>
Signed-off-by: Adam Hassick <ahassick@iol.unh.edu>
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/migrations/0017_auto_20210630_1629.py | 18 | ||||
-rw-r--r-- | src/api/migrations/0018_cloudinitfile.py (renamed from src/api/migrations/0017_cloudinitfile.py) | 6 | ||||
-rw-r--r-- | src/api/models.py | 18 | ||||
-rw-r--r-- | src/api/urls.py | 10 | ||||
-rw-r--r-- | src/api/views.py | 90 |
5 files changed, 131 insertions, 11 deletions
diff --git a/src/api/migrations/0017_auto_20210630_1629.py b/src/api/migrations/0017_auto_20210630_1629.py new file mode 100644 index 0000000..643ff5f --- /dev/null +++ b/src/api/migrations/0017_auto_20210630_1629.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2 on 2021-06-30 16:29 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0016_auto_20201109_2149'), + ] + + operations = [ + migrations.AlterField( + model_name='snapshotconfig', + name='image', + field=models.CharField(max_length=200, null=True), + ), + ] diff --git a/src/api/migrations/0017_cloudinitfile.py b/src/api/migrations/0018_cloudinitfile.py index f14aea1..4e41b39 100644 --- a/src/api/migrations/0017_cloudinitfile.py +++ b/src/api/migrations/0018_cloudinitfile.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2 on 2021-06-11 20:42 +# Generated by Django 2.2 on 2021-07-01 20:45 from django.db import migrations, models import django.db.models.deletion @@ -7,9 +7,9 @@ import django.db.models.deletion class Migration(migrations.Migration): dependencies = [ - ('resource_inventory', '0017_auto_20201218_1516'), + ('resource_inventory', '0019_auto_20210701_1947'), ('booking', '0008_auto_20201109_1947'), - ('api', '0016_auto_20201109_2149'), + ('api', '0017_auto_20210630_1629'), ] operations = [ diff --git a/src/api/models.py b/src/api/models.py index 36d1b8c..a207044 100644 --- a/src/api/models.py +++ b/src/api/models.py @@ -25,6 +25,7 @@ from resource_inventory.models import ( Lab, ResourceProfile, Image, + Opsys, Interface, ResourceOPNFVConfig, RemoteInfo, @@ -85,6 +86,18 @@ class LabManager(object): def __init__(self, lab): self.lab = lab + def get_opsyss(self): + return Opsys.objects.filter(from_lab=self.lab) + + def get_images(self): + return Image.objects.filter(from_lab=self.lab) + + def get_image(self, image_id): + return Image.objects.filter(from_lab=self.lab, lab_id=image_id) + + def get_opsys(self, opsys_id): + return Opsys.objects.filter(from_lab=self.lab, lab_id=opsys_id) + def get_downtime(self): return Downtime.objects.filter(start__lt=timezone.now(), end__gt=timezone.now(), lab=self.lab) @@ -408,7 +421,7 @@ class CloudInitFile(models.Model): return full_dict @classmethod - def get(booking_id: int, resource_lab_id: str): + def get(cls, booking_id: int, resource_lab_id: str): return CloudInitFile.objects.get(resource_id=resource_lab_id, booking__id=booking_id) def _resource(self): @@ -768,7 +781,6 @@ class HardwareConfig(TaskConfig): # TODO: grab the CloudInitFile urls from self.hosthardwarerelation.get_resource() return self.format_delta( self.hosthardwarerelation.get_resource().get_configuration(self.state), - self.cloudinit_file.get_delta_url(), self.hosthardwarerelation.lab_token) @@ -819,7 +831,7 @@ class NetworkConfig(TaskConfig): class SnapshotConfig(TaskConfig): resource_id = models.CharField(max_length=200, default="default_id") - image = models.IntegerField(null=True) + image = models.CharField(max_length=200,null=True) # cobbler ID dashboard_id = models.IntegerField() delta = models.TextField(default="{}") diff --git a/src/api/urls.py b/src/api/urls.py index 7adeef6..e5ddd97 100644 --- a/src/api/urls.py +++ b/src/api/urls.py @@ -47,9 +47,17 @@ from api.views import ( GenerateTokenView, analytics_job, resource_cidata, + all_images, + all_opsyss, + single_image, + single_opsys ) urlpatterns = [ + path('labs/<slug:lab_name>/opsys/<slug:opsys_id>', single_opsys), + path('labs/<slug:lab_name>/image/<slug:image_id>', single_image), + path('labs/<slug:lab_name>/opsys', all_opsyss), + path('labs/<slug:lab_name>/image', all_images), path('labs/<slug:lab_name>/profile', lab_profile), path('labs/<slug:lab_name>/status', lab_status), path('labs/<slug:lab_name>/inventory', lab_inventory), @@ -60,7 +68,7 @@ urlpatterns = [ path('labs/<slug:lab_name>/booking/<int:booking_id>/idf', get_idf, name="get-idf"), path('labs/<slug:lab_name>/jobs/<int:job_id>', specific_job), path('labs/<slug:lab_name>/jobs/<int:job_id>/<slug:task_id>', specific_task), - path('labs/<slug:lab_name>/jobs/<int:job_id>/cidata/<slug:resource_id', resource_cidata), + path('labs/<slug:lab_name>/jobs/<int:job_id>/cidata/<slug:resource_id>', resource_cidata), path('labs/<slug:lab_name>/jobs/new', new_jobs), path('labs/<slug:lab_name>/jobs/current', current_jobs), path('labs/<slug:lab_name>/jobs/done', done_jobs), diff --git a/src/api/views.py b/src/api/views.py index 3a3effa..4b887e6 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -14,6 +14,7 @@ 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 import QueryDict from django.http.response import JsonResponse, HttpResponse from rest_framework import viewsets from rest_framework.authtoken.models import Token @@ -25,9 +26,14 @@ 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, CloudInitFile +from api.models import LabManagerTracker, get_task, CloudInitFile, Job from notifier.manager import NotificationHandler from analytics.models import ActiveVPNUser +from resource_inventory.models import ( + Image, + Opsys +) + import json """ @@ -80,6 +86,81 @@ def lab_host(request, lab_name="", host_id=""): if request.method == "POST": return JsonResponse(lab_manager.update_host(host_id, request.POST), safe=False) +# API extension for Cobbler integration + +def all_images(request, lab_name=""): + a = [] + for i in Image.objects.all(): + a.append(i.serialize()) + return JsonResponse(a, safe=False) + + +def all_opsyss(request, lab_name=""): + a = [] + for opsys in Opsys.objects.all(): + a.append(opsys.serialize()) + + return JsonResponse(a, safe=False) + +@csrf_exempt +def single_image(request, lab_name="", image_id=""): + lab_token = request.META.get('HTTP_AUTH_TOKEN') + lab_manager = LabManagerTracker.get(lab_name, lab_token) + img = lab_manager.get_image(image_id).first() + + if request.method == "GET": + if not img: + return HttpResponse(status=404) + return JsonResponse(img.serialize(), safe=False) + + if request.method == "POST": + # get POST data + data = json.loads(request.body.decode('utf-8')) + if img: + img.update(data) + else: + # append lab name and the ID from the URL + data['from_lab_id'] = lab_name + data['lab_id'] = image_id + + # create and save a new Image object + img = Image.new_from_data(data) + + img.save() + + # indicate success in response + return HttpResponse(status=200) + return HttpResponse(status=405) + + +@csrf_exempt +def single_opsys(request, lab_name="", opsys_id=""): + lab_token = request.META.get('HTTP_AUTH_TOKEN') + lab_manager = LabManagerTracker.get(lab_name, lab_token) + opsys = lab_manager.get_opsys(opsys_id).first() + + if request.method == "GET": + if not opsys: + return HttpResponse(status=404) + return JsonResponse(opsys.serialize(), safe=False) + + if request.method == "POST": + data = json.loads(request.body.decode('utf-8')) + if opsys: + opsys.update(data) + else: + # only name, available, and obsolete are needed to create an Opsys + # other fields are derived from the URL parameters + + data['from_lab_id'] = lab_name + data['lab_id'] = opsys_id + opsys = Opsys.new_from_data(data) + + opsys.save() + return HttpResponse(status=200) + return HttpResponse(status=405) + +# end API extension def get_pdf(request, lab_name="", booking_id=""): lab_token = request.META.get('HTTP_AUTH_TOKEN') @@ -168,10 +249,11 @@ def specific_job(request, lab_name="", job_id=""): @csrf_exempt def resource_cidata(request, lab_name="", job_id="", resource_id=""): - lab_token = request.META.get('HTTP_AUTH_TOKEN') - lab_manager = LabManagerTracker.get(lab_name, lab_token) + #lab_token = request.META.get('HTTP_AUTH_TOKEN') + #lab_manager = LabManagerTracker.get(lab_name, lab_token) - job = lab_manager.get_job(job_id) + #job = lab_manager.get_job(job_id) + job = Job.objects.get(id=job_id) cifile = None try: |