Debian Codegraph @

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

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