Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2023, Gaspard Micol (@gmicol) <[email protected]>
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_match_community_term
16 short_description: Manage Match Community Term (rtctrl:MatchCommTerm)
17 description:
18 - Manage Match Term Based on Community for Match Rule Profiles on Cisco ACI fabrics.
19 options:
20 tenant:
21 description:
22 - The name of an existing tenant.
23 type: str
24 aliases: [ tenant_name ]
25 match_rule:
26 description:
27 - the name of an exising match rule profile.
28 type: str
29 aliases: [ match_rule_name ]
30 match_community_term:
31 description:
32 - the name of the Match Community Term.
33 type: str
34 aliases: [ name, match_community_term_name ]
35 description:
36 description:
37 - The description for the Match Community Term.
38 type: str
39 aliases: [ descr ]
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 notes:
57 - The C(tenant) and the C(match_rule) used must exist before using this module in your playbook.
58 The M(cisco.aci.aci_tenant) and the M(cisco.aci.aci_match_rule) modules can be used for this.
59 seealso:
60 - module: cisco.aci.aci_tenant
61 - module: cisco.aci.aci_match_rule
62 - name: APIC Management Information Model reference
63 description: More information about the internal APIC class B(rtctrl:MatchCommTerm).
64 link: https://developer.cisco.com/docs/apic-mim-ref/
65 author:
66 - Gaspard Micol (@gmicol)
67 """
68
69 EXAMPLES = r"""
70 - name: Create a match with community term
71 cisco.aci.aci_match_community_term:
72 host: apic
73 username: admin
74 password: SomeSecretPassword
75 match_rule: prod_match_rule
76 match_community_term: prod_match_community_term
77 tenant: production
78 state: present
79 delegate_to: localhost
80
81 - name: Delete a match with community term
82 cisco.aci.aci_match_community_term:
83 host: apic
84 username: admin
85 password: SomeSecretPassword
86 match_rule: prod_match_rule
87 tenant: production
88 match_community_term: prod_match_community_term
89 state: absent
90 delegate_to: localhost
91
92 - name: Query all with community terms
93 cisco.aci.aci_match_community_term:
94 host: apic
95 username: admin
96 password: SomeSecretPassword
97 state: query
98 delegate_to: localhost
99 register: query_result
100
101 - name: Query a specific match with community term
102 cisco.aci.aci_match_community_term:
103 host: apic
104 username: admin
105 password: SomeSecretPassword
106 match_rule: prod_match_rule
107 tenant: production
108 match_community_term: prod_match_community_term
109 state: query
110 delegate_to: localhost
111 register: query_result
112 """
113
114 RETURN = r"""
115 current:
116 description: The existing configuration from the APIC after the module has finished
117 returned: success
118 type: list
119 sample:
120 [
121 {
122 "fvTenant": {
123 "attributes": {
124 "descr": "Production environment",
125 "dn": "uni/tn-production",
126 "name": "production",
127 "nameAlias": "",
128 "ownerKey": "",
129 "ownerauto_continue": ""
130 }
131 }
132 }
133 ]
134 error:
135 description: The error information as returned from the APIC
136 returned: failure
137 type: dict
138 sample:
139 {
140 "code": "122",
141 "text": "unknown managed object class foo"
142 }
143 raw:
144 description: The raw output returned by the APIC REST API (xml or json)
145 returned: parse error
146 type: str
147 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
148 sent:
149 description: The actual/minimal configuration pushed to the APIC
150 returned: info
151 type: list
152 sample:
153 {
154 "fvTenant": {
155 "attributes": {
156 "descr": "Production environment"
157 }
158 }
159 }
160 previous:
161 description: The original configuration from the APIC before the module has started
162 returned: info
163 type: list
164 sample:
165 [
166 {
167 "fvTenant": {
168 "attributes": {
169 "descr": "Production",
170 "dn": "uni/tn-production",
171 "name": "production",
172 "nameAlias": "",
173 "ownerKey": "",
174 "ownerauto_continue": ""
175 }
176 }
177 }
178 ]
179 proposed:
180 description: The assembled configuration from the user-provided parameters
181 returned: info
182 type: dict
183 sample:
184 {
185 "fvTenant": {
186 "attributes": {
187 "descr": "Production environment",
188 "name": "production"
189 }
190 }
191 }
192 filter_string:
193 description: The filter string used for the request
194 returned: failure or debug
195 type: str
196 sample: ?rsp-prop-include=config-only
197 method:
198 description: The HTTP method used for the request to the APIC
199 returned: failure or debug
200 type: str
201 sample: POST
202 response:
203 description: The HTTP response from the APIC
204 returned: failure or debug
205 type: str
206 sample: OK (30 bytes)
207 status:
208 description: The HTTP status from the APIC
209 returned: failure or debug
210 type: int
211 sample: 200
212 url:
213 description: The HTTP url used for the request to the APIC
214 returned: failure or debug
215 type: str
216 sample: https://10.11.12.13/api/mo/uni/tn-production.json
217 """
218
219 from ansible.module_utils.basic import AnsibleModule
220 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
221
222
223 def main():
224 argument_spec = aci_argument_spec()
225 argument_spec.update(aci_annotation_spec())
226 argument_spec.update(aci_owner_spec())
227 argument_spec.update(
228 tenant=dict(type="str", aliases=["tenant_name"]), # Not required for querying all objects
229 match_rule=dict(type="str", aliases=["match_rule_name"]), # Not required for querying all objects
230 match_community_term=dict(type="str", aliases=["name", "match_community_term_name"]),
231 description=dict(type="str", aliases=["descr"]),
232 name_alias=dict(type="str"),
233 state=dict(type="str", default="present", choices=["present", "absent", "query"]),
234 )
235
236 module = AnsibleModule(
237 argument_spec=argument_spec,
238 supports_check_mode=True,
239 required_if=[
240 ["state", "absent", ["match_community_term", "tenant", "match_rule"]],
241 ["state", "present", ["match_community_term", "tenant", "match_rule"]],
242 ],
243 )
244
245 match_community_term = module.params.get("match_community_term")
246 description = module.params.get("description")
247 state = module.params.get("state")
248 tenant = module.params.get("tenant")
249 match_rule = module.params.get("match_rule")
250 name_alias = module.params.get("name_alias")
251
252 aci = ACIModule(module)
253
254 aci.construct_url(
255 root_class=dict(
256 aci_class="fvTenant",
257 aci_rn="tn-{0}".format(tenant),
258 module_object=tenant,
259 target_filter={"name": tenant},
260 ),
261 subclass_1=dict(
262 aci_class="rtctrlSubjP",
263 aci_rn="subj-{0}".format(match_rule),
264 module_object=match_rule,
265 target_filter={"name": match_rule},
266 ),
267 subclass_2=dict(
268 aci_class="rtctrlMatchCommTerm",
269 aci_rn="commtrm-{0}".format(match_community_term),
270 module_object=match_community_term,
271 target_filter={"name": match_community_term},
272 ),
273 )
274
275 aci.get_existing()
276
277 if state == "present":
278 aci.payload(
279 aci_class="rtctrlMatchCommTerm",
280 class_config=dict(
281 name=match_community_term,
282 descr=description,
283 nameAlias=name_alias,
284 ),
285 )
286
287 aci.get_diff(aci_class="rtctrlMatchCommTerm")
288
289 aci.post_config()
290
291 elif state == "absent":
292 aci.delete_config()
293
294 aci.exit_json()
295
296
297 if __name__ == "__main__":
298 main()