summaryrefslogtreecommitdiffstats
path: root/server/backends.py
blob: 4546807501279ac02dd516a511daa1ec18a519b2 (plain)
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
import requests
import json


class Backend(object):
    def __init__(self, config):
        self.config = config

    def add(self, name, value, type_):
        raise NotImplemented

    def delete(self, name, type_):
        raise NotImplemented


class OVHBackend(Backend):
    def __init__(self, config):
        import ovh
        import ovh.exceptions

        self.config = config
        self.client = ovh.Client()
        try:
            self.client.get('/auth/currentCredential')
        except (ovh.exceptions.InvalidKey, ovh.exceptions.InvalidCredential):
            req = ovh.Client().request_consumerkey([
                {'method': 'GET', 'path': '/domain/zone/*/record'},
                {'method': 'POST', 'path': '/domain/zone/*/record'},
                {'method': 'POST', 'path': '/domain/zone/*/refresh'},
                {'method': 'DELETE', 'path': '/domain/zone/*/record/*'},
                ])
            print(req)

    def add(self, name, value, type_):
        if not name.endswith(self.config['OVH_ZONE']):
            raise Exception()

        v = self.client.post('/domain/zone/%s/record' % (self.config['OVH_ZONE'],),
            fieldType=type_, subDomain=name+'.', target=value)
        print(v)
        self.client.post('/domain/zone/%s/refresh' % (self.config['OVH_ZONE'],))

    def delete(self, name, type_):
        ids = self.client.get(
            '/domain/zone/%s/record' % (self.config['OVH_ZONE'],), fieldType=type_, subDomain=name+'.')
        for i in ids:
            print('Removing', i)
            self.client.delete('/domain/zone/%s/record/%d' % (self.config['OVH_ZONE'], i))
        self.client.post('/domain/zone/%s/refresh' % (self.config['OVH_ZONE'],))


class PowerDNSBackend(Backend):
    def do_patch(self, reqdata):
        api_url = '%s/api/v1/servers/localhost/zones/%s.' % (
            self.config.get('PDNS_URL', 'http://127.0.0.1:8081'),
            self.config['PDNS_ZONE'],
            )
        return requests.patch(api_url, data=json.dumps(reqdata), headers={
            'X-API-Key': self.config['PDNS_TOKEN']
            }).json()

    def add(self, name, value, type_):
        self.do_patch({
            "rrsets": [{
                "name": name + ".",
                "type": "A",
                "ttl": 86400,
                "changetype": "REPLACE",
                "records": [{
                    "content": value,
                    "disabled": False
                }]
            }]
        })

    def delete(self, name, type_):
        self.do_patch({
            "rrsets": [{
                "name": name + ".",
                "type": type_,
                "changetype": "DELETE",
            }]
        })