Debian Codegraph @

ansible_collections/community/dns/plugins/module_utils/_provider.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 abc
11 import typing as t
12
13 from ansible.module_utils.common.validation import (
14 check_type_bool,
15 check_type_dict,
16 check_type_float,
17 check_type_int,
18 check_type_list,
19 check_type_str,
20 )
21
22 if t.TYPE_CHECKING:
23 AnsibleType = t.Literal["int", "str", "float", "bool"] # pragma: no cover
24
25
26 def ensure_type(value: t.Any, type_name: str) -> t.Any:
27 if type_name == "str":
28 return check_type_str(value)
29 if type_name == "list":
30 return check_type_list(value)
31 if type_name == "dict":
32 return check_type_dict(value)
33 if type_name == "bool":
34 return check_type_bool(value)
35 if type_name == "int":
36 return check_type_int(value)
37 if type_name == "float":
38 return check_type_float(value)
39 return value
40
41
42 class ProviderInformation(metaclass=abc.ABCMeta):
43 @abc.abstractmethod
44 def get_zone_id_type(self) -> AnsibleType:
45 """
46 Return the (short) type for zone IDs, like ``'int'`` or ``'str'``.
47 """
48
49 @abc.abstractmethod
50 def get_record_id_type(self) -> AnsibleType:
51 """
52 Return the (short) type for record IDs, like ``'int'`` or ``'str'``.
53 """
54
55 @abc.abstractmethod
56 def get_record_default_ttl(self) -> int | None:
57 """
58 Return the default TTL for records, like 300, 3600 or None.
59 None means that some other TTL (usually from the zone) will be used.
60 """
61
62 @abc.abstractmethod
63 def get_supported_record_types(self) -> list[str]:
64 """
65 Return a list of supported record types.
66 """
67
68 def normalize_prefix(self, prefix: str | None) -> str | None:
69 """
70 Given a prefix (string or ``None``), return its normalized form.
71
72 The result should always be None for the trivial prefix, and a non-zero length DNS name
73 for a non-trivial prefix.
74
75 If a provider supports other identifiers for the trivial prefix, such as '@', this
76 function needs to convert them to ``None`` as well.
77 """
78 return prefix or None
79
80 def supports_bulk_actions(self) -> bool:
81 """
82 Return whether the API supports some kind of bulk actions.
83 """
84 return False
85
86 @abc.abstractmethod
87 def txt_record_handling(
88 self,
89 ) -> t.Literal["decoded", "encoded", "encoded-no-char-encoding"]:
90 """
91 Return how the API handles TXT records.
92
93 Returns one of the following strings:
94 * 'decoded' - the API works with unencoded values
95 * 'encoded' - the API works with encoded values
96 * 'encoded-no-char-encoding' - the API works with encoded values, but without character encoding
97 """
98
99 def txt_character_encoding(self) -> t.Literal["decimal", "octal"]:
100 """
101 Return how the API handles escape sequences in TXT records.
102
103 Returns one of the following strings:
104 * 'octal' - the API works with octal escape sequences
105 * 'decimal' - the API works with decimal escape sequences
106
107 This return value is only used if txt_record_handling returns 'encoded'.
108
109 Note: the default return value changed from 'octal' to 'decimal' in community.dns 3.0.0.
110 """
111 return "decimal"
112
113 def txt_always_quote(self) -> bool:
114 """
115 Return whether TXT records sent to the API should always be quoted.
116
117 Returns a boolean.
118
119 This return value is only used if txt_record_handling does not return 'decoded'.
120 """
121 return False