diff --git a/src/netcup_dns/main.py b/src/netcup_dns/main.py index 469a778..fc695ce 100644 --- a/src/netcup_dns/main.py +++ b/src/netcup_dns/main.py @@ -33,19 +33,44 @@ def main(): type_ = cfg[domain]['type'].upper() if type_ == 'A': destination = external_ipv4() - update_record_destination(api, domain, hostname, type_, destination=destination) - print(f'Set {hostname}.{domain} {type_} record to {destination}') + if update_record_destination(api, domain, hostname, type_, destination=destination): + print(f'Set {hostname}.{domain} {type_} record to {destination}') + else: + print(f'The {hostname}.{domain} {type_} record points already to {destination}.') else: raise Exception(f'DNS record type {type_} is not supported.') -def update_record_destination(api: Client, domain: str, hostname: str, type_: str, destination: str) -> None: +def update_record_destination(api: Client, domain: str, hostname: str, type_: str, destination: str) -> bool: + """ + Updates the `destination` of the DNS record identified by `domain`, `hostname` and `type`. + + :param api: + :param domain: + :param hostname: + :param type_: + :param destination: + :return: True if `destination` differs from the old destination. + """ record = get_record(api, domain, hostname, type_) - record.destination = destination - api.update_dns_record(domain, record) + if record.destination == destination: + # The new destination is identical with the current one. + # Thus, we don't need to call the api.update_dns_record() method. + return False + else: + record.destination = destination + api.update_dns_record(domain, record) + return True def get_record(api: Client, domain: str, hostname: str, type_: str) -> DNSRecord: + """ + :param api: + :param domain: + :param hostname: + :param type_: + :return: The DNS record identified by `domain`, `hostname` and `type`. + """ records: list[DNSRecord] = api.dns_records(domain) record: DNSRecord