Debian Codegraph @

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

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