-
Notifications
You must be signed in to change notification settings - Fork 0
/
gandi.py
105 lines (93 loc) · 3.46 KB
/
gandi.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
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
100
101
102
103
104
105
import httplib2
import json
import os
def mk_record(ty, name, values, ttl=1800):
r = {}
r['rrset_type'] = ty
r['rrset_ttl'] = ttl
r['rrset_name'] = name
r['rrset_values'] = values
return r
class Domain:
def __init__(self, domain):
self.name = domain
self.records = {}
def check_name(self, name):
if(not name in self.records):
self.records[name] = {}
def a(self, name, value):
self.check_name(name)
if(not 'a' in self.records[name]):
self.records[name]['a'] = []
self.records[name]['a'].append(value)
def aaaa(self, name, value):
self.check_name(name)
if(not 'aaaa' in self.records[name]):
self.records[name]['aaaa'] = []
self.records[name]['aaaa'].append(value)
def cname(self, name, value):
self.check_name(name)
if('cname' in self.records[name]):
raise Exception("record of type CNAME already defined for name " + name)
self.records[name]['cname'] = value
def mx(self, name, domain):
self.check_name(name)
if(not 'mx' in self.records[name]):
self.records[name]['mx'] = []
self.records[name]['mx'].append(domain)
def spf(self, name, value):
self.check_name(name)
if(not 'spf' in self.records[name]):
self.records[name]['spf'] = ""
self.records[name]['spf'] += " " + value
def txt(self, name, value):
self.check_name(name)
if(not 'txt' in self.records[name]):
self.records[name]['txt'] = []
self.records[name]['txt'].append("\"" + value + "\"")
def subdomain(self, domain):
if(domain.name in self.records):
raise Exception("sub domain " + domain.name + "." + self.name + " already defined")
self.records[domain.name] = {}
for name in domain.records:
if(name == "@"):
new_name = domain.name
else:
new_name = name + "." + domain.name
self.records[new_name] = domain.records[name]
def body(self):
items = []
for name in self.records:
r = self.records[name]
if('a' in r):
items.append(mk_record("A", name, r['a']))
if('aaaa' in r):
items.append(mk_record("AAAA", name, r['aaaa']))
if('cname' in r):
items.append(mk_record("CNAME", name, [r['cname']]))
if('mx' in r):
values = []
p = 0
for domain in r['mx']:
p += 10
values.append(str(p) + " " + domain)
items.append(mk_record("MX", name, values))
if('txt' in r or 'spf' in r):
values = []
if('txt' in r):
values += r['txt']
if('spf' in r):
values.append("\"v=spf1" + r['spf'] + " ~all\"")
items.append(mk_record("TXT", name, values))
return json.dumps({'items': items}).encode()
def push(self):
token = os.environ['TOKEN']
api_url = "https://api.gandi.net/v5/livedns"
url = api_url + "/domains/" + self.name + "/records"
headers = {
'Authorization': "Bearer " + token,
'Content-Type': 'application/json',
}
h = httplib2.Http(".cache")
(resp, content) = h.request(url, "PUT", headers=headers, body=self.body())
return content