Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2022, Mark Ciecior (@markciecior)
5 # Copyright: (c) 2024, Akini Ross <[email protected]>
6 # GNU General Public License v3.0+ (see LICENSE 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": "certified"}
13
14 DOCUMENTATION = r"""
15 ---
16 module: aci_subject_label
17 short_description: Manage Subject Labels (vz:ConsSubjLbl and vz:ProvSubjLbl)
18 description:
19 - Manage Subject Labels on Cisco ACI fabrics.
20 options:
21 tenant:
22 description:
23 - The name of the Tenant.
24 type: str
25 aliases: [ tenant_name ]
26 l2out:
27 description:
28 - The name of the L2Out.
29 type: str
30 aliases: [ l2out_name ]
31 l3out:
32 description:
33 - The name of the L3Out.
34 type: str
35 aliases: [ l3out_name ]
36 external_epg:
37 description:
38 - The name of the External End Point Group.
39 type: str
40 aliases: [ extepg, extepg_name, external_epg_name ]
41 contract:
42 description:
43 - The name of the Contract.
44 type: str
45 aliases: [ contract_name ]
46 subject:
47 description:
48 - The name of the Subject.
49 type: str
50 aliases: [ subject_name ]
51 ap:
52 description:
53 - The name of the Application Profile.
54 type: str
55 aliases: [ app_profile, app_profile_name, application_profile, application_profile_name]
56 epg:
57 description:
58 - The name of the End Point Group.
59 type: str
60 aliases: [ epg_name ]
61 esg:
62 description:
63 - The name of the Endpoint Security Group.
64 type: str
65 aliases: [ esg_name ]
66 subject_label:
67 description:
68 - The name of the Subject Label.
69 type: str
70 aliases: [ subject_label_name, name, label ]
71 subject_label_type:
72 description:
73 - Determines the type of the Subject Label.
74 type: str
75 required: true
76 choices: [ consumer, provider ]
77 aliases: [ type ]
78 complement:
79 description:
80 - Whether complement is enabled on the Subject Label.
81 - The APIC defaults to C(false) when unset during creation.
82 type: bool
83 tag:
84 description:
85 - The color of a policy label of the Subject Label.
86 - The APIC defaults to C(yellow-green) when unset during creation.
87 type: str
88 choices:
89 - alice_blue
90 - antique_white
91 - aqua
92 - aquamarine
93 - azure
94 - beige
95 - bisque
96 - black
97 - blanched_almond
98 - blue
99 - blue_violet
100 - brown
101 - burlywood
102 - cadet_blue
103 - chartreuse
104 - chocolate
105 - coral
106 - cornflower_blue
107 - cornsilk
108 - crimson
109 - cyan
110 - dark_blue
111 - dark_cyan
112 - dark_goldenrod
113 - dark_gray
114 - dark_green
115 - dark_khaki
116 - dark_magenta
117 - dark_olive_green
118 - dark_orange
119 - dark_orchid
120 - dark_red
121 - dark_salmon
122 - dark_sea_green
123 - dark_slate_blue
124 - dark_slate_gray
125 - dark_turquoise
126 - dark_violet
127 - deep_pink
128 - deep_sky_blue
129 - dim_gray
130 - dodger_blue
131 - fire_brick
132 - floral_white
133 - forest_green
134 - fuchsia
135 - gainsboro
136 - ghost_white
137 - gold
138 - goldenrod
139 - gray
140 - green
141 - green_yellow
142 - honeydew
143 - hot_pink
144 - indian_red
145 - indigo
146 - ivory
147 - khaki
148 - lavender
149 - lavender_blush
150 - lawn_green
151 - lemon_chiffon
152 - light_blue
153 - light_coral
154 - light_cyan
155 - light_goldenrod_yellow
156 - light_gray
157 - light_green
158 - light_pink
159 - light_salmon
160 - light_sea_green
161 - light_sky_blue
162 - light_slate_gray
163 - light_steel_blue
164 - light_yellow
165 - lime
166 - lime_green
167 - linen
168 - magenta
169 - maroon
170 - medium_aquamarine
171 - medium_blue
172 - medium_orchid
173 - medium_purple
174 - medium_sea_green
175 - medium_slate_blue
176 - medium_spring_green
177 - medium_turquoise
178 - medium_violet_red
179 - midnight_blue
180 - mint_cream
181 - misty_rose
182 - moccasin
183 - navajo_white
184 - navy
185 - old_lace
186 - olive
187 - olive_drab
188 - orange
189 - orange_red
190 - orchid
191 - pale_goldenrod
192 - pale_green
193 - pale_turquoise
194 - pale_violet_red
195 - papaya_whip
196 - peachpuff
197 - peru
198 - pink
199 - plum
200 - powder_blue
201 - purple
202 - red
203 - rosy_brown
204 - royal_blue
205 - saddle_brown
206 - salmon
207 - sandy_brown
208 - sea_green
209 - seashell
210 - sienna
211 - silver
212 - sky_blue
213 - slate_blue
214 - slate_gray
215 - snow
216 - spring_green
217 - steel_blue
218 - tan
219 - teal
220 - thistle
221 - tomato
222 - turquoise
223 - violet
224 - wheat
225 - white
226 - white_smoke
227 - yellow
228 - yellow_green
229 description:
230 description:
231 - The description for the Subject Label.
232 type: str
233 aliases: [ descr ]
234 state:
235 description:
236 - Use C(present) or C(absent) for adding or removing.
237 - Use C(query) for listing an object or multiple objects.
238 type: str
239 choices: [ absent, present, query ]
240 default: present
241 name_alias:
242 description:
243 - The alias for the current object. This relates to the nameAlias field in ACI.
244 type: str
245 extends_documentation_fragment:
246 - cisco.aci.aci
247 - cisco.aci.annotation
248 - cisco.aci.owner
249
250 seealso:
251 - name: APIC Management Information Model reference
252 description: More information about the internal APIC class B(vz:ConsSubjLbl) and (vz:ProvSubjLbl).
253 link: https://developer.cisco.com/docs/apic-mim-ref/
254 author:
255 - Mark Ciecior (@markciecior)
256 - Akini Ross (@akinross)
257 """
258
259 EXAMPLES = r"""
260 - name: Add a Subject Label on a Contract Subject
261 cisco.aci.aci_subject_label:
262 host: apic
263 username: admin
264 password: SomeSecretPassword
265 tenant: production
266 contract: web
267 subject: web_subject
268 subject_label: web_subject_label
269 subject_type: consumer
270 state: present
271 delegate_to: localhost
272
273 - name: Add a Subject Label on a L2Out External EPG
274 cisco.aci.aci_subject_label:
275 host: apic
276 username: admin
277 password: SomeSecretPassword
278 tenant: production
279 l2out: l2out_name
280 external_epg: external_epg_name
281 subject_label: l2out_subject_label
282 subject_type: consumer
283 state: present
284 delegate_to: localhost
285
286 - name: Add a Subject Label on a L3Out External EPG
287 cisco.aci.aci_subject_label:
288 host: apic
289 username: admin
290 password: SomeSecretPassword
291 tenant: production
292 l3out: l3out_name
293 external_epg: external_epg_name
294 subject_label: l3out_subject_label
295 subject_type: consumer
296 state: present
297 delegate_to: localhost
298
299 - name: Add a Subject Label on a L3Out External EPG Contract
300 cisco.aci.aci_subject_label:
301 host: apic
302 username: admin
303 password: SomeSecretPassword
304 tenant: production
305 l3out: l3out_name
306 external_epg: external_epg_name
307 contract: web
308 subject_label: l3out_subject_label
309 subject_type: consumer
310 state: present
311 delegate_to: localhost
312
313 - name: Add a Subject Label on a ESG
314 cisco.aci.aci_subject_label:
315 host: apic
316 username: admin
317 password: SomeSecretPassword
318 tenant: production
319 ap: app_profile_name
320 esg: esg_name
321 subject_label: esg_subject_label
322 subject_type: consumer
323 state: present
324 delegate_to: localhost
325
326 - name: Add a Subject Label on a EPG
327 cisco.aci.aci_subject_label:
328 host: apic
329 username: admin
330 password: SomeSecretPassword
331 tenant: production
332 ap: app_profile_name
333 epg: epg_name
334 subject_label: epg_subject_label
335 subject_type: consumer
336 state: present
337 delegate_to: localhost
338
339 - name: Add a Subject Label on a EPG Contract
340 cisco.aci.aci_subject_label:
341 host: apic
342 username: admin
343 password: SomeSecretPassword
344 tenant: production
345 ap: app_profile_name
346 epg: epg_name
347 contract: web
348 subject_label: epg_subject_label
349 subject_type: consumer
350 state: present
351 delegate_to: localhost
352
353 - name: Query a Subject Label on a Contract Subject
354 cisco.aci.aci_subject_label:
355 host: apic
356 username: admin
357 password: SomeSecretPassword
358 tenant: production
359 contract: web
360 subject: web_subject
361 subject_label: web_subject_label
362 subject_type: consumer
363 state: query
364 delegate_to: localhost
365 register: query_result
366
367 - name: Query all Subject Labels
368 cisco.aci.aci_subject_label:
369 host: apic
370 username: admin
371 password: SomeSecretPassword
372 state: query
373 delegate_to: localhost
374 register: query_result
375
376 - name: Remove a Subject Label on a Contract Subject
377 cisco.aci.aci_subject_label:
378 host: apic
379 username: admin
380 password: SomeSecretPassword
381 tenant: production
382 contract: web
383 subject: web_subject
384 subject_label: web_subject_label
385 subject_type: consumer
386 state: absent
387 delegate_to: localhost
388 """
389
390 RETURN = r"""
391 current:
392 description: The existing configuration from the APIC after the module has finished
393 returned: success
394 type: list
395 sample:
396 [
397 {
398 "fvTenant": {
399 "attributes": {
400 "descr": "Production environment",
401 "dn": "uni/tn-production",
402 "name": "production",
403 "nameAlias": "",
404 "ownerKey": "",
405 "ownerTag": ""
406 }
407 }
408 }
409 ]
410 error:
411 description: The error information as returned from the APIC
412 returned: failure
413 type: dict
414 sample:
415 {
416 "code": "122",
417 "text": "unknown managed object class foo"
418 }
419 raw:
420 description: The raw output returned by the APIC REST API (xml or json)
421 returned: parse error
422 type: str
423 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
424 sent:
425 description: The actual/minimal configuration pushed to the APIC
426 returned: info
427 type: list
428 sample:
429 {
430 "fvTenant": {
431 "attributes": {
432 "descr": "Production environment"
433 }
434 }
435 }
436 previous:
437 description: The original configuration from the APIC before the module has started
438 returned: info
439 type: list
440 sample:
441 [
442 {
443 "fvTenant": {
444 "attributes": {
445 "descr": "Production",
446 "dn": "uni/tn-production",
447 "name": "production",
448 "nameAlias": "",
449 "ownerKey": "",
450 "ownerTag": ""
451 }
452 }
453 }
454 ]
455 proposed:
456 description: The assembled configuration from the user-provided parameters
457 returned: info
458 type: dict
459 sample:
460 {
461 "fvTenant": {
462 "attributes": {
463 "descr": "Production environment",
464 "name": "production"
465 }
466 }
467 }
468 filter_string:
469 description: The filter string used for the request
470 returned: failure or debug
471 type: str
472 sample: ?rsp-prop-include=config-only
473 method:
474 description: The HTTP method used for the request to the APIC
475 returned: failure or debug
476 type: str
477 sample: POST
478 response:
479 description: The HTTP response from the APIC
480 returned: failure or debug
481 type: str
482 sample: OK (30 bytes)
483 status:
484 description: The HTTP status from the APIC
485 returned: failure or debug
486 type: int
487 sample: 200
488 url:
489 description: The HTTP url used for the request to the APIC
490 returned: failure or debug
491 type: str
492 sample: https://10.11.12.13/api/mo/uni/tn-production.json
493 """
494
495 from ansible.module_utils.basic import AnsibleModule
496 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
497 from ansible_collections.cisco.aci.plugins.module_utils.constants import ACI_CLASS_MAPPING, SUBJ_LABEL_MAPPING, SUBJ_LABEL_RN, POLICY_LABEL_COLORS
498
499
500 def main():
501 argument_spec = aci_argument_spec()
502 argument_spec.update(aci_annotation_spec())
503 argument_spec.update(aci_owner_spec())
504 argument_spec.update(
505 tenant=dict(type="str", aliases=["tenant_name"]),
506 l2out=dict(type="str", aliases=["l2out_name"]),
507 l3out=dict(type="str", aliases=["l3out_name"]),
508 external_epg=dict(type="str", aliases=["extepg", "extepg_name", "external_epg_name"]),
509 contract=dict(type="str", aliases=["contract_name"]),
510 subject=dict(type="str", aliases=["subject_name"]),
511 ap=dict(type="str", aliases=["app_profile", "app_profile_name", "application_profile", "application_profile_name"]),
512 epg=dict(type="str", aliases=["epg_name"]),
513 esg=dict(type="str", aliases=["esg_name"]),
514 complement=dict(type="bool"),
515 description=dict(type="str", aliases=["descr"]),
516 subject_label=dict(type="str", aliases=["subject_label_name", "name", "label"]),
517 subject_label_type=dict(type="str", choices=["consumer", "provider"], aliases=["type"], required=True),
518 tag=dict(type="str", choices=POLICY_LABEL_COLORS),
519 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
520 name_alias=dict(type="str"),
521 )
522
523 module = AnsibleModule(
524 argument_spec=argument_spec,
525 supports_check_mode=True,
526 required_if=[
527 ["state", "present", ["tenant", "subject_label"]],
528 ["state", "present", ["l2out", "l3out", "epg", "esg", "subject"], True],
529 ["state", "absent", ["tenant", "subject_label"]],
530 ["state", "absent", ["l2out", "l3out", "epg", "esg", "subject"], True],
531 ],
532 mutually_exclusive=[
533 ["l2out", "l3out", "epg", "esg", "subject"],
534 ["esg", "contract"],
535 ["l2out", "contract"],
536 ],
537 required_by={
538 "subject": ["contract"],
539 "l2out": ["external_epg"],
540 "l3out": ["external_epg"],
541 "epg": ["ap"],
542 "esg": ["ap"],
543 },
544 )
545
546 aci = ACIModule(module)
547
548 tenant = module.params.get("tenant")
549 l2out = module.params.get("l2out")
550 l3out = module.params.get("l3out")
551 external_epg = module.params.get("external_epg")
552 contract = module.params.get("contract")
553 subject_label_type = module.params.get("subject_label_type")
554 subject = module.params.get("subject")
555 ap = module.params.get("ap")
556 epg = module.params.get("epg")
557 esg = module.params.get("esg")
558 complement = aci.boolean(module.params.get("complement"))
559 description = module.params.get("description")
560 subject_label = module.params.get("subject_label")
561 tag = module.params.get("tag")
562 state = module.params.get("state")
563 name_alias = module.params.get("name_alias")
564
565 aci_class = SUBJ_LABEL_MAPPING.get(subject_label_type)
566 aci_rn = SUBJ_LABEL_RN.get(subject_label_type) + subject_label if subject_label else None
567
568 if contract:
569 contract_rn = ACI_CLASS_MAPPING.get(subject_label_type).get("rn") + contract
570 contract_class = ACI_CLASS_MAPPING.get(subject_label_type).get("class")
571
572 root_class = dict(
573 aci_class="fvTenant",
574 aci_rn="tn-{0}".format(tenant),
575 module_object=tenant,
576 target_filter={"name": tenant},
577 )
578 subclass_1 = None
579 subclass_2 = None
580 subclass_3 = None
581 subclass_4 = None
582 if esg:
583 subclass_1 = dict(
584 aci_class="fvAp",
585 aci_rn="ap-{0}".format(ap),
586 module_object=ap,
587 target_filter={"name": ap},
588 )
589 subclass_2 = dict(
590 aci_class="fvESg",
591 aci_rn="esg-{0}".format(esg),
592 module_object=esg,
593 target_filter={"name": esg},
594 )
595 subclass_3 = dict(
596 aci_class=aci_class,
597 aci_rn=aci_rn,
598 module_object=subject_label,
599 target_filter={"name": subject_label},
600 )
601 elif l2out:
602 subclass_1 = dict(
603 aci_class="l2extOut",
604 aci_rn="l2out-{0}".format(l2out),
605 module_object=l2out,
606 target_filter={"name": l2out},
607 )
608 subclass_2 = dict(
609 aci_class="l2extInstP",
610 aci_rn="instP-{0}".format(external_epg),
611 module_object=external_epg,
612 target_filter={"name": external_epg},
613 )
614 subclass_3 = dict(
615 aci_class=aci_class,
616 aci_rn=aci_rn,
617 module_object=subject_label,
618 target_filter={"name": subject_label},
619 )
620 elif epg:
621 subclass_1 = dict(
622 aci_class="fvAp",
623 aci_rn="ap-{0}".format(ap),
624 module_object=ap,
625 target_filter={"name": ap},
626 )
627 subclass_2 = dict(
628 aci_class="fvAEPg",
629 aci_rn="epg-{0}".format(epg),
630 module_object=epg,
631 target_filter={"name": epg},
632 )
633 if contract:
634 subclass_3 = dict(
635 aci_class=contract_class,
636 aci_rn=contract_rn,
637 module_object=contract,
638 target_filter={"name": contract},
639 )
640 subclass_4 = dict(
641 aci_class=aci_class,
642 aci_rn=aci_rn,
643 module_object=subject_label,
644 target_filter={"name": subject_label},
645 )
646 else:
647 subclass_3 = dict(
648 aci_class=aci_class,
649 aci_rn=aci_rn,
650 module_object=subject_label,
651 target_filter={"name": subject_label},
652 )
653 elif l3out:
654 subclass_1 = subclass_1 = dict(
655 aci_class="l3extOut",
656 aci_rn="out-{0}".format(l3out),
657 module_object=l3out,
658 target_filter={"name": l3out},
659 )
660 subclass_2 = dict(
661 aci_class="l3extInstP",
662 aci_rn="instP-{0}".format(external_epg),
663 module_object=external_epg,
664 target_filter={"name": external_epg},
665 )
666 if contract:
667 subclass_3 = dict(
668 aci_class=contract_class,
669 aci_rn=contract_rn,
670 module_object=contract,
671 target_filter={"name": contract},
672 )
673 subclass_4 = dict(
674 aci_class=aci_class,
675 aci_rn=aci_rn,
676 module_object=subject_label,
677 target_filter={"name": subject_label},
678 )
679 else:
680 subclass_3 = dict(
681 aci_class=aci_class,
682 aci_rn=aci_rn,
683 module_object=subject_label,
684 target_filter={"name": subject_label},
685 )
686 elif subject:
687 subclass_1 = dict(
688 aci_class="vzBrCP",
689 aci_rn="brc-{0}".format(contract),
690 module_object=contract,
691 target_filter={"name": contract},
692 )
693 subclass_2 = dict(
694 aci_class="vzSubj",
695 aci_rn="subj-{0}".format(subject),
696 module_object=subject,
697 target_filter={"name": subject},
698 )
699 subclass_3 = dict(
700 aci_class=aci_class,
701 aci_rn=aci_rn,
702 module_object=subject_label,
703 target_filter={"name": subject_label},
704 )
705 else: # Query scenario without any filters forcing class query on the subject_label_class
706 root_class = dict(
707 aci_class=aci_class,
708 aci_rn=aci_rn,
709 module_object=subject_label,
710 target_filter={"name": subject_label},
711 )
712
713 aci.construct_url(
714 root_class=root_class,
715 subclass_1=subclass_1,
716 subclass_2=subclass_2,
717 subclass_3=subclass_3,
718 subclass_4=subclass_4,
719 )
720
721 aci.get_existing()
722
723 if state == "present":
724 aci.payload(
725 aci_class=aci_class,
726 class_config=dict(
727 name=subject_label,
728 descr=description,
729 nameAlias=name_alias,
730 isComplement=complement,
731 tag=tag.replace("_", "-") if tag else None,
732 ),
733 )
734
735 aci.get_diff(aci_class=aci_class)
736
737 aci.post_config()
738
739 elif state == "absent":
740 aci.delete_config()
741
742 aci.exit_json()
743
744
745 if __name__ == "__main__":
746 main()