Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_contract_subject.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_contract_subject
15 short_description: Manage initial Contract Subjects (vz:Subj)
16 description:
17 - Manage initial Contract Subjects on Cisco ACI fabrics.
18 options:
19 tenant:
20 description:
21 - The name of the tenant.
22 type: str
23 aliases: [ tenant_name ]
24 subject:
25 description:
26 - The contract subject name.
27 type: str
28 aliases: [ contract_subject, name, subject_name ]
29 apply_both_direction:
30 description:
31 - The direction of traffic matching for the filter.
32 type: str
33 default: both
34 choices: [ both, one-way ]
35 contract:
36 description:
37 - The name of the Contract.
38 type: str
39 aliases: [ contract_name ]
40 contract_type:
41 description:
42 - The type of contract, either standard or Out of Band (oob).
43 type: str
44 choices: [ standard, oob ]
45 default: standard
46 reverse_filter:
47 description:
48 - Determines if the APIC should reverse the src and dst ports to allow the
49 return traffic back, since ACI is stateless filter.
50 - The APIC defaults to C(true) when unset during creation.
51 type: bool
52 priority:
53 description:
54 - The QoS class.
55 - The APIC defaults to C(unspecified) when unset during creation.
56 type: str
57 choices: [ level1, level2, level3, unspecified ]
58 dscp:
59 description:
60 - The target DSCP.
61 - The APIC defaults to C(unspecified) when unset during creation.
62 type: str
63 choices: [ AF11, AF12, AF13, AF21, AF22, AF23, AF31, AF32, AF33, AF41, AF42, AF43,
64 CS0, CS1, CS2, CS3, CS4, CS5, CS6, CS7, EF, VA, unspecified ]
65 aliases: [ target ]
66 priority_consumer_to_provider:
67 description:
68 - The QoS class of Filter Chain For Consumer to Provider.
69 - The APIC defaults to C(unspecified) when unset during creation.
70 type: str
71 choices: [ level1, level2, level3, unspecified ]
72 dscp_consumer_to_provider:
73 description:
74 - The target DSCP of Filter Chain For Consumer to Provider.
75 - The APIC defaults to C(unspecified) when unset during creation.
76 type: str
77 choices: [ AF11, AF12, AF13, AF21, AF22, AF23, AF31, AF32, AF33, AF41, AF42, AF43,
78 CS0, CS1, CS2, CS3, CS4, CS5, CS6, CS7, EF, VA, unspecified ]
79 aliases: [ target_consumer_to_provider ]
80 priority_provider_to_consumer:
81 description:
82 - The QoS class of Filter Chain For Provider to Consumer.
83 - The APIC defaults to C(unspecified) when unset during creation.
84 type: str
85 choices: [ level1, level2, level3, unspecified ]
86 dscp_provider_to_consumer:
87 description:
88 - The target DSCP of Filter Chain For Provider to Consumer.
89 - The APIC defaults to C(unspecified) when unset during creation.
90 type: str
91 choices: [ AF11, AF12, AF13, AF21, AF22, AF23, AF31, AF32, AF33, AF41, AF42, AF43,
92 CS0, CS1, CS2, CS3, CS4, CS5, CS6, CS7, EF, VA, unspecified ]
93 aliases: [ target_provider_to_consumer ]
94 description:
95 description:
96 - Description for the contract subject.
97 type: str
98 aliases: [ descr ]
99 consumer_match:
100 description:
101 - The match criteria across consumers.
102 - The APIC defaults to C(at_least_one) when unset during creation.
103 type: str
104 choices: [ all, at_least_one, at_most_one, none ]
105 provider_match:
106 description:
107 - The match criteria across providers.
108 - The APIC defaults to C(at_least_one) when unset during creation.
109 type: str
110 choices: [ all, at_least_one, at_most_one, none ]
111 state:
112 description:
113 - Use C(present) or C(absent) for adding or removing.
114 - Use C(query) for listing an object or multiple objects.
115 type: str
116 choices: [ absent, present, query ]
117 default: present
118 name_alias:
119 description:
120 - The alias for the current object. This relates to the nameAlias field in ACI.
121 type: str
122 extends_documentation_fragment:
123 - cisco.aci.aci
124 - cisco.aci.annotation
125
126 notes:
127 - The C(tenant) and C(contract) used must exist before using this module in your playbook.
128 The M(cisco.aci.aci_tenant) and M(cisco.aci.aci_contract) or M(cisco.aci.aci_oob_contract) modules can be used for this.
129 seealso:
130 - module: cisco.aci.aci_contract
131 - module: cisco.aci.aci_oob_contract
132 - module: cisco.aci.aci_tenant
133 - name: APIC Management Information Model reference
134 description: More information about the internal APIC class B(vz:Subj).
135 link: https://developer.cisco.com/docs/apic-mim-ref/
136 author:
137 - Swetha Chunduri (@schunduri)
138 """
139
140 EXAMPLES = r"""
141 - name: Add a new contract subject
142 cisco.aci.aci_contract_subject:
143 host: apic
144 username: admin
145 password: SomeSecretPassword
146 tenant: production
147 contract: web_to_db
148 subject: default
149 description: test
150 reverse_filter: true
151 priority: level1
152 dscp: unspecified
153 state: present
154 register: query_result
155
156 - name: Add a new subject to an out of band contract
157 cisco.aci.aci_contract_subject:
158 host: apic
159 username: admin
160 password: SomeSecretPassword
161 tenant: production
162 contract: oob_mgmt_ctr
163 contract_type: oob
164 subject: default
165 description: test
166 state: present
167 register: query_result
168
169 - name: Remove a contract subject
170 cisco.aci.aci_contract_subject:
171 host: apic
172 username: admin
173 password: SomeSecretPassword
174 tenant: production
175 contract: web_to_db
176 subject: default
177 state: absent
178 delegate_to: localhost
179
180 - name: Query a contract subject
181 cisco.aci.aci_contract_subject:
182 host: apic
183 username: admin
184 password: SomeSecretPassword
185 tenant: production
186 contract: web_to_db
187 subject: default
188 state: query
189 delegate_to: localhost
190 register: query_result
191
192 - name: Query all contract subjects
193 cisco.aci.aci_contract_subject:
194 host: apic
195 username: admin
196 password: SomeSecretPassword
197 state: query
198 delegate_to: localhost
199 register: query_result
200 """
201
202 RETURN = r"""
203 current:
204 description: The existing configuration from the APIC after the module has finished
205 returned: success
206 type: list
207 sample:
208 [
209 {
210 "fvTenant": {
211 "attributes": {
212 "descr": "Production environment",
213 "dn": "uni/tn-production",
214 "name": "production",
215 "nameAlias": "",
216 "ownerKey": "",
217 "ownerTag": ""
218 }
219 }
220 }
221 ]
222 error:
223 description: The error information as returned from the APIC
224 returned: failure
225 type: dict
226 sample:
227 {
228 "code": "122",
229 "text": "unknown managed object class foo"
230 }
231 raw:
232 description: The raw output returned by the APIC REST API (xml or json)
233 returned: parse error
234 type: str
235 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
236 sent:
237 description: The actual/minimal configuration pushed to the APIC
238 returned: info
239 type: list
240 sample:
241 {
242 "fvTenant": {
243 "attributes": {
244 "descr": "Production environment"
245 }
246 }
247 }
248 previous:
249 description: The original configuration from the APIC before the module has started
250 returned: info
251 type: list
252 sample:
253 [
254 {
255 "fvTenant": {
256 "attributes": {
257 "descr": "Production",
258 "dn": "uni/tn-production",
259 "name": "production",
260 "nameAlias": "",
261 "ownerKey": "",
262 "ownerTag": ""
263 }
264 }
265 }
266 ]
267 proposed:
268 description: The assembled configuration from the user-provided parameters
269 returned: info
270 type: dict
271 sample:
272 {
273 "fvTenant": {
274 "attributes": {
275 "descr": "Production environment",
276 "name": "production"
277 }
278 }
279 }
280 filter_string:
281 description: The filter string used for the request
282 returned: failure or debug
283 type: str
284 sample: ?rsp-prop-include=config-only
285 method:
286 description: The HTTP method used for the request to the APIC
287 returned: failure or debug
288 type: str
289 sample: POST
290 response:
291 description: The HTTP response from the APIC
292 returned: failure or debug
293 type: str
294 sample: OK (30 bytes)
295 status:
296 description: The HTTP status from the APIC
297 returned: failure or debug
298 type: int
299 sample: 200
300 url:
301 description: The HTTP url used for the request to the APIC
302 returned: failure or debug
303 type: str
304 sample: https://10.11.12.13/api/mo/uni/tn-production.json
305 """
306
307 from ansible.module_utils.basic import AnsibleModule
308 from ansible_collections.cisco.aci.plugins.module_utils.aci import (
309 ACIModule,
310 aci_argument_spec,
311 aci_annotation_spec,
312 aci_contract_dscp_spec,
313 aci_contract_qos_spec,
314 )
315 from ansible_collections.cisco.aci.plugins.module_utils.constants import CONTRACT_CLASS_MAPPING
316
317 MATCH_MAPPING = dict(
318 all="All",
319 at_least_one="AtleastOne",
320 at_most_one="AtmostOne",
321 none="None",
322 )
323
324
325 def main():
326 argument_spec = aci_argument_spec()
327 argument_spec.update(aci_annotation_spec())
328 argument_spec.update(
329 tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects
330 contract=dict(type="str", aliases=["contract_name"]), # Not required for querying all objects
331 contract_type=dict(type="str", choices=["standard", "oob"], default="standard"),
332 subject=dict(type="str", aliases=["contract_subject", "name", "subject_name"]), # Not required for querying all objects
333 reverse_filter=dict(type="bool"),
334 description=dict(type="str", aliases=["descr"]),
335 consumer_match=dict(type="str", choices=["all", "at_least_one", "at_most_one", "none"]),
336 provider_match=dict(type="str", choices=["all", "at_least_one", "at_most_one", "none"]),
337 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
338 name_alias=dict(type="str"),
339 # default both because of back-worth compatibility and for determining which config to push
340 apply_both_direction=dict(type="str", default="both", choices=["both", "one-way"]),
341 priority=aci_contract_qos_spec(),
342 dscp=aci_contract_dscp_spec(),
343 priority_consumer_to_provider=aci_contract_qos_spec(),
344 dscp_consumer_to_provider=aci_contract_dscp_spec("consumer_to_provider"),
345 priority_provider_to_consumer=aci_contract_qos_spec(),
346 dscp_provider_to_consumer=aci_contract_dscp_spec("provider_to_consumer"),
347 )
348
349 module = AnsibleModule(
350 argument_spec=argument_spec,
351 supports_check_mode=True,
352 required_if=[
353 ["state", "absent", ["contract", "subject", "tenant"]],
354 ["state", "present", ["contract", "subject", "tenant"]],
355 ],
356 )
357
358 aci = ACIModule(module)
359
360 subject = module.params.get("subject")
361 priority = module.params.get("priority")
362 dscp = module.params.get("dscp")
363 priority_consumer_to_provider = module.params.get("priority_consumer_to_provider")
364 dscp_consumer_to_provider = module.params.get("dscp_consumer_to_provider")
365 priority_provider_to_consumer = module.params.get("priority_provider_to_consumer")
366 dscp_provider_to_consumer = module.params.get("dscp_provider_to_consumer")
367 reverse_filter = aci.boolean(module.params.get("reverse_filter"))
368 contract = module.params.get("contract")
369 contract_type = module.params.get("contract_type")
370 description = module.params.get("description")
371 consumer_match = module.params.get("consumer_match")
372 if consumer_match is not None:
373 consumer_match = MATCH_MAPPING.get(consumer_match)
374 provider_match = module.params.get("provider_match")
375 if provider_match is not None:
376 provider_match = MATCH_MAPPING.get(provider_match)
377 state = module.params.get("state")
378 tenant = module.params.get("tenant")
379 name_alias = module.params.get("name_alias")
380 direction = module.params.get("apply_both_direction")
381
382 subject_class = "vzSubj"
383
384 base_subject_dict = dict(
385 root_class=dict(
386 aci_class="fvTenant",
387 aci_rn="tn-{0}".format(tenant),
388 module_object=tenant,
389 target_filter={"name": tenant},
390 ),
391 subclass_1=dict(
392 aci_class=CONTRACT_CLASS_MAPPING[contract_type]["class"],
393 aci_rn=CONTRACT_CLASS_MAPPING[contract_type]["rn"].format(contract),
394 module_object=contract,
395 target_filter={"name": contract},
396 ),
397 subclass_2=dict(
398 aci_class=subject_class,
399 aci_rn="subj-{0}".format(subject),
400 module_object=subject,
401 target_filter={"name": subject},
402 ),
403 )
404
405 # start logic to be consistent with GUI to only allow both direction or one-way
406 aci.construct_url(
407 root_class=base_subject_dict.get("root_class"),
408 subclass_1=base_subject_dict.get("subclass_1"),
409 subclass_2=base_subject_dict.get("subclass_2"),
410 child_classes=["vzInTerm", "vzOutTerm"],
411 )
412 aci.get_existing()
413 direction_options = ["both", "one-way"]
414 if state != "query":
415 if aci.existing and subject_class in aci.existing[0]:
416 direction_options = ["one-way"] if "children" in aci.existing[0][subject_class] else ["both"]
417 if direction not in direction_options:
418 module.fail_json(msg="Direction is not allowed, valid option is {0}.".format(" or ".join(direction_options)))
419 # end logic to be consistent with GUI to only allow both direction or one-way
420
421 if state == "present":
422 config = dict(
423 name=subject,
424 prio=priority,
425 revFltPorts=reverse_filter,
426 targetDscp=dscp,
427 consMatchT=consumer_match,
428 provMatchT=provider_match,
429 descr=description,
430 nameAlias=name_alias,
431 )
432
433 child_configs = []
434 if direction == "one-way" and (
435 len(direction_options) == 2
436 or dscp_consumer_to_provider is not None
437 or priority_consumer_to_provider is not None
438 or dscp_provider_to_consumer is not None
439 or priority_provider_to_consumer is not None
440 ):
441 subj_dn = "uni/tn-{0}/brc-{1}/subj-{2}".format(tenant, contract, subject)
442 child_configs = [
443 dict(
444 vzInTerm=dict(attributes=dict(dn="{0}/intmnl".format(subj_dn), targetDscp=dscp_consumer_to_provider, prio=priority_consumer_to_provider))
445 ),
446 dict(
447 vzOutTerm=dict(attributes=dict(dn="{0}/outtmnl".format(subj_dn), targetDscp=dscp_provider_to_consumer, prio=priority_provider_to_consumer))
448 ),
449 ]
450
451 aci.payload(aci_class=subject_class, class_config=config, child_configs=child_configs)
452
453 aci.get_diff(aci_class=subject_class)
454
455 aci.post_config()
456
457 elif state == "absent":
458 aci.delete_config()
459
460 aci.exit_json()
461
462
463 if __name__ == "__main__":
464 main()