Debian Codegraph @

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

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