Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_interface_policy_l2.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_policy_l2
15 short_description: Manage Layer 2 interface policies (l2:IfPol)
16 description:
17 - Manage Layer 2 interface policies on Cisco ACI fabrics.
18 options:
19 l2_policy:
20 description:
21 - The name of the Layer 2 interface policy.
22 type: str
23 aliases: [ name ]
24 description:
25 description:
26 - The description of the Layer 2 interface policy.
27 type: str
28 aliases: [ descr ]
29 qinq:
30 description:
31 - Determines if QinQ is disabled or if the port should be considered a core or edge port.
32 - The APIC defaults to C(disabled) when unset during creation.
33 type: str
34 choices: [ core, disabled, edge ]
35 vepa:
36 description:
37 - Determines if Virtual Ethernet Port Aggregator is disabled or enabled.
38 - The APIC defaults to C(false) when unset during creation.
39 type: bool
40 vlan_scope:
41 description:
42 - The scope of the VLAN.
43 - The APIC defaults to C(global) when unset during creation.
44 type: str
45 choices: [ global, portlocal ]
46 state:
47 description:
48 - Use C(present) or C(absent) for adding or removing.
49 - Use C(query) for listing an object or multiple objects.
50 type: str
51 choices: [ absent, present, query ]
52 default: present
53 name_alias:
54 description:
55 - The alias for the current object. This relates to the nameAlias field in ACI.
56 type: str
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 class B(l2:IfPol).
65 link: https://developer.cisco.com/docs/apic-mim-ref/
66 author:
67 - Dag Wieers (@dagwieers)
68 """
69
70 EXAMPLES = r"""
71 - name: Create a Layer2 interface policy
72 cisco.aci.aci_interface_policy_l2:
73 host: apic
74 username: admin
75 password: SomeSecretPassword
76 l2_policy: PORT_LOCAL
77 vlan_scope: portlocal
78 state: present
79 delegate_to: localhost
80
81 - name: Delete a Layer2 interface policy
82 cisco.aci.aci_interface_policy_l2:
83 host: apic
84 username: admin
85 password: SomeSecretPassword
86 l2_policy: PORT_LOCAL
87 state: absent
88 delegate_to: localhost
89
90 - name: Query all Layer2 interface policies
91 cisco.aci.aci_interface_policy_l2:
92 host: apic
93 username: admin
94 password: SomeSecretPassword
95 state: query
96 delegate_to: localhost
97 register: query_result
98
99 - name: Query a specific Layer2 interface policy
100 cisco.aci.aci_interface_policy_l2:
101 host: apic
102 username: admin
103 password: SomeSecretPassword
104 l2_policy: PORT_LOCAL
105 state: query
106 delegate_to: localhost
107 register: query_result
108 """
109
110 RETURN = r"""
111 current:
112 description: The existing configuration from the APIC after the module has finished
113 returned: success
114 type: list
115 sample:
116 [
117 {
118 "fvTenant": {
119 "attributes": {
120 "descr": "Production environment",
121 "dn": "uni/tn-production",
122 "name": "production",
123 "nameAlias": "",
124 "ownerKey": "",
125 "ownerTag": ""
126 }
127 }
128 }
129 ]
130 error:
131 description: The error information as returned from the APIC
132 returned: failure
133 type: dict
134 sample:
135 {
136 "code": "122",
137 "text": "unknown managed object class foo"
138 }
139 raw:
140 description: The raw output returned by the APIC REST API (xml or json)
141 returned: parse error
142 type: str
143 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
144 sent:
145 description: The actual/minimal configuration pushed to the APIC
146 returned: info
147 type: list
148 sample:
149 {
150 "fvTenant": {
151 "attributes": {
152 "descr": "Production environment"
153 }
154 }
155 }
156 previous:
157 description: The original configuration from the APIC before the module has started
158 returned: info
159 type: list
160 sample:
161 [
162 {
163 "fvTenant": {
164 "attributes": {
165 "descr": "Production",
166 "dn": "uni/tn-production",
167 "name": "production",
168 "nameAlias": "",
169 "ownerKey": "",
170 "ownerTag": ""
171 }
172 }
173 }
174 ]
175 proposed:
176 description: The assembled configuration from the user-provided parameters
177 returned: info
178 type: dict
179 sample:
180 {
181 "fvTenant": {
182 "attributes": {
183 "descr": "Production environment",
184 "name": "production"
185 }
186 }
187 }
188 filter_string:
189 description: The filter string used for the request
190 returned: failure or debug
191 type: str
192 sample: ?rsp-prop-include=config-only
193 method:
194 description: The HTTP method used for the request to the APIC
195 returned: failure or debug
196 type: str
197 sample: POST
198 response:
199 description: The HTTP response from the APIC
200 returned: failure or debug
201 type: str
202 sample: OK (30 bytes)
203 status:
204 description: The HTTP status from the APIC
205 returned: failure or debug
206 type: int
207 sample: 200
208 url:
209 description: The HTTP url used for the request to the APIC
210 returned: failure or debug
211 type: str
212 sample: https://10.11.12.13/api/mo/uni/tn-production.json
213 """
214
215 from ansible.module_utils.basic import AnsibleModule
216 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
217
218 # Mapping dicts are used to normalize the proposed data to what the APIC expects, which will keep diffs accurate
219 QINQ_MAPPING = dict(
220 core="corePort",
221 disabled="disabled",
222 edge="edgePort",
223 )
224
225
226 def main():
227 argument_spec = aci_argument_spec()
228 argument_spec.update(aci_annotation_spec())
229 argument_spec.update(aci_owner_spec())
230 argument_spec.update(
231 l2_policy=dict(type="str", aliases=["name"]), # Not required for querying all policies
232 description=dict(type="str", aliases=["descr"]),
233 vlan_scope=dict(type="str", choices=["global", "portlocal"]), # No default provided on purpose
234 qinq=dict(type="str", choices=["core", "disabled", "edge"]),
235 vepa=dict(type="bool"),
236 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
237 name_alias=dict(type="str"),
238 )
239
240 module = AnsibleModule(
241 argument_spec=argument_spec,
242 supports_check_mode=True,
243 required_if=[
244 ["state", "absent", ["l2_policy"]],
245 ["state", "present", ["l2_policy"]],
246 ],
247 )
248
249 aci = ACIModule(module)
250
251 l2_policy = module.params.get("l2_policy")
252 vlan_scope = module.params.get("vlan_scope")
253 qinq = module.params.get("qinq")
254 if qinq is not None:
255 qinq = QINQ_MAPPING.get(qinq)
256 vepa = aci.boolean(module.params.get("vepa"), "enabled", "disabled")
257 description = module.params.get("description")
258 state = module.params.get("state")
259 name_alias = module.params.get("name_alias")
260
261 aci.construct_url(
262 root_class=dict(
263 aci_class="l2IfPol",
264 aci_rn="infra/l2IfP-{0}".format(l2_policy),
265 module_object=l2_policy,
266 target_filter={"name": l2_policy},
267 ),
268 )
269
270 aci.get_existing()
271
272 if state == "present":
273 aci.payload(
274 aci_class="l2IfPol",
275 class_config=dict(
276 name=l2_policy,
277 descr=description,
278 vlanScope=vlan_scope,
279 qinq=qinq,
280 vepa=vepa,
281 nameAlias=name_alias,
282 ),
283 )
284
285 aci.get_diff(aci_class="l2IfPol")
286
287 aci.post_config()
288
289 elif state == "absent":
290 aci.delete_config()
291
292 aci.exit_json()
293
294
295 if __name__ == "__main__":
296 main()