-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigital_ocean_api.py
More file actions
99 lines (72 loc) · 3.16 KB
/
digital_ocean_api.py
File metadata and controls
99 lines (72 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import logging
import os
import requests
from utils import logError
RTYPE = os.getenv('RTYPE')
class NoRecord(Exception):
pass
class DigitalOceanApi:
_headers: dict
_digitalOceanApiUrl: str
_digitalOceanToken: str
def __init__(self, digitalOceanApiUrl, digitalOceanToken):
self._digitalOceanToken = digitalOceanToken
self._digitalOceanApiUrl = digitalOceanApiUrl
self.setHeaders()
def setHeaders(self, extraHeaders=None):
headers = {
'Authorization': "Bearer {}".format(self._digitalOceanToken),
'Content-Type': 'application/json'
}
if extraHeaders:
headers.update(extraHeaders)
self._headers = headers
def getRecord(self, domainName, subdomainName, rtype):
logging.info('Fetching Record ID for: {}.{}'.format(subdomainName, domainName))
url = '{}/domains/{}/records'.format(self._digitalOceanApiUrl, domainName)
while True:
result = requests.get(url, headers=self._headers).json()
for record in result['domain_records']:
if record['type'] == rtype and record['name'] == subdomainName:
return record
if 'pages' in result['links'] and 'next' in result['links']['pages']:
url = result['links']['pages']['next']
# Replace http to https.
# DigitalOcean forces https request, but links are returned as http
url = url.replace('http://', 'https://')
else:
break
raise NoRecord('Could not find record: {}.{}'.format(subdomainName, domainName))
def createRecord(self, domainName, subdomainName, ipAddress, rtype, ttl):
logging.info('Creating record {}.{} with value {}'.format(subdomainName, domainName, ipAddress))
url = '{}/domains/{}/records'.format(self._digitalOceanApiUrl, domainName)
payload = {
'type': rtype,
'name': subdomainName,
'data': ipAddress,
'priority': None,
'port': None,
'ttl': ttl,
'weight': None,
'flags': None,
'tag': None
}
result = requests.post(url, json=payload, headers=self._headers).json()
if 'domain_record' not in result:
logError('Creating record failed with the following error: {}'.format(result['message']))
return False
return result['domain_record']
def updateRecord(self, domainName, record, ipAddress, rtype, ttl):
logging.info('Updating record {}.{} to {}'.format(record['name'], domainName, ipAddress))
url = '{}/domains/{}/records/{}'.format(self._digitalOceanApiUrl, domainName, record['id'])
payload = {
'type': rtype,
'data': ipAddress,
'ttl': ttl
}
result = requests.put(url, json=payload, headers=self._headers).json()
if 'domain_record' not in result:
logError('Updating record failed with the following error: {}'.format(result['message']))
return False
if result['domain_record']['data'] == ipAddress:
logging.info('Success!')