Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023, Eric Girard <@netgirard>
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": "certified"}
12
13 DOCUMENTATION = r"""
14 ---
15 module: aci_interface_policy_spanning_tree
16 short_description: Manage spanning tree interface policies (stp:IfPol)
17 description:
18 - Manage spanning tree interface policies on Cisco ACI fabrics.
19 options:
20 stp_policy:
21 description:
22 - The name of the STP policy.
23 type: str
24 aliases: [ name ]
25 description:
26 description:
27 - The description for the policy.
28 type: str
29 aliases: [ descr ]
30 bpdu_guard:
31 description:
32 - The BPDU-Guard state.
33 type: bool
34 default: false
35 bpdu_filter:
36 description:
37 - The BPDU-Filter state.
38 type: bool
39 default: false
40 state:
41 description:
42 - Use C(present) or C(absent) for adding or removing.
43 - Use C(query) for listing an object or multiple objects.
44 type: str
45 choices: [ absent, present, query ]
46 default: present
47 name_alias:
48 description:
49 - The alias for the current object. This relates to the nameAlias field in ACI.
50 type: str
51 extends_documentation_fragment:
52 - cisco.aci.aci
53 - cisco.aci.annotation
54 - cisco.aci.owner
55
56 seealso:
57 - name: APIC Management Information Model reference
58 description: More information about the internal APIC class B(stp:IfPol).
59 link: https://developer.cisco.com/docs/apic-mim-ref/
60 author:
61 - Eric Girard (@netgirard)
62 """
63
64 EXAMPLES = r"""
65 - name: Add a spanning interface policy
66 cisco.aci.aci_interface_policy_spanning_tree:
67 host: '{{ inventory_hostname }}'
68 username: '{{ username }}'
69 password: '{{ password }}'
70 stp_policy: 'my_policy'
71 description: 'my_description'
72 bpdu_guard: true
73 bpdu_filter: false
74 state: present
75 delegate_to: localhost
76
77 - name: Query a specific spanning interface policy
78 cisco.aci.aci_interface_policy_spanning_tree:
79 host: '{{ inventory_hostname }}'
80 username: '{{ username }}'
81 password: '{{ password }}'
82 stp_policy: 'my_policy'
83 state: query
84 delegate_to: localhost
85
86 - name: Query all spanning interface policies
87 cisco.aci.aci_interface_policy_spanning_tree:
88 host: '{{ inventory_hostname }}'
89 username: '{{ username }}'
90 password: '{{ password }}'
91 state: query
92 delegate_to: localhost
93
94 - name: Remove a specific spanning interface policy
95 cisco.aci.aci_interface_policy_spanning_tree:
96 host: '{{ inventory_hostname }}'
97 username: '{{ username }}'
98 password: '{{ password }}'
99 stp_policy: 'my_policy'
100 state: absent
101 delegate_to: localhost
102 """
103
104 RETURN = r"""
105 current:
106 description: The existing configuration from the APIC after the module has finished
107 returned: success
108 type: list
109 sample:
110 [
111 {
112 "fvTenant": {
113 "attributes": {
114 "descr": "Production environment",
115 "dn": "uni/tn-production",
116 "name": "production",
117 "nameAlias": "",
118 "ownerKey": "",
119 "ownerTag": ""
120 }
121 }
122 }
123 ]
124 error:
125 description: The error information as returned from the APIC
126 returned: failure
127 type: dict
128 sample:
129 {
130 "code": "122",
131 "text": "unknown managed object class foo"
132 }
133 raw:
134 description: The raw output returned by the APIC REST API (xml or json)
135 returned: parse error
136 type: str
137 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
138 sent:
139 description: The actual/minimal configuration pushed to the APIC
140 returned: info
141 type: list
142 sample:
143 {
144 "fvTenant": {
145 "attributes": {
146 "descr": "Production environment"
147 }
148 }
149 }
150 previous:
151 description: The original configuration from the APIC before the module has started
152 returned: info
153 type: list
154 sample:
155 [
156 {
157 "fvTenant": {
158 "attributes": {
159 "descr": "Production",
160 "dn": "uni/tn-production",
161 "name": "production",
162 "nameAlias": "",
163 "ownerKey": "",
164 "ownerTag": ""
165 }
166 }
167 }
168 ]
169 proposed:
170 description: The assembled configuration from the user-provided parameters
171 returned: info
172 type: dict
173 sample:
174 {
175 "fvTenant": {
176 "attributes": {
177 "descr": "Production environment",
178 "name": "production"
179 }
180 }
181 }
182 filter_string:
183 description: The filter string used for the request
184 returned: failure or debug
185 type: str
186 sample: ?rsp-prop-include=config-only
187 method:
188 description: The HTTP method used for the request to the APIC
189 returned: failure or debug
190 type: str
191 sample: POST
192 response:
193 description: The HTTP response from the APIC
194 returned: failure or debug
195 type: str
196 sample: OK (30 bytes)
197 status:
198 description: The HTTP status from the APIC
199 returned: failure or debug
200 type: int
201 sample: 200
202 url:
203 description: The HTTP url used for the request to the APIC
204 returned: failure or debug
205 type: str
206 sample: https://10.11.12.13/api/mo/uni/tn-production.json
207 """
208
209 from ansible.module_utils.basic import AnsibleModule
210 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
211
212
213 def main():
214 argument_spec = aci_argument_spec()
215 argument_spec.update(aci_annotation_spec())
216 argument_spec.update(aci_owner_spec())
217 argument_spec.update(
218 stp_policy=dict(type="str", aliases=["name"]), # Not required for querying all objects
219 description=dict(type="str", aliases=["descr"]),
220 bpdu_guard=dict(type="bool", default=False),
221 bpdu_filter=dict(type="bool", default=False),
222 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
223 name_alias=dict(type="str"),
224 )
225
226 module = AnsibleModule(
227 argument_spec=argument_spec,
228 supports_check_mode=True,
229 required_if=[
230 ["state", "absent", ["stp_policy"]],
231 ["state", "present", ["stp_policy"]],
232 ],
233 )
234
235 stp_policy = module.params.get("stp_policy")
236 description = module.params.get("description")
237 state = module.params.get("state")
238 name_alias = module.params.get("name_alias")
239
240 # Build ctrl value for request
241 ctrl = []
242 if module.params.get("bpdu_filter") is True:
243 ctrl.append("bpdu-filter")
244 if module.params.get("bpdu_guard") is True:
245 ctrl.append("bpdu-guard")
246
247 # Order of control string must match ACI return value for idempotency
248 ctrl = ",".join(sorted(ctrl)) if ctrl else None
249
250 aci = ACIModule(module)
251 aci.construct_url(
252 root_class=dict(
253 aci_class="stpIfPol",
254 aci_rn="infra/ifPol-{0}".format(stp_policy),
255 module_object=stp_policy,
256 target_filter={"name": stp_policy},
257 ),
258 )
259
260 aci.get_existing()
261
262 if state == "present":
263 aci.payload(
264 aci_class="stpIfPol",
265 class_config=dict(
266 name=stp_policy,
267 ctrl=ctrl,
268 descr=description,
269 nameAlias=name_alias,
270 ),
271 )
272
273 aci.get_diff(aci_class="stpIfPol")
274
275 aci.post_config()
276
277 elif state == "absent":
278 aci.delete_config()
279
280 aci.exit_json()
281
282
283 if __name__ == "__main__":
284 main()