diff options
Diffstat (limited to 'src/api')
-rw-r--r-- | src/api/__init__.py | 2 | ||||
-rw-r--r-- | src/api/admin.py | 13 | ||||
-rw-r--r-- | src/api/models.py | 114 | ||||
-rw-r--r-- | src/api/serializers/booking_serializer.py | 37 | ||||
-rw-r--r-- | src/api/tests/__init__.py | 2 | ||||
-rw-r--r-- | src/api/tests/test_serializers.py | 80 | ||||
-rw-r--r-- | src/api/urls.py | 14 | ||||
-rw-r--r-- | src/api/views.py | 6 |
8 files changed, 169 insertions, 99 deletions
diff --git a/src/api/__init__.py b/src/api/__init__.py index b5914ce..b6fef6c 100644 --- a/src/api/__init__.py +++ b/src/api/__init__.py @@ -6,5 +6,3 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## - - diff --git a/src/api/admin.py b/src/api/admin.py index f1bc70a..3d32c78 100644 --- a/src/api/admin.py +++ b/src/api/admin.py @@ -11,12 +11,23 @@ from django.apps import AppConfig from django.contrib import admin -from api.models import * +from api.models import ( + Job, + OpnfvApiConfig, + HardwareConfig, + NetworkConfig, + SoftwareConfig, + AccessRelation, + SoftwareRelation, + HostHardwareRelation, + HostNetworkRelation, +) class ApiConfig(AppConfig): name = 'apiJobs' + admin.site.register(Job) admin.site.register(OpnfvApiConfig) admin.site.register(HardwareConfig) diff --git a/src/api/models.py b/src/api/models.py index 4237559..78ec920 100644 --- a/src/api/models.py +++ b/src/api/models.py @@ -8,14 +8,21 @@ ############################################################################## +from django.contrib.auth.models import User from django.db import models from django.core.exceptions import PermissionDenied import json import uuid -from resource_inventory.models import * from booking.models import Booking +from resource_inventory.models import ( + Lab, + HostProfile, + Host, + Image, + Interface +) class JobStatus(object): @@ -57,16 +64,18 @@ class LabManager(object): prof = {} prof['name'] = self.lab.name prof['contact'] = { - "phone": self.lab.contact_phone, - "email": self.lab.contact_email - } + "phone": self.lab.contact_phone, + "email": self.lab.contact_email + } prof['host_count'] = [] for host in HostProfile.objects.filter(labs=self.lab): count = Host.objects.filter(profile=host, lab=self.lab).count() - prof['host_count'].append({ - "type": host.name, - "count": count - }) + prof['host_count'].append( + { + "type": host.name, + "count": count + } + ) return prof def get_inventory(self): @@ -135,11 +144,13 @@ class LabManager(object): def serialize_images(self, images): images_ser = [] for image in images: - images_ser.append({ - "name": image.name, - "lab_id": image.lab_id, - "dashboard_id": image.id - }) + images_ser.append( + { + "name": image.name, + "lab_id": image.lab_id, + "dashboard_id": image.id + } + ) return images_ser def serialize_host_profiles(self, profiles): @@ -147,25 +158,27 @@ class LabManager(object): for profile in profiles: p = {} p['cpu'] = { - "cores": profile.cpuprofile.first().cores, - "arch": profile.cpuprofile.first().architecture, - "cpus": profile.cpuprofile.first().cpus, - } + "cores": profile.cpuprofile.first().cores, + "arch": profile.cpuprofile.first().architecture, + "cpus": profile.cpuprofile.first().cpus, + } p['disks'] = [] for disk in profile.storageprofile.all(): d = { - "size": disk.size, - "type": disk.media_type, - "name": disk.name - } + "size": disk.size, + "type": disk.media_type, + "name": disk.name + } p['disks'].append(d) p['description'] = profile.description p['interfaces'] = [] for iface in profile.interfaceprofile.all(): - p['interfaces'].append({ - "speed": iface.speed, - "name": iface.name - }) + p['interfaces'].append( + { + "speed": iface.speed, + "name": iface.name + } + ) p['ram'] = {"amount": profile.ramprofile.first().amount} p['name'] = profile.name @@ -228,7 +241,6 @@ class Job(models.Model): return False return True - def get_delta(self, status): d = {} j = {} @@ -270,6 +282,7 @@ class TaskConfig(models.Model): def clear_delta(self): self.delta = '{}' + class OpnfvApiConfig(models.Model): installer = models.CharField(max_length=100) @@ -324,6 +337,7 @@ class OpnfvApiConfig(models.Model): self.save() return json.loads(self.delta) + class AccessConfig(TaskConfig): access_type = models.CharField(max_length=50) user = models.ForeignKey(User, on_delete=models.CASCADE) @@ -380,6 +394,7 @@ class AccessConfig(TaskConfig): d['context'] = context self.delta = json.dumps(d) + class SoftwareConfig(TaskConfig): """ handled opnfv installations, etc @@ -409,6 +424,7 @@ class SoftwareConfig(TaskConfig): def to_json(self): return json.dumps(self.to_dict()) + class HardwareConfig(TaskConfig): """ handles imaging, user accounts, etc @@ -609,37 +625,37 @@ class JobFactory(object): except: job = Job.objects.create(status=JobStatus.NEW, booking=booking) cls.makeHardwareConfigs( - hosts=hosts, - job=job - ) + hosts=hosts, + job=job + ) cls.makeNetworkConfigs( - hosts=hosts, - job=job - ) + hosts=hosts, + job=job + ) cls.makeSoftware( - hosts=hosts, - job=job - ) + hosts=hosts, + job=job + ) all_users = list(booking.collaborators.all()) all_users.append(booking.owner) cls.makeAccessConfig( - users=all_users, - access_type="vpn", - revoke=False, - job=job - ) + users=all_users, + access_type="vpn", + revoke=False, + job=job + ) for user in all_users: try: cls.makeAccessConfig( - users=[user], - access_type="ssh", - revoke=False, - job=job, - context={ - "key": user.userprofile.ssh_public_key.read(), - "hosts": [host.labid for host in hosts] - } - ) + users=[user], + access_type="ssh", + revoke=False, + job=job, + context={ + "key": user.userprofile.ssh_public_key.read(), + "hosts": [host.labid for host in hosts] + } + ) except Exception: continue diff --git a/src/api/serializers/booking_serializer.py b/src/api/serializers/booking_serializer.py index e891de4..9b5c059 100644 --- a/src/api/serializers/booking_serializer.py +++ b/src/api/serializers/booking_serializer.py @@ -10,7 +10,16 @@ from rest_framework import serializers -from resource_inventory.models import * +from resource_inventory.models import ( + HostConfiguration, + CpuProfile, + DiskProfile, + InterfaceProfile, + RamProfile, + Image, + Interface +) + class BookingField(serializers.Field): @@ -28,17 +37,17 @@ class BookingField(serializers.Field): host_configs[host.name] = HostConfiguration.objects.get(host=host.template) if "jumphost" not in ser and host_configs[host.name].opnfvRole.name.lower() == "jumphost": ser['jumphost'] = host.name - #host is a Host model + # host is a Host model for i in range(len(host.interfaces.all())): interface = host.interfaces.all()[i] - #interface is an Interface model + # interface is an Interface model for vlan in interface.config.all(): - #vlan is Vlan model + # vlan is Vlan model if vlan.id not in networks: networks[vlan.id] = [] - net_host = {"hostname": host.name, "tagged": vlan.tagged, "interface":i} + net_host = {"hostname": host.name, "tagged": vlan.tagged, "interface": i} networks[vlan.id].append(net_host) - #creates networking object of proper form + # creates networking object of proper form networking = [] for vlanid in networks: network = {} @@ -47,7 +56,7 @@ class BookingField(serializers.Field): ser['networking'] = networking - #creates hosts object of correct form + # creates hosts object of correct form hosts = [] for hostname in host_configs: host = {"hostname": hostname} @@ -75,32 +84,37 @@ class BookingField(serializers.Field): """ return None + class BookingSerializer(serializers.Serializer): booking = BookingField() -#Host Type stuff, for inventory +# Host Type stuff, for inventory class CPUSerializer(serializers.ModelSerializer): class Meta: model = CpuProfile fields = ('cores', 'architecture', 'cpus') + class DiskSerializer(serializers.ModelSerializer): class Meta: model = DiskProfile fields = ('size', 'media_type', 'name') + class InterfaceProfileSerializer(serializers.ModelSerializer): class Meta: model = InterfaceProfile fields = ('speed', 'name') + class RamSerializer(serializers.ModelSerializer): class Meta: model = RamProfile fields = ('amount', 'channels') + class HostTypeSerializer(serializers.Serializer): name = serializers.CharField(max_length=200) ram = RamSerializer() @@ -109,20 +123,24 @@ class HostTypeSerializer(serializers.Serializer): disks = DiskSerializer() cpu = CPUSerializer() -#the rest of the inventory stuff + +# the rest of the inventory stuff class NetworkSerializer(serializers.Serializer): cidr = serializers.CharField(max_length=200) gateway = serializers.IPAddressField(max_length=200) vlan = serializers.IntegerField() + class ImageSerializer(serializers.ModelSerializer): lab_id = serializers.IntegerField() id = serializers.IntegerField(source="dashboard_id") name = serializers.CharField(max_length=50) description = serializers.CharField(max_length=200) + class Meta: model = Image + class InterfaceField(serializers.Field): def to_representation(self, interface): pass @@ -143,6 +161,7 @@ class InterfaceField(serializers.Field): port_name=port_name ) + class InventoryHostSerializer(serializers.Serializer): hostname = serializers.CharField(max_length=100) host_type = serializers.CharField(max_length=100) diff --git a/src/api/tests/__init__.py b/src/api/tests/__init__.py index fe2a32d..2435a9f 100644 --- a/src/api/tests/__init__.py +++ b/src/api/tests/__init__.py @@ -5,4 +5,4 @@ # 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 -##############################################################################
\ No newline at end of file +############################################################################## diff --git a/src/api/tests/test_serializers.py b/src/api/tests/test_serializers.py index c49010c..c1fa5af 100644 --- a/src/api/tests/test_serializers.py +++ b/src/api/tests/test_serializers.py @@ -8,12 +8,29 @@ ############################################################################## from django.test import TestCase from booking.models import Booking -from resource_inventory.models import * from account.models import Lab -from api.serializers.booking_serializer import * +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): @@ -24,35 +41,34 @@ class BookingSerializerTestCase(TestCase): 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 + 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 - ) + 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" - ) + name=name, + description="stuff" + ) HostConfiguration.objects.create( - host=host, - image=image, - bundle=config, - opnfvRole=role - ) - jumphost=False - + host=host, + image=image, + bundle=config, + opnfvRole=role + ) + jumphost = False def setUp(self): self.serializer = BookingField() @@ -64,30 +80,30 @@ class BookingSerializerTestCase(TestCase): name='Test profile', description='a test profile' ) - interfaceProfile = InterfaceProfile.objects.create( + InterfaceProfile.objects.create( speed=1000, name='eno3', host=hostProfile ) - diskProfile = DiskProfile.objects.create( + DiskProfile.objects.create( size=1000, media_type="SSD", name='/dev/sda', host=hostProfile ) - cpuProfile = CpuProfile.objects.create( + CpuProfile.objects.create( cores=96, architecture="x86_64", cpus=2, host=hostProfile ) - ramProfile = RamProfile.objects.create( + RamProfile.objects.create( amount=256, channels=4, host=hostProfile ) - #create GenericResourceBundle + # create GenericResourceBundle genericBundle = GenericResourceBundle.objects.create() gres1 = GenericResource.objects.create( @@ -119,9 +135,9 @@ class BookingSerializerTestCase(TestCase): conf = ConfigBundle.objects.create(owner=user1, name="test conf") self.makeHostConfigurations([gHost1, gHost2], conf) - #actual resource bundle + # actual resource bundle bundle = ResourceBundle.objects.create( - template = genericBundle + template=genericBundle ) host1 = Host.objects.create( @@ -168,8 +184,8 @@ class BookingSerializerTestCase(TestCase): # finally, can create booking self.booking = Booking.objects.create( owner=user1, - start = timezone.now(), - end = timezone.now() + timedelta(weeks=1), + start=timezone.now(), + end=timezone.now() + timedelta(weeks=1), purpose='Testing', resource=bundle, config_bundle=conf diff --git a/src/api/urls.py b/src/api/urls.py index 4b1fe40..50cc6ac 100644 --- a/src/api/urls.py +++ b/src/api/urls.py @@ -28,7 +28,19 @@ from django.conf.urls import url, include from django.urls import path from rest_framework import routers -from api.views import * +from api.views import ( + BookingViewSet, + UserViewSet, + lab_profile, + lab_status, + lab_inventory, + specific_job, + specific_task, + new_jobs, + current_jobs, + done_jobs, + GenerateTokenView +) router = routers.DefaultRouter() router.register(r'bookings', BookingViewSet) diff --git a/src/api/views.py b/src/api/views.py index cc3a668..c72c85c 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -18,13 +18,11 @@ from rest_framework import viewsets from rest_framework.authtoken.models import Token from django.views.decorators.csrf import csrf_exempt -import json - -from api.serializers.booking_serializer import * +from api.serializers.booking_serializer import BookingSerializer from api.serializers.old_serializers import UserSerializer from account.models import UserProfile from booking.models import Booking -from api.models import * +from api.models import LabManagerTracker, get_task from notifier.manager import NotificationHandler |