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:
parent
c7252cde53
commit
d658b9635d
7 changed files with 66 additions and 9 deletions
|
@ -54,9 +54,9 @@ class Command(BaseCommand):
|
||||||
user_in_db = get_user_model().objects.get(email=vm_owner_email)
|
user_in_db = get_user_model().objects.get(email=vm_owner_email)
|
||||||
except get_user_model().DoesNotExist:
|
except get_user_model().DoesNotExist:
|
||||||
user_in_db = get_user_model().objects.create_user(username=user.uid, email=vm_owner_email)
|
user_in_db = get_user_model().objects.create_user(username=user.uid, email=vm_owner_email)
|
||||||
|
|
||||||
VMModel.objects.update_or_create(
|
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)
|
print('User with email but not found in ldap:', unknown_user_with_email)
|
||||||
else:
|
else:
|
||||||
|
|
27
uncloud/opennebula/migrations/0002_auto_20200225_1335.py
Normal file
27
uncloud/opennebula/migrations/0002_auto_20200225_1335.py
Normal 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),
|
||||||
|
),
|
||||||
|
]
|
19
uncloud/opennebula/migrations/0003_auto_20200225_1428.py
Normal file
19
uncloud/opennebula/migrations/0003_auto_20200225_1428.py
Normal 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),
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,15 +1,17 @@
|
||||||
import uuid
|
import uuid
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
|
|
||||||
from django.contrib.postgres.fields import JSONField
|
from django.contrib.postgres.fields import JSONField
|
||||||
|
|
||||||
|
|
||||||
class VM(models.Model):
|
class VM(models.Model):
|
||||||
vmid = models.IntegerField(primary_key=True)
|
id = models.CharField(primary_key=True, editable=True, default=uuid.uuid4, unique=True, max_length=64)
|
||||||
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
|
|
||||||
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
|
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
|
||||||
data = JSONField()
|
data = JSONField()
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
self.id = 'opennebula' + str(self.data.get("ID"))
|
||||||
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cores(self):
|
def cores(self):
|
||||||
|
@ -48,3 +50,11 @@ class VM(models.Model):
|
||||||
]
|
]
|
||||||
|
|
||||||
return disks
|
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', {})
|
||||||
|
|
|
@ -5,10 +5,10 @@ from opennebula.models import VM
|
||||||
class VMSerializer(serializers.HyperlinkedModelSerializer):
|
class VMSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = VM
|
model = VM
|
||||||
fields = ['vmid', 'owner', 'data']
|
fields = ['id', 'owner', 'data']
|
||||||
|
|
||||||
|
|
||||||
class OpenNebulaVMSerializer(serializers.HyperlinkedModelSerializer):
|
class OpenNebulaVMSerializer(serializers.HyperlinkedModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = VM
|
model = VM
|
||||||
fields = ['vmid', 'owner', 'cores', 'ram_in_gb', 'disks' ]
|
fields = ['id', 'owner', 'cores', 'ram_in_gb', 'disks', 'last_host', 'graphics']
|
||||||
|
|
|
@ -22,6 +22,6 @@ class VMViewSet(viewsets.ViewSet):
|
||||||
|
|
||||||
def retrieve(self, request, pk=None):
|
def retrieve(self, request, pk=None):
|
||||||
queryset = VM.objects.filter(owner=request.user)
|
queryset = VM.objects.filter(owner=request.user)
|
||||||
user = get_object_or_404(queryset, pk=pk)
|
vm = get_object_or_404(queryset, pk=pk)
|
||||||
serializer = OpenNebulaVMSerializer(queryset)
|
serializer = OpenNebulaVMSerializer(vm, context={'request': request})
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
|
@ -148,6 +148,7 @@ AUTH_USER_MODEL = 'uncloud_auth.User'
|
||||||
# AUTH/REST
|
# AUTH/REST
|
||||||
REST_FRAMEWORK = {
|
REST_FRAMEWORK = {
|
||||||
'DEFAULT_AUTHENTICATION_CLASSES': [
|
'DEFAULT_AUTHENTICATION_CLASSES': [
|
||||||
|
'rest_framework.authentication.BasicAuthentication',
|
||||||
'rest_framework.authentication.SessionAuthentication',
|
'rest_framework.authentication.SessionAuthentication',
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue