Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_dhcp_relay.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2022, Tim Cragg (@timcragg)
5 # Copyright: (c) 2023, Akini Ross (@akinross)
6 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
7
8 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: aci_dhcp_relay
17 short_description: Manage DHCP relay policies (dhcp:RelayP)
18 description:
19 - Manage DHCP relay policy configuration on Cisco ACI fabrics.
20 options:
21 tenant:
22 description:
23 - Name of an existing tenant.
24 - When tenant is not provided the module will be applied to the global (infra) policy.
25 type: str
26 name:
27 description:
28 - Name of the DHCP relay policy
29 type: str
30 aliases: [ relay_policy ]
31 description:
32 description:
33 - Description of the relay policy
34 type: str
35 state:
36 description:
37 - Use C(present) or C(absent) for adding or removing.
38 - Use C(query) for listing an object or multiple objects.
39 type: str
40 choices: [ absent, present, query ]
41 default: present
42 extends_documentation_fragment:
43 - cisco.aci.aci
44 - cisco.aci.annotation
45
46 notes:
47 - The C(tenant) must exist before using this module in your playbook.
48 The M(cisco.aci.aci_tenant) modules can be used for this.
49 seealso:
50 - name: APIC Management Information Model reference
51 description: More information about the internal APIC class B(dhcp:RelayP).
52 link: https://developer.cisco.com/docs/apic-mim-ref/
53 author:
54 - Tim Cragg (@timcragg)
55 - Akini Ross (@akinross)
56 """
57
58 EXAMPLES = r"""
59 - name: Add a new DHCP relay policy
60 cisco.aci.aci_dhcp_relay:
61 host: apic
62 username: admin
63 password: SomeSecretPassword
64 tenant: Auto-Demo
65 name: my_dhcp_relay
66 description: via Ansible
67 state: present
68 delegate_to: localhost
69
70 - name: Add a new global (infra) DHCP relay policy
71 cisco.aci.aci_dhcp_relay:
72 host: apic
73 username: admin
74 password: SomeSecretPassword
75 name: my_dhcp_relay
76 description: via Ansible
77 state: present
78 delegate_to: localhost
79
80 - name: Remove a DHCP relay policy
81 cisco.aci.aci_dhcp_relay:
82 host: apic
83 username: admin
84 password: SomeSecretPassword
85 tenant: Auto-Demo
86 name: my_dhcp_relay
87 state: absent
88 delegate_to: localhost
89
90 - name: Remove a global (infra) DHCP relay policy
91 cisco.aci.aci_dhcp_relay:
92 host: apic
93 username: admin
94 password: SomeSecretPassword
95 name: my_dhcp_relay
96 state: absent
97 delegate_to: localhost
98
99 - name: Query a DHCP relay policy
100 cisco.aci.aci_dhcp_relay:
101 host: apic
102 username: admin
103 password: SomeSecretPassword
104 tenant: Auto-Demo
105 name: my_dhcp_relay
106 state: query
107 delegate_to: localhost
108 register: query_result
109
110 - name: Query a global (infra) DHCP relay policy
111 cisco.aci.aci_dhcp_relay:
112 host: apic
113 username: admin
114 password: SomeSecretPassword
115 name: my_dhcp_relay
116 state: query
117 delegate_to: localhost
118 register: query_result
119
120 - name: Query all DHCP relay policies in a specific tenant
121 cisco.aci.aci_dhcp_relay:
122 host: apic
123 username: admin
124 password: SomeSecretPassword
125 tenant: Auto-Demo
126 state: query
127 delegate_to: localhost
128 register: query_result
129
130 - name: Query all DHCP relay policies
131 cisco.aci.aci_dhcp_relay:
132 host: apic
133 username: admin
134 password: SomeSecretPassword
135 state: query
136 delegate_to: localhost
137 register: query_result
138 """
139
140 RETURN = r"""
141 current:
142 description: The existing configuration from the APIC after the module has finished
143 returned: success
144 type: list
145 sample:
146 [
147 {
148 "fvTenant": {
149 "attributes": {
150 "descr": "Production environment",
151 "dn": "uni/tn-production",
152 "name": "production",
153 "nameAlias": "",
154 "ownerKey": "",
155 "ownerTag": ""
156 }
157 }
158 }
159 ]
160 error:
161 description: The error information as returned from the APIC
162 returned: failure
163 type: dict
164 sample:
165 {
166 "code": "122",
167 "text": "unknown managed object class foo"
168 }
169 raw:
170 description: The raw output returned by the APIC REST API (xml or json)
171 returned: parse error
172 type: str
173 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class "/></imdata>'
174 sent:
175 description: The actual/minimal configuration pushed to the APIC
176 returned: info
177 type: list
178 sample:
179 {
180 "fvTenant": {
181 "attributes": {
182 "descr": "Production environment"
183 }
184 }
185 }
186 previous:
187 description: The original configuration from the APIC before the module has started
188 returned: info
189 type: list
190 sample:
191 [
192 {
193 "fvTenant": {
194 "attributes": {
195 "descr": "Production",
196 "dn": "uni/tn-production",
197 "name": "production",
198 "nameAlias": "",
199 "ownerKey": "",
200 "ownerTag": ""
201 }
202 }
203 }
204 ]
205 proposed:
206 description: The assembled configuration from the user-provided parameters
207 returned: info
208 type: dict
209 sample:
210 {
211 "fvTenant": {
212 "attributes": {
213 "descr": "Production environment",
214 "name": "production"
215 }
216 }
217 }
218 filter_string:
219 description: The filter string used for the request
220 returned: failure or debug
221 type: str
222 sample: ?rsp-prop-include=config-only
223 method:
224 description: The HTTP method used for the request to the APIC
225 returned: failure or debug
226 type: str
227 sample: POST
228 response:
229 description: The HTTP response from the APIC
230 returned: failure or debug
231 type: str
232 sample: OK (30 bytes)
233 status:
234 description: The HTTP status from the APIC
235 returned: failure or debug
236 type: int
237 sample: 200
238 url:
239 description: The HTTP url used for the request to the APIC
240 returned: failure or debug
241 type: str
242 sample: https://10.11.12.13/api/mo/uni/tn-production.json
243 """
244
245 from ansible.module_utils.basic import AnsibleModule
246 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
247
248
249 def main():
250 argument_spec = aci_argument_spec()
251 argument_spec.update(aci_annotation_spec())
252 argument_spec.update(
253 name=dict(type="str", aliases=["relay_policy"]),
254 description=dict(type="str"),
255 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
256 tenant=dict(type="str"),
257 )
258
259 module = AnsibleModule(
260 argument_spec=argument_spec,
261 supports_check_mode=True,
262 required_if=[
263 ["state", "absent", ["name"]],
264 ["state", "present", ["name"]],
265 ],
266 )
267
268 name = module.params.get("name")
269 description = module.params.get("description")
270 state = module.params.get("state")
271 tenant = module.params.get("tenant")
272 child_classes = ["dhcpRsProv"]
273
274 if tenant is None:
275 root_class = dict(
276 aci_class="dhcpRelayP",
277 aci_rn="infra/relayp-{0}".format(name),
278 module_object=name,
279 target_filter={"name": name},
280 )
281 subclass_1 = None
282 owner = "infra"
283 else:
284 root_class = dict(
285 aci_class="fvTenant",
286 aci_rn="tn-{0}".format(tenant),
287 module_object=tenant,
288 target_filter={"name": tenant},
289 )
290 subclass_1 = dict(
291 aci_class="dhcpRelayP",
292 aci_rn="relayp-{0}".format(name),
293 module_object=name,
294 target_filter={"name": name},
295 )
296 owner = "tenant"
297
298 aci = ACIModule(module)
299
300 aci.construct_url(root_class=root_class, subclass_1=subclass_1, child_classes=child_classes)
301
302 aci.get_existing()
303
304 if state == "present":
305 aci.payload(
306 aci_class="dhcpRelayP",
307 class_config=dict(name=name, descr=description, owner=owner),
308 )
309
310 aci.get_diff(aci_class="dhcpRelayP")
311
312 aci.post_config()
313
314 elif state == "absent":
315 aci.delete_config()
316
317 aci.exit_json()
318
319
320 if __name__ == "__main__":
321 main()