Debian Codegraph @

ansible_collections/community/dns/plugins/module_utils/_hosttech/api.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._argspec import ArgumentSpec
13 from ansible_collections.community.dns.plugins.module_utils._hosttech.json_api import (
14 HostTechJSONAPI,
15 )
16 from ansible_collections.community.dns.plugins.module_utils._hosttech.wsdl_api import (
17 HostTechWSDLAPI,
18 )
19 from ansible_collections.community.dns.plugins.module_utils._provider import (
20 ProviderInformation,
21 )
22 from ansible_collections.community.dns.plugins.module_utils._wsdl import HAS_LXML_ETREE
23 from ansible_collections.community.dns.plugins.module_utils._zone_record_api import (
24 DNSAPIError,
25 )
26
27 if t.TYPE_CHECKING:
28 from .._argspec import OptionProvider # pragma: no cover
29 from .._http import HTTPHelper # pragma: no cover
30 from .._provider import AnsibleType # pragma: no cover
31
32
33 class HosttechProviderInformation(ProviderInformation):
34 def get_supported_record_types(self) -> list[str]:
35 """
36 Return a list of supported record types.
37 """
38 return ["A", "CNAME", "MX", "AAAA", "TXT", "PTR", "SRV", "SPF", "NS", "CAA"]
39
40 def get_zone_id_type(self) -> AnsibleType:
41 """
42 Return the (short) type for zone IDs, like ``'int'`` or ``'str'``.
43 """
44 return "int"
45
46 def get_record_id_type(self) -> AnsibleType:
47 """
48 Return the (short) type for record IDs, like ``'int'`` or ``'str'``.
49 """
50 return "int"
51
52 def get_record_default_ttl(self) -> int | None:
53 """
54 Return the default TTL for records, like 300, 3600 or None.
55 None means that some other TTL (usually from the zone) will be used.
56 """
57 return 3600
58
59 def normalize_prefix(self, prefix: str | None) -> str | None:
60 """
61 Given a prefix (string or None), return its normalized form.
62
63 The result should always be None for the trivial prefix, and a non-zero length DNS name
64 for a non-trivial prefix.
65
66 If a provider supports other identifiers for the trivial prefix, such as '@', this
67 function needs to convert them to None as well.
68 """
69 return prefix or None
70
71 def txt_record_handling(
72 self,
73 ) -> t.Literal["decoded", "encoded", "encoded-no-char-encoding"]:
74 """
75 Return how the API handles TXT records.
76
77 Returns one of the following strings:
78 * 'decoded' - the API works with unencoded values
79 * 'encoded' - the API works with encoded values
80 * 'encoded-no-char-encoding' - the API works with encoded values, but without character encoding
81 """
82 return "decoded"
83
84
85 def create_hosttech_provider_information() -> HosttechProviderInformation:
86 return HosttechProviderInformation()
87
88
89 def create_hosttech_argument_spec() -> ArgumentSpec:
90 return ArgumentSpec(
91 argument_spec={
92 "hosttech_username": {"type": "str"},
93 "hosttech_password": {"type": "str", "no_log": True},
94 "hosttech_token": {"type": "str", "no_log": True, "aliases": ["api_token"]},
95 },
96 required_together=[("hosttech_username", "hosttech_password")],
97 mutually_exclusive=[("hosttech_username", "hosttech_token")],
98 )
99
100
101 def create_hosttech_api(
102 option_provider: OptionProvider, http_helper: HTTPHelper
103 ) -> HostTechJSONAPI | HostTechWSDLAPI:
104 username = option_provider.get_option("hosttech_username")
105 password = option_provider.get_option("hosttech_password")
106 if username is not None and password is not None:
107 if not HAS_LXML_ETREE:
108 raise DNSAPIError("Needs lxml Python module (pip install lxml)")
109
110 return HostTechWSDLAPI(http_helper, username, password, debug=False)
111
112 token = option_provider.get_option("hosttech_token")
113 if token is not None:
114 return HostTechJSONAPI(http_helper, token)
115
116 raise DNSAPIError(
117 "One of hosttech_token or both hosttech_username and hosttech_password must be provided!"
118 )