only update records and notify if IP has changed
Build and Push Container / build-and-push (push) Successful in 1m19s
Build and Push Container / build-and-push (push) Successful in 1m19s
This commit is contained in:
+19
-6
@@ -93,7 +93,7 @@ def _create_or_update_policy(
|
||||
ip_address: str,
|
||||
ttl_seconds: int,
|
||||
existing_policy: dict | None,
|
||||
) -> None:
|
||||
) -> bool:
|
||||
payload: dict = {
|
||||
"type": record_type,
|
||||
"enabled": True,
|
||||
@@ -109,10 +109,13 @@ def _create_or_update_policy(
|
||||
logger.debug("Payload for %s on %s: %s", record_type, domain, payload)
|
||||
|
||||
if existing_policy and existing_policy.get("id"):
|
||||
current_ip = existing_policy.get("ipv4Address") or existing_policy.get("ipv6Address")
|
||||
if current_ip == ip_address:
|
||||
logger.info("%s policy for %s is already %s, no update needed", record_type, domain, ip_address)
|
||||
return False
|
||||
policy_id = existing_policy["id"]
|
||||
logger.info("Updating existing %s policy for %s (id=%s, current_ip=%s)",
|
||||
record_type, domain, policy_id,
|
||||
existing_policy.get("ipv4Address") or existing_policy.get("ipv6Address"))
|
||||
record_type, domain, policy_id, current_ip)
|
||||
url = f"{api_base}/sites/{site_id}/dns/policies/{policy_id}"
|
||||
logger.debug("Sending PUT to %s", url)
|
||||
response = session.put(url, json=payload, verify=session.verify)
|
||||
@@ -124,13 +127,14 @@ def _create_or_update_policy(
|
||||
|
||||
response.raise_for_status()
|
||||
logger.info("Successfully updated %s policy for %s -> %s", record_type, domain, ip_address)
|
||||
return True
|
||||
|
||||
|
||||
def update_records(
|
||||
unifi_config: UnifiConfig,
|
||||
ipv4: str | None = None,
|
||||
ipv6: str | None = None,
|
||||
) -> None:
|
||||
) -> tuple[set[str], set[str]]:
|
||||
base_url = unifi_config["host"]
|
||||
site_id = unifi_config["site_id"]
|
||||
api_token = unifi_config["api_token"]
|
||||
@@ -144,6 +148,9 @@ def update_records(
|
||||
policies = list_dns_policies(session, api_base, site_id)
|
||||
policy_map = _get_policy_map(policies)
|
||||
|
||||
updated_ipv4: set[str] = set()
|
||||
updated_ipv6: set[str] = set()
|
||||
|
||||
for record in records:
|
||||
domain = record["record"]
|
||||
ttl = record.get("ttl_seconds", 14400)
|
||||
@@ -156,11 +163,13 @@ def update_records(
|
||||
domain, existing["id"], existing.get("ipv4Address"))
|
||||
else:
|
||||
logger.debug("No existing A_RECORD policy for %s, will create new", domain)
|
||||
_create_or_update_policy(
|
||||
changed = _create_or_update_policy(
|
||||
session, api_base, site_id,
|
||||
"A_RECORD", domain, ipv4, ttl,
|
||||
existing,
|
||||
)
|
||||
if changed:
|
||||
updated_ipv4.add(domain)
|
||||
elif ipv4 and record.get("skip_ipv4"):
|
||||
logger.info("Skipping IPv4 for %s (skip_ipv4=true)", domain)
|
||||
|
||||
@@ -171,12 +180,16 @@ def update_records(
|
||||
domain, existing["id"], existing.get("ipv6Address"))
|
||||
else:
|
||||
logger.debug("No existing AAAA_RECORD policy for %s, will create new", domain)
|
||||
_create_or_update_policy(
|
||||
changed = _create_or_update_policy(
|
||||
session, api_base, site_id,
|
||||
"AAAA_RECORD", domain, ipv6, ttl,
|
||||
existing,
|
||||
)
|
||||
if changed:
|
||||
updated_ipv6.add(domain)
|
||||
elif ipv6 and record.get("skip_ipv6"):
|
||||
logger.info("Skipping IPv6 for %s (skip_ipv6=true)", domain)
|
||||
|
||||
logger.info("=== Done processing UniFi record: %s ===", domain)
|
||||
|
||||
return updated_ipv4, updated_ipv6
|
||||
|
||||
Reference in New Issue
Block a user