Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to update multiple records per request #20

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/docker-build-push.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name: Build and push Docker images to GitHub Packages
on: push
on:
push:
workflow_dispatch:
jobs:
cloudflare_dyndns_docker-build-push:
name: Build and push cloudflare-dyndns docker image
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/helmchart-push.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
name: Build and push helm chart to GitHub Packages
on: push
on:
push:
workflow_dispatch:
jobs:
cloudflare_dyndns_helmchart-push:
name: Push cloudflare-dyndns helm chart
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,6 @@ dmypy.json
.pyre/

.DS_Store

# IntelliJ
.idea/
53 changes: 33 additions & 20 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import waitress
import flask


app = flask.Flask(__name__)


Expand All @@ -12,6 +11,7 @@ def main():
token = flask.request.args.get('token')
zone = flask.request.args.get('zone')
record = flask.request.args.get('record')
records = flask.request.args.get('records')
ipv4 = flask.request.args.get('ipv4')
ipv6 = flask.request.args.get('ipv6')
cf = CloudFlare.CloudFlare(token=token)
Expand All @@ -20,45 +20,58 @@ def main():
return flask.jsonify({'status': 'error', 'message': 'Missing token URL parameter.'}), 400
if not zone:
return flask.jsonify({'status': 'error', 'message': 'Missing zone URL parameter.'}), 400
if not record:
return flask.jsonify({'status': 'error', 'message': 'Missing record URL parameter.'}), 400
if not record and not records:
return flask.jsonify({'status': 'error', 'message': 'Missing record or records URL parameter.'}), 400
if not ipv4 and not ipv6:
return flask.jsonify({'status': 'error', 'message': 'Missing ipv4 or ipv6 URL parameter.'}), 400

try:
zones = cf.zones.get(params={'name': zone})
zones = cf.zones.get(params={'name': zone})

if not zones:
return flask.jsonify({'status': 'error', 'message': 'Zone {} does not exist.'.format(zone)}), 404

if record:
put_dns_record(cf, zones, record, zone, ipv4, ipv6)
if records:
records = records.split(",")
for r in records:
put_dns_record(cf, zones, r, zone, ipv4, ipv6)

return flask.jsonify({'status': 'success', 'message': 'Update successful.'}), 200


if not zones:
return flask.jsonify({'status': 'error', 'message': 'Zone {} does not exist.'.format(zone)}), 404
@app.route('/healthz', methods=['GET'])
def healthz():
return flask.jsonify({'status': 'success', 'message': 'OK'}), 200


def put_dns_record(cf, zones, record, zone, ipv4, ipv6):
try:
a_record = cf.zones.dns_records.get(zones[0]['id'], params={
'name': '{}.{}'.format(record, zone), 'match': 'all', 'type': 'A'})
'name': '{}.{}'.format(record, zone), 'match': 'all', 'type': 'A'})
aaaa_record = cf.zones.dns_records.get(zones[0]['id'], params={
'name': '{}.{}'.format(record, zone), 'match': 'all', 'type': 'AAAA'})
'name': '{}.{}'.format(record, zone), 'match': 'all', 'type': 'AAAA'})

if ipv4 is not None and not a_record:
return flask.jsonify({'status': 'error', 'message': 'A record for {}.{} does not exist.'.format(record, zone)}), 404
return flask.jsonify(
{'status': 'error', 'message': 'A record for {}.{} does not exist.'.format(record, zone)}), 404

if ipv6 is not None and not aaaa_record:
return flask.jsonify({'status': 'error', 'message': 'AAAA record for {}.{} does not exist.'.format(record, zone)}), 404
return flask.jsonify(
{'status': 'error', 'message': 'AAAA record for {}.{} does not exist.'.format(record, zone)}), 404

if ipv4 is not None and a_record[0]['content'] != ipv4:
cf.zones.dns_records.put(zones[0]['id'], a_record[0]['id'], data={
'name': a_record[0]['name'], 'type': 'A', 'content': ipv4, 'proxied': a_record[0]['proxied'], 'ttl': a_record[0]['ttl']})
'name': a_record[0]['name'], 'type': 'A', 'content': ipv4, 'proxied': a_record[0]['proxied'],
'ttl': a_record[0]['ttl']})

if ipv6 is not None and aaaa_record[0]['content'] != ipv6:
cf.zones.dns_records.put(zones[0]['id'], aaaa_record[0]['id'], data={
'name': aaaa_record[0]['name'], 'type': 'AAAA', 'content': ipv6, 'proxied': aaaa_record[0]['proxied'], 'ttl': aaaa_record[0]['ttl']})
'name': aaaa_record[0]['name'], 'type': 'AAAA', 'content': ipv6, 'proxied': aaaa_record[0]['proxied'],
'ttl': aaaa_record[0]['ttl']})
except CloudFlare.exceptions.CloudFlareAPIError as e:
return flask.jsonify({'status': 'error', 'message': str(e)}), 500

return flask.jsonify({'status': 'success', 'message': 'Update successful.'}), 200


@app.route('/healthz', methods=['GET'])
def healthz():
return flask.jsonify({'status': 'success', 'message': 'OK'}), 200


app.secret_key = os.urandom(24)
waitress.serve(app, host='0.0.0.0', port=80)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cloudflare==2.9.11
cloudflare==2.11.1
Flask==2.2.2
waitress==2.1.2