From 739bd7252612ccbb9f3f72183084effd0e743099 Mon Sep 17 00:00:00 2001 From: meow Date: Sun, 23 Feb 2020 23:00:42 +0500 Subject: [PATCH] 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