Migration fixed + opennebula/views.py fixed

This commit is contained in:
ahmadbilalkhalid 2020-02-23 23:00:42 +05:00
parent b3e505d37c
commit 739bd72526
10 changed files with 17 additions and 120 deletions

View file

@ -11,6 +11,7 @@ from opennebula.models import VM as VMModel
import uncloud.secrets import uncloud.secrets
class Command(BaseCommand): class Command(BaseCommand):
help = 'Syncronize VM information from OpenNebula' help = 'Syncronize VM information from OpenNebula'
@ -24,6 +25,7 @@ class Command(BaseCommand):
) )
if success: if success:
vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM'] vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM']
unknown_user_with_email = set()
for i, vm in enumerate(vms): for i, vm in enumerate(vms):
vm_id = vm['ID'] vm_id = vm['ID']
vm_owner_email = vm['UNAME'] vm_owner_email = vm['UNAME']
@ -31,7 +33,7 @@ class Command(BaseCommand):
try: try:
user = get_user_model().objects.get(email=vm_owner_email) user = get_user_model().objects.get(email=vm_owner_email)
except get_user_model().DoesNotExist: 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 continue
# user = get_user_model().objects.create_user(username=vm_owner) # user = get_user_model().objects.create_user(username=vm_owner)
@ -40,7 +42,7 @@ class Command(BaseCommand):
'owner': user }, 'owner': user },
vmid=vm_id vmid=vm_id
) )
print('User with email but no username:', unknown_user_with_email)
else: else:
print(response) print(response)
print(uncloud.secrets.OPENNEBULA_USER_PASS) print(uncloud.secrets.OPENNEBULA_USER_PASS)

View file

@ -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 from django.conf import settings
import django.contrib.postgres.fields.jsonb import django.contrib.postgres.fields.jsonb
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
import uuid
class Migration(migrations.Migration): class Migration(migrations.Migration):
@ -19,6 +20,7 @@ class Migration(migrations.Migration):
name='VM', name='VM',
fields=[ fields=[
('vmid', models.IntegerField(primary_key=True, serialize=False)), ('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()), ('data', django.contrib.postgres.fields.jsonb.JSONField()),
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), ('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
], ],

View file

@ -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),
),
]

View file

@ -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),
),
]

View file

@ -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),
),
]

View file

@ -1,11 +1,10 @@
from rest_framework import viewsets, generics, permissions from rest_framework import viewsets, permissions
from rest_framework.response import Response from rest_framework.response import Response
from django.contrib.auth import get_user_model
from .models import VM from .models import VM
from .serializers import VMSerializer, OpenNebulaVMSerializer from .serializers import VMSerializer, OpenNebulaVMSerializer
class RawVMViewSet(viewsets.ModelViewSet): class RawVMViewSet(viewsets.ModelViewSet):
queryset = VM.objects.all() queryset = VM.objects.all()
serializer_class = VMSerializer serializer_class = VMSerializer
@ -14,14 +13,15 @@ class RawVMViewSet(viewsets.ModelViewSet):
class VMViewSet(viewsets.ModelViewSet): class VMViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated] permission_classes = [permissions.IsAuthenticated]
serializer_class = OpenNebulaVMSerializer
def get_queryset(self):
return VM.objects.filter(owner=self.request.user)
def list(self, request): def list(self, request):
queryset = VM.objects.filter(owner=request.user) serializer = OpenNebulaVMSerializer(self.queryset, many=True, context={'request': request})
serializer = OpenNebulaVMSerializer(queryset, many=True, context={'request': request})
return Response(serializer.data) return Response(serializer.data)
def retrieve(self, request, pk=None): def retrieve(self, request, pk=None):
queryset = VM.objects.filter(owner=request.user) serializer = OpenNebulaVMSerializer(self.queryset)
user = get_object_or_404(queryset, pk=pk)
serializer = OpenNebulaVMSerializer(queryset)
return Response(serializer.data) return Response(serializer.data)

View file

@ -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.conf import settings
from django.db import migrations, models from django.db import migrations, models

View file

@ -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),
),
]

View file

@ -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.models
import django.contrib.auth.validators import django.contrib.auth.validators