summaryrefslogtreecommitdiffstats
path: root/server/backends.py
blob: be95f8c56a5f50874224c7c64b6520be732818d2 (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
class Backend(object):
    def __init__(self, config):
        self.config = config

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

    def remove(self, name):
        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):
        if not name.endswith(self.config['OVH_ZONE']):
            raise Exception()

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

    def remove(self, name):
        ids = self.client.get(
            '/domain/zone/%s/record' % (self.config['OVH_ZONE'],), fieldType='TXT', 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'],))