Replace (vmid,uuid) with id in VM model + Add last_host and graphics in VM model + Fixed retrieve view in uncloud.opennebula

This commit is contained in:
ahmadbilalkhalid 2020-02-25 21:03:20 +05:00
parent c7252cde53
commit d658b9635d
7 changed files with 66 additions and 9 deletions

View file

@ -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:

View file

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

View file

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

View file

@ -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', {})

View file

@ -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']

View file

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

View file

@ -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',
]
}