2020-02-23 10:55:57 +00:00
|
|
|
import uuid
|
2020-02-21 15:33:37 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.contrib.auth import get_user_model
|
2020-02-23 10:42:03 +00:00
|
|
|
from django.contrib.postgres.fields import JSONField
|
2020-02-21 15:33:37 +00:00
|
|
|
|
2020-02-25 16:03:20 +00:00
|
|
|
|
2020-02-21 15:33:37 +00:00
|
|
|
class VM(models.Model):
|
2020-02-25 18:23:39 +00:00
|
|
|
vmid = models.IntegerField(primary_key=True)
|
2020-02-21 15:33:37 +00:00
|
|
|
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
|
2020-02-23 10:42:03 +00:00
|
|
|
data = JSONField()
|
|
|
|
|
2020-02-25 16:03:20 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.id = 'opennebula' + str(self.data.get("ID"))
|
|
|
|
super().save(*args, **kwargs)
|
2020-02-23 10:42:03 +00:00
|
|
|
|
2020-02-23 10:55:57 +00:00
|
|
|
@property
|
2020-02-23 10:42:03 +00:00
|
|
|
def cores(self):
|
2020-02-23 10:55:57 +00:00
|
|
|
return int(self.data['TEMPLATE']['VCPU'])
|
2020-02-23 10:42:03 +00:00
|
|
|
|
2020-02-23 14:09:58 +00:00
|
|
|
@property
|
2020-02-23 10:42:03 +00:00
|
|
|
def ram_in_gb(self):
|
|
|
|
return (int(self.data['TEMPLATE']['MEMORY'])/1024.)
|
|
|
|
|
2020-02-23 10:55:57 +00:00
|
|
|
@property
|
2020-02-23 10:42:03 +00:00
|
|
|
def disks(self):
|
|
|
|
"""
|
|
|
|
If there is no disk then the key DISK does not exist.
|
|
|
|
|
|
|
|
If there is only one disk, we have a dictionary in the database.
|
|
|
|
|
|
|
|
If there are multiple disks, we have a list of dictionaries in the database.
|
|
|
|
"""
|
|
|
|
|
2020-02-23 14:33:26 +00:00
|
|
|
disks = []
|
|
|
|
|
|
|
|
if 'DISK' in self.data['TEMPLATE']:
|
|
|
|
if type(self.data['TEMPLATE']['DISK']) is dict:
|
|
|
|
disks = [ self.data['TEMPLATE']['DISK'] ]
|
|
|
|
else:
|
|
|
|
disks = self.data['TEMPLATE']['DISK']
|
|
|
|
|
|
|
|
disks = [
|
|
|
|
{
|
|
|
|
'size_in_gb': int(d['SIZE'])/1024. ,
|
|
|
|
'opennebula_source': d['SOURCE'],
|
|
|
|
'opennebula_name': d['IMAGE'],
|
|
|
|
}
|
|
|
|
for d in disks
|
|
|
|
]
|
|
|
|
|
|
|
|
return disks
|
2020-02-25 16:03:20 +00:00
|
|
|
|
|
|
|
@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', {})
|