Debian Codegraph @

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