Debian Codegraph @

ansible_collections/community/dns/plugins/module_utils/_module/_utils.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 from ansible_collections.community.dns.plugins.module_utils._names import (
13 join_labels,
14 normalize_label,
15 split_into_labels,
16 )
17 from ansible_collections.community.dns.plugins.module_utils._zone_record_api import (
18 DNSAPIError,
19 )
20
21 if t.TYPE_CHECKING:
22 from .._provider import ProviderInformation # pragma: no cover
23
24
25 @t.overload
26 def normalize_dns_name(name: str) -> str: ...
27
28
29 @t.overload
30 def normalize_dns_name(name: str | None) -> str | None: ...
31
32
33 def normalize_dns_name(name: str | None) -> str | None:
34 if name is None:
35 return name
36 labels, dummy = split_into_labels(name)
37 return join_labels([normalize_label(label) for label in labels])
38
39
40 def get_prefix(
41 normalized_zone: str,
42 provider_information: ProviderInformation,
43 *,
44 normalized_record: str | None = None,
45 prefix: str | None = None
46 ) -> tuple[str, str | None]:
47 # If normalized_record is not specified, use prefix
48 if normalized_record is None:
49 if prefix is not None:
50 prefix = provider_information.normalize_prefix(normalize_dns_name(prefix))
51 return (prefix + "." + normalized_zone) if prefix else normalized_zone, prefix
52 # Convert record to prefix
53 if (
54 not normalized_record.endswith("." + normalized_zone)
55 and normalized_record != normalized_zone
56 ):
57 raise DNSAPIError("Record must be in zone")
58 if normalized_record == normalized_zone:
59 return normalized_record, None
60 return (
61 normalized_record,
62 normalized_record[: len(normalized_record) - len(normalized_zone) - 1],
63 )