- Created unifi_targets.txt and unifi_targets_strict.txt files to define DNS records for services including authentik, caddy, and others. - Included ingress CNAMEs for modules such as audiobookshelf, gitea, grafana, and more. - Ensured both files contain comprehensive lists of DNS records to support service discovery and routing.
102 lines
3.0 KiB
Python
102 lines
3.0 KiB
Python
import requests
|
|
import json
|
|
import urllib3
|
|
import re
|
|
|
|
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
|
|
|
UNIFI_URL = "https://udm.renner.uno"
|
|
USERNAME = "OpenTofu"
|
|
PASSWORD = "5c&DKC4AFUU3hcWN!S3n8TF&B%Rx!vM"
|
|
|
|
def login():
|
|
session = requests.Session()
|
|
session.verify = False
|
|
|
|
headers = {
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
payload = {
|
|
"username": USERNAME,
|
|
"password": PASSWORD
|
|
}
|
|
|
|
resp = session.post(f"{UNIFI_URL}/api/auth/login", json=payload, headers=headers)
|
|
resp.raise_for_status()
|
|
return session
|
|
|
|
def get_dns_records(session):
|
|
resp = session.get(f"{UNIFI_URL}/proxy/network/v2/api/site/default/static-dns")
|
|
resp.raise_for_status()
|
|
# print(resp.json()) # Debug formatting
|
|
return resp.json() # Adjusted to inspecting the list directly if needed
|
|
|
|
def extract_key_from_address(address):
|
|
# Module pattern: module.xyz.unifi_dns_record.ingress_cnames["key"]
|
|
match = re.search(r'ingress_cnames\["([^"]+)"\]', address)
|
|
if match:
|
|
return match.group(1)
|
|
|
|
# Module pattern: module.xyz.unifi_dns_record.cnames["key"]
|
|
match = re.search(r'cnames\["([^"]+)"\]', address)
|
|
if match:
|
|
return match.group(1)
|
|
|
|
# Resource pattern: unifi_dns_record.renner_uno["key"]
|
|
match = re.search(r'unifi_dns_record\.renner_uno\["([^"]+)"\]', address)
|
|
if match:
|
|
key = match.group(1)
|
|
if key == "@":
|
|
return "renner.uno"
|
|
return f"{key}.renner.uno"
|
|
|
|
# specific lookups
|
|
if "unifi_dns_record.authentik" in address:
|
|
return "authentik.renner.uno"
|
|
if "unifi_dns_record.caddy_vip" in address:
|
|
return "ingress.renner.uno"
|
|
if "unifi_dns_record.s3_vip" in address:
|
|
return "s3.renner.uno"
|
|
if "unifi_dns_record.time" in address:
|
|
return "time.renner.uno"
|
|
|
|
return None
|
|
|
|
def main():
|
|
try:
|
|
session = login()
|
|
dns_records = get_dns_records(session)
|
|
|
|
# Debugging the structure
|
|
if isinstance(dns_records, dict) and 'data' in dns_records:
|
|
dns_records = dns_records['data']
|
|
elif isinstance(dns_records, list):
|
|
pass # it is already a list
|
|
else:
|
|
print("Unknown response format", type(dns_records))
|
|
return
|
|
|
|
# Create a map of key -> id
|
|
dns_map = {r['key']: r['_id'] for r in dns_records}
|
|
|
|
with open("dns_targets.txt", "r") as f:
|
|
targets = [line.strip() for line in f if line.strip()]
|
|
|
|
for target in targets:
|
|
key = extract_key_from_address(target)
|
|
if not key:
|
|
print(f"# Could not determine key for {target}")
|
|
continue
|
|
|
|
if key in dns_map:
|
|
print(f"tofu import '{target}' {dns_map[key]}")
|
|
else:
|
|
print(f"# Key {key} not found in Unifi for {target}")
|
|
|
|
except Exception as e:
|
|
print(f"Error: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|