Debian Codegraph @

ansible_collections/community/dns/plugins/module_utils/_record.py

raw
1 # Copyright (c) 2017-2021 Felix Fontein
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
6 # Do not use this from other collections or standalone plugins/modules!
7
8 from __future__ import annotations
9
10 import typing as t
11
12 if t.TYPE_CHECKING:
13 from collections.abc import Collection, Mapping, Sequence # pragma: no cover
14
15 from ._conversion.converter import RecordConverter # pragma: no cover
16
17
18 def format_ttl(ttl: int | None) -> str:
19 if ttl is None:
20 return "default"
21 sec = ttl % 60
22 ttl //= 60
23 mins = ttl % 60
24 ttl //= 60
25 h = ttl
26 result = []
27 if h:
28 result.append(f"{h}h")
29 if mins:
30 result.append(f"{mins}m")
31 if sec:
32 result.append(f"{sec}s")
33 return " ".join(result)
34
35
36 RecordIDT = t.TypeVar("RecordIDT")
37 RecordIDT_co = t.TypeVar("RecordIDT_co", covariant=True)
38
39
40 class IDNSRecord(t.Protocol, t.Generic[RecordIDT_co]):
41 @property
42 def id(self) -> RecordIDT_co: ...
43
44 @property
45 def type(self) -> str: ...
46
47 @property
48 def prefix(self) -> str | None: ...
49
50 @property
51 def target(self) -> str: ...
52
53 @property
54 def ttl(self) -> int | None: ...
55
56 @property
57 def extra(self) -> Mapping[str, str]: ...
58
59
60 class DNSRecord(t.Generic[RecordIDT]):
61 def __init__(self, *, record_id: RecordIDT, record_type: str, target: str) -> None:
62 self.id = record_id
63 self.type = record_type
64 self.prefix: str | None = None
65 self.target = target
66 self.ttl: int | None = 86400 # 24 * 60 * 60
67 self.extra: dict[str, str] = {}
68
69 def clone(self) -> DNSRecord[RecordIDT]:
70 result = DNSRecord(record_id=self.id, record_type=self.type, target=self.target)
71 result.prefix = self.prefix
72 result.ttl = self.ttl
73 result.extra = dict(self.extra)
74 return result
75
76 def __str__(self) -> str:
77 data = []
78 if self.id is not None:
79 data.append(f"id: {self.id}")
80 data.append(f"type: {self.type}")
81 if self.prefix is not None:
82 data.append(f"prefix: {self.prefix!r}")
83 else:
84 data.append("prefix: (none)")
85 data.append(f"target: {self.target!r}")
86 data.append(f"ttl: {format_ttl(self.ttl)}")
87 if self.extra:
88 data.append(f"extra: {self.extra}")
89 return "DNSRecord(" + ", ".join(data) + ")"
90
91 def __repr__(self) -> str:
92 return self.__str__()
93
94
95 @t.overload
96 def sorted_ttls(ttls: Collection[int]) -> list[int]: ...
97
98
99 @t.overload
100 def sorted_ttls(ttls: Collection[int | None]) -> list[int | None]: ...
101
102
103 def sorted_ttls(ttls: Collection[int | None]) -> list[int | None] | list[int]:
104 return sorted(ttls, key=lambda ttl: 0 if ttl is None else ttl)
105
106
107 def format_records_for_output(
108 records: Sequence[IDNSRecord[RecordIDT_co]],
109 record_name: str | None,
110 prefix: str | None = None,
111 record_converter: RecordConverter | None = None,
112 ) -> dict[str, t.Any]:
113 ttls = sorted_ttls({record.ttl for record in records})
114 entry: dict[str, t.Any] = {
115 "prefix": prefix or "",
116 "type": min(record.type for record in records) if records else None,
117 "ttl": ttls[0] if len(ttls) > 0 else None,
118 "value": [record.target for record in records],
119 }
120 if record_converter:
121 entry["value"] = record_converter.process_values_to_user(
122 entry["type"], entry["value"]
123 )
124 if record_name is not None:
125 entry["record"] = record_name
126 if len(ttls) > 1:
127 entry["ttls"] = ttls
128 return entry
129
130
131 def format_record_for_output(
132 record: IDNSRecord[RecordIDT_co],
133 record_name: str | None,
134 prefix: str | None = None,
135 record_converter: RecordConverter | None = None,
136 ) -> dict[str, t.Any]:
137 entry: dict[str, t.Any] = {
138 "prefix": prefix or "",
139 "type": record.type,
140 "ttl": record.ttl,
141 "value": record.target,
142 "extra": record.extra,
143 }
144 if record_converter:
145 entry["value"] = record_converter.process_value_to_user(
146 entry["type"], entry["value"]
147 )
148 if record_name is not None:
149 entry["record"] = record_name
150 return entry