-
Notifications
You must be signed in to change notification settings - Fork 1
/
hetzner_dns_record.py
42 lines (36 loc) · 1.52 KB
/
hetzner_dns_record.py
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
import json
from tornado.httpclient import AsyncHTTPClient, HTTPRequest, HTTPClientError
class HetznerDNSRecord:
def __init__(self, api_token: str, zone_id: str, record_id: str, rtype: str, name: str, ttl: int):
self.api_token: str = api_token
self.zone_id: str = zone_id
self.record_id: str = record_id
self.type: str = rtype
self.name: str = name
self.ttl: int = ttl
@classmethod
def from_dict(cls, api_token: str, record: dict):
return cls(api_token, record['zone_id'], record['id'], record['type'], record['name'], record['ttl'])
@classmethod
def from_config(cls, config: dict):
return cls(config['api_token'], config['zone_id'], config['record']['id'], config['record']['type'],
config['record']['name'], config['record']['ttl'])
async def update(self, value: str):
try:
await AsyncHTTPClient().fetch(HTTPRequest(
url=f'https://dns.hetzner.com/api/v1/records/{self.record_id}',
method='PUT',
headers={
'Content-Type': 'application/json',
'Auth-API-Token': self.api_token
},
body=json.dumps({
'name': self.name,
'ttl': self.ttl,
'type': self.type,
'value': value,
'zone_id': self.zone_id
})
))
except HTTPClientError as e:
print(f'update failed, {e}')