Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_dhcp_option_policy.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_policy
16 short_description: Manage DHCP Option Policy (dhcp:OptionPol)
17 description:
18 - Manage DHCP Option Policy for tenants on Cisco ACI fabrics.
19 options:
20 tenant:
21 description:
22 - The name of an existing tenant.
23 type: str
24 aliases: [ tenant_name ]
25 dhcp_option_policy:
26 description:
27 - The name of the DHCP Option Policy.
28 type: str
29 aliases: [ dhcp_option_policy_name, name ]
30 description:
31 description:
32 - The description for the DHCP Option Policy.
33 type: str
34 aliases: [ descr ]
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 - cisco.aci.owner
46
47 notes:
48 - The C(tenant) must exist before using this module in your playbook.
49 The M(cisco.aci.aci_tenant) can be used for this.
50 seealso:
51 - module: cisco.aci.aci_tenant
52 - name: APIC Management Information Model reference
53 description: More information about the internal APIC classes
54 link: https://developer.cisco.com/docs/apic-mim-ref/
55 author:
56 - Gaspard Micol (@gmicol)
57 """
58
59 EXAMPLES = r"""
60 - name: Add a new DHCP Option Policy
61 cisco.aci.aci_dhcp_option_policy:
62 host: apic
63 username: admin
64 password: SomeSecretPassword
65 tenant: my_tenant
66 dhcp_option_policy: my_dhcp_option_policy
67 state: present
68 delegate_to: localhost
69
70 - name: Delete an DHCP Option Policy
71 cisco.aci.aci_dhcp_option_policy:
72 host: apic
73 username: admin
74 password: SomeSecretPassword
75 tenant: my_tenant
76 dhcp_option_policy: my_dhcp_option_policy
77 state: absent
78 delegate_to: localhost
79
80 - name: Query a DHCP Option Policy
81 cisco.aci.aci_dhcp_option_policy:
82 host: apic
83 username: admin
84 password: SomeSecretPassword
85 tenant: my_tenant
86 dhcp_option_policy: my_dhcp_option_policy
87 state: query
88 delegate_to: localhost
89 register: query_result
90
91 - name: Query all DHCP Option Policies in my_tenant
92 cisco.aci.aci_dhcp_option_policy:
93 host: apic
94 username: admin
95 password: SomeSecretPassword
96 tenant: my_tenant
97 state: query
98 delegate_to: localhost
99 register: query_result
100 """
101
102 RETURN = r"""
103 current:
104 description: The existing configuration from the APIC after the module has finished
105 returned: success
106 type: list
107 sample:
108 [
109 {
110 "fvTenant": {
111 "attributes": {
112 "descr": "Production environment",
113 "dn": "uni/tn-production",
114 "name": "production",
115 "nameAlias": "",
116 "ownerKey": "",
117 "ownerTag": ""
118 }
119 }
120 }
121 ]
122 error:
123 description: The error information as returned from the APIC
124 returned: failure
125 type: dict
126 sample:
127 {
128 "code": "122",
129 "text": "unknown managed object class foo"
130 }
131 raw:
132 description: The raw output returned by the APIC REST API (xml or json)
133 returned: parse error
134 type: str
135 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
136 sent:
137 description: The actual/minimal configuration pushed to the APIC
138 returned: info
139 type: list
140 sample:
141 {
142 "fvTenant": {
143 "attributes": {
144 "descr": "Production environment"
145 }
146 }
147 }
148 previous:
149 description: The original configuration from the APIC before the module has started
150 returned: info
151 type: list
152 sample:
153 [
154 {
155 "fvTenant": {
156 "attributes": {
157 "descr": "Production",
158 "dn": "uni/tn-production",
159 "name": "production",
160 "nameAlias": "",
161 "ownerKey": "",
162 "ownerTag": ""
163 }
164 }
165 }
166 ]
167 proposed:
168 description: The assembled configuration from the user-provided parameters
169 returned: info
170 type: dict
171 sample:
172 {
173 "fvTenant": {
174 "attributes": {
175 "descr": "Production environment",
176 "name": "production"
177 }
178 }
179 }
180 filter_string:
181 description: The filter string used for the request
182 returned: failure or debug
183 type: str
184 sample: ?rsp-prop-include=config-only
185 method:
186 description: The HTTP method used for the request to the APIC
187 returned: failure or debug
188 type: str
189 sample: POST
190 response:
191 description: The HTTP response from the APIC
192 returned: failure or debug
193 type: str
194 sample: OK (30 bytes)
195 status:
196 description: The HTTP status from the APIC
197 returned: failure or debug
198 type: int
199 sample: 200
200 url:
201 description: The HTTP url used for the request to the APIC
202 returned: failure or debug
203 type: str
204 sample: https://10.11.12.13/api/mo/uni/tn-production.json
205 """
206
207
208 from ansible.module_utils.basic import AnsibleModule
209 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
210
211
212 def main():
213 argument_spec = aci_argument_spec()
214 argument_spec.update(aci_annotation_spec())
215 argument_spec.update(aci_owner_spec())
216 argument_spec.update(
217 tenant=dict(type="str", aliases=["tenant_name"]),
218 dhcp_option_policy=dict(type="str", aliases=["dhcp_option_policy_name", "name"]),
219 description=dict(type="str", aliases=["descr"]),
220 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
221 )
222
223 module = AnsibleModule(
224 argument_spec=argument_spec,
225 supports_check_mode=True,
226 required_if=[
227 ["state", "absent", ["tenant", "dhcp_option_policy"]],
228 ["state", "present", ["tenant", "dhcp_option_policy"]],
229 ],
230 )
231
232 tenant = module.params.get("tenant")
233 description = module.params.get("description")
234 dhcp_option_policy = module.params.get("dhcp_option_policy")
235 state = module.params.get("state")
236
237 aci = ACIModule(module)
238
239 aci.construct_url(
240 root_class=dict(
241 aci_class="fvTenant",
242 aci_rn="tn-{0}".format(tenant),
243 module_object=tenant,
244 target_filter={"name": tenant},
245 ),
246 subclass_1=dict(
247 aci_class="dhcpOptionPol",
248 aci_rn="dhcpoptpol-{0}".format(dhcp_option_policy),
249 module_object=dhcp_option_policy,
250 target_filter={"name": dhcp_option_policy},
251 ),
252 )
253
254 aci.get_existing()
255
256 if state == "present":
257 aci.payload(
258 aci_class="dhcpOptionPol",
259 class_config=dict(
260 name=dhcp_option_policy,
261 descr=description,
262 ),
263 )
264
265 aci.get_diff(aci_class="dhcpOptionPol")
266
267 aci.post_config()
268
269 elif state == "absent":
270 aci.delete_config()
271
272 aci.exit_json()
273
274
275 if __name__ == "__main__":
276 main()