uncloud/uncloud/opennebula/models.py

38 lines
1.1 KiB
Python
Raw Normal View History

2020-02-23 10:55:57 +00:00
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)
2020-02-23 10:59:09 +00:00
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
data = JSONField()
2020-02-23 10:55:57 +00:00
@property
def cores(self):
2020-02-23 10:55:57 +00:00
return int(self.data['TEMPLATE']['VCPU'])
2020-02-23 14:09:58 +00:00
@property
def ram_in_gb(self):
return (int(self.data['TEMPLATE']['MEMORY'])/1024.)
2020-02-23 10:55:57 +00:00
@property
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.
"""
if not 'DISK' in self.data['TEMPLATE']['DISK']:
return []
elif type(self.data['TEMPLATE']['DISK']) is dict:
return [ self.data['TEMPLATE']['DISK'] ]
else:
return self.data['TEMPLATE']['DISK']