DnsManager   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 71
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_dns_zone_manageable() 0 6 1
A _split_create_domain_challenge() 0 16 2
A __init__() 0 6 1
A delete_entry() 0 13 1
A create_txt_entry() 0 21 1
1
#!/usr/bin/env python
2
# -*- encoding: utf-8 -*-
3
4
from ovh_interface.InterfaceBase import InterfaceBase
5
6
7
class DnsManager(InterfaceBase):
8
    """
9
    This class provide an easy object way to interact with OVH DNS API
10
    """
11
12
    def __init__(self, ovh_client=None):
13
        """
14
        Constructor
15
        :param ovh_client: the ovh client you want to use. If none use environment variable to initialize the ovh client.
16
        """
17
        InterfaceBase.__init__(self, ovh_client)
18
19
    def get_dns_zone_manageable(self):
20
        """
21
        Get all DNS zone which can be managed by the ovh api
22
        :return: The list of manageable DNS zone
23
        """
24
        return self.ovh_client.get('/domain/zone')
25
26
    @staticmethod
27
    def _split_create_domain_challenge(domain):
28
        """
29
        Split the domain to base domain / sub domain and add the let's encrypt domain challenge to the sub domain
30
        :param domain: The domain you want to split
31
        :return: The base domain and the sub domain
32
        """
33
        ndd = domain.split(".")
34
        if len(ndd) == 2:
35
            subdomain = "_acme-challenge"
36
            basedomain = ndd[0] + "." + ndd[1]
37
        else:
38
            subdomain = "_acme-challenge." + ndd[0]
39
            basedomain = ndd[1] + "." + ndd[2]
40
41
        return basedomain, subdomain
42
43
    def create_txt_entry(self, domain, value):
44
        """
45
        Create a TXT entry in a DNS zone
46
        :param domain: The domain where the DNS entry will be inserted
47
        :param value: The value which will be inserted
48
        :return: The DNS records created
49
        """
50
51
        basedomain, subdomain = self._split_create_domain_challenge(domain)
52
53
        dns_entry = self.ovh_client.post('/domain/zone/{}/record'.format(basedomain),
54
                                         fieldType="TXT",
55
                                         subDomain=subdomain,
56
                                         ttl=0,
57
                                         target=value
58
                                         )
59
        self.ovh_client.post('/domain/zone/{}/refresh'.format(basedomain))
60
        self.logger.info("DNS challenge was added for domain: {}".format(basedomain))
61
        self.logger.debug("{}.{} with value ({}) was written".format(subdomain, basedomain, value))
62
63
        return dns_entry
64
65
    def delete_entry(self, domain, id_record):
66
        """
67
        Delete a DNS entry
68
        :param domain: The domain where the entry you want to delete is present
69
        :param id_record: The record id you want to delete
70
        """
71
72
        basedomain, _ = self._split_create_domain_challenge(domain)
73
        self.ovh_client.delete('/domain/zone/{}/record/{}'.format(basedomain, id_record))
74
        self.ovh_client.post('/domain/zone/{}/refresh'.format(basedomain))
75
76
        self.logger.info("Clean DNS entry for domain: {}".format(basedomain))
77
        self.logger.debug("DNS entry {} in {} was deleted".format(id_record, basedomain))
78
79