Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2021, Manuel Widmer <[email protected]>
5 # Copyright: (c) 2021, Anvitha Jain (@anvitha-jain) <[email protected]>
6 # Copyright: (c) 2023, Akini Ross (@akinross) <[email protected]>
7 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
8
9 from __future__ import absolute_import, division, print_function
10
11 __metaclass__ = type
12
13 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
14
15 DOCUMENTATION = r"""
16 ---
17 module: aci_vmm_controller
18 short_description: Manage VMM Controller for virtual domains profiles (vmm:CtrlrP)
19 description:
20 - Manage vCenter virtual domains on Cisco ACI fabrics.
21 options:
22 name:
23 description:
24 - Name of VMM Controller.
25 type: str
26 aliases: []
27 controller_hostname:
28 description:
29 - Hostname or IP of the controller.
30 type: str
31 aliases: []
32 dvs_version:
33 description:
34 - Version of the VMware DVS.
35 type: str
36 aliases: []
37 choices: [ 'unmanaged', '5.1', '5.5', '6.0', '6.5', '6.6', '7.0', '8.0' ]
38 stats_collection:
39 description:
40 - Whether stats collection is enabled.
41 type: str
42 choices: [ 'enabled', 'disabled' ]
43 default: disabled
44 aliases: []
45 domain:
46 description:
47 - Name of the virtual domain profile.
48 type: str
49 aliases: [ domain_name, domain_profile ]
50 state:
51 description:
52 - Use C(present) or C(absent) for adding or removing.
53 - Use C(query) for listing an object or multiple objects.
54 type: str
55 choices: [ absent, present, query ]
56 default: present
57 credentials:
58 description:
59 - Name of the VMM credentials to be used
60 type: str
61 inband_management_epg:
62 description:
63 - Name of the management EPG to be used by the controller. Only supports in-band management EPGs for now.
64 type: str
65 name_alias:
66 description:
67 - The alias for the current object. This relates to the nameAlias field in ACI.
68 type: str
69 datacenter:
70 description:
71 - Name of the data center, as seen in vCenter
72 type: str
73 vm_provider:
74 description:
75 - The VM platform for VMM Domains.
76 - Support for Kubernetes was added in ACI v3.0.
77 - Support for CloudFoundry, OpenShift and Red Hat was added in ACI v3.1.
78 type: str
79 choices: [ cloudfoundry, kubernetes, microsoft, openshift, openstack, redhat, vmware, nutanix ]
80 extends_documentation_fragment:
81 - cisco.aci.aci
82 - cisco.aci.annotation
83
84 seealso:
85 - module: cisco.aci.aci_domain
86 - module: cisco.aci.aci_vmm_credential
87 - name: APIC Management Information Model reference
88 description: More information about the internal APIC classes B(vmm:DomP)
89 link: https://developer.cisco.com/docs/apic-mim-ref/
90 author:
91 - Manuel Widmer (@lumean)
92 - Anvitha Jain (@anvitha-jain)
93 - Akini Ross (@akinross)
94 """
95
96 EXAMPLES = r"""
97 - name: Add controller to VMware VMM domain
98 cisco.aci.aci_vmm_controller:
99 host: apic
100 username: admin
101 password: SomeSecretPassword
102 domain: vmware_dom
103 name: vCenter1
104 controller_hostname: 10.1.1.1
105 dvs_version: unmanaged
106 vm_provider: vmware
107 credentials: vCenterCredentials1
108 datacenter: DC1
109 state: present
110
111 - name: Remove controller from VMware VMM domain
112 cisco.aci.aci_vmm_controller:
113 host: apic
114 username: admin
115 password: SomeSecretPassword
116 domain: vmware_dom
117 name: vCenter1
118 vm_provider: vmware
119 state: absent
120
121 - name: Query a specific VMware VMM controller
122 cisco.aci.aci_vmm_controller:
123 host: apic
124 username: admin
125 password: SomeSecretPassword
126 domain: vmware_dom
127 name: vCenter1
128 vm_provider: vmware
129 state: query
130 delegate_to: localhost
131 register: query_result
132
133 - name: Query all VMware VMM controller
134 cisco.aci.aci_vmm_controller:
135 host: apic
136 username: admin
137 password: SomeSecretPassword
138 state: query
139 delegate_to: localhost
140 register: query_result
141 """
142
143
144 RETURN = r"""
145 current:
146 description: The existing configuration from the APIC after the module has finished
147 returned: success
148 type: list
149 sample:
150 [
151 {
152 "fvTenant": {
153 "attributes": {
154 "descr": "Production environment",
155 "dn": "uni/tn-production",
156 "name": "production",
157 "nameAlias": "",
158 "ownerKey": "",
159 "ownerTag": ""
160 }
161 }
162 }
163 ]
164 error:
165 description: The error information as returned from the APIC
166 returned: failure
167 type: dict
168 sample:
169 {
170 "code": "122",
171 "text": "unknown managed object class foo"
172 }
173 raw:
174 description: The raw output returned by the APIC REST API (xml or json)
175 returned: parse error
176 type: str
177 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
178 sent:
179 description: The actual/minimal configuration pushed to the APIC
180 returned: info
181 type: list
182 sample:
183 {
184 "fvTenant": {
185 "attributes": {
186 "descr": "Production environment"
187 }
188 }
189 }
190 previous:
191 description: The original configuration from the APIC before the module has started
192 returned: info
193 type: list
194 sample:
195 [
196 {
197 "fvTenant": {
198 "attributes": {
199 "descr": "Production",
200 "dn": "uni/tn-production",
201 "name": "production",
202 "nameAlias": "",
203 "ownerKey": "",
204 "ownerTag": ""
205 }
206 }
207 }
208 ]
209 proposed:
210 description: The assembled configuration from the user-provided parameters
211 returned: info
212 type: dict
213 sample:
214 {
215 "fvTenant": {
216 "attributes": {
217 "descr": "Production environment",
218 "name": "production"
219 }
220 }
221 }
222 filter_string:
223 description: The filter string used for the request
224 returned: failure or debug
225 type: str
226 sample: ?rsp-prop-include=config-only
227 method:
228 description: The HTTP method used for the request to the APIC
229 returned: failure or debug
230 type: str
231 sample: POST
232 response:
233 description: The HTTP response from the APIC
234 returned: failure or debug
235 type: str
236 sample: OK (30 bytes)
237 status:
238 description: The HTTP status from the APIC
239 returned: failure or debug
240 type: int
241 sample: 200
242 url:
243 description: The HTTP url used for the request to the APIC
244 returned: failure or debug
245 type: str
246 sample: https://10.11.12.13/api/mo/uni/tn-production.json
247 """
248
249 from ansible.module_utils.basic import AnsibleModule
250 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec
251 from ansible_collections.cisco.aci.plugins.module_utils.constants import VM_PROVIDER_MAPPING, VM_SCOPE_MAPPING
252
253
254 def main():
255 argument_spec = aci_argument_spec()
256 argument_spec.update(aci_annotation_spec())
257 argument_spec.update(
258 name=dict(type="str"),
259 controller_hostname=dict(type="str"),
260 dvs_version=dict(type="str", choices=["unmanaged", "5.1", "5.5", "6.0", "6.5", "6.6", "7.0", "8.0"]),
261 stats_collection=dict(type="str", default="disabled", choices=["enabled", "disabled"]),
262 domain=dict(type="str", aliases=["domain_name", "domain_profile"]),
263 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
264 credentials=dict(type="str"),
265 inband_management_epg=dict(type="str"),
266 name_alias=dict(type="str"),
267 datacenter=dict(type="str"),
268 vm_provider=dict(type="str", choices=list(VM_PROVIDER_MAPPING)),
269 )
270
271 module = AnsibleModule(
272 argument_spec=argument_spec,
273 supports_check_mode=True,
274 required_if=[
275 ["state", "absent", ["domain", "vm_provider", "name"]],
276 ["state", "present", ["domain", "vm_provider", "name"]],
277 ],
278 )
279
280 name = module.params.get("name")
281 controller_hostname = module.params.get("controller_hostname")
282 dvs_version = module.params.get("dvs_version")
283 stats_collection = module.params.get("stats_collection")
284 domain = module.params.get("domain")
285 state = module.params.get("state")
286 credentials = module.params.get("credentials")
287 inband_management_epg = module.params.get("inband_management_epg")
288 name_alias = module.params.get("name_alias")
289 datacenter = module.params.get("datacenter")
290 vm_provider = module.params.get("vm_provider")
291
292 controller_class = "vmmCtrlrP"
293
294 aci = ACIModule(module)
295
296 child_classes = ["vmmRsMgmtEPg", "vmmRsAcc"]
297
298 aci.construct_url(
299 root_class=dict(
300 aci_class="vmmProvP",
301 aci_rn="vmmp-{0}".format(VM_PROVIDER_MAPPING.get(vm_provider)),
302 module_object=vm_provider,
303 target_filter={"name": vm_provider},
304 ),
305 subclass_1=dict(
306 aci_class="vmmDomP",
307 aci_rn="dom-{0}".format(domain),
308 module_object=domain,
309 target_filter={"name": domain},
310 ),
311 subclass_2=dict(
312 aci_class="vmmCtrlrP",
313 aci_rn="ctrlr-{0}".format(name),
314 module_object=name,
315 target_filter={"name": name},
316 ),
317 child_classes=child_classes,
318 )
319
320 # vmmProvP is not allowed to execute a query with rsp-subtree set in the filter_string
321 # due to complicated url construction logic which should be refactored creating a temporary fix inside module
322 # TODO refactor url construction logic if more occurences of rsp-subtree not supported problem appear
323 # check if the url is pointing towards the vmmProvP class and rsp-subtree is set in the filter_string
324 if aci.url.split("/")[-1].startswith("vmmp-") and "rsp-subtree" in aci.filter_string:
325 if name:
326 aci.url = "{0}/api/class/vmmCtrlrP.json".format(aci.base_url)
327 aci.filter_string = '?query-target-filter=eq(vmmCtrlrP.name,"{0}")&rsp-subtree=full&rsp-subtree-class={1}'.format(name, ",".join(child_classes))
328 else:
329 aci.url = "{0}/api/mo/uni/vmmp-{1}.json".format(aci.base_url, VM_PROVIDER_MAPPING.get(vm_provider))
330 aci.filter_string = ""
331
332 aci.get_existing()
333
334 if state == "present":
335 children = list()
336 if inband_management_epg is not None:
337 children.append(dict(vmmRsMgmtEPg=dict(attributes=dict(tDn="uni/tn-mgmt/mgmtp-default/inb-{0}".format(inband_management_epg)))))
338
339 if credentials is not None:
340 children.append(
341 dict(vmmRsAcc=dict(attributes=dict(tDn="uni/vmmp-{0}/dom-{1}/usracc-{2}".format(VM_PROVIDER_MAPPING.get(vm_provider), domain, credentials))))
342 )
343
344 aci.payload(
345 aci_class=controller_class,
346 class_config=dict(
347 name=name,
348 hostOrIp=controller_hostname,
349 dvsVersion=dvs_version,
350 statsMode=stats_collection,
351 rootContName=datacenter,
352 nameAlias=name_alias,
353 scope=VM_SCOPE_MAPPING.get(vm_provider),
354 ),
355 child_configs=children,
356 )
357
358 aci.get_diff(aci_class=controller_class)
359
360 aci.post_config()
361
362 elif state == "absent":
363 aci.delete_config()
364
365 aci.exit_json()
366
367
368 if __name__ == "__main__":
369 main()