return machine
-def create(user, fields):
- name = fields.getfirst('name')
- if not validMachineName(name):
- raise MyException("Invalid name '%s'" % name)
- name = user.username + '_' + name.lower()
-
- if Machine.get_by(name=name):
- raise MyException("A machine named '%s' already exists" % name)
-
- memory = fields.getfirst('memory')
+def validMemory(user, memory, machine=None):
try:
memory = int(memory)
if memory <= 0:
raise ValueError
except ValueError:
raise MyException("Invalid memory amount")
- if memory > maxMemory(user):
+ if memory > maxMemory(user, machine):
raise MyException("Too much memory requested")
-
- disk = fields.getfirst('disk')
+ return memory
+
+def validDisk(user, disk, machine=None):
try:
disk = float(disk)
- if disk > maxDisk(user):
+ if disk > maxDisk(user, machine):
raise MyException("Too much disk requested")
disk = int(disk * 1024)
if disk <= 0:
raise ValueError
except ValueError:
raise MyException("Invalid disk amount")
+ return disk
+
+def create(user, fields):
+ name = fields.getfirst('name')
+ if not validMachineName(name):
+ raise MyException("Invalid name '%s'" % name)
+ name = user.username + '_' + name.lower()
+
+ if Machine.get_by(name=name):
+ raise MyException("A machine named '%s' already exists" % name)
+
+ memory = fields.getfirst('memory')
+ memory = validMemory(user, memory)
+ disk = fields.getfirst('disk')
+ disk = validDisk(user, disk)
+
vm_type = fields.getfirst('vmtype')
if vm_type not in ('hvm', 'paravm'):
raise MyException("Invalid vm type '%s'" % vm_type)