2016-12-20 23:05:20 +00:00
|
|
|
from django.db import models
|
|
|
|
|
2017-02-15 04:34:06 +00:00
|
|
|
|
|
|
|
class BetaAccessVMType(models.Model):
|
|
|
|
ssd = models.IntegerField()
|
|
|
|
ram = models.IntegerField()
|
|
|
|
cpu = models.IntegerField()
|
|
|
|
price = models.FloatField()
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-02-17 03:32:14 +00:00
|
|
|
return "ID: %s - SSD %s - RAM %s - CPU %s - Price %s " % \
|
2017-07-26 17:38:38 +00:00
|
|
|
(self.id, str(self.ssd), self.ram, self.cpu, self.price)
|
2016-12-20 23:05:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BetaAccess(models.Model):
|
|
|
|
email = models.CharField(max_length=250)
|
2017-03-31 00:51:15 +00:00
|
|
|
name = models.CharField(max_length=250)
|
2017-07-26 17:38:38 +00:00
|
|
|
|
2017-02-15 04:34:06 +00:00
|
|
|
# vm = models.ForeignKey(BetaAccessVM)
|
2016-12-20 23:05:20 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2017-02-15 04:34:06 +00:00
|
|
|
vms = self.betaaccessvm_set.all()
|
|
|
|
rep = "Email: %s " % self.email
|
|
|
|
for vm in vms:
|
|
|
|
rep += "(vm:%s - amount:%s) - " % (vm.type.id, vm.amount)
|
|
|
|
return rep
|
|
|
|
|
|
|
|
|
|
|
|
class BetaAccessVM(models.Model):
|
|
|
|
type = models.ForeignKey(BetaAccessVMType)
|
|
|
|
access = models.ForeignKey(BetaAccess)
|
|
|
|
amount = models.IntegerField()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create(cls, data):
|
|
|
|
VM_KEY_ID = 0
|
|
|
|
VM_AMOUNT = 1
|
2017-02-17 03:32:14 +00:00
|
|
|
ZERO = 0
|
2017-02-15 04:34:06 +00:00
|
|
|
email = data.get('email')
|
|
|
|
beta_access = BetaAccess.objects.create(email=email)
|
|
|
|
vm_data = [(key, value) for key, value in data.items() if 'vm' in key]
|
2017-02-17 03:32:14 +00:00
|
|
|
created_vms = []
|
2017-02-15 04:34:06 +00:00
|
|
|
for vm in vm_data:
|
2017-02-17 03:32:14 +00:00
|
|
|
if int(vm[VM_AMOUNT]) == ZERO:
|
|
|
|
continue
|
2017-02-15 04:34:06 +00:00
|
|
|
vm_id = vm[VM_KEY_ID].split('-').pop()
|
|
|
|
vm_type = BetaAccessVMType.objects.get(id=vm_id)
|
2017-02-17 03:32:14 +00:00
|
|
|
created_vms.append(cls.objects.create(access=beta_access,
|
|
|
|
amount=vm[VM_AMOUNT], type=vm_type))
|
|
|
|
|
|
|
|
return created_vms
|
2017-07-26 17:38:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VMTemplate(models.Model):
|
|
|
|
name = models.CharField(max_length=50)
|
2017-07-26 18:35:12 +00:00
|
|
|
opennebula_vm_template_id = models.IntegerField()
|
2017-07-26 17:38:38 +00:00
|
|
|
|
|
|
|
@classmethod
|
2017-07-26 18:35:12 +00:00
|
|
|
def create(cls, name, opennebula_vm_template_id):
|
|
|
|
vm_template = cls(name=name, opennebula_vm_template_id=opennebula_vm_template_id)
|
2017-07-26 17:38:38 +00:00
|
|
|
return vm_template
|