Autoinstallers for Debian Buster and Ubuntu Bionic
[invirt/packages/invirt-web.git] / code / validation.py
index 4782fdb..da01d8d 100755 (executable)
@@ -16,7 +16,7 @@ MIN_DISK_SINGLE = 0.1
 class Validate:
     def __init__(self, username, state, machine_id=None, name=None, description=None, owner=None,
                  admin=None, contact=None, memory=None, disksize=None,
-                 vmtype=None, nictype=None, cdrom=None, autoinstall=None, strict=False):
+                 vmtype=None, cdrom=None, autoinstall=None, strict=False):
         # XXX Successive quota checks aren't a good idea, since you
         # can't necessarily change the locker and disk size at the
         # same time.
@@ -56,8 +56,6 @@ class Validate:
             self.disksize = validDisk(self.owner, state, disksize, machine)
         if vmtype is not None:
             self.vmtype = validVmType(vmtype)
-        if nictype is not None:
-            self.nictype = validNICType(nictype)
         if cdrom is not None:
             if not CDROM.query.get(cdrom):
                 raise CodeError("Invalid cdrom type '%s'" % cdrom)
@@ -101,19 +99,26 @@ def maxDisk(owner, machine=None):
     """Return the maximum disk that a machine can reach.
 
     If machine is None, the maximum disk for a new machine. Otherwise,
-    return the maximum that a given machine can be changed to.
+    return the maximum that a given machine can be changed to.  If the
+    disk currently exceeds the quotas, it can be changed to anything up
+    to its current size.
     """
     (quota_total, quota_single) = Owner.getDiskQuotas(machine.owner if machine else owner)
 
-    if machine is not None:
+    machine_id, current_size = None, 0
+    if machine is not None and machine.disks:
         machine_id = machine.machine_id
-    else:
-        machine_id = None
+        # XXX The machine modification form doesn't currently handle the
+        # case of machines with multiple disks.  It simply addresses the "first"
+        # disk (which is possibly nondeterministic and wrong).  Since we're
+        # doing validation for that form, we have to use the same logic it
+        # does.
+        current_size = machine.disks[0].size / 1024.
     disk_usage_query = Disk.query.filter(Disk.machine_id != machine_id).\
         join('machine').filter_by(owner=owner)
 
     disk_usage = sum([m.size for m in disk_usage_query]) or 0
-    return min(quota_single, quota_total-disk_usage/1024.)
+    return max(current_size, min(quota_single, quota_total-disk_usage/1024.))
 
 def cantAddVm(owner, g):
     machines = getMachinesByOwner(owner)
@@ -189,14 +194,6 @@ def validVmType(vm_type):
         raise CodeError("Invalid vm type '%s'"  % vm_type)
     return t
 
-def validNICType(nic_type):
-    if nic_type is None:
-        return None
-    t = nic_type
-    if not (t is "e1000" or t is "pcnet"):
-        raise CodeError("Invalid nic type '%s'"  % nic_type)
-    return t
-
 def testMachineId(user, state, machine_id, exists=True):
     """Parse, validate and check authorization for a given user and machine.
 
@@ -254,7 +251,8 @@ def testOwner(user, owner, machine=None):
     try:
         if user not in authz.expandOwner(owner):
             raise InvalidInput('owner', owner, 'You do not have access to the '
-                               + owner + ' locker')
+                               + owner + ' locker (Is system:anyuser missing '
+                               + 'the l permission?)')
     except getafsgroups.AfsProcessError, e:
         raise InvalidInput('owner', owner, str(e))
     return owner