Debian Codegraph @

ansible_collections/community/dns/tests/unit/plugins/modules/hetzner.py

raw
1 # Copyright (c) 2021 Felix Fontein <[email protected]>
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 from __future__ import annotations
6
7 HETZNER_DEFAULT_ZONE = {
8 "id": "42",
9 "created": "2021-07-09T11:18:37Z",
10 "modified": "2021-07-09T11:18:37Z",
11 "legacy_dns_host": "string",
12 "legacy_ns": ["foo", "bar"],
13 "name": "example.com",
14 "ns": ["string"],
15 "owner": "Example",
16 "paused": True,
17 "permission": "string",
18 "project": "string",
19 "registrar": "string",
20 "status": "verified",
21 "ttl": 10800,
22 "verified": "2021-07-09T11:18:37Z",
23 "records_count": 0,
24 "is_secondary_dns": True,
25 "txt_verification": {
26 "name": "string",
27 "token": "string",
28 },
29 }
30
31 HETZNER_DEFAULT_ZONE_NO_LEGACY = {
32 "id": "42",
33 "created": "2021-07-09T11:18:37Z",
34 "modified": "2021-07-09T11:18:37Z",
35 "name": "example.com",
36 "ns": ["string"],
37 "owner": "Example",
38 "paused": True,
39 "permission": "string",
40 "project": "string",
41 "registrar": "string",
42 "status": "verified",
43 "ttl": 10800,
44 "verified": "2021-07-09T11:18:37Z",
45 "records_count": 0,
46 "is_secondary_dns": True,
47 "txt_verification": {
48 "name": "string",
49 "token": "string",
50 },
51 }
52
53 HETZNER_JSON_DEFAULT_ENTRIES = [
54 {
55 "id": "125",
56 "type": "A",
57 "name": "@",
58 "value": "1.2.3.4",
59 "ttl": 3600,
60 "zone_id": "42",
61 "created": "2021-07-09T11:18:37Z",
62 "modified": "2021-07-09T11:18:37Z",
63 },
64 {
65 "id": "126",
66 "type": "A",
67 "name": "*",
68 "value": "1.2.3.5",
69 "ttl": 3600,
70 "zone_id": "42",
71 "created": "2021-07-09T11:18:37Z",
72 "modified": "2021-07-09T11:18:37Z",
73 },
74 {
75 "id": "127",
76 "type": "AAAA",
77 "name": "@",
78 "value": "2001:1:2::3",
79 "ttl": 3600,
80 "zone_id": "42",
81 "created": "2021-07-09T11:18:37Z",
82 "modified": "2021-07-09T11:18:37Z",
83 },
84 {
85 "id": "128",
86 "type": "AAAA",
87 "name": "*",
88 "value": "2001:1:2::4",
89 "ttl": 3600,
90 "zone_id": "42",
91 "created": "2021-07-09T11:18:37Z",
92 "modified": "2021-07-09T11:18:37Z",
93 },
94 {
95 "id": "129",
96 "type": "MX",
97 "name": "@",
98 "value": "10 example.com",
99 "ttl": 3600,
100 "zone_id": "42",
101 "created": "2021-07-09T11:18:37Z",
102 "modified": "2021-07-09T11:18:37Z",
103 },
104 {
105 "id": "130",
106 "type": "NS",
107 "name": "@",
108 "value": "helium.ns.hetzner.de.",
109 "zone_id": "42",
110 "created": "2021-07-09T11:18:37Z",
111 "modified": "2021-07-09T11:18:37Z",
112 },
113 {
114 "id": "131",
115 "type": "NS",
116 "name": "@",
117 "value": "hydrogen.ns.hetzner.com.",
118 "zone_id": "42",
119 "created": "2021-07-09T11:18:37Z",
120 "modified": "2021-07-09T11:18:37Z",
121 },
122 {
123 "id": "132",
124 "type": "NS",
125 "name": "@",
126 "value": "oxygen.ns.hetzner.com.",
127 "zone_id": "42",
128 "created": "2021-07-09T11:18:37Z",
129 "modified": "2021-07-09T11:18:37Z",
130 },
131 {
132 "id": "200",
133 "type": "SOA",
134 "name": "@",
135 "value": "hydrogen.ns.hetzner.com. dns.hetzner.com. 2021070900 86400 10800 3600000 3600",
136 "zone_id": "42",
137 "created": "2021-07-09T11:18:37Z",
138 "modified": "2021-07-09T11:18:37Z",
139 },
140 {
141 "id": "201",
142 "type": "TXT",
143 "name": "foo",
144 "value": 'bär " \\"with quotes\\"" " " "(use \\\\ to escape)"',
145 "zone_id": "42",
146 "created": "2021-07-09T11:18:37Z",
147 "modified": "2021-07-09T11:18:37Z",
148 },
149 ]
150
151 HETZNER_JSON_ZONE_LIST_RESULT = {
152 "zones": [
153 HETZNER_DEFAULT_ZONE,
154 ],
155 }
156
157 HETZNER_JSON_ZONE_GET_RESULT = {
158 "zone": HETZNER_DEFAULT_ZONE,
159 }
160
161 HETZNER_JSON_ZONE_LIST_RESULT_NO_LEGACY = {
162 "zones": [
163 HETZNER_DEFAULT_ZONE_NO_LEGACY,
164 ],
165 }
166
167 HETZNER_JSON_ZONE_GET_RESULT_NO_LEGACY = {
168 "zone": HETZNER_DEFAULT_ZONE_NO_LEGACY,
169 }
170
171 HETZNER_JSON_ZONE_RECORDS_GET_RESULT = {
172 "records": HETZNER_JSON_DEFAULT_ENTRIES,
173 }
174
175 #############################################################################################
176 # New API
177
178 HETZNER_ZONE_NEW_JSON = {
179 "zone": {
180 "id": 42,
181 "name": "example.com",
182 "mode": "primary",
183 "ttl": 3600,
184 "labels": {},
185 "primary_nameservers": [],
186 "created": "2016-01-30T23:55:00Z",
187 "protection": {
188 "delete": False,
189 },
190 "status": "ok",
191 "authoritative_nameservers": {
192 "assigned": [
193 "hydrogen.ns.hetzner.com.",
194 "oxygen.ns.hetzner.com.",
195 "helium.ns.hetzner.de.",
196 ],
197 "delegated": [
198 "hydrogen.ns.hetzner.com.",
199 "oxygen.ns.hetzner.com.",
200 "helium.ns.hetzner.de.",
201 ],
202 "delegation_last_check": "2016-01-30T23:55:00Z",
203 "delegation_status": "valid",
204 },
205 "record_count": 23,
206 "registrar": "other",
207 },
208 }
209
210 HETZNER_NEW_JSON_DEFAULT_ENTRIES = [
211 {
212 "id": "@/A",
213 "type": "A",
214 "name": "@",
215 "labels": {},
216 "protection": {
217 "change": False,
218 },
219 "records": [
220 {
221 "value": "1.2.3.4",
222 "comment": "",
223 },
224 ],
225 "ttl": 3600,
226 "zone": "42",
227 },
228 {
229 "id": "*/A",
230 "type": "A",
231 "name": "*",
232 "labels": {},
233 "protection": {
234 "change": False,
235 },
236 "records": [
237 {
238 "value": "1.2.3.5",
239 "comment": "",
240 },
241 ],
242 "ttl": 3600,
243 "zone": "42",
244 },
245 {
246 "id": "@/AAAA",
247 "type": "AAAA",
248 "name": "@",
249 "labels": {},
250 "protection": {
251 "change": False,
252 },
253 "records": [
254 {
255 "value": "2001:1:2::3",
256 "comment": "",
257 },
258 ],
259 "ttl": 3600,
260 "zone": "42",
261 },
262 {
263 "id": "*/AAAA",
264 "type": "AAAA",
265 "name": "*",
266 "labels": {},
267 "protection": {
268 "change": False,
269 },
270 "records": [
271 {
272 "value": "2001:1:2::4",
273 "comment": "",
274 },
275 ],
276 "ttl": 3600,
277 "zone": "42",
278 },
279 {
280 "id": "@/MX",
281 "type": "MX",
282 "name": "@",
283 "labels": {},
284 "protection": {
285 "change": False,
286 },
287 "records": [
288 {
289 "value": "10 example.com",
290 "comment": "",
291 },
292 ],
293 "ttl": 3600,
294 "zone": "42",
295 },
296 {
297 "id": "@/NS",
298 "type": "NS",
299 "name": "@",
300 "labels": {},
301 "protection": {
302 "change": False,
303 },
304 "records": [
305 {
306 "value": "helium.ns.hetzner.de.",
307 "comment": "",
308 },
309 {
310 "value": "hydrogen.ns.hetzner.com.",
311 "comment": "",
312 },
313 {
314 "value": "oxygen.ns.hetzner.com.",
315 "comment": "foo",
316 },
317 ],
318 "ttl": None,
319 "zone": "42",
320 },
321 {
322 "id": "@/SOA",
323 "type": "SOA",
324 "name": "@",
325 "labels": {},
326 "protection": {
327 "change": False,
328 },
329 "records": [
330 {
331 "value": "hydrogen.ns.hetzner.com. dns.hetzner.com. 2021070900 86400 10800 3600000 3600",
332 "comment": "",
333 },
334 ],
335 "ttl": None,
336 "zone": "42",
337 },
338 {
339 "id": "foo/TXT",
340 "type": "TXT",
341 "name": "foo",
342 "labels": {},
343 "protection": {
344 "change": False,
345 },
346 "records": [
347 {
348 "value": '"bär" " \\"with quotes\\"" " " "(use \\\\ to escape)"',
349 "comment": "",
350 },
351 ],
352 "ttl": None,
353 "zone": "42",
354 },
355 ]
356
357
358 def get_hetzner_new_json_pagination_meta(
359 total_entries, page=1, per_page=100, last_page=1
360 ):
361 return {
362 "pagination": {
363 "last_page": last_page,
364 "next_page": None if page == last_page else page + 1,
365 "page": page,
366 "per_page": per_page,
367 "previous_page": None if page == 1 else page - 1,
368 "total_entries": total_entries,
369 }
370 }
371
372
373 def get_hetzner_new_json_records(name=None, record_type=None, update=None):
374 rrsets = list(HETZNER_NEW_JSON_DEFAULT_ENTRIES)
375 if record_type is not None:
376 rrsets = [rrset for rrset in rrsets if rrset["type"] == record_type]
377 if name is not None:
378 rrsets = [rrset for rrset in rrsets if rrset["name"] == name]
379 if update is not None:
380 for index, rrset in enumerate(rrsets):
381 upd = update.get((rrset["name"], rrset["type"]))
382 if upd is not None:
383 rrsets[index] = upd
384 return {
385 "meta": get_hetzner_new_json_pagination_meta(len(rrsets)),
386 "rrsets": rrsets,
387 }