Exemple #1
0
def add_rr(fqdn, rr_type, value, ttl):
    fn = 'add_rr'
    # obj_ids is a list of ids
    obj_ids = []
    if config.Debug:
        Logger.debug('{}, Input data: {} {} {} {}'.format(
            fn, fqdn, rr_type, value, ttl))
    obj_prop_key = config.RRTypeMap[rr_type]['prop_key']
    obj_ids = find_rr(fqdn, rr_type, value)
    if obj_ids:
        print('This {} Record already exists'.format(rr_type))
        if not config.Silent:
            bind_print(obj_ids)
        return obj_ids
    elif rr_type == 'A':
        obj_ids = find_rr(fqdn, rr_type)
        if obj_ids:
            ent = api.get_entity_by_id(obj_ids[0])
            d = props2dict(ent['properties'])
            ip_list = d[obj_prop_key]
            ip_list += ',' + value
            d[obj_prop_key] = ip_list
            d['ttl'] = ttl
            ent['properties'] = dict2props(d)
            api.update(ent)
            if not config.Silent:
                print('Added Host Record:')
        else:
            # adding to the top level of the zone requires a leading dot
            if is_zone(fqdn):
                fqdn = '.' + fqdn
            new_id = api.add_host_record(fqdn, value, ttl)
            obj_ids = [new_id]
    elif rr_type == 'TXT':
        if is_zone(fqdn):
            fqdn = '.' + fqdn
        new_id = api.add_TXT_Record(fqdn, value, ttl)
        obj_ids = [new_id]
    elif rr_type == 'CNAME':
        if is_zone(fqdn):
            print('Cannot add a CNAME record at the top of a Zone')
            return obj_ids
        else:
            new_id = api.add_Alias_Record(fqdn, value, ttl)
            obj_ids = [new_id]
    elif rr_type == 'MX':
        (mx_host, priority) = mx_parse(value)
        if is_zone(fqdn):
            fqdn = '.' + fqdn
        new_id = api.add_MX_Record(fqdn, priority, mx_host, ttl)
        obj_ids = [new_id]

    if type(obj_ids[0]) is not int:
        print('Cannot add: {} of type {} Error: {}'.format(
            fqdn, rr_type, obj_ids))
    else:
        if not config.Silent:
            print('added new {} record:'.format(rr_type))
            bind_print(obj_ids)
    return obj_ids
Exemple #2
0
def bind_print(ent_ids):
    if len(ent_ids) == 0:
        print('No RRs to display')
        return
    str_lens = []
    for ent_id in ent_ids:
        ent = api.get_entity_by_id(ent_id)
        d = props2dict(ent['properties'])
        fqdn = d['absoluteName']
        str_lens.append(len(fqdn))
    maxlen = max(str_lens)
    fmt_str = '{:<' + str(maxlen + 4) + '} IN {:>5} {:<6} {}'
    mx_str = fmt_str + ' {}'
    for ent_id in ent_ids:
        ent = api.get_entity_by_id(ent_id)
        d = props2dict(ent['properties'])
        fqdn = d['absoluteName']
        if 'ttl' in d.keys():
            ttl = d['ttl']
        else:
            ttl = d['ttl'] = '86400'
            ent['properties'] = dict2props(d)
            api.update(ent)
        if ttl == '86400':
            ttl = '     '
        rr_type = config.BAM2Bind[ent['type']]
        value = d[config.RRTypeMap[rr_type]['prop_key']]
        if rr_type == 'A':
            values = value.split(',')
            for val in values:
                print(fmt_str.format(fqdn, ttl, rr_type, val))
        elif rr_type == 'TXT':
            value = '"' + value + '"'
            print(fmt_str.format(fqdn, ttl, rr_type, value))
        elif rr_type == 'MX':
            pri = d['priority']
            print(mx_str.format(fqdn, ttl, rr_type, pri, value))
        else:
            print(fmt_str.format(fqdn, ttl, rr_type, value))
Exemple #3
0
def update_rr(fqdn, rr_type, value, ttl):
    fn = 'update_rr'
    id_list = find_rr(fqdn, rr_type)
    if not id_list:
        print('Can not find a RR to update with name {} with type {}'.format(
            fqdn, rr_type))
        return
# There should only be one RR found with the fqdb and type given and value
# If there are more than one, update the first one only
    obj_id = id_list[0]
    ent = api.get_entity_by_id(obj_id)
    if config.Debug:
        print('{} fqdn {} rr_type {} value {}'.format(fn, fqdn, rr_type,
                                                      value))
        print('{} ent bef:'.formt(fn))
        Pp.pprint(ent)
    prop_key = config.RRTypeMap[rr_type]['prop_key']
    d = props2dict(ent['properties'])
    d['ttl'] = ttl
    if rr_type == 'MX':
        (pri, mx) = value.split(',')
        d[prop_key] = mx
        d['priority'] = pri
        ent['properties'] = dict2props(d)
    elif rr_type == 'A':
        org_value = value
        ip_list = value.split(',')
        ip_tup = tuple(ip_list)
        for ip in ip_tup:
            ip_test_ent = api.get_ipranged_by_ip(ip)
            if type(ip_test_ent) is str or ip_test_ent['id'] == 0:
                print('IP address: {} is not in a defined network'.format(ip))
                ip_list.remove(ip)
        if ip_list:
            d[prop_key] = ','.join(ip_list)
            ent['properties'] = dict2props(d)
        else:
            print('None of the IP addresses in {} could be updated'.format(
                org_value))
            return
    elif rr_type == 'TXT' or rr_type == 'CNAME':
        d[prop_key] = value
        ent['properties'] = dict2props(d)
    if config.Debug:
        print('{} ent aft:'.format(fn))
        Pp.pprint(ent)
    api.update(ent)
    if not config.Silent:
        print('Updated RR as follows:')
        bind_print([obj_id])
Exemple #4
0
def delete_rr(fqdn, rr_type, *argv):
    fn = 'delete_rr'
    if argv:
        value = argv[0]
    else:
        value = '*'
    if config.Debug:
        Logger.debug('{} Input data: {} {} {}'.format(fn, fqdn, rr_type,
                                                      value))

# there can only be one CNAME record for a FQDN so the value does not matter
    if rr_type == 'CNAME':
        obj_id = find_rr(fqdn, rr_type)
    else:
        obj_id = find_rr(fqdn, rr_type, value)
    if config.Debug:
        Logger.debug('{} obj_id: {}'.format(fn, obj_id))
    if obj_id:
        if not config.Silent:
            print('This RR exists and will be deleted')
        if rr_type == 'A':
            obj_prop_key = config.RRTypeMap[rr_type]['prop_key']
            ent = api.get_entity_by_id(obj_id[0])
            d = props2dict(ent['properties'])
            ip_list = d[obj_prop_key].split(',')
            ip_list.remove(value)
            if ip_list:
                d[obj_prop_key] = ','.join(ip_list)
                ent['properties'] = dict2props(d)
                if config.Debug:
                    Logger.debug('before:')
                    bind_print(obj_id)
                api.update(ent)
                if config.Debug:
                    Logger.debug('after:')
                    bind_print(obj_id)
            else:
                api.delete(obj_id[0])
        else:
            api.delete(obj_id[0])
    else:
        print('No RR exists matching {} {} {}'.format(fqdn, rr_type, value))
Exemple #5
0
def object_find(fqdn, rr_type, value):
    fn = object_find
    id = 0
    if rr_type == 'MX':
        (value, priority) = mx_parse(value)

    obj_type = config.RRTypeMap[rr_type]['obj_type']
    prop_key = config.RRTypeMap[rr_type]['prop_key']

    names = fqdn.split('.')
    tld = names.pop()
    tld_ent = api.get_entity_by_name(config.ViewId, tld, 'Zone')
    pid = tld_ent['id']
    pname = tld
    while (len(names)):
        name = names.pop()
        ent = api.get_entity_by_name(pid, name, 'Entity')
        obj_id = ent['id']
        obj_ent = api.get_entity_by_id(obj_id)
        if config.Debug:
            Logger.debug('{} name: {} id: {} ent: {}'.format(
                fn, name, obj_id, ent))
        if len(names) == 0 and obj_id:
            obj_type = obj_ent['type']
            if obj_type == 'Zone':
                pid = obj_id
            else:
                ents = api.get_entities(pid, obj_type, 0, 100)
                if len(ents):
                    for ent in ents:
                        if 'properties' in ent and ent[
                                'properties'] is not None:
                            d = props2dict(ent['properties'])
                            if d['absoluteName'] == fqdn:
                                if 'addresses' in d:
                                    obj_id = ent['id']
                                elif value == d[prop_key]:
                                    obj_id = ent['id']
        pname = name
        pid = obj_id
    return obj_id
Exemple #6
0
def get_info_by_id(id):
    ent = api.get_entity_by_id(id)
    pid = api.get_parent(ent['id'])
    ent['pid'] = pid
    return ent