Skip to content
Open
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
23 changes: 11 additions & 12 deletions vultr-dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
VULTR_BIND_DELAY = 30


def vultr_request(method, path, data=None):
url = "https://api.vultr.com/v1{}".format(path)
def vultr_request(method, zone, path, data=None):
url = "https://api.vultr.com/v2/domains{}{}".format(zone, path)

resp = requests.request(method, url, data=data, headers={
"API-Key": VULTR_API_KEY})
resp = requests.request(method, url, json=data, headers={
"Authorization": "Bearer " + VULTR_API_KEY})
resp.raise_for_status()
if resp.headers['Content-Type'] == 'application/json':
return resp.json()
Expand All @@ -28,8 +28,8 @@ def normalize_fqdn(fqdn):


def find_zone_for_name(domain):
resp = vultr_request("GET", "/dns/list")
zones = [entry['domain'] for entry in resp]
resp = vultr_request("GET", "", "")
zones = [entry['domain'] for entry in resp["domains"]]

# api doesn't have a trailing . on its zones
if domain[-1:] == '.':
Expand All @@ -46,16 +46,16 @@ def find_zone_for_name(domain):


def list_records(zone):
return vultr_request("GET", "/dns/records?domain={}".format(zone))
return vultr_request("GET", "/" + zone, "/records")


def create_record(domain, txt_value):
to_add = normalize_fqdn('_acme-challenge.{}'.format(domain))
print("Creating {} TXT: {}".format(to_add, txt_value))
zone = find_zone_for_name(domain)
create_params = {'domain': zone, 'name': to_add, 'type': 'TXT',
create_params = {'name': to_add, 'type': 'TXT',
'data': '"{}"'.format(txt_value)}
vultr_request("POST", "/dns/create_record", create_params)
vultr_request("POST", "/" + zone, "/records", create_params)

print("Will sleep {} seconds to wait for DNS cluster to reload".
format(VULTR_BIND_DELAY))
Expand All @@ -76,15 +76,14 @@ def remove_record(domain, txt_value):
'name' in rec and rec['name'] == to_remove and
'type' in rec and rec['type'] == 'TXT' and
rec['data'] == '"{}"'.format(txt_value),
recs)
recs["records"])

if len(found) == 0:
print("Could not find record to remove: {} with value {}".
format(to_remove, txt_value))
return

delete_params = {'domain': zone, 'RECORDID': found[0]['RECORDID']}
vultr_request("POST", "/dns/delete_record", delete_params)
vultr_request("DELETE", "/" + zone, "/records/" + found[0]['id'])


act = sys.argv[1]
Expand Down