From d658b9635dff80764c2edbe1580bdcccbc32d438 Mon Sep 17 00:00:00 2001 From: meow Date: Tue, 25 Feb 2020 21:03:20 +0500 Subject: [PATCH] Replace (vmid,uuid) with id in VM model + Add last_host and graphics in VM model + Fixed retrieve view in uncloud.opennebula --- .../opennebula/management/commands/syncvm.py | 4 +-- .../migrations/0002_auto_20200225_1335.py | 27 +++++++++++++++++++ .../migrations/0003_auto_20200225_1428.py | 19 +++++++++++++ uncloud/opennebula/models.py | 16 ++++++++--- uncloud/opennebula/serializers.py | 4 +-- uncloud/opennebula/views.py | 4 +-- uncloud/uncloud/settings.py | 1 + 7 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 uncloud/opennebula/migrations/0002_auto_20200225_1335.py create mode 100644 uncloud/opennebula/migrations/0003_auto_20200225_1428.py diff --git a/uncloud/opennebula/management/commands/syncvm.py b/uncloud/opennebula/management/commands/syncvm.py index 00108f0..55844e3 100644 --- a/uncloud/opennebula/management/commands/syncvm.py +++ b/uncloud/opennebula/management/commands/syncvm.py @@ -54,9 +54,9 @@ class Command(BaseCommand): user_in_db = get_user_model().objects.get(email=vm_owner_email) except get_user_model().DoesNotExist: user_in_db = get_user_model().objects.create_user(username=user.uid, email=vm_owner_email) - VMModel.objects.update_or_create( - defaults={'data': vm, 'owner': user_in_db}, vmid=vm_id + id=f'opennebula{vm_id}', + defaults={'data': vm, 'owner': user_in_db} ) print('User with email but not found in ldap:', unknown_user_with_email) else: diff --git a/uncloud/opennebula/migrations/0002_auto_20200225_1335.py b/uncloud/opennebula/migrations/0002_auto_20200225_1335.py new file mode 100644 index 0000000..1554aa6 --- /dev/null +++ b/uncloud/opennebula/migrations/0002_auto_20200225_1335.py @@ -0,0 +1,27 @@ +# Generated by Django 3.0.3 on 2020-02-25 13:35 + +from django.db import migrations, models +import uuid + + +class Migration(migrations.Migration): + + dependencies = [ + ('opennebula', '0001_initial'), + ] + + operations = [ + migrations.RemoveField( + model_name='vm', + name='uuid', + ), + migrations.RemoveField( + model_name='vm', + name='vmid', + ), + migrations.AddField( + model_name='vm', + name='id', + field=models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False, unique=True), + ), + ] diff --git a/uncloud/opennebula/migrations/0003_auto_20200225_1428.py b/uncloud/opennebula/migrations/0003_auto_20200225_1428.py new file mode 100644 index 0000000..8bb3d8d --- /dev/null +++ b/uncloud/opennebula/migrations/0003_auto_20200225_1428.py @@ -0,0 +1,19 @@ +# Generated by Django 3.0.3 on 2020-02-25 14:28 + +from django.db import migrations, models +import uuid + + +class Migration(migrations.Migration): + + dependencies = [ + ('opennebula', '0002_auto_20200225_1335'), + ] + + operations = [ + migrations.AlterField( + model_name='vm', + name='id', + field=models.CharField(default=uuid.uuid4, max_length=64, primary_key=True, serialize=False, unique=True), + ), + ] diff --git a/uncloud/opennebula/models.py b/uncloud/opennebula/models.py index 0b0f307..904699d 100644 --- a/uncloud/opennebula/models.py +++ b/uncloud/opennebula/models.py @@ -1,15 +1,17 @@ import uuid from django.db import models from django.contrib.auth import get_user_model - from django.contrib.postgres.fields import JSONField + class VM(models.Model): - vmid = models.IntegerField(primary_key=True) - uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True) + id = models.CharField(primary_key=True, editable=True, default=uuid.uuid4, unique=True, max_length=64) owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE) data = JSONField() + def save(self, *args, **kwargs): + self.id = 'opennebula' + str(self.data.get("ID")) + super().save(*args, **kwargs) @property def cores(self): @@ -48,3 +50,11 @@ class VM(models.Model): ] return disks + + @property + def last_host(self): + return ((self.data.get('HISTORY_RECORDS', {}) or {}).get('HISTORY', {}) or {}).get('HOSTNAME', None) + + @property + def graphics(self): + return self.data.get('TEMPLATE', {}).get('GRAPHICS', {}) diff --git a/uncloud/opennebula/serializers.py b/uncloud/opennebula/serializers.py index 30bd20a..6bfaf56 100644 --- a/uncloud/opennebula/serializers.py +++ b/uncloud/opennebula/serializers.py @@ -5,10 +5,10 @@ from opennebula.models import VM class VMSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = VM - fields = ['vmid', 'owner', 'data'] + fields = ['id', 'owner', 'data'] class OpenNebulaVMSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = VM - fields = ['vmid', 'owner', 'cores', 'ram_in_gb', 'disks' ] + fields = ['id', 'owner', 'cores', 'ram_in_gb', 'disks', 'last_host', 'graphics'] diff --git a/uncloud/opennebula/views.py b/uncloud/opennebula/views.py index 66269c7..61ed5a4 100644 --- a/uncloud/opennebula/views.py +++ b/uncloud/opennebula/views.py @@ -22,6 +22,6 @@ class VMViewSet(viewsets.ViewSet): def retrieve(self, request, pk=None): queryset = VM.objects.filter(owner=request.user) - user = get_object_or_404(queryset, pk=pk) - serializer = OpenNebulaVMSerializer(queryset) + vm = get_object_or_404(queryset, pk=pk) + serializer = OpenNebulaVMSerializer(vm, context={'request': request}) return Response(serializer.data) diff --git a/uncloud/uncloud/settings.py b/uncloud/uncloud/settings.py index e8530e7..91d2f73 100644 --- a/uncloud/uncloud/settings.py +++ b/uncloud/uncloud/settings.py @@ -148,6 +148,7 @@ AUTH_USER_MODEL = 'uncloud_auth.User' # AUTH/REST REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': [ + 'rest_framework.authentication.BasicAuthentication', 'rest_framework.authentication.SessionAuthentication', ] }