summaryrefslogtreecommitdiffstats
path: root/pkb_client/client/dns.py
blob: 8bedad2d21730bd147d4763c1d64db7413b76e89 (plain) (blame)
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
from dataclasses import dataclass
from enum import Enum
from typing import Optional, Any


class DNSRecordType(str, Enum):
    A = "A"
    AAAA = "AAAA"
    MX = "MX"
    CNAME = "CNAME"
    ALIAS = "ALIAS"
    TXT = "TXT"
    NS = "NS"
    SRV = "SRV"
    TLSA = "TLSA"
    CAA = "CAA"

    def __str__(self):
        return self.value


DNS_RECORDS_WITH_PRIORITY = {DNSRecordType.MX, DNSRecordType.SRV}


@dataclass
class DNSRecord:
    id: str
    name: str
    type: DNSRecordType
    content: str
    ttl: int
    prio: Optional[int]
    notes: str

    @staticmethod
    def from_dict(d: dict[str, Any]) -> "DNSRecord":
        """
        Create a DNSRecord instance from a dictionary representation.

        :param d: Dictionary containing DNS record data.
        :return: DNSRecord instance.
        """

        # only use prio for supported record types since the API returns it for all records with default value 0
        prio = int(d["prio"]) if d["type"] in DNS_RECORDS_WITH_PRIORITY else None
        return DNSRecord(
            id=d["id"],
            name=d["name"],
            type=DNSRecordType[d["type"]],
            content=d["content"],
            ttl=int(d["ttl"]),
            prio=prio,
            notes=d["notes"],
        )

    def to_dict(self) -> dict[str, Any]:
        """
        Convert the DNSRecord instance to a dictionary representation.

        :return: Dictionary containing DNS record data.
        """

        return {
            "id": self.id,
            "name": self.name,
            "type": str(self.type),
            "content": self.content,
            "ttl": self.ttl,
            "prio": self.prio,
            "notes": self.notes,
        }


class DNSRestoreMode(Enum):
    clear = 0
    replace = 1
    keep = 2

    def __str__(self):
        return self.name

    @staticmethod
    def from_string(a: str) -> "DNSRestoreMode":
        """
        Convert a string to a DNSRestoreMode enum member.

        :param a: String representation of the restore mode.
        :return: Corresponding DNSRestoreMode enum member.
        :raises KeyError: If the string does not match any enum member.
        """

        return DNSRestoreMode[a]