Debian Codegraph @

ansible_collections/community/dns/plugins/module_utils/_module/zone_info.py

raw
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 traceback
11 import typing as t
12
13 from ansible.module_utils.common.text.converters import to_text
14
15 from ansible_collections.community.dns.plugins.module_utils._argspec import ArgumentSpec
16 from ansible_collections.community.dns.plugins.module_utils._zone_record_api import (
17 DNSAPIAuthenticationError,
18 DNSAPIError,
19 )
20
21 from ._utils import normalize_dns_name
22
23 if t.TYPE_CHECKING:
24 from ansible.module_utils.basic import AnsibleModule # pragma: no cover
25
26 from .._provider import ProviderInformation # pragma: no cover
27 from .._zone_record_api import ZoneRecordAPI # pragma: no cover
28 from .._zone_record_set_api import ZoneRecordSetAPI # pragma: no cover
29
30
31 def create_module_argument_spec(
32 provider_information: ProviderInformation,
33 ) -> ArgumentSpec:
34 return ArgumentSpec(
35 argument_spec={
36 "zone_name": {"type": "str", "aliases": ["zone"]},
37 "zone_id": {"type": provider_information.get_zone_id_type()},
38 },
39 required_one_of=[
40 ("zone_name", "zone_id"),
41 ],
42 mutually_exclusive=[
43 ("zone_name", "zone_id"),
44 ],
45 )
46
47
48 def run_module(
49 module: AnsibleModule,
50 create_api: t.Callable[[], ZoneRecordAPI | ZoneRecordSetAPI],
51 provider_information: ProviderInformation,
52 ) -> t.NoReturn:
53 try:
54 # Create API
55 api = create_api()
56
57 # Get zone information
58 if module.params.get("zone_name") is not None:
59 zone_id = normalize_dns_name(module.params.get("zone_name"))
60 zone = api.get_zone_by_name(zone_id)
61 if zone is None:
62 module.fail_json(msg="Zone not found")
63 else:
64 zone = api.get_zone_by_id(module.params.get("zone_id"))
65 if zone is None:
66 module.fail_json(msg="Zone not found")
67
68 module.exit_json(
69 changed=False,
70 zone_name=zone.name,
71 zone_id=zone.id,
72 zone_info=zone.info,
73 )
74 except DNSAPIAuthenticationError as e:
75 module.fail_json(
76 msg=f"Cannot authenticate: {e}",
77 error=to_text(e),
78 exception=traceback.format_exc(),
79 )
80 except DNSAPIError as e:
81 module.fail_json(
82 msg=f"Error: {e}",
83 error=to_text(e),
84 exception=traceback.format_exc(),
85 )