Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_esg_ip_subnet_selector.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_ip_subnet_selector
17 short_description: Manage ESG IP Subnet selector (fv:EPSelector)
18 description:
19 - Manage Endpoint Security Groups (ESG) IP Subnet selector 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 ]
37 ip:
38 description:
39 - IP address of the subnet selector.
40 type: str
41 aliases: [ subnet ]
42 description:
43 description:
44 - Description of the ESG IP Subnet selector.
45 type: str
46 aliases: [ ip_subnet_selector_description ]
47 state:
48 description:
49 - Use C(present) or C(absent) for adding or removing.
50 - Use C(query) for listing an object or multiple objects.
51 type: str
52 choices: [ absent, present, query ]
53 default: present
54 name_alias:
55 description:
56 - The alias for the current object. This relates to the nameAlias field in ACI.
57 type: str
58 extends_documentation_fragment:
59 - cisco.aci.aci
60 - cisco.aci.annotation
61
62 seealso:
63 - module: cisco.aci.aci_esg
64 - name: Manage Endpoint Security Groups (ESGs) objects (fv:ESg)
65 description: Manage Endpoint Security Groups (ESGs) on Cisco ACI fabrics.
66 link: https://developer.cisco.com/docs/apic-mim-ref/
67 author:
68 - Sabari Jaganathan (@sajagana)
69 """
70
71
72 EXAMPLES = r"""
73 - name: Add an IP subnet selector
74 cisco.aci.aci_esg_ip_subnet_selector:
75 host: apic
76 username: admin
77 password: SomeSecretPassword
78 tenant: production
79 ap: production_ap
80 esg: web_esg
81 ip: "10.0.0.0"
82 description: "IP Subnet Selector Description"
83 state: present
84 delegate_to: localhost
85
86 - name: Query one IP subnet selector
87 cisco.aci.aci_esg_ip_subnet_selector:
88 host: apic
89 username: admin
90 password: SomeSecretPassword
91 tenant: production
92 ap: production_ap
93 esg: web_esg
94 ip: "10.0.0.0"
95 state: query
96 delegate_to: localhost
97
98 - name: Query all IP subnet selector
99 cisco.aci.aci_esg_ip_subnet_selector:
100 host: apic
101 username: admin
102 password: SomeSecretPassword
103 state: query
104 delegate_to: localhost
105
106 - name: Remove an IP subnet selector
107 cisco.aci.aci_esg_ip_subnet_selector:
108 host: apic
109 username: admin
110 password: SomeSecretPassword
111 tenant: production
112 ap: production_ap
113 esg: web_esg
114 ip: "10.0.0.0"
115 state: absent
116 delegate_to: localhost
117 """
118
119 RETURN = r"""
120 current:
121 description: The existing configuration from the APIC after the module has finished
122 returned: success
123 type: list
124 sample:
125 [
126 {
127 "fvTenant": {
128 "attributes": {
129 "descr": "Production environment",
130 "dn": "uni/tn-production",
131 "name": "production",
132 "nameAlias": "",
133 "ownerKey": "",
134 "ownerTag": ""
135 }
136 }
137 }
138 ]
139 error:
140 description: The error information as returned from the APIC
141 returned: failure
142 type: dict
143 sample:
144 {
145 "code": "122",
146 "text": "unknown managed object class foo"
147 }
148 raw:
149 description: The raw output returned by the APIC REST API (xml or json)
150 returned: parse error
151 type: str
152 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
153 sent:
154 description: The actual/minimal configuration pushed to the APIC
155 returned: info
156 type: list
157 sample:
158 {
159 "fvTenant": {
160 "attributes": {
161 "descr": "Production environment"
162 }
163 }
164 }
165 previous:
166 description: The original configuration from the APIC before the module has started
167 returned: info
168 type: list
169 sample:
170 [
171 {
172 "fvTenant": {
173 "attributes": {
174 "descr": "Production",
175 "dn": "uni/tn-production",
176 "name": "production",
177 "nameAlias": "",
178 "ownerKey": "",
179 "ownerTag": ""
180 }
181 }
182 }
183 ]
184 proposed:
185 description: The assembled configuration from the user-provided parameters
186 returned: info
187 type: dict
188 sample:
189 {
190 "fvTenant": {
191 "attributes": {
192 "descr": "Production environment",
193 "name": "production"
194 }
195 }
196 }
197 filter_string:
198 description: The filter string used for the request
199 returned: failure or debug
200 type: str
201 sample: ?rsp-prop-include=config-only
202 method:
203 description: The HTTP method used for the request to the APIC
204 returned: failure or debug
205 type: str
206 sample: POST
207 response:
208 description: The HTTP response from the APIC
209 returned: failure or debug
210 type: str
211 sample: OK (30 bytes)
212 status:
213 description: The HTTP status from the APIC
214 returned: failure or debug
215 type: int
216 sample: 200
217 url:
218 description: The HTTP url used for the request to the APIC
219 returned: failure or debug
220 type: str
221 sample: https://10.11.12.13/api/mo/uni/tn-production.json
222 """
223
224 from ansible.module_utils.basic import AnsibleModule
225 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
226
227
228 def main():
229 argument_spec = aci_argument_spec()
230 argument_spec.update(aci_annotation_spec())
231 argument_spec.update(
232 tenant=dict(type="str", aliases=["tenant_name"]),
233 ap=dict(type="str", aliases=["app_profile", "app_profile_name"]),
234 esg=dict(type="str", aliases=["esg_name"]),
235 ip=dict(type="str", aliases=["subnet"]),
236 description=dict(type="str", aliases=["ip_subnet_selector_description"]),
237 state=dict(
238 type="str",
239 default="present",
240 choices=["absent", "present", "query"],
241 ),
242 name_alias=dict(type="str"),
243 )
244
245 module = AnsibleModule(
246 argument_spec=argument_spec,
247 supports_check_mode=True,
248 required_if=[
249 ["state", "absent", ["tenant", "ap", "esg", "ip"]],
250 ["state", "present", ["tenant", "ap", "esg", "ip"]],
251 ],
252 )
253
254 aci = ACIModule(module)
255 tenant = module.params.get("tenant")
256 ap = module.params.get("ap")
257 esg = module.params.get("esg")
258 ip = module.params.get("ip")
259 description = module.params.get("description")
260 state = module.params.get("state")
261
262 match_expression = "ip=='{0}'".format(ip)
263 subnet_selector_rn = "epselector-[{0}]".format(match_expression)
264 aci.construct_url(
265 root_class=dict(
266 aci_class="fvTenant",
267 aci_rn="tn-{0}".format(tenant),
268 module_object=tenant,
269 target_filter={"name": tenant},
270 ),
271 subclass_1=dict(
272 aci_class="fvAp",
273 aci_rn="ap-{0}".format(ap),
274 module_object=ap,
275 target_filter={"name": ap},
276 ),
277 subclass_2=dict(
278 aci_class="fvESg",
279 aci_rn="esg-{0}".format(esg),
280 module_object=esg,
281 target_filter={"name": esg},
282 ),
283 subclass_3=dict(
284 aci_class="fvEPSelector",
285 aci_rn=subnet_selector_rn,
286 module_object=ip,
287 target_filter={},
288 ),
289 )
290
291 aci.get_existing()
292
293 if state == "present":
294 aci.payload(
295 aci_class="fvEPSelector",
296 class_config=dict(
297 matchExpression=match_expression,
298 descr=description,
299 ),
300 )
301
302 aci.get_diff(aci_class="fvEPSelector")
303
304 aci.post_config()
305
306 elif state == "absent":
307 aci.delete_config()
308
309 aci.exit_json()
310
311
312 if __name__ == "__main__":
313 main()