Merge pull request #120 from DeeeeLAN/master

[feature] Add load balancer support
This commit is contained in:
Timothy Miller 2023-02-15 17:27:03 -05:00 committed by GitHub
commit 190b90f769
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View File

@ -186,6 +186,26 @@ def commitRecord(ip):
return True return True
def updateLoadBalancer(ip):
for option in config["load_balancer"]:
pools = cf_api('user/load_balancers/pools', 'GET', option)
if pools:
idxr = dict((p['id'], i) for i, p in enumerate(pools['result']))
idx = idxr.get(option['pool_id'])
origins = pools['result'][idx]['origins']
idxr = dict((o['name'], i) for i, o in enumerate(origins))
idx = idxr.get(option['origin'])
origins[idx]['address'] = ip['ip']
data = {'origins': origins}
response = cf_api(f'user/load_balancers/pools/{option["pool_id"]}', 'PATCH', option, {}, data)
def cf_api(endpoint, method, config, headers={}, data=False): def cf_api(endpoint, method, config, headers={}, data=False):
api_token = config['authentication']['api_token'] api_token = config['authentication']['api_token']
if api_token != '' and api_token != 'api_token_here': if api_token != '' and api_token != 'api_token_here':
@ -222,6 +242,7 @@ def cf_api(endpoint, method, config, headers={}, data=False):
def updateIPs(ips): def updateIPs(ips):
for ip in ips.values(): for ip in ips.values():
commitRecord(ip) commitRecord(ip)
updateLoadBalancer(ip)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -21,8 +21,21 @@
] ]
} }
], ],
"load_balancer": [
{
"authentication": {
"api_token": "api_token_here",
"api_key": {
"api_key": "api_key_here",
"account_email": "your_email_here"
}
},
"pool_id": "your_pool_id_here",
"origin": "your_origin_name_here"
}
],
"a": true, "a": true,
"aaaa": true, "aaaa": true,
"purgeUnknownRecords": false, "purgeUnknownRecords": false,
"ttl": 300 "ttl": 300
} }