Completed
Push — master ( 06db4d...fed45f )
by Tomaz
02:15
created

GetDnsZone   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 31
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 30 4
1
import dns.rdataset
2
import dns.zone
3
4
from lib.base_action import BaseAction
5
6
7
class GetDnsZone(BaseAction):
8
    def run(self, domain, record_type=None, name=None, nameserver=None, content=None, tags=None,
9
            tags_and=None):
10
        response = self.getAPI("/api/1.0/dns/records/", {
11
            "domain": domain,
12
            "type": record_type,
13
            "name": name,
14
            "nameserver": nameserver,
15
            "content": content,
16
            "tags": tags,
17
            "tags_and": tags_and
18
        })
19
20
        result = ""
21
        if len(response["records"]) > 0:
22
            zone = dns.zone.Zone(dns.name.from_text(response["records"][0]["dns_zone"]))
23
            for record in response["records"]:
24
                try:
25
                    rdtype = dns.rdatatype.from_text(record["type"])
26
                    rdata = dns.rdata.from_text(dns.rdataclass.IN, rdtype, record["content"])
27
                    n = zone.get_rdataset(record["name"], rdtype, create=True)
28
                    n.add(rdata, record["ttl"])
29
                except Exception, e:
30
                    print "Error: %s" % e.message
31
                    print "Record Name: %s" % record["name"]
32
                    print "Record Type: %s" % record["type"]
33
                    print "Record Content: %s" % record["content"]
34
                    print "Record TTL: %d" % record["ttl"]
35
                    print "---"
36
            result = zone.to_text()
37
        return result
38