Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2021, Cindy Zhao <[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 DOCUMENTATION = r"""
12 ---
13 module: aci_cloud_epg_selector
14 short_description: Manage Cloud Endpoint Selector (cloud:EPSelector)
15 description:
16 - Manage Cloud Endpoint Selector on Cisco Cloud ACI
17 notes:
18 - More information about the internal APIC class B(cloud:EPSelector) from
19 L(the APIC Management Information Model reference,https://developer.cisco.com/docs/apic-mim-ref/).
20 author:
21 - Cindy Zhao (@cizhao)
22 options:
23 description:
24 description:
25 - Description of the Cloud Endpoint Selector.
26 type: str
27 expressions:
28 description:
29 - Expressions associated to this selector.
30 type: list
31 elements: dict
32 suboptions:
33 key:
34 description:
35 - The key of the expression.
36 - The key is custom or is one of region, zone and ip
37 - The key can be zone only when the site is AWS.
38 required: true
39 type: str
40 operator:
41 description:
42 - The operator associated to the expression.
43 - Operator C(has_key) or C(does_not_have_key) is only available for key custom or zone
44 required: true
45 type: str
46 choices: [ not_in, in, equals, not_equals, has_key, does_not_have_key ]
47 value:
48 description:
49 - The value associated to the expression.
50 - If the operator is C(in) or C(not_in), the value should be a comma separated string.
51 type: str
52 name:
53 description:
54 - The name of the Cloud Endpoint selector.
55 aliases: [ selector, selector_name ]
56 type: str
57 tenant:
58 description:
59 - The name of the existing tenant.
60 required: true
61 type: str
62 ap:
63 description:
64 - The name of the cloud application profile.
65 required: true
66 type: str
67 epg:
68 description:
69 - The name of the cloud EPG.
70 required: true
71 type: str
72 state:
73 description:
74 - Use C(present) or C(absent) for adding or removing.
75 - Use C(query) for listing an object or multiple objects.
76 choices: [ absent, present, query ]
77 default: present
78 type: str
79 extends_documentation_fragment:
80 - cisco.aci.aci
81 - cisco.aci.annotation
82 - cisco.aci.owner
83 """
84
85 EXAMPLES = r"""
86 - name: Create aci cloud epg selector
87 cisco.aci.aci_cloud_epg_selector:
88 host: apic
89 username: admin
90 password: SomeSecretPassword
91 tenant: tenantName
92 ap: apName
93 epg: epgName
94 description: cloud epg selector
95 name: selectorName
96 expressions:
97 - key: ip
98 operator: in
99 value: 10.10.10.1
100 state: present
101 delegate_to: localhost
102
103 - name: Remove cloud epg selector
104 cisco.aci.aci_cloud_epg_selector:
105 host: apic
106 username: admin
107 password: SomeSecretPassword
108 tenant: tenantName
109 ap: apName
110 epg: epgName
111 name: selectorName
112 state: absent
113 delegate_to: localhost
114
115 - name: query all
116 cisco.aci.aci_cloud_epg:
117 host: apic
118 username: admin
119 password: SomeSecretPassword
120 tenant: tenantName
121 ap: apName
122 epg: epgName
123 state: query
124 delegate_to: localhost
125
126 - name: query a specific cloud epg selector
127 cisco.aci.aci_cloud_epg:
128 host: apic
129 username: admin
130 password: SomeSecretPassword
131 tenant: tenantName
132 ap: apName
133 epg: epgName
134 name: selectorName
135 state: query
136 delegate_to: localhost
137 """
138
139 RETURN = r"""
140 current:
141 description: The existing configuration from the APIC after the module has finished
142 returned: success
143 type: list
144 sample:
145 [
146 {
147 "fvTenant": {
148 "attributes": {
149 "descr": "Production environment",
150 "dn": "uni/tn-production",
151 "name": "production",
152 "nameAlias": "",
153 "ownerKey": "",
154 "ownerTag": ""
155 }
156 }
157 }
158 ]
159 error:
160 description: The error information as returned from the APIC
161 returned: failure
162 type: dict
163 sample:
164 {
165 "code": "122",
166 "text": "unknown managed object class foo"
167 }
168 raw:
169 description: The raw output returned by the APIC REST API (xml or json)
170 returned: parse error
171 type: str
172 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
173 sent:
174 description: The actual/minimal configuration pushed to the APIC
175 returned: info
176 type: list
177 sample:
178 {
179 "fvTenant": {
180 "attributes": {
181 "descr": "Production environment"
182 }
183 }
184 }
185 previous:
186 description: The original configuration from the APIC before the module has started
187 returned: info
188 type: list
189 sample:
190 [
191 {
192 "fvTenant": {
193 "attributes": {
194 "descr": "Production",
195 "dn": "uni/tn-production",
196 "name": "production",
197 "nameAlias": "",
198 "ownerKey": "",
199 "ownerTag": ""
200 }
201 }
202 }
203 ]
204 proposed:
205 description: The assembled configuration from the user-provided parameters
206 returned: info
207 type: dict
208 sample:
209 {
210 "fvTenant": {
211 "attributes": {
212 "descr": "Production environment",
213 "name": "production"
214 }
215 }
216 }
217 filter_string:
218 description: The filter string used for the request
219 returned: failure or debug
220 type: str
221 sample: ?rsp-prop-include=config-only
222 method:
223 description: The HTTP method used for the request to the APIC
224 returned: failure or debug
225 type: str
226 sample: POST
227 response:
228 description: The HTTP response from the APIC
229 returned: failure or debug
230 type: str
231 sample: OK (30 bytes)
232 status:
233 description: The HTTP status from the APIC
234 returned: failure or debug
235 type: int
236 sample: 200
237 url:
238 description: The HTTP url used for the request to the APIC
239 returned: failure or debug
240 type: str
241 sample: https://10.11.12.13/api/mo/uni/tn-production.json
242 """
243
244 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, expression_spec, aci_annotation_spec, aci_owner_spec
245 from ansible.module_utils.basic import AnsibleModule
246
247 EXPRESSION_KEYS = {
248 "ip": "IP",
249 "region": "Region",
250 "zone": "Zone",
251 }
252
253 EXPRESSION_OPERATORS = {
254 "not_in": "notin",
255 "not_equals": "!=",
256 "in": "in",
257 "equals": "==",
258 }
259
260
261 def main():
262 argument_spec = aci_argument_spec()
263 argument_spec.update(aci_annotation_spec())
264 argument_spec.update(aci_owner_spec())
265 argument_spec.update(
266 {
267 "description": dict(type="str"),
268 "expressions": dict(type="list", elements="dict", options=expression_spec()),
269 "name": dict(type="str", aliases=["selector", "selector_name"]),
270 "tenant": dict(type="str", required=True),
271 "ap": dict(type="str", required=True),
272 "epg": dict(type="str", required=True),
273 "state": dict(type="str", default="present", choices=["absent", "present", "query"]),
274 }
275 )
276
277 module = AnsibleModule(
278 argument_spec=argument_spec,
279 supports_check_mode=True,
280 required_if=[
281 ["state", "absent", ["name"]],
282 ["state", "present", ["name"]],
283 ],
284 )
285
286 description = module.params.get("description")
287 expressions = module.params.get("expressions")
288 name = module.params.get("name")
289 tenant = module.params.get("tenant")
290 ap = module.params.get("ap")
291 epg = module.params.get("epg")
292 state = module.params.get("state")
293 child_configs = []
294
295 aci = ACIModule(module)
296 aci.construct_url(
297 root_class={
298 "aci_class": "fvTenant",
299 "aci_rn": "tn-{0}".format(tenant),
300 "target_filter": 'eq(fvTenant.name, "{0}")'.format(tenant),
301 "module_object": tenant,
302 },
303 subclass_1={"aci_class": "cloudApp", "aci_rn": "cloudapp-{0}".format(ap), "target_filter": 'eq(cloudApp.name, "{0}")'.format(ap), "module_object": ap},
304 subclass_2={
305 "aci_class": "cloudEPg",
306 "aci_rn": "cloudepg-{0}".format(epg),
307 "target_filter": 'eq(cloudEPg.name, "{0}")'.format(epg),
308 "module_object": epg,
309 },
310 subclass_3={
311 "aci_class": "cloudEPSelector",
312 "aci_rn": "epselector-{0}".format(name),
313 "target_filter": 'eq(cloudEPSelector.name, "{0}")'.format(name),
314 "module_object": name,
315 },
316 child_classes=[],
317 )
318
319 aci.get_existing()
320
321 if state == "present":
322 expressions_list = []
323 for expression in expressions:
324 key = expression.get("key")
325 operator = expression.get("operator")
326 if expression.get("value"):
327 value = "'" + "','".join(expression.get("value").split(",")) + "'"
328 else:
329 value = None
330 if operator in ["has_key", "does_not_have_key"]:
331 if value:
332 module.fail_json(msg="Attribute 'value' is not supported for operator '{0}' in expression '{1}'".format(operator, key))
333 if key in ["ip", "region"]:
334 module.fail_json(msg="Operator '{0}' is not supported when expression key is '{1}'".format(operator, key))
335 if operator in ["not_in", "in", "equals", "not_equals"] and not value:
336 module.fail_json(msg="Attribute 'value' needed for operator '{0}' in expression '{1}'".format(operator, key))
337 if key in ["ip", "region", "zone"]:
338 key = EXPRESSION_KEYS.get(key)
339 else:
340 key = "custom:" + key
341 if operator in ["not_in", "in"]:
342 expressions_list.append("{0} {1}({2})".format(key, EXPRESSION_OPERATORS.get(operator), value))
343 elif operator in ["equals", "not_equals"]:
344 expressions_list.append("{0}{1}{2}".format(key, EXPRESSION_OPERATORS.get(operator), value))
345 elif operator == "does_not_have_key":
346 expressions_list.append("!{0}".format(key))
347 else:
348 expressions_list.append(key)
349 matchExpression = ",".join(expressions_list)
350 aci.payload(
351 aci_class="cloudEPSelector",
352 class_config={
353 "descr": description,
354 "matchExpression": matchExpression,
355 "name": name,
356 },
357 child_configs=child_configs,
358 )
359
360 aci.get_diff(aci_class="cloudEPSelector")
361
362 aci.post_config()
363
364 elif state == "absent":
365 aci.delete_config()
366
367 aci.exit_json()
368
369
370 if __name__ == "__main__":
371 main()