diff options
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/models.py | 46 | ||||
-rw-r--r-- | src/api/tests/test_serializers.py | 229 | ||||
-rw-r--r-- | src/api/urls.py | 4 | ||||
-rw-r--r-- | src/api/views.py | 23 |
4 files changed, 72 insertions, 230 deletions
diff --git a/src/api/models.py b/src/api/models.py index 30f0f75..b35adf2 100644 --- a/src/api/models.py +++ b/src/api/models.py @@ -11,6 +11,7 @@ from django.contrib.auth.models import User from django.db import models from django.core.exceptions import PermissionDenied +from django.shortcuts import get_object_or_404 import json import uuid @@ -21,7 +22,8 @@ from resource_inventory.models import ( HostProfile, Host, Image, - Interface + Interface, + RemoteInfo ) @@ -60,6 +62,32 @@ class LabManager(object): def __init__(self, lab): self.lab = lab + def update_host_remote_info(self, data, host_id): + host = get_object_or_404(Host, labid=host_id, lab=self.lab) + info = {} + try: + info['address'] = data['address'] + info['mac_address'] = data['mac_address'] + info['password'] = data['password'] + info['user'] = data['user'] + info['type'] = data['type'] + info['versions'] = json.dumps(data['versions']) + except Exception as e: + return {"error": "invalid arguement: " + str(e)} + remote_info = host.remote_management + if "default" in remote_info.mac_address: + remote_info = RemoteInfo() + remote_info.address = info['address'] + remote_info.mac_address = info['mac_address'] + remote_info.password = info['password'] + remote_info.user = info['user'] + remote_info.type = info['type'] + remote_info.versions = info['versions'] + remote_info.save() + host.remote_management = remote_info + host.save() + return {"status": "success"} + def get_profile(self): prof = {} prof['name'] = self.lab.name @@ -88,6 +116,22 @@ class LabManager(object): inventory['host_types'] = self.serialize_host_profiles(profiles) return inventory + def get_host(self, hostname): + host = get_object_or_404(Host, labid=hostname, lab=self.lab) + return { + "booked": host.booked, + "working": host.working, + "type": host.profile.name + } + + def update_host(self, hostname, data): + host = get_object_or_404(Host, labid=hostname, lab=self.lab) + if "working" in data: + working = data['working'] == "true" + host.working = working + host.save() + return self.get_host(hostname) + def get_status(self): return {"status": self.lab.status} diff --git a/src/api/tests/test_serializers.py b/src/api/tests/test_serializers.py deleted file mode 100644 index c1fa5af..0000000 --- a/src/api/tests/test_serializers.py +++ /dev/null @@ -1,229 +0,0 @@ -############################################################################## -# Copyright (c) 2018 Sawyer Bergeron 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 -############################################################################## -from django.test import TestCase -from booking.models import Booking -from account.models import Lab -from api.serializers.booking_serializer import BookingField -from datetime import timedelta -from django.utils import timezone -from django.contrib.auth.models import Permission, User -from resource_inventory.models import ( - Image, - OPNFVRole, - HostConfiguration, - HostProfile, - InterfaceProfile, - DiskProfile, - CpuProfile, - RamProfile, - GenericResourceBundle, - GenericResource, - GenericHost, - Host, - Vlan, - Interface, - ConfigBundle, - ResourceBundle -) - - -class BookingSerializerTestCase(TestCase): - - count = 0 - - def makeHostConfigurations(self, hosts, config): - lab_user = User.objects.create(username="asfasdfasdf") - owner = User.objects.create(username="asfasdfasdffffff") - lab = Lab.objects.create( - lab_user=lab_user, - name="TestLab123123", - contact_email="mail@email.com", - contact_phone="" - ) - jumphost = True - for host in hosts: - image = Image.objects.create( - lab_id=12, - from_lab=lab, - name="this is a test image", - owner=owner - ) - name = "jumphost" - if not jumphost: - name = "compute" - role = OPNFVRole.objects.create( - name=name, - description="stuff" - ) - - HostConfiguration.objects.create( - host=host, - image=image, - bundle=config, - opnfvRole=role - ) - jumphost = False - - def setUp(self): - self.serializer = BookingField() - lab_user = User.objects.create(username="lab user") - lab = Lab.objects.create(name="test lab", lab_user=lab_user) - # create hostProfile - hostProfile = HostProfile.objects.create( - host_type=0, - name='Test profile', - description='a test profile' - ) - InterfaceProfile.objects.create( - speed=1000, - name='eno3', - host=hostProfile - ) - DiskProfile.objects.create( - size=1000, - media_type="SSD", - name='/dev/sda', - host=hostProfile - ) - CpuProfile.objects.create( - cores=96, - architecture="x86_64", - cpus=2, - host=hostProfile - ) - RamProfile.objects.create( - amount=256, - channels=4, - host=hostProfile - ) - - # create GenericResourceBundle - genericBundle = GenericResourceBundle.objects.create() - - gres1 = GenericResource.objects.create( - bundle=genericBundle, - name='generic resource ' + str(self.count) - ) - self.count += 1 - gHost1 = GenericHost.objects.create( - resource=gres1, - profile=hostProfile - ) - - gres2 = GenericResource.objects.create( - bundle=genericBundle, - name='generic resource ' + str(self.count) - ) - self.count += 1 - gHost2 = GenericHost.objects.create( - resource=gres2, - profile=hostProfile - ) - user1 = User.objects.create(username='user1') - - add_booking_perm = Permission.objects.get(codename='add_booking') - user1.user_permissions.add(add_booking_perm) - - user1 = User.objects.get(pk=user1.id) - - conf = ConfigBundle.objects.create(owner=user1, name="test conf") - self.makeHostConfigurations([gHost1, gHost2], conf) - - # actual resource bundle - bundle = ResourceBundle.objects.create( - template=genericBundle - ) - - host1 = Host.objects.create( - template=gHost1, - booked=True, - name='host1', - bundle=bundle, - profile=hostProfile, - lab=lab - ) - - host2 = Host.objects.create( - template=gHost2, - booked=True, - name='host2', - bundle=bundle, - profile=hostProfile, - lab=lab - ) - - vlan1 = Vlan.objects.create(vlan_id=300, tagged=False) - vlan2 = Vlan.objects.create(vlan_id=300, tagged=False) - - iface1 = Interface.objects.create( - mac_address='00:11:22:33:44:55', - bus_address='some bus address', - switch_name='switch1', - port_name='port10', - host=host1 - ) - - iface1.config = [vlan1] - - iface2 = Interface.objects.create( - mac_address='00:11:22:33:44:56', - bus_address='some bus address', - switch_name='switch1', - port_name='port12', - host=host2 - ) - - iface2.config = [vlan2] - - # finally, can create booking - self.booking = Booking.objects.create( - owner=user1, - start=timezone.now(), - end=timezone.now() + timedelta(weeks=1), - purpose='Testing', - resource=bundle, - config_bundle=conf - ) - - serialized_booking = {} - - host1 = {} - host1['hostname'] = 'host1' - host1['image'] = {} # TODO: Images - host1['deploy_image'] = True - host2 = {} - host2['hostname'] = 'host2' - host2['image'] = {} # TODO: Images - host2['deploy_image'] = True - - serialized_booking['hosts'] = [host1, host2] - - net = {} - net['name'] = 'network_name' - net['vlan_id'] = 300 - netHost1 = {} - netHost1['hostname'] = 'host1' - netHost1['tagged'] = False - netHost1['interface'] = 0 - netHost2 = {} - netHost2['hostname'] = 'host2' - netHost2['tagged'] = False - netHost2['interface'] = 0 - net['hosts'] = [netHost1, netHost2] - - serialized_booking['networking'] = [net] - serialized_booking['jumphost'] = 'host1' - - self.serialized_booking = serialized_booking - - def test_to_representation(self): - keys = ['hosts', 'networking', 'jumphost'] - serialized_form = self.serializer.to_representation(self.booking) - for key in keys: - self.assertEquals(serialized_form[key], self.serialized_booking) diff --git a/src/api/urls.py b/src/api/urls.py index 50cc6ac..d18a04d 100644 --- a/src/api/urls.py +++ b/src/api/urls.py @@ -39,6 +39,8 @@ from api.views import ( new_jobs, current_jobs, done_jobs, + update_host_bmc, + lab_host, GenerateTokenView ) @@ -51,6 +53,8 @@ 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>/hosts/<slug:host_id>', lab_host), + path('labs/<slug:lab_name>/hosts/<slug:host_id>/bmc', update_host_bmc), 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/new', new_jobs), diff --git a/src/api/views.py b/src/api/views.py index c72c85c..a56dcfe 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -54,6 +54,16 @@ def lab_inventory(request, lab_name=""): return JsonResponse(lab_manager.get_inventory(), safe=False) +@csrf_exempt +def lab_host(request, lab_name="", host_id=""): + 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_host(host_id), safe=False) + if request.method == "POST": + return JsonResponse(lab_manager.update_host(host_id, request.POST), safe=False) + + def lab_status(request, lab_name=""): lab_token = request.META.get('HTTP_AUTH_TOKEN') lab_manager = LabManagerTracker.get(lab_name, lab_token) @@ -62,6 +72,18 @@ def lab_status(request, lab_name=""): return JsonResponse(lab_manager.get_status(), safe=False) +@csrf_exempt +def update_host_bmc(request, lab_name="", host_id=""): + lab_token = request.META.get('HTTP_AUTH_TOKEN') + lab_manager = LabManagerTracker.get(lab_name, lab_token) + if request.method == "POST": + # update / create RemoteInfo for host + return JsonResponse( + lab_manager.update_host_remote_info(request.POST, host_id), + safe=False + ) + + def lab_profile(request, lab_name=""): lab_token = request.META.get('HTTP_AUTH_TOKEN') lab_manager = LabManagerTracker.get(lab_name, lab_token) @@ -93,6 +115,7 @@ def specific_task(request, lab_name="", job_id="", task_id=""): return JsonResponse(get_task(task_id).config.get_delta()) +@csrf_exempt def specific_job(request, lab_name="", job_id=""): lab_token = request.META.get('HTTP_AUTH_TOKEN') lab_manager = LabManagerTracker.get(lab_name, lab_token) |