From 3b677cd582460bdf8b79650bca152f621876c0c0 Mon Sep 17 00:00:00 2001 From: Captain-Knots Date: Mon, 29 Jan 2024 19:53:10 -0600 Subject: [PATCH] update to not require a 'load balancer' section --- cloudflare-ddns.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/cloudflare-ddns.py b/cloudflare-ddns.py index 94126fd..1da3328 100755 --- a/cloudflare-ddns.py +++ b/cloudflare-ddns.py @@ -187,26 +187,28 @@ def commitRecord(ip): def updateLoadBalancer(ip): + try: + for option in config["load_balancer"]: + pools = cf_api('user/load_balancers/pools', 'GET', option) + if pools: + name = option['origin'] + idxr = dict((p['id'], i) for i, p in enumerate(pools['result'])) + idx = idxr.get(option['pool_id']) - for option in config["load_balancer"]: - pools = cf_api('user/load_balancers/pools', 'GET', option) + origins = pools['result'][idx]['origins'] - if pools: - name = option['origin'] - idxr = dict((p['id'], i) for i, p in enumerate(pools['result'])) - idx = idxr.get(option['pool_id']) + idxr = dict((o['name'], i) for i, o in enumerate(origins)) + idx = idxr.get(option['origin']) - origins = pools['result'][idx]['origins'] + if origins[idx]['address'] != ip['ip']: + origins[idx]['address'] = ip['ip'] + data = {'origins': origins} - idxr = dict((o['name'], i) for i, o in enumerate(origins)) - idx = idxr.get(option['origin']) - - if origins[idx]['address'] != ip['ip']: - origins[idx]['address'] = ip['ip'] - data = {'origins': origins} - - print("📡 Updating LB Pool: " + name) - response = cf_api(f'user/load_balancers/pools/{option["pool_id"]}', 'PATCH', option, {}, data) + print("📡 Updating LB Pool: " + name) + response = cf_api(f'user/load_balancers/pools/{option["pool_id"]}', 'PATCH', option, {}, data) + except: + print("No load balancer section found") + def cf_api(endpoint, method, config, headers={}, data=False):