Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2022, Sabari Jaganathan (@sajagana) <[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
14 DOCUMENTATION = r"""
15 ---
16 module: aci_esg
17 short_description: Manage Endpoint Security Groups (ESGs) objects (fv:ESg)
18 description:
19 - Manage Endpoint Security Groups (ESGs) on Cisco ACI fabrics.
20
21 options:
22 tenant:
23 description:
24 - Name of the tenant.
25 type: str
26 aliases: [ tenant_name ]
27 ap:
28 description:
29 - The name of the application profile.
30 type: str
31 aliases: [ app_profile, app_profile_name ]
32 esg:
33 description:
34 - Name of the Endpoint Security Group.
35 type: str
36 aliases: [ esg_name, name ]
37 admin_state:
38 description:
39 - Use C(false) to set 'Admin Up' on the ESG Admin state and is the default.
40 - Use C(true) to set 'Admin Shut' on the ESG Admin state
41 type: bool
42 choices: [ false, true ]
43 vrf:
44 description:
45 - Name of the VRF
46 type: str
47 aliases: [ vrf_name ]
48 description:
49 description:
50 - Endpoint security group description.
51 type: str
52 aliases: [ descr ]
53 intra_esg_isolation:
54 description:
55 - The default value of Intra ESG Isolation is C(unenforced).
56 type: str
57 choices: [ enforced, unenforced ]
58 preferred_group_member:
59 description:
60 - The default value of Preferred Group Member is C(exclude).
61 type: str
62 choices: [ exclude, include ]
63 state:
64 description:
65 - Use C(present) or C(absent) for adding or removing.
66 - Use C(query) for listing an object or multiple objects.
67 type: str
68 choices: [ absent, present, query ]
69 default: present
70 name_alias:
71 description:
72 - The alias for the current object. This relates to the nameAlias field in ACI.
73 type: str
74 extends_documentation_fragment:
75 - cisco.aci.aci
76 - cisco.aci.annotation
77
78 seealso:
79 - module: cisco.aci.aci_aep_to_domain
80 - name: APIC Management Information Model reference
81 description: More information about the internal APIC classes B(infra:AttEntityP) and B(infra:ProvAcc).
82 link: https://developer.cisco.com/docs/apic-mim-ref/
83 author:
84 - Sabari Jaganathan (@sajagana)
85 """
86
87
88 EXAMPLES = r"""
89 - name: Add a new ESG
90 cisco.aci.aci_esg:
91 host: apic
92 username: admin
93 password: SomeSecretPassword
94 tenant: production
95 ap: intranet
96 esg: web_esg
97 vrf: 'default'
98 description: Web Intranet ESG
99 state: present
100 delegate_to: localhost
101
102 - name: Add list of ESGs
103 cisco.aci.aci_esg:
104 host: apic
105 username: admin
106 password: SomeSecretPassword
107 tenant: production
108 ap: ticketing
109 esg: "{{ item.esg }}"
110 description: Ticketing ESG
111 vrf: 'default'
112 state: present
113 delegate_to: localhost
114 with_items:
115 - esg: web
116 - esg: database
117
118 - name: Query an ESG
119 cisco.aci.aci_esg:
120 host: apic
121 username: admin
122 password: SomeSecretPassword
123 tenant: production
124 ap: ticketing
125 esg: web_esg
126 state: query
127 delegate_to: localhost
128
129 - name: Query all ESGs
130 cisco.aci.aci_esg:
131 host: apic
132 username: admin
133 password: SomeSecretPassword
134 state: query
135 delegate_to: localhost
136
137 - name: Query all ESGs with a Specific Name
138 cisco.aci.aci_esg:
139 host: apic
140 username: admin
141 password: SomeSecretPassword
142 esg: web_esg
143 state: query
144 delegate_to: localhost
145
146 - name: Query all ESGs of an App Profile
147 cisco.aci.aci_esg:
148 host: apic
149 username: admin
150 password: SomeSecretPassword
151 ap: ticketing
152 state: query
153 delegate_to: localhost
154
155 - name: Remove an ESG
156 cisco.aci.aci_esg:
157 host: apic
158 username: admin
159 password: SomeSecretPassword
160 tenant: production
161 app_profile: intranet
162 esg: web_esg
163 state: absent
164 delegate_to: localhost
165 """
166
167 RETURN = r"""
168 current:
169 description: The existing configuration from the APIC after the module has finished
170 returned: success
171 type: list
172 sample:
173 [
174 {
175 "fvTenant": {
176 "attributes": {
177 "descr": "Production environment",
178 "dn": "uni/tn-production",
179 "name": "production",
180 "nameAlias": "",
181 "ownerKey": "",
182 "ownerTag": ""
183 }
184 }
185 }
186 ]
187 error:
188 description: The error information as returned from the APIC
189 returned: failure
190 type: dict
191 sample:
192 {
193 "code": "122",
194 "text": "unknown managed object class foo"
195 }
196 raw:
197 description: The raw output returned by the APIC REST API (xml or json)
198 returned: parse error
199 type: str
200 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
201 sent:
202 description: The actual/minimal configuration pushed to the APIC
203 returned: info
204 type: list
205 sample:
206 {
207 "fvTenant": {
208 "attributes": {
209 "descr": "Production environment"
210 }
211 }
212 }
213 previous:
214 description: The original configuration from the APIC before the module has started
215 returned: info
216 type: list
217 sample:
218 [
219 {
220 "fvTenant": {
221 "attributes": {
222 "descr": "Production",
223 "dn": "uni/tn-production",
224 "name": "production",
225 "nameAlias": "",
226 "ownerKey": "",
227 "ownerTag": ""
228 }
229 }
230 }
231 ]
232 proposed:
233 description: The assembled configuration from the user-provided parameters
234 returned: info
235 type: dict
236 sample:
237 {
238 "fvTenant": {
239 "attributes": {
240 "descr": "Production environment",
241 "name": "production"
242 }
243 }
244 }
245 filter_string:
246 description: The filter string used for the request
247 returned: failure or debug
248 type: str
249 sample: ?rsp-prop-include=config-only
250 method:
251 description: The HTTP method used for the request to the APIC
252 returned: failure or debug
253 type: str
254 sample: POST
255 response:
256 description: The HTTP response from the APIC
257 returned: failure or debug
258 type: str
259 sample: OK (30 bytes)
260 status:
261 description: The HTTP status from the APIC
262 returned: failure or debug
263 type: int
264 sample: 200
265 url:
266 description: The HTTP url used for the request to the APIC
267 returned: failure or debug
268 type: str
269 sample: https://10.11.12.13/api/mo/uni/tn-production.json
270 """
271
272 from ansible.module_utils.basic import AnsibleModule
273 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
274
275
276 def main():
277 argument_spec = aci_argument_spec()
278 argument_spec.update(aci_annotation_spec())
279 argument_spec.update(
280 tenant=dict(type="str", aliases=["tenant_name"]),
281 ap=dict(type="str", aliases=["app_profile", "app_profile_name"]),
282 esg=dict(type="str", aliases=["name", "esg_name"]),
283 admin_state=dict(type="bool", choices=[False, True]), # ESG Admin State
284 vrf=dict(type="str", aliases=["vrf_name"]), # ESG VRF name
285 description=dict(type="str", aliases=["descr"]),
286 intra_esg_isolation=dict(
287 type="str",
288 choices=["enforced", "unenforced"],
289 ), # Intra ESG Isolation
290 preferred_group_member=dict(type="str", choices=["exclude", "include"]), # Preferred Group Member
291 state=dict(
292 type="str",
293 default="present",
294 choices=["absent", "present", "query"],
295 ),
296 name_alias=dict(type="str"),
297 )
298
299 module = AnsibleModule(
300 argument_spec=argument_spec,
301 supports_check_mode=True,
302 required_if=[
303 ["state", "absent", ["tenant", "ap", "esg"]],
304 ["state", "present", ["tenant", "ap", "esg", "vrf"]],
305 ],
306 )
307
308 aci = ACIModule(module)
309 tenant = module.params.get("tenant")
310 ap = module.params.get("ap")
311 esg = module.params.get("esg")
312 admin_state = module.params.get("admin_state")
313 vrf = module.params.get("vrf")
314 description = module.params.get("description")
315 intra_esg_isolation = module.params.get("intra_esg_isolation")
316 preferred_group_member = module.params.get("preferred_group_member")
317 state = module.params.get("state")
318 name_alias = module.params.get("name_alias")
319
320 aci.construct_url(
321 root_class=dict(
322 aci_class="fvTenant",
323 aci_rn="tn-{0}".format(tenant),
324 module_object=tenant,
325 target_filter={"name": tenant},
326 ),
327 subclass_1=dict(
328 aci_class="fvAp",
329 aci_rn="ap-{0}".format(ap),
330 module_object=ap,
331 target_filter={"name": ap},
332 ),
333 subclass_2=dict(
334 aci_class="fvESg",
335 aci_rn="esg-{0}".format(esg),
336 module_object=esg,
337 target_filter={"name": esg},
338 ),
339 child_classes=[
340 "fvRsScope",
341 ],
342 )
343
344 aci.get_existing()
345
346 if state == "present":
347 state_mapping = {True: "yes", False: "no"}
348 shutdown = state_mapping.get(admin_state)
349 # VRF Selection - fvRsScope
350 aci.payload(
351 aci_class="fvESg",
352 class_config=dict(
353 name=esg,
354 descr=description,
355 shutdown=shutdown,
356 pcEnfPref=intra_esg_isolation,
357 prefGrMemb=preferred_group_member,
358 nameAlias=name_alias,
359 ),
360 child_configs=[dict(fvRsScope=dict(attributes=dict(tnFvCtxName=vrf)))],
361 )
362
363 aci.get_diff(aci_class="fvESg")
364
365 aci.post_config()
366
367 elif state == "absent":
368 aci.delete_config()
369
370 aci.exit_json()
371
372
373 if __name__ == "__main__":
374 main()