Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7
8 __metaclass__ = type
9
10 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "certified"}
11
12 DOCUMENTATION = r"""
13 ---
14 module: aci_interface_description
15 short_description: Setting and removing description on physical interfaces (infra:HPathS, infra:RsHPathAtt, infra:SHPathS, and infra:RsSHPathAtt)
16 description:
17 - Setting and removing description on physical interfaces on Cisco ACI fabrics.
18 options:
19 pod_id:
20 description:
21 - The pod number.
22 - C(pod_id) is usually an integer below C(12)
23 type: int
24 aliases: [ pod, pod_number ]
25 node_id:
26 description:
27 - The switch ID that the C(interface) belongs to.
28 - The C(node_id) value is usually something like '101'.
29 type: int
30 aliases: [ leaf, spine, node ]
31 node_type:
32 description:
33 - The type of node the C(interface) is configured on.
34 type: str
35 choices: [ leaf, spine ]
36 interface:
37 description:
38 - The name of the C(interface) that is targeted.
39 - Usually an interface name with the following format C(1/7).
40 type: str
41 fex_id:
42 description:
43 - The fex ID that the C(interface) belongs to.
44 - The C(fex_id) value is usually something like '123'.
45 type: int
46 description:
47 description:
48 - The C(description) that should be attached to the C(interface).
49 type: str
50 state:
51 description:
52 - Use C(present) or C(absent) for adding or removing.
53 - Use C(query) for listing an object or multiple objects.
54 type: str
55 choices: [ absent, present, query ]
56 default: present
57 extends_documentation_fragment:
58 - cisco.aci.aci
59 - cisco.aci.annotation
60 - cisco.aci.owner
61
62 seealso:
63 - name: APIC Management Information Model reference
64 description: More information about the internal APIC classes B(infra:HPathS), B(infra:RsHPathAtt), B(infra:SHPathS), and B(infra:RsSHPathAtt).
65 link: https://developer.cisco.com/docs/apic-mim-ref/
66 author:
67 - Akini Ross (@akinross)
68 """
69
70 EXAMPLES = r"""
71 - name: Set Interface Description
72 cisco.aci.aci_interface_description:
73 host: "{{ inventory_hostname }}"
74 username: "{{ username }}"
75 password: "{{ password }}"
76 validate_certs: false
77 pod_id: 1
78 node_id: 105
79 node_type: leaf
80 interface: 1/49
81 description: foobar
82 state: present
83 delegate_to: localhost
84
85 - name: Remove Interface Description
86 cisco.aci.aci_interface_description:
87 host: "{{ inventory_hostname }}"
88 username: "{{ username }}"
89 password: "{{ password }}"
90 validate_certs: false
91 pod_id: 1
92 node_id: 105
93 node_type: leaf
94 interface: 1/49
95 description: foobar
96 state: absent
97 delegate_to: localhost
98
99 - name: Set Interface Description on Fex
100 cisco.aci.aci_interface_description:
101 host: "{{ inventory_hostname }}"
102 username: "{{ username }}"
103 password: "{{ password }}"
104 validate_certs: false
105 pod_id: 1
106 node_id: 105
107 fex_id: 123
108 interface: 1/49
109 description: foobar
110 state: present
111 delegate_to: localhost
112
113 - name: Remove Interface Description on Fex
114 cisco.aci.aci_interface_description:
115 host: "{{ inventory_hostname }}"
116 username: "{{ username }}"
117 password: "{{ password }}"
118 validate_certs: false
119 pod_id: 1
120 node_id: 105
121 fex_id: 123
122 interface: 1/49
123 description: foobar
124 state: absent
125 delegate_to: localhost
126
127 - name: Query Interface
128 cisco.aci.aci_interface_description:
129 host: "{{ inventory_hostname }}"
130 username: "{{ username }}"
131 password: "{{ password }}"
132 validate_certs: false
133 pod_id: 1
134 node_id: 105
135 node_type: leaf
136 interface: 1/49
137 state: query
138 delegate_to: localhost
139 """
140
141 RETURN = r"""
142 current:
143 description: The existing configuration from the APIC after the module has finished
144 returned: success
145 type: list
146 sample:
147 [
148 {
149 "fvTenant": {
150 "attributes": {
151 "descr": "Production environment",
152 "dn": "uni/tn-production",
153 "name": "production",
154 "nameAlias": "",
155 "ownerKey": "",
156 "ownerTag": ""
157 }
158 }
159 }
160 ]
161 error:
162 description: The error information as returned from the APIC
163 returned: failure
164 type: dict
165 sample:
166 {
167 "code": "122",
168 "text": "unknown managed object class foo"
169 }
170 raw:
171 description: The raw output returned by the APIC REST API (xml or json)
172 returned: parse error
173 type: str
174 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
175 sent:
176 description: The actual/minimal configuration pushed to the APIC
177 returned: info
178 type: list
179 sample:
180 {
181 "fvTenant": {
182 "attributes": {
183 "descr": "Production environment"
184 }
185 }
186 }
187 previous:
188 description: The original configuration from the APIC before the module has started
189 returned: info
190 type: list
191 sample:
192 [
193 {
194 "fvTenant": {
195 "attributes": {
196 "descr": "Production",
197 "dn": "uni/tn-production",
198 "name": "production",
199 "nameAlias": "",
200 "ownerKey": "",
201 "ownerTag": ""
202 }
203 }
204 }
205 ]
206 proposed:
207 description: The assembled configuration from the user-provided parameters
208 returned: info
209 type: dict
210 sample:
211 {
212 "fvTenant": {
213 "attributes": {
214 "descr": "Production environment",
215 "name": "production"
216 }
217 }
218 }
219 filter_string:
220 description: The filter string used for the request
221 returned: failure or debug
222 type: str
223 sample: '?rsp-prop-include=config-only'
224 method:
225 description: The HTTP method used for the request to the APIC
226 returned: failure or debug
227 type: str
228 sample: POST
229 response:
230 description: The HTTP response from the APIC
231 returned: failure or debug
232 type: str
233 sample: OK (30 bytes)
234 status:
235 description: The HTTP status from the APIC
236 returned: failure or debug
237 type: int
238 sample: 200
239 url:
240 description: The HTTP url used for the request to the APIC
241 returned: failure or debug
242 type: str
243 sample: https://10.11.12.13/api/mo/uni/tn-production.json
244 """
245
246 from ansible.module_utils.basic import AnsibleModule
247 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
248
249
250 def main():
251 argument_spec = aci_argument_spec()
252 argument_spec.update(aci_annotation_spec())
253 argument_spec.update(aci_owner_spec())
254 argument_spec.update(
255 pod_id=dict(type="int", aliases=["pod", "pod_number"]),
256 node_id=dict(type="int", aliases=["leaf", "spine", "node"]),
257 fex_id=dict(type="int"),
258 node_type=dict(type="str", choices=["leaf", "spine"]),
259 interface=dict(type="str"),
260 description=dict(type="str"),
261 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
262 )
263
264 module = AnsibleModule(
265 argument_spec=argument_spec,
266 supports_check_mode=True,
267 required_one_of=[
268 ("node_type", "fex_id"),
269 ],
270 required_if=[
271 ["state", "absent", ["pod_id", "node_id", "interface"]],
272 ["state", "present", ["pod_id", "node_id", "interface", "description"]],
273 ],
274 )
275
276 aci = ACIModule(module)
277
278 pod_id = module.params.get("pod_id")
279 node_id = module.params.get("node_id")
280 interface = module.params.get("interface")
281 description = module.params.get("description")
282 fex_id = module.params.get("fex_id")
283 node_type = module.params.get("node_type")
284 state = module.params.get("state")
285
286 class_name = "infraHPathS"
287 children = ["infraRsHPathAtt"]
288 if node_type == "spine":
289 class_name = "infraSHPathS"
290 children = ["infraRsSHPathAtt"]
291 rn = None
292 child_configs = None
293
294 if node_id and interface:
295 if fex_id:
296 rn = "hpaths-{0}_eth{1}_{2}".format(node_id, fex_id, interface.replace("/", "_"))
297 child_configs = [
298 dict(
299 infraRsHPathAtt=dict(
300 attributes=dict(tDn="topology/pod-{0}/paths-{1}/extpaths-{2}/pathep-[eth{3}]".format(pod_id, node_id, fex_id, interface))
301 )
302 ),
303 ]
304 elif node_type == "spine":
305 rn = "shpaths-{0}_eth{1}".format(node_id, interface.replace("/", "_"))
306 child_configs = [
307 dict(infraRsSHPathAtt=dict(attributes=dict(tDn="topology/pod-{0}/paths-{1}/pathep-[eth{2}]".format(pod_id, node_id, interface)))),
308 ]
309 elif node_type == "leaf":
310 rn = "hpaths-{0}_eth{1}".format(node_id, interface.replace("/", "_"))
311 child_configs = [
312 dict(infraRsHPathAtt=dict(attributes=dict(tDn="topology/pod-{0}/paths-{1}/pathep-[eth{2}]".format(pod_id, node_id, interface)))),
313 ]
314
315 dn = None
316 interface_name = None
317 infra_mo = None
318 if rn:
319 dn = "uni/infra/{0}".format(rn)
320 interface_name = rn.split("-")[1]
321 infra_mo = "infra"
322
323 aci.construct_url(
324 root_class=dict(aci_class="infraInfra", aci_rn="infra", module_object=infra_mo, target_filter=dict(name="infra")),
325 subclass_1=dict(aci_class=class_name, aci_rn=rn, module_object=dn, target_filter=dict(name=interface_name)),
326 child_classes=children,
327 )
328
329 aci.get_existing()
330
331 if state == "present":
332 aci.payload(
333 aci_class=class_name,
334 class_config=dict(
335 descr=description,
336 ),
337 child_configs=child_configs,
338 )
339
340 aci.get_diff(aci_class=class_name)
341
342 aci.post_config()
343
344 elif state == "absent":
345 aci.delete_config()
346
347 aci.exit_json()
348
349
350 if __name__ == "__main__":
351 main()