Skip to content

Suggestion for helper function. #16

@jerlich

Description

@jerlich

This is a great library. Thanks!

I wrote this function which i find useful, thought others might too:
if you are doing a lot of additions/updates it is better to generate exist first and pass it in rather than have each call get the existing records.

def add_or_update(domain, ip, value, record_type='A', exist=None):

    if not exist:
        exist = client.get_records(domain, record_type=record_type)
    names = [x['name'] for x in exist]
    if value in names:
        if exist[names.index(value)]['data'] != ip:
            client.update_record_ip(ip, domain,value,record_type)
            print(f'Updated {value}.{domain} to point to {ip}.')

        else:
            print(f'{value}.{domain} already points to {ip}. Not changing anything.')
    else:
        client.add_record(domain,
              {
                  'data': ip,
                  'name': value,
                  'ttl':3600,
                  'type':record_type
               })
        print(f'Added record for {value}.{domain} to point to {ip}.')

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions