Ignore:
Timestamp:
Oct 31, 2008, 10:58:23 PM (16 years ago)
Author:
broder
Message:

Validate hostnames by querying to see if the name exists in DNS, and
add an appropriate dependency

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/invirt-web/code/validation.py

    r1318 r1473  
    55import re
    66import string
     7import dns.resolver
    78from invirt.database import Machine, NIC, Type, Disk, CDROM, Autoinstall
    89from invirt.config import structs as config
     
    271272    if machine is not None and name == machine.name:
    272273        return None
    273     if not Machine.query().filter_by(name=name).count():
     274    try:
     275        f = open('/tmp/log', 'a')
     276        dns.resolver.query('%s.%s.' % (name, config.dns.domains[0]), 'A')
     277        # If the hostname didn't exist, it would have thrown an
     278        # exception by now - error out
     279        raise InvalidInput('name', name, 'Name is already taken.')
     280    except dns.resolver.NXDOMAIN, e:
    274281        if not validMachineName(name):
    275282            raise InvalidInput('name', name, 'You must provide a machine name.  Max 63 chars, alnum plus \'-\', does not begin or end with \'-\'.')
    276283        return name
    277     raise InvalidInput('name', name, "Name is already taken.")
     284    except InvalidInput:
     285        raise
     286    except:
     287        # Any other error is a validation failure
     288        raise InvalidInput('name', name, 'We were unable to verify that this name is available. If you believe this is in error, please contact us at %s' % config.dns.contact)
    278289
    279290def testDescription(user, description, machine=None):
Note: See TracChangeset for help on using the changeset viewer.