Ignore:
Timestamp:
Jun 2, 2008, 11:25:47 PM (16 years ago)
Author:
ecprice
Message:

Put validation behind more abstraction.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/sipb-xen-www/code/validation.py

    r537 r572  
    66import string
    77from sipb_xen_database import Machine, NIC, Type, Disk
    8 from webcommon import InvalidInput, g
     8from webcommon import InvalidInput
    99
    1010MAX_MEMORY_TOTAL = 512
     
    1717MAX_VMS_ACTIVE = 4
    1818
    19 def getMachinesByOwner(user, machine=None):
     19class Validate:
     20    def __init__(self, username, state, machine_id=None, name=None, owner=None,
     21                 admin=None, contact=None, memory=None, disksize=None,
     22                 vmtype=None, cdrom=None, clone_from=None):
     23        # XXX Successive quota checks aren't a good idea, since you
     24        # can't necessarily change the locker and disk size at the
     25        # same time.
     26        created_new = (machine_id is None)
     27
     28        if machine_id is not None:
     29            self.machine = testMachineId(username, machine_id)
     30        machine = getattr(self, 'machine', None)
     31
     32        owner = testOwner(username, owner, machine)
     33        if owner is not None:
     34            self.owner = owner
     35        admin = testAdmin(username, admin, machine)
     36        if admin is not None:
     37            self.admin = admin
     38        contact = testContact(username, contact, machine)
     39        if contact is not None:
     40            self.contact = contact
     41        name = testName(username, name, machine)
     42        if name is not None:
     43            self.name = name
     44        if memory is not None:
     45            self.memory = validMemory(self.owner, state, memory, machine,
     46                                      on=not created_new)
     47        if disksize is not None:
     48            self.disksize = validDisk(self.owner, disksize, machine)
     49        if vmtype is not None:
     50            self.vmtype = validVmType(vmtype)
     51        if cdrom is not None:
     52            if not CDROM.get(cdrom):
     53                raise CodeError("Invalid cdrom type '%s'" % cdrom)
     54            self.cdrom = cdrom
     55        if clone_from is not None:
     56            if clone_from not in ('ice3', ):
     57                raise CodeError("Invalid clone image '%s'" % clone_from)
     58            self.clone_from = clone_from
     59
     60
     61def getMachinesByOwner(owner, machine=None):
    2062    """Return the machines owned by the same as a machine.
    2163
     
    2567    if machine:
    2668        owner = machine.owner
    27     else:
    28         owner = user
    2969    return Machine.select_by(owner=owner)
    3070
    31 def maxMemory(user, machine=None, on=True):
     71def maxMemory(owner, g, machine=None, on=True):
    3272    """Return the maximum memory for a machine or a user.
    3373
     
    4484    if not on:
    4585        return MAX_MEMORY_SINGLE
    46     machines = getMachinesByOwner(user, machine)
     86    machines = getMachinesByOwner(owner, machine)
    4787    active_machines = [x for x in machines if g.xmlist.get(x)]
    4888    mem_usage = sum([x.memory for x in active_machines if x != machine])
    4989    return min(MAX_MEMORY_SINGLE, MAX_MEMORY_TOTAL-mem_usage)
    5090
    51 def maxDisk(user, machine=None):
     91def maxDisk(owner, machine=None):
    5292    """Return the maximum disk that a machine can reach.
    5393
     
    60100        machine_id = None
    61101    disk_usage = Disk.query().filter_by(Disk.c.machine_id != machine_id,
    62                                         owner=user).sum(Disk.c.size) or 0
     102                                        owner=owner).sum(Disk.c.size) or 0
    63103    return min(MAX_DISK_SINGLE, MAX_DISK_TOTAL-disk_usage/1024.)
    64104
    65 def cantAddVm(user):
    66     machines = getMachinesByOwner(user)
     105def cantAddVm(owner, g):
     106    machines = getMachinesByOwner(owner)
    67107    active_machines = [x for x in machines if g.xmlist.get(x)]
    68108    if len(machines) >= MAX_VMS_TOTAL:
     
    72112                'To create more, turn one off.')
    73113    return False
    74 
    75 def validAddVm(user):
    76     reason = cantAddVm(user)
    77     if reason:
    78         raise InvalidInput('create', True, reason)
    79     return True
    80114
    81115def haveAccess(user, machine):
     
    99133    return True
    100134
    101 def validMemory(user, memory, machine=None, on=True):
    102     """Parse and validate limits for memory for a given user and machine.
     135def validMemory(owner, g, memory, machine=None, on=True):
     136    """Parse and validate limits for memory for a given owner and machine.
    103137
    104138    on is whether the memory must be valid after the machine is
     
    112146        raise InvalidInput('memory', memory,
    113147                           "Minimum %s MiB" % MIN_MEMORY_SINGLE)
    114     if memory > maxMemory(user, machine, on):
     148    max_val = maxMemory(owner, g, machine, on)
     149    if memory > max_val:
    115150        raise InvalidInput('memory', memory,
    116                            'Maximum %s MiB for %s' % (maxMemory(user, machine),
    117                                                       user))
     151                           'Maximum %s MiB for %s' % (max_val, owner))
    118152    return memory
    119153
    120 def validDisk(user, disk, machine=None):
    121     """Parse and validate limits for disk for a given user and machine."""
     154def validDisk(owner, disk, machine=None):
     155    """Parse and validate limits for disk for a given owner and machine."""
    122156    try:
    123157        disk = float(disk)
    124         if disk > maxDisk(user, machine):
     158        if disk > maxDisk(owner, machine):
    125159            raise InvalidInput('disk', disk,
    126                                "Maximum %s G" % maxDisk(user, machine))
     160                               "Maximum %s G" % maxDisk(owner, machine))
    127161        disk = int(disk * 1024)
    128162        if disk < MIN_DISK_SINGLE * 1024:
     
    191225    If machine is None, this is the owner of a new machine.
    192226    """
    193     if owner == user or machine is not None and owner == machine.owner:
     227    if owner == user:
    194228        return owner
     229    if machine is not None and owner in (machine.owner, None):
     230        return None
    195231    if owner is None:
    196232        raise InvalidInput('owner', owner, "Owner must be specified")
     
    214250
    215251def testName(user, name, machine=None):
    216     if name in (None, machine.name):
     252    if name is None:
     253        return None
     254    if machine is not None and name == machine.name:
    217255        return None
    218256    if not Machine.select_by(name=name):
     257        if not validMachineName(name):
     258            raise InvalidInput('name', name, 'You must provide a machine name.  Max 22 chars, alnum plus \'-\' and \'_\'.')
    219259        return name
    220260    raise InvalidInput('name', name, "Name is already taken.")
Note: See TracChangeset for help on using the changeset viewer.