Ejemplo n.º 1
0
def add_cname_record(db, cname_record_name, target_name, fail_on_exists=True):
    """
    Creates a CNAME-record.

    If fail_on_exists=False, it will simply return without doing anything,
    if the CNAME-record already exists.
    This is due to the method being used by OU._setup_project_hosts, which
    can be run several times for the same project when it is reconfigured.

    :param db: A Cerebrum-database instance
    :type db: Cerebrum.database.Database
    :param cname_record_name: FQDN of the CNAME-record
    :type cname_record_name: str
    :param target_name: FQDN of the target domain
    :type target_name: str
    :param fail_on_exists: True or False
    :type fail_on_exists: bool
    """

    cname = CNameRecord(db)
    dns_owner = DnsOwner(db)
    constants = Factory.get('Constants')

    try:
        dns_owner.find_by_name(target_name)
        proxy_dns_owner_ref = dns_owner.entity_id
    except Errors.NotFoundError:
        raise Errors.NotFoundError('%s does not exist.' % target_name)

    dns_owner.clear()

    try:
        dns_owner.find_by_name(cname_record_name)
    except Errors.NotFoundError:
        dns_owner.populate(constants.DnsZone(cereconf.DNS_DEFAULT_ZONE),
                           cname_record_name)
        dns_owner.write_db()
    cname_dns_owner_ref = dns_owner.entity_id

    try:
        cname.find_by_cname_owner_id(cname_dns_owner_ref)
        if fail_on_exists:
            raise Errors.RealityError('CNAME %s already exists.'
                                      % cname_record_name)
    except Errors.NotFoundError:
        cname.populate(cname_dns_owner_ref,
                       proxy_dns_owner_ref)
        cname.write_db()
Ejemplo n.º 2
0
def add_cname_record(db, cname_record_name, target_name, fail_on_exists=True):
    """
    Creates a CNAME-record.

    If fail_on_exists=False, it will simply return without doing anything,
    if the CNAME-record already exists.
    This is due to the method being used by OU._setup_project_hosts, which
    can be run several times for the same project when it is reconfigured.

    :param db: A Cerebrum-database instance
    :type db: Cerebrum.Database
    :param cname_record_name: FQDN of the CNAME-record
    :type cname_record_name: str
    :param target_name: FQDN of the target domain
    :type target_name: str
    :param fail_on_exists: True or False
    :type fail_on_exists: bool
    """

    cname = CNameRecord(db)
    dns_owner = DnsOwner(db)
    constants = Factory.get('Constants')

    try:
        dns_owner.find_by_name(target_name)
        proxy_dns_owner_ref = dns_owner.entity_id
    except Errors.NotFoundError:
        raise Errors.NotFoundError('%s does not exist.' % target_name)

    dns_owner.clear()

    try:
        dns_owner.find_by_name(cname_record_name)
    except Errors.NotFoundError:
        dns_owner.populate(constants.DnsZone(cereconf.DNS_DEFAULT_ZONE),
                           cname_record_name)
        dns_owner.write_db()
    cname_dns_owner_ref = dns_owner.entity_id

    try:
        cname.find_by_cname_owner_id(cname_dns_owner_ref)
        if fail_on_exists:
            raise Errors.RealityError('CNAME %s already exists.'
                                      % cname_record_name)
    except Errors.NotFoundError:
        cname.populate(cname_dns_owner_ref,
                       proxy_dns_owner_ref)
        cname.write_db()