From b3e505d37cc1f267c6ee7752fb85f8aa440be5be Mon Sep 17 00:00:00 2001 From: meow Date: Sun, 23 Feb 2020 21:34:22 +0500 Subject: [PATCH 1/5] migration fix --- uncloud/opennebula/migrations/0004_auto_20200222_0713.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uncloud/opennebula/migrations/0004_auto_20200222_0713.py b/uncloud/opennebula/migrations/0004_auto_20200222_0713.py index a298c06..89913cb 100644 --- a/uncloud/opennebula/migrations/0004_auto_20200222_0713.py +++ b/uncloud/opennebula/migrations/0004_auto_20200222_0713.py @@ -7,7 +7,7 @@ import uuid class Migration(migrations.Migration): dependencies = [ - ('opennebula', '0003_auto_20200221_1113'), + ('opennebula', '0003_auto_20200223_1058'), ] operations = [ From 739bd7252612ccbb9f3f72183084effd0e743099 Mon Sep 17 00:00:00 2001 From: meow Date: Sun, 23 Feb 2020 23:00:42 +0500 Subject: [PATCH 2/5] Migration fixed + opennebula/views.py fixed --- .../opennebula/management/commands/syncvm.py | 6 ++- uncloud/opennebula/migrations/0001_initial.py | 4 +- uncloud/opennebula/migrations/0002_vm_uuid.py | 19 -------- .../migrations/0003_auto_20200223_1058.py | 19 -------- .../migrations/0004_auto_20200222_0713.py | 23 ---------- uncloud/opennebula/views.py | 16 +++---- .../uncloud_api/migrations/0001_initial.py | 2 +- .../migrations/0002_auto_20200222_0719.py | 46 ------------------- .../uncloud_auth/migrations/0001_initial.py | 2 +- uncloud/uncloud_vm/migrations/__init__.py | 0 10 files changed, 17 insertions(+), 120 deletions(-) delete mode 100644 uncloud/opennebula/migrations/0002_vm_uuid.py delete mode 100644 uncloud/opennebula/migrations/0003_auto_20200223_1058.py delete mode 100644 uncloud/opennebula/migrations/0004_auto_20200222_0713.py delete mode 100644 uncloud/uncloud_api/migrations/0002_auto_20200222_0719.py delete mode 100644 uncloud/uncloud_vm/migrations/__init__.py diff --git a/uncloud/opennebula/management/commands/syncvm.py b/uncloud/opennebula/management/commands/syncvm.py index 795d53a..f5f80b1 100644 --- a/uncloud/opennebula/management/commands/syncvm.py +++ b/uncloud/opennebula/management/commands/syncvm.py @@ -11,6 +11,7 @@ from opennebula.models import VM as VMModel import uncloud.secrets + class Command(BaseCommand): help = 'Syncronize VM information from OpenNebula' @@ -24,6 +25,7 @@ class Command(BaseCommand): ) if success: vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM'] + unknown_user_with_email = set() for i, vm in enumerate(vms): vm_id = vm['ID'] vm_owner_email = vm['UNAME'] @@ -31,7 +33,7 @@ class Command(BaseCommand): try: user = get_user_model().objects.get(email=vm_owner_email) except get_user_model().DoesNotExist: - print("Skipping VM import for unknown user with email: {}".format(vm_owner_email)) + unknown_user_with_email.add(vm_owner_email) continue # user = get_user_model().objects.create_user(username=vm_owner) @@ -40,7 +42,7 @@ class Command(BaseCommand): 'owner': user }, vmid=vm_id ) - + print('User with email but no username:', unknown_user_with_email) else: print(response) print(uncloud.secrets.OPENNEBULA_USER_PASS) diff --git a/uncloud/opennebula/migrations/0001_initial.py b/uncloud/opennebula/migrations/0001_initial.py index f1d3d6b..4c0527a 100644 --- a/uncloud/opennebula/migrations/0001_initial.py +++ b/uncloud/opennebula/migrations/0001_initial.py @@ -1,9 +1,10 @@ -# Generated by Django 3.0.3 on 2020-02-23 10:02 +# Generated by Django 3.0.3 on 2020-02-23 17:12 from django.conf import settings import django.contrib.postgres.fields.jsonb from django.db import migrations, models import django.db.models.deletion +import uuid class Migration(migrations.Migration): @@ -19,6 +20,7 @@ class Migration(migrations.Migration): name='VM', fields=[ ('vmid', models.IntegerField(primary_key=True, serialize=False)), + ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, unique=True)), ('data', django.contrib.postgres.fields.jsonb.JSONField()), ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), ], diff --git a/uncloud/opennebula/migrations/0002_vm_uuid.py b/uncloud/opennebula/migrations/0002_vm_uuid.py deleted file mode 100644 index 595fd05..0000000 --- a/uncloud/opennebula/migrations/0002_vm_uuid.py +++ /dev/null @@ -1,19 +0,0 @@ -# Generated by Django 3.0.3 on 2020-02-23 10:55 - -from django.db import migrations, models -import uuid - - -class Migration(migrations.Migration): - - dependencies = [ - ('opennebula', '0001_initial'), - ] - - operations = [ - migrations.AddField( - model_name='vm', - name='uuid', - field=models.UUIDField(default=uuid.uuid4, editable=False), - ), - ] diff --git a/uncloud/opennebula/migrations/0003_auto_20200223_1058.py b/uncloud/opennebula/migrations/0003_auto_20200223_1058.py deleted file mode 100644 index d2173da..0000000 --- a/uncloud/opennebula/migrations/0003_auto_20200223_1058.py +++ /dev/null @@ -1,19 +0,0 @@ -# Generated by Django 3.0.3 on 2020-02-23 10:58 - -from django.db import migrations, models -import uuid - - -class Migration(migrations.Migration): - - dependencies = [ - ('opennebula', '0002_vm_uuid'), - ] - - operations = [ - migrations.AlterField( - model_name='vm', - name='uuid', - field=models.UUIDField(default=uuid.uuid4, editable=False, unique=True), - ), - ] diff --git a/uncloud/opennebula/migrations/0004_auto_20200222_0713.py b/uncloud/opennebula/migrations/0004_auto_20200222_0713.py deleted file mode 100644 index 89913cb..0000000 --- a/uncloud/opennebula/migrations/0004_auto_20200222_0713.py +++ /dev/null @@ -1,23 +0,0 @@ -# Generated by Django 3.0.3 on 2020-02-22 07:13 - -from django.db import migrations, models -import uuid - - -class Migration(migrations.Migration): - - dependencies = [ - ('opennebula', '0003_auto_20200223_1058'), - ] - - operations = [ - migrations.RemoveField( - model_name='vm', - name='id', - ), - migrations.AddField( - model_name='vm', - name='uuid', - field=models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False), - ), - ] diff --git a/uncloud/opennebula/views.py b/uncloud/opennebula/views.py index 29fdb64..5498928 100644 --- a/uncloud/opennebula/views.py +++ b/uncloud/opennebula/views.py @@ -1,11 +1,10 @@ -from rest_framework import viewsets, generics, permissions +from rest_framework import viewsets, permissions from rest_framework.response import Response -from django.contrib.auth import get_user_model - from .models import VM from .serializers import VMSerializer, OpenNebulaVMSerializer + class RawVMViewSet(viewsets.ModelViewSet): queryset = VM.objects.all() serializer_class = VMSerializer @@ -14,14 +13,15 @@ class RawVMViewSet(viewsets.ModelViewSet): class VMViewSet(viewsets.ModelViewSet): permission_classes = [permissions.IsAuthenticated] + serializer_class = OpenNebulaVMSerializer + + def get_queryset(self): + return VM.objects.filter(owner=self.request.user) def list(self, request): - queryset = VM.objects.filter(owner=request.user) - serializer = OpenNebulaVMSerializer(queryset, many=True, context={'request': request}) + serializer = OpenNebulaVMSerializer(self.queryset, many=True, context={'request': request}) return Response(serializer.data) def retrieve(self, request, pk=None): - queryset = VM.objects.filter(owner=request.user) - user = get_object_or_404(queryset, pk=pk) - serializer = OpenNebulaVMSerializer(queryset) + serializer = OpenNebulaVMSerializer(self.queryset) return Response(serializer.data) diff --git a/uncloud/uncloud_api/migrations/0001_initial.py b/uncloud/uncloud_api/migrations/0001_initial.py index d8d9630..c549a9d 100644 --- a/uncloud/uncloud_api/migrations/0001_initial.py +++ b/uncloud/uncloud_api/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 3.0.3 on 2020-02-23 10:16 +# Generated by Django 3.0.3 on 2020-02-23 17:12 from django.conf import settings from django.db import migrations, models diff --git a/uncloud/uncloud_api/migrations/0002_auto_20200222_0719.py b/uncloud/uncloud_api/migrations/0002_auto_20200222_0719.py deleted file mode 100644 index a52eade..0000000 --- a/uncloud/uncloud_api/migrations/0002_auto_20200222_0719.py +++ /dev/null @@ -1,46 +0,0 @@ -# Generated by Django 3.0.3 on 2020-02-22 07:19 - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('uncloud_api', '0001_initial'), - ] - - operations = [ - migrations.CreateModel( - name='VMSnapshotOrder', - fields=[ - ('order_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='uncloud_api.Order')), - ], - bases=('uncloud_api.order',), - ), - migrations.CreateModel( - name='VMSnapshotProduct', - fields=[ - ('product_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='uncloud_api.Product')), - ('gb_ssd', models.FloatField()), - ('gb_hdd', models.FloatField()), - ], - bases=('uncloud_api.product',), - ), - migrations.DeleteModel( - name='OrderReference', - ), - migrations.RemoveField( - model_name='product', - name='name', - ), - migrations.RemoveField( - model_name='product', - name='recurring_period', - ), - migrations.AddField( - model_name='product', - name='status', - field=models.CharField(choices=[('pending', 'Pending'), ('being_created', 'Being created'), ('created_active', 'Created'), ('deleted', 'Deleted')], default='pending', max_length=256), - ), - ] diff --git a/uncloud/uncloud_auth/migrations/0001_initial.py b/uncloud/uncloud_auth/migrations/0001_initial.py index a3ade55..63885c4 100644 --- a/uncloud/uncloud_auth/migrations/0001_initial.py +++ b/uncloud/uncloud_auth/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 3.0.3 on 2020-02-23 10:02 +# Generated by Django 3.0.3 on 2020-02-23 17:11 import django.contrib.auth.models import django.contrib.auth.validators diff --git a/uncloud/uncloud_vm/migrations/__init__.py b/uncloud/uncloud_vm/migrations/__init__.py deleted file mode 100644 index e69de29..0000000 From a72bc142a68bcbe9f5a339e723fb3b3db0a5dfcc Mon Sep 17 00:00:00 2001 From: meow Date: Tue, 25 Feb 2020 11:50:49 +0500 Subject: [PATCH 3/5] Fixed issues in opennebula/views.py + syncvm now behaves correctly and print users which are not in ldap as per their email address --- .../opennebula/management/commands/syncvm.py | 54 ++++++++++++------- uncloud/opennebula/views.py | 14 ++--- uncloud/requirements.txt | 1 + 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/uncloud/opennebula/management/commands/syncvm.py b/uncloud/opennebula/management/commands/syncvm.py index f5f80b1..00108f0 100644 --- a/uncloud/opennebula/management/commands/syncvm.py +++ b/uncloud/opennebula/management/commands/syncvm.py @@ -1,15 +1,22 @@ -import os import json +import uncloud.secrets as secrets + + +from xmlrpc.client import ServerProxy as RPCClient + from django.core.management.base import BaseCommand from django.contrib.auth import get_user_model -from xmlrpc.client import ServerProxy as RPCClient - from xmltodict import parse +from ungleich_common.ldap.ldap_manager import LdapManager from opennebula.models import VM as VMModel -import uncloud.secrets + +def find_user_based_on_email(users, email): + for user in users: + if email in user.mail.values: + return user class Command(BaseCommand): @@ -19,30 +26,39 @@ class Command(BaseCommand): pass def handle(self, *args, **options): - with RPCClient(uncloud.secrets.OPENNEBULA_URL) as rpc_client: + ldap_server_uri = secrets.LDAP_SERVER_URI.split(',')[0] + ldap_manager = LdapManager( + server=ldap_server_uri, + admin_dn=secrets.LDAP_ADMIN_DN, + admin_password=secrets.LDAP_ADMIN_PASSWORD, + ) + users = ldap_manager.get('') # Get all users + + with RPCClient(secrets.OPENNEBULA_URL) as rpc_client: success, response, *_ = rpc_client.one.vmpool.infoextended( - uncloud.secrets.OPENNEBULA_USER_PASS, -2, -1, -1, -1 + secrets.OPENNEBULA_USER_PASS, -2, -1, -1, -1 ) if success: vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM'] unknown_user_with_email = set() - for i, vm in enumerate(vms): + + for vm in vms: vm_id = vm['ID'] vm_owner_email = vm['UNAME'] - try: - user = get_user_model().objects.get(email=vm_owner_email) - except get_user_model().DoesNotExist: + user = find_user_based_on_email(users, vm_owner_email) + if not user: unknown_user_with_email.add(vm_owner_email) - continue - # user = get_user_model().objects.create_user(username=vm_owner) + else: + try: + 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 }, - vmid=vm_id - ) - print('User with email but no username:', unknown_user_with_email) + VMModel.objects.update_or_create( + defaults={'data': vm, 'owner': user_in_db}, vmid=vm_id + ) + print('User with email but not found in ldap:', unknown_user_with_email) else: print(response) - print(uncloud.secrets.OPENNEBULA_USER_PASS) + print(secrets.OPENNEBULA_USER_PASS) diff --git a/uncloud/opennebula/views.py b/uncloud/opennebula/views.py index 5498928..66269c7 100644 --- a/uncloud/opennebula/views.py +++ b/uncloud/opennebula/views.py @@ -1,5 +1,6 @@ from rest_framework import viewsets, permissions from rest_framework.response import Response +from django.shortcuts import get_object_or_404 from .models import VM from .serializers import VMSerializer, OpenNebulaVMSerializer @@ -11,17 +12,16 @@ class RawVMViewSet(viewsets.ModelViewSet): permission_classes = [permissions.IsAdminUser] -class VMViewSet(viewsets.ModelViewSet): +class VMViewSet(viewsets.ViewSet): permission_classes = [permissions.IsAuthenticated] - serializer_class = OpenNebulaVMSerializer - - def get_queryset(self): - return VM.objects.filter(owner=self.request.user) def list(self, request): - serializer = OpenNebulaVMSerializer(self.queryset, many=True, context={'request': request}) + queryset = VM.objects.filter(owner=request.user) + serializer = OpenNebulaVMSerializer(queryset, many=True, context={'request': request}) return Response(serializer.data) def retrieve(self, request, pk=None): - serializer = OpenNebulaVMSerializer(self.queryset) + queryset = VM.objects.filter(owner=request.user) + user = get_object_or_404(queryset, pk=pk) + serializer = OpenNebulaVMSerializer(queryset) return Response(serializer.data) diff --git a/uncloud/requirements.txt b/uncloud/requirements.txt index 11ab309..e79f479 100644 --- a/uncloud/requirements.txt +++ b/uncloud/requirements.txt @@ -3,3 +3,4 @@ djangorestframework django-auth-ldap stripe xmltodict +git+https://code.ungleich.ch/ahmedbilal/ungleich-common/#egg=ungleich-common-ldap&subdirectory=ldap From c7252cde5312046492514e6a271aa7eccbbfed24 Mon Sep 17 00:00:00 2001 From: meow Date: Tue, 25 Feb 2020 13:09:54 +0500 Subject: [PATCH 4/5] Introduced local settings in meow-pay/uncloud django app --- uncloud/.gitignore | 3 +- uncloud/uncloud/settings.py | 57 ++++++++++--------------------------- 2 files changed, 17 insertions(+), 43 deletions(-) diff --git a/uncloud/.gitignore b/uncloud/.gitignore index 4ade18f..71202e1 100644 --- a/uncloud/.gitignore +++ b/uncloud/.gitignore @@ -1,3 +1,4 @@ db.sqlite3 uncloud/secrets.py -debug.log \ No newline at end of file +debug.log +uncloud/local_settings.py \ No newline at end of file diff --git a/uncloud/uncloud/settings.py b/uncloud/uncloud/settings.py index 5ce8e92..e8530e7 100644 --- a/uncloud/uncloud/settings.py +++ b/uncloud/uncloud/settings.py @@ -12,18 +12,26 @@ https://docs.djangoproject.com/en/3.0/ref/settings/ import os +import stripe +import ldap # Uncommitted file with secrets import uncloud.secrets -import stripe -import ldap - -import uncloud.secrets as secrets - from django_auth_ldap.config import LDAPSearch - +# Uncommitted file with local settings i.e logging +try: + from uncloud.local_settings import LOGGING, DATABASES +except ModuleNotFoundError: + LOGGING = {} + # https://docs.djangoproject.com/en/3.0/ref/settings/#databases + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': uncloud.secrets.POSTGRESQL_DB_NAME, + } + } # Build paths inside the project like this: os.path.join(BASE_DIR, ...) @@ -88,8 +96,6 @@ TEMPLATES = [ WSGI_APPLICATION = 'uncloud.wsgi.application' - - # Password validation # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators @@ -167,37 +173,4 @@ USE_TZ = True STATIC_URL = '/static/' -stripe.api_key = secrets.STRIPE_KEY - -# FIXME: not sure if we really need this -LOGGING = { - 'version': 1, - 'disable_existing_loggers': False, - 'handlers': { - 'file': { - 'level': 'DEBUG', - 'class': 'logging.FileHandler', - 'filename': 'debug.log', - }, - }, - 'loggers': { - 'django': { - 'handlers': ['file'], - 'level': 'DEBUG', - 'propagate': True, - }, - 'django_auth_ldap': { - 'handlers': ['file'], - 'level': 'DEBUG', - 'propagate': True - } - }, -} - -# https://docs.djangoproject.com/en/3.0/ref/settings/#databases -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.postgresql', - 'NAME': uncloud.secrets.POSTGRESQL_DB_NAME, - } -} +stripe.api_key = uncloud.secrets.STRIPE_KEY From d658b9635dff80764c2edbe1580bdcccbc32d438 Mon Sep 17 00:00:00 2001 From: meow Date: Tue, 25 Feb 2020 21:03:20 +0500 Subject: [PATCH 5/5] 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', ] }