-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathdomain.py
More file actions
424 lines (351 loc) · 9.94 KB
/
domain.py
File metadata and controls
424 lines (351 loc) · 9.94 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Literal, TypedDict
from ..core import BaseDomain, DomainIdentityMixin
if TYPE_CHECKING:
from ..actions import BoundAction
from .client import BoundZone, BoundZoneRRSet
ZoneMode = Literal["primary", "secondary"]
ZoneStatus = Literal["ok", "updating", "error"]
ZoneRegistrar = Literal["hetzner", "other", "unknown"]
class Zone(BaseDomain, DomainIdentityMixin):
"""
Zone Domain.
See https://docs.hetzner.cloud/reference/cloud#zones.
"""
MODE_PRIMARY = "primary"
"""
Zone in primary mode, resource record sets (RRSets) and resource records (RRs) are
managed via the Cloud API or Cloud Console.
"""
MODE_SECONDARY = "secondary"
"""
Zone in secondary mode, Hetzner's nameservers query RRSets and RRs from given
primary nameservers via AXFR.
"""
STATUS_OK = "ok"
"""The Zone is pushed to the authoritative nameservers."""
STATUS_UPDATING = "updating"
"""The Zone is currently being published to the authoritative nameservers."""
STATUS_ERROR = "error"
"""The Zone could not be published to the authoritative nameservers."""
REGISTRAR_HETZNER = "hetzner"
REGISTRAR_OTHER = "other"
REGISTRAR_UNKNOWN = "unknown"
__api_properties__ = (
"id",
"name",
"created",
"mode",
"ttl",
"labels",
"protection",
"status",
"record_count",
"registrar",
"primary_nameservers",
"authoritative_nameservers",
)
__slots__ = __api_properties__
def __init__(
self,
id: int | None = None,
name: str | None = None,
created: str | None = None,
mode: ZoneMode | None = None,
ttl: int | None = None,
labels: dict[str, str] | None = None,
protection: ZoneProtection | None = None,
status: ZoneStatus | None = None,
record_count: int | None = None,
registrar: ZoneRegistrar | None = None,
primary_nameservers: list[ZonePrimaryNameserver] | None = None,
authoritative_nameservers: ZoneAuthoritativeNameservers | None = None,
):
self.id = id
self.name = name
self.created = self._parse_datetime(created)
self.mode = mode
self.ttl = ttl
self.labels = labels
self.protection = protection
self.status = status
self.record_count = record_count
self.registrar = registrar
self.primary_nameservers = primary_nameservers
self.authoritative_nameservers = authoritative_nameservers
ZonePrimaryNameserverTSIGAlgorithm = Literal[
"hmac-md5",
"hmac-sha1",
"hmac-sha256",
]
class ZonePrimaryNameserver(BaseDomain):
"""
Zone Primary Nameserver Domain.
"""
TSIG_ALGORITHM_HMAC_MD5 = "hmac-md5"
"""Transaction signature (TSIG) algorithm used to generate the TSIG key."""
TSIG_ALGORITHM_HMAC_SHA1 = "hmac-sha1"
"""Transaction signature (TSIG) algorithm used to generate the TSIG key."""
TSIG_ALGORITHM_HMAC_SHA256 = "hmac-sha256"
"""Transaction signature (TSIG) algorithm used to generate the TSIG key."""
__api_properties__ = (
"address",
"port",
"tsig_algorithm",
"tsig_key",
)
__slots__ = __api_properties__
def __init__(
self,
address: str,
port: int | None = None,
tsig_algorithm: ZonePrimaryNameserverTSIGAlgorithm | None = None,
tsig_key: str | None = None,
):
self.address = address
self.port = port
self.tsig_algorithm = tsig_algorithm
self.tsig_key = tsig_key
def to_payload(self) -> dict[str, Any]:
"""
Generates the request payload from this domain object.
"""
payload: dict[str, Any] = {
"address": self.address,
}
if self.port is not None:
payload["port"] = self.port
if self.tsig_algorithm is not None:
payload["tsig_algorithm"] = self.tsig_algorithm
if self.tsig_key is not None:
payload["tsig_key"] = self.tsig_key
return payload
ZoneAuthoritativeNameserversDelegationStatus = Literal[
"valid",
"partially-valid",
"invalid",
"lame",
"unregistered",
"unknown",
]
class ZoneAuthoritativeNameservers(BaseDomain):
"""
Zone Authoritative Nameservers Domain.
"""
DELEGATION_STATUS_VALID = "valid"
DELEGATION_STATUS_PARTIALLY_VALID = "partially-valid"
DELEGATION_STATUS_INVALID = "invalid"
DELEGATION_STATUS_LAME = "lame"
DELEGATION_STATUS_UNREGISTERED = "unregistered"
DELEGATION_STATUS_UNKNOWN = "unknown"
__api_properties__ = (
"assigned",
"delegated",
"delegation_last_check",
"delegation_status",
)
__slots__ = __api_properties__
def __init__(
self,
assigned: list[str] | None = None,
delegated: list[str] | None = None,
delegation_last_check: str | None = None,
delegation_status: ZoneAuthoritativeNameserversDelegationStatus | None = None,
):
self.assigned = assigned
self.delegated = delegated
self.delegation_last_check = self._parse_datetime(delegation_last_check)
self.delegation_status = delegation_status
class ZoneProtection(TypedDict):
"""
Zone Protection.
"""
delete: bool
class CreateZoneResponse(BaseDomain):
"""
Create Zone Response Domain.
"""
__api_properties__ = ("zone", "action")
__slots__ = __api_properties__
def __init__(
self,
zone: BoundZone,
action: BoundAction,
):
self.zone = zone
self.action = action
class DeleteZoneResponse(BaseDomain):
"""
Delete Zone Response Domain.
"""
__api_properties__ = ("action",)
__slots__ = __api_properties__
def __init__(
self,
action: BoundAction,
):
self.action = action
class ExportZonefileResponse(BaseDomain):
"""
Export Zonefile Response Domain.
"""
__api_properties__ = ("zonefile",)
__slots__ = __api_properties__
def __init__(
self,
zonefile: str,
):
self.zonefile = zonefile
ZoneRRSetType = Literal[
"A",
"AAAA",
"CAA",
"CNAME",
"DS",
"HINFO",
"HTTPS",
"MX",
"NS",
"PTR",
"RP",
"SOA",
"SRV",
"SVCB",
"TLSA",
"TXT",
]
class ZoneRRSet(BaseDomain):
"""
Zone RRSet Domain.
See https://docs.hetzner.cloud/reference/cloud#zone-rrsets
"""
TYPE_A = "A"
TYPE_AAAA = "AAAA"
TYPE_CAA = "CAA"
TYPE_CNAME = "CNAME"
TYPE_DS = "DS"
TYPE_HINFO = "HINFO"
TYPE_HTTPS = "HTTPS"
TYPE_MX = "MX"
TYPE_NS = "NS"
TYPE_PTR = "PTR"
TYPE_RP = "RP"
TYPE_SOA = "SOA"
TYPE_SRV = "SRV"
TYPE_SVCB = "SVCB"
TYPE_TLSA = "TLSA"
TYPE_TXT = "TXT"
__api_properties__ = (
"name",
"type",
"ttl",
"labels",
"protection",
"records",
"id",
"zone",
)
__slots__ = __api_properties__
def __init__(
self,
name: str | None = None,
type: ZoneRRSetType | None = None,
ttl: int | None = None,
labels: dict[str, str] | None = None,
protection: ZoneRRSetProtection | None = None,
records: list[ZoneRecord] | None = None,
id: str | None = None,
zone: BoundZone | Zone | None = None,
):
# Ensure that 'id', 'name' and 'type' are always populated.
if name is not None and type is not None:
if id is None:
id = f"{name}/{type}"
else:
if id is not None:
name, _, type = id.partition("/") # type: ignore[assignment]
else:
raise ValueError("id or name and type must be set")
self.name = name
self.type = type
self.ttl = ttl
self.labels = labels
self.protection = protection
self.records = records
self.id = id
self.zone = zone
def to_payload(self) -> dict[str, Any]:
"""
Generates the request payload from this domain object.
"""
payload: dict[str, Any] = {
"name": self.name,
"type": self.type,
}
if self.ttl is not None:
payload["ttl"] = self.ttl
if self.labels is not None:
payload["labels"] = self.labels
if self.protection is not None:
payload["protection"] = self.protection
if self.records is not None:
payload["records"] = [o.to_payload() for o in self.records]
return payload
class ZoneRRSetProtection(TypedDict):
"""
Zone RRSet Protection.
"""
change: bool
class ZoneRecord(BaseDomain):
"""
Zone Record Domain.
"""
__api_properties__ = (
"value",
"comment",
)
__slots__ = __api_properties__
def __init__(
self,
value: str,
comment: str | None = None,
):
self.value = value
self.comment = comment
def to_payload(self) -> dict[str, Any]:
"""
Generates the request payload from this domain object.
"""
payload: dict[str, Any] = {
"value": self.value,
}
if self.comment is not None:
payload["comment"] = self.comment
return payload
class CreateZoneRRSetResponse(BaseDomain):
"""
Create Zone RRSet Response Domain.
"""
__api_properties__ = (
"rrset",
"action",
)
__slots__ = __api_properties__
def __init__(
self,
rrset: BoundZoneRRSet,
action: BoundAction,
):
self.rrset = rrset
self.action = action
class DeleteZoneRRSetResponse(BaseDomain):
"""
Delete Zone RRSet Response Domain.
"""
__api_properties__ = ("action",)
__slots__ = __api_properties__
def __init__(
self,
action: BoundAction,
):
self.action = action