Debian Codegraph @

ansible_collections/community/general/plugins/modules/ipa_dnszone.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2017, Fran Fitzpatrick ([email protected])
3 # Borrowed heavily from other work by Abhijeet Kasurde ([email protected])
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: ipa_dnszone
11 author: Fran Fitzpatrick (@fxfitz)
12 short_description: Manage FreeIPA DNS Zones
13 description:
14 - Add and delete an IPA DNS Zones using IPA API.
15 attributes:
16 check_mode:
17 support: full
18 diff_mode:
19 support: none
20 options:
21 zone_name:
22 description:
23 - The DNS zone name to which needs to be managed.
24 required: true
25 type: str
26 state:
27 description: State to ensure.
28 default: present
29 choices: ["absent", "present"]
30 type: str
31 dynamicupdate:
32 description: Apply dynamic update to zone.
33 default: false
34 type: bool
35 allowsyncptr:
36 description: Allow synchronization of forward and reverse records in the zone.
37 default: false
38 type: bool
39 version_added: 4.3.0
40 extends_documentation_fragment:
41 - community.general._ipa.documentation
42 - community.general._ipa.connection_notes
43 - community.general._attributes
44 """
45
46 EXAMPLES = r"""
47 - name: Ensure dns zone is present
48 community.general.ipa_dnszone:
49 ipa_host: spider.example.com
50 ipa_pass: Passw0rd!
51 state: present
52 zone_name: example.com
53
54 - name: Ensure dns zone is present and is dynamic update
55 community.general.ipa_dnszone:
56 ipa_host: spider.example.com
57 ipa_pass: Passw0rd!
58 state: present
59 zone_name: example.com
60 dynamicupdate: true
61
62 - name: Ensure that dns zone is removed
63 community.general.ipa_dnszone:
64 zone_name: example.com
65 ipa_host: localhost
66 ipa_user: admin
67 ipa_pass: topsecret
68 state: absent
69
70 - name: Ensure dns zone is present and is allowing sync
71 community.general.ipa_dnszone:
72 ipa_host: spider.example.com
73 ipa_pass: Passw0rd!
74 state: present
75 zone_name: example.com
76 allowsyncptr: true
77 """
78
79 RETURN = r"""
80 zone:
81 description: DNS zone as returned by IPA API.
82 returned: always
83 type: dict
84 """
85
86 from ansible.module_utils.basic import AnsibleModule
87
88 from ansible_collections.community.general.plugins.module_utils._ipa import IPAClient, ipa_argument_spec
89
90
91 class DNSZoneIPAClient(IPAClient):
92 def __init__(self, module, host, port, protocol):
93 super().__init__(module, host, port, protocol)
94
95 def dnszone_find(self, zone_name, details=None):
96 items = {
97 "all": "true",
98 "idnsname": zone_name,
99 }
100 if details is not None:
101 items.update(details)
102
103 return self._post_json(method="dnszone_find", name=zone_name, item=items)
104
105 def dnszone_add(self, zone_name=None, details=None):
106 items = {}
107 if details is not None:
108 items.update(details)
109
110 return self._post_json(method="dnszone_add", name=zone_name, item=items)
111
112 def dnszone_mod(self, zone_name=None, details=None):
113 items = {}
114 if details is not None:
115 items.update(details)
116
117 return self._post_json(method="dnszone_mod", name=zone_name, item=items)
118
119 def dnszone_del(self, zone_name=None, record_name=None, details=None):
120 return self._post_json(method="dnszone_del", name=zone_name, item={})
121
122
123 def ensure(module, client):
124 zone_name = module.params["zone_name"]
125 state = module.params["state"]
126 dynamicupdate = module.params["dynamicupdate"]
127 allowsyncptr = module.params["allowsyncptr"]
128
129 changed = False
130
131 # does zone exist
132 ipa_dnszone = client.dnszone_find(zone_name)
133
134 if state == "present":
135 if not ipa_dnszone:
136 changed = True
137 if not module.check_mode:
138 client.dnszone_add(
139 zone_name=zone_name, details={"idnsallowdynupdate": dynamicupdate, "idnsallowsyncptr": allowsyncptr}
140 )
141 elif (
142 ipa_dnszone["idnsallowdynupdate"][0] != str(dynamicupdate).upper()
143 or ipa_dnszone.get("idnsallowsyncptr")
144 and ipa_dnszone["idnsallowsyncptr"][0] != str(allowsyncptr).upper()
145 ):
146 changed = True
147 if not module.check_mode:
148 client.dnszone_mod(
149 zone_name=zone_name, details={"idnsallowdynupdate": dynamicupdate, "idnsallowsyncptr": allowsyncptr}
150 )
151 else:
152 changed = False
153
154 # state is absent
155 else:
156 # check for generic zone existence
157 if ipa_dnszone:
158 changed = True
159 if not module.check_mode:
160 client.dnszone_del(zone_name=zone_name)
161
162 return changed, client.dnszone_find(zone_name)
163
164
165 def main():
166 argument_spec = ipa_argument_spec()
167 argument_spec.update(
168 zone_name=dict(type="str", required=True),
169 state=dict(type="str", default="present", choices=["present", "absent"]),
170 dynamicupdate=dict(type="bool", default=False),
171 allowsyncptr=dict(type="bool", default=False),
172 )
173
174 module = AnsibleModule(
175 argument_spec=argument_spec,
176 supports_check_mode=True,
177 )
178
179 client = DNSZoneIPAClient(
180 module=module,
181 host=module.params["ipa_host"],
182 port=module.params["ipa_port"],
183 protocol=module.params["ipa_prot"],
184 )
185
186 try:
187 client.login(username=module.params["ipa_user"], password=module.params["ipa_pass"])
188 changed, zone = ensure(module, client)
189 module.exit_json(changed=changed, zone=zone)
190 except Exception as e:
191 module.fail_json(msg=f"{e}")
192
193
194 if __name__ == "__main__":
195 main()