Debian Codegraph @

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

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