| 1 |
# Copyright (c) 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.module_utils.common.text.converters import to_text |
| 13 |
|
| 14 |
from ansible_collections.community.dns.plugins.module_utils._conversion.base import ( |
| 15 |
DNSConversionError, |
| 16 |
) |
| 17 |
from ansible_collections.community.dns.plugins.module_utils._conversion.txt import ( |
| 18 |
decode_txt_value, |
| 19 |
encode_txt_value, |
| 20 |
) |
| 21 |
from ansible_collections.community.dns.plugins.module_utils._record import DNSRecord |
| 22 |
|
| 23 |
if t.TYPE_CHECKING: |
| 24 |
from collections.abc import Sequence # pragma: no cover |
| 25 |
|
| 26 |
from .._argspec import OptionProvider # pragma: no cover |
| 27 |
from .._provider import ProviderInformation # pragma: no cover |
| 28 |
from .._record import RecordIDT # pragma: no cover |
| 29 |
from .._record_set import DNSRecordSet, RecordSetIDT # pragma: no cover |
| 30 |
|
| 31 |
|
| 32 |
class RecordConverter: |
| 33 |
def __init__( |
| 34 |
self, provider_information: ProviderInformation, option_provider: OptionProvider |
| 35 |
) -> None: |
| 36 |
""" |
| 37 |
Create a record converter. |
| 38 |
""" |
| 39 |
self._provider_information = provider_information |
| 40 |
self._option_provider = option_provider |
| 41 |
|
| 42 |
# Valid values: 'decoded', 'encoded', 'encoded-no-char-encoding' |
| 43 |
self._txt_api_handling = self._provider_information.txt_record_handling() |
| 44 |
self._txt_api_character_encoding = ( |
| 45 |
self._provider_information.txt_character_encoding() |
| 46 |
) |
| 47 |
self._txt_always_quote = self._provider_information.txt_always_quote() |
| 48 |
# Valid values: 'api', 'quoted', 'unquoted' |
| 49 |
self._txt_transformation = self._option_provider.get_option( |
| 50 |
"txt_transformation" |
| 51 |
) |
| 52 |
# Valid values: 'decimal', 'octal' |
| 53 |
self._txt_character_encoding = self._option_provider.get_option( |
| 54 |
"txt_character_encoding" |
| 55 |
) |
| 56 |
|
| 57 |
def emit_deprecations(self, deprecator: t.Callable[[str], None]) -> None: |
| 58 |
pass |
| 59 |
|
| 60 |
def _handle_txt_api(self, to_api: bool, record: DNSRecord[RecordIDT]) -> None: |
| 61 |
""" |
| 62 |
Handle TXT records for sending to/from the API. |
| 63 |
""" |
| 64 |
if self._txt_transformation == "api": |
| 65 |
# Do not touch record values |
| 66 |
return |
| 67 |
|
| 68 |
# We assume that records internally use decoded values |
| 69 |
if self._txt_api_handling in ( |
| 70 |
"encoded", |
| 71 |
"encoded-no-char-encoding", |
| 72 |
): |
| 73 |
if to_api: |
| 74 |
record.target = encode_txt_value( |
| 75 |
record.target, |
| 76 |
always_quote=self._txt_always_quote, |
| 77 |
use_character_encoding=self._txt_api_handling == "encoded", |
| 78 |
character_encoding=self._txt_api_character_encoding, |
| 79 |
) |
| 80 |
else: |
| 81 |
record.target = decode_txt_value( |
| 82 |
record.target, character_encoding=self._txt_api_character_encoding |
| 83 |
) |
| 84 |
|
| 85 |
def _handle_txt_user(self, to_user: bool, record: DNSRecord[RecordIDT]) -> None: |
| 86 |
""" |
| 87 |
Handle TXT records for sending to/from the user. |
| 88 |
""" |
| 89 |
if self._txt_transformation == "api": |
| 90 |
# Do not touch record values |
| 91 |
return |
| 92 |
|
| 93 |
# We assume that records internally use decoded values |
| 94 |
if self._txt_transformation == "quoted": |
| 95 |
if to_user: |
| 96 |
record.target = encode_txt_value( |
| 97 |
record.target, character_encoding=self._txt_character_encoding |
| 98 |
) |
| 99 |
else: |
| 100 |
record.target = decode_txt_value( |
| 101 |
record.target, character_encoding=self._txt_character_encoding |
| 102 |
) |
| 103 |
|
| 104 |
def process_from_api(self, record: DNSRecord[RecordIDT]) -> DNSRecord[RecordIDT]: |
| 105 |
""" |
| 106 |
Process a record object (DNSRecord) after receiving from API. |
| 107 |
Modifies the record in-place. |
| 108 |
""" |
| 109 |
try: |
| 110 |
record.target = to_text(record.target) |
| 111 |
if record.type == "TXT": |
| 112 |
self._handle_txt_api(False, record) |
| 113 |
return record |
| 114 |
except DNSConversionError as e: |
| 115 |
raise DNSConversionError( |
| 116 |
f"While processing record from API: {e.error_message}" |
| 117 |
) from e |
| 118 |
|
| 119 |
def process_to_api(self, record: DNSRecord[RecordIDT]) -> DNSRecord[RecordIDT]: |
| 120 |
""" |
| 121 |
Process a record object (DNSRecord) for sending to API. |
| 122 |
Modifies the record in-place. |
| 123 |
""" |
| 124 |
try: |
| 125 |
if record.type == "TXT": |
| 126 |
self._handle_txt_api(True, record) |
| 127 |
return record |
| 128 |
except DNSConversionError as e: # pragma: no cover |
| 129 |
# This can never happen |
| 130 |
raise DNSConversionError( |
| 131 |
f"While processing record for the API: {e.error_message}" |
| 132 |
) from e # pragma: no cover |
| 133 |
|
| 134 |
def process_from_user(self, record: DNSRecord[RecordIDT]) -> DNSRecord[RecordIDT]: |
| 135 |
""" |
| 136 |
Process a record object (DNSRecord) after receiving from the user. |
| 137 |
Modifies the record in-place. |
| 138 |
""" |
| 139 |
try: |
| 140 |
record.target = to_text(record.target) |
| 141 |
if record.type == "TXT": |
| 142 |
self._handle_txt_user(False, record) |
| 143 |
return record |
| 144 |
except DNSConversionError as e: |
| 145 |
raise DNSConversionError( |
| 146 |
f"While processing record from the user: {e.error_message}" |
| 147 |
) from e |
| 148 |
|
| 149 |
def process_to_user(self, record: DNSRecord[RecordIDT]) -> DNSRecord[RecordIDT]: |
| 150 |
""" |
| 151 |
Process a record object (DNSRecord) for sending to the user. |
| 152 |
Modifies the record in-place. |
| 153 |
""" |
| 154 |
try: |
| 155 |
if record.type == "TXT": |
| 156 |
self._handle_txt_user(True, record) |
| 157 |
return record |
| 158 |
except DNSConversionError as e: # pragma: no cover |
| 159 |
# This can never happen |
| 160 |
raise DNSConversionError( |
| 161 |
f"While processing record for the user: {e.error_message}" |
| 162 |
) from e # pragma: no cover |
| 163 |
|
| 164 |
def clone_from_api(self, record: DNSRecord[RecordIDT]) -> DNSRecord[RecordIDT]: |
| 165 |
""" |
| 166 |
Process a record object (DNSRecord) after receiving from API. |
| 167 |
Return a modified clone of the record; the original will not be modified. |
| 168 |
""" |
| 169 |
record = record.clone() |
| 170 |
self.process_from_api(record) |
| 171 |
return record |
| 172 |
|
| 173 |
def clone_to_api(self, record: DNSRecord[RecordIDT]) -> DNSRecord[RecordIDT]: |
| 174 |
""" |
| 175 |
Process a record object (DNSRecord) for sending to API. |
| 176 |
Return a modified clone of the record; the original will not be modified. |
| 177 |
""" |
| 178 |
record = record.clone() |
| 179 |
self.process_to_api(record) |
| 180 |
return record |
| 181 |
|
| 182 |
def clone_multiple_from_api( |
| 183 |
self, records: Sequence[DNSRecord[RecordIDT]] |
| 184 |
) -> list[DNSRecord[RecordIDT]]: |
| 185 |
""" |
| 186 |
Process a list of record object (DNSRecord) after receiving from API. |
| 187 |
Return a list of modified clones of the records; the originals will not be modified. |
| 188 |
""" |
| 189 |
return [self.clone_from_api(record) for record in records] |
| 190 |
|
| 191 |
def clone_multiple_to_api( |
| 192 |
self, records: Sequence[DNSRecord[RecordIDT]] |
| 193 |
) -> list[DNSRecord[RecordIDT]]: |
| 194 |
""" |
| 195 |
Process a list of record objects (DNSRecord) for sending to API. |
| 196 |
Return a list of modified clones of the records; the originals will not be modified. |
| 197 |
""" |
| 198 |
return [self.clone_to_api(record) for record in records] |
| 199 |
|
| 200 |
def clone_set_to_api( |
| 201 |
self, record_set: DNSRecordSet[RecordSetIDT, RecordIDT] |
| 202 |
) -> DNSRecordSet[RecordSetIDT, RecordIDT]: |
| 203 |
""" |
| 204 |
Process a record set object (DNSRecordSet) for sending to API. |
| 205 |
Return a modified clone of the record set; the original will not be modified. |
| 206 |
""" |
| 207 |
record_set = record_set.clone() |
| 208 |
record_set.records = [ |
| 209 |
self.clone_to_api(record) for record in record_set.records |
| 210 |
] |
| 211 |
return record_set |
| 212 |
|
| 213 |
def process_multiple_from_api( |
| 214 |
self, records: list[DNSRecord[RecordIDT]] |
| 215 |
) -> list[DNSRecord[RecordIDT]]: |
| 216 |
""" |
| 217 |
Process a list of record object (DNSRecord) after receiving from API. |
| 218 |
Modifies the records in-place. |
| 219 |
""" |
| 220 |
for record in records: |
| 221 |
self.process_from_api(record) |
| 222 |
return records |
| 223 |
|
| 224 |
def process_multiple_to_api( |
| 225 |
self, records: list[DNSRecord[RecordIDT]] |
| 226 |
) -> list[DNSRecord[RecordIDT]]: |
| 227 |
""" |
| 228 |
Process a list of record objects (DNSRecord) for sending to API. |
| 229 |
Modifies the records in-place. |
| 230 |
""" |
| 231 |
for record in records: |
| 232 |
self.process_to_api(record) |
| 233 |
return records |
| 234 |
|
| 235 |
def process_multiple_from_user( |
| 236 |
self, records: list[DNSRecord[RecordIDT]] |
| 237 |
) -> list[DNSRecord[RecordIDT]]: |
| 238 |
""" |
| 239 |
Process a list of record object (DNSRecord) after receiving from the user. |
| 240 |
Modifies the records in-place. |
| 241 |
""" |
| 242 |
for record in records: |
| 243 |
self.process_from_user(record) |
| 244 |
return records |
| 245 |
|
| 246 |
def process_multiple_to_user( |
| 247 |
self, records: list[DNSRecord[RecordIDT]] |
| 248 |
) -> list[DNSRecord[RecordIDT]]: |
| 249 |
""" |
| 250 |
Process a list of record objects (DNSRecord) for sending to the user. |
| 251 |
Modifies the records in-place. |
| 252 |
""" |
| 253 |
for record in records: |
| 254 |
self.process_to_user(record) |
| 255 |
return records |
| 256 |
|
| 257 |
def process_set_from_api( |
| 258 |
self, record_set: DNSRecordSet[RecordSetIDT, RecordIDT] |
| 259 |
) -> DNSRecordSet[RecordSetIDT, RecordIDT]: |
| 260 |
""" |
| 261 |
Process a record set object (DNSRecordSet) after receiving from API. |
| 262 |
Modifies the records in-place. |
| 263 |
""" |
| 264 |
for record in record_set.records: |
| 265 |
self.process_from_api(record) |
| 266 |
return record_set |
| 267 |
|
| 268 |
def process_set_to_api( |
| 269 |
self, record_set: DNSRecordSet[RecordSetIDT, RecordIDT] |
| 270 |
) -> DNSRecordSet[RecordSetIDT, RecordIDT]: |
| 271 |
""" |
| 272 |
Process a record set object (DNSRecordSet) for sending to API. |
| 273 |
Modifies the records in-place. |
| 274 |
""" |
| 275 |
for record in record_set.records: |
| 276 |
self.process_to_api(record) |
| 277 |
return record_set |
| 278 |
|
| 279 |
def process_set_from_user( |
| 280 |
self, record_set: DNSRecordSet[RecordSetIDT, RecordIDT] |
| 281 |
) -> DNSRecordSet[RecordSetIDT, RecordIDT]: |
| 282 |
""" |
| 283 |
Process a record set object (DNSRecordSet) after receiving from the user. |
| 284 |
Modifies the records in-place. |
| 285 |
""" |
| 286 |
for record in record_set.records: |
| 287 |
self.process_from_user(record) |
| 288 |
return record_set |
| 289 |
|
| 290 |
def process_set_to_user( |
| 291 |
self, record_set: DNSRecordSet[RecordSetIDT, RecordIDT] |
| 292 |
) -> DNSRecordSet[RecordSetIDT, RecordIDT]: |
| 293 |
""" |
| 294 |
Process a record set objects (DNSRecordSet) for sending to the user. |
| 295 |
Modifies the records in-place. |
| 296 |
""" |
| 297 |
for record in record_set.records: |
| 298 |
self.process_to_user(record) |
| 299 |
return record_set |
| 300 |
|
| 301 |
def process_value_from_user(self, record_type: str, value: str) -> str: |
| 302 |
""" |
| 303 |
Process a record value (string) after receiving from the user. |
| 304 |
""" |
| 305 |
record: DNSRecord[None] = DNSRecord( |
| 306 |
record_id=None, record_type=record_type, target=value |
| 307 |
) |
| 308 |
self.process_from_user(record) |
| 309 |
return record.target |
| 310 |
|
| 311 |
def process_values_from_user( |
| 312 |
self, record_type: str, values: Sequence[str] |
| 313 |
) -> list[str]: |
| 314 |
""" |
| 315 |
Process a list of record values (strings) after receiving from the user. |
| 316 |
""" |
| 317 |
return [self.process_value_from_user(record_type, value) for value in values] |
| 318 |
|
| 319 |
def process_value_to_user(self, record_type: str, value: str) -> str: |
| 320 |
""" |
| 321 |
Process a record value (string) for sending to the user. |
| 322 |
""" |
| 323 |
record: DNSRecord[None] = DNSRecord( |
| 324 |
record_id=None, record_type=record_type, target=value |
| 325 |
) |
| 326 |
self.process_to_user(record) |
| 327 |
return record.target |
| 328 |
|
| 329 |
def process_values_to_user( |
| 330 |
self, record_type: str, values: Sequence[str] |
| 331 |
) -> list[str]: |
| 332 |
""" |
| 333 |
Process a list of record values (strings) for sending to the user. |
| 334 |
""" |
| 335 |
return [self.process_value_to_user(record_type, value) for value in values] |