Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7
8 __metaclass__ = type
9
10 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
11
12 DOCUMENTATION = r"""
13 ---
14 module: aci_vmm_credential
15 short_description: Manage virtual domain credential profiles (vmm:UsrAccP)
16 description:
17 - Manage virtual domain credential profiles on Cisco ACI fabrics.
18 options:
19 name:
20 description:
21 - Name of the credential profile.
22 type: str
23 aliases: [ credential_name, credential_profile ]
24 credential_password:
25 description:
26 - VMM controller password.
27 - Providing this option will always result in a change because it is a secure property that cannot be retrieved from APIC.
28 type: str
29 aliases: []
30 credential_username:
31 description:
32 - VMM controller username.
33 type: str
34 aliases: []
35 description:
36 description:
37 - Description for the tenant.
38 type: str
39 aliases: [ descr ]
40 domain:
41 description:
42 - Name of the virtual domain profile.
43 type: str
44 aliases: [ domain_name, domain_profile ]
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 type: str
50 choices: [ absent, present, query ]
51 default: present
52 name_alias:
53 description:
54 - The alias for the current object. This relates to the nameAlias field in ACI.
55 type: str
56 vm_provider:
57 description:
58 - The VM platform for VMM Domains.
59 - Support for Kubernetes was added in ACI v3.0.
60 - Support for CloudFoundry, OpenShift and Red Hat was added in ACI v3.1.
61 type: str
62 choices: [ cloudfoundry, kubernetes, microsoft, openshift, openstack, redhat, vmware, nutanix ]
63 extends_documentation_fragment:
64 - cisco.aci.aci
65 - cisco.aci.annotation
66 - cisco.aci.owner
67
68 seealso:
69 - module: cisco.aci.aci_domain
70 - name: APIC Management Information Model reference
71 description: More information about the internal APIC classes B(vmm:DomP)
72 link: https://developer.cisco.com/docs/apic-mim-ref/
73 author:
74 - Jason Juenger (@jasonjuenger)
75 """
76
77 EXAMPLES = r"""
78 - name: Add credential to VMware VMM domain
79 cisco.aci.aci_vmm_credential:
80 host: apic
81 username: admin
82 password: SomeSecretPassword
83 domain: vmware_dom
84 description: secure credential
85 name: vCenterCredential
86 credential_username: vCenterUsername
87 credential_password: vCenterPassword
88 vm_provider: vmware
89 state: present
90
91 - name: Remove credential from VMware VMM domain
92 cisco.aci.aci_vmm_credential:
93 host: apic
94 username: admin
95 password: SomeSecretPassword
96 domain: vmware_dom
97 name: myCredential
98 vm_provider: vmware
99 state: absent
100
101 - name: Query a specific VMware VMM credential
102 cisco.aci.aci_vmm_credential:
103 host: apic
104 username: admin
105 password: SomeSecretPassword
106 domain: vmware_dom
107 name: vCenterCredential
108 vm_provider: vmware
109 state: query
110 delegate_to: localhost
111 register: query_result
112
113 - name: Query all VMware VMM credentials
114 cisco.aci.aci_vmm_credential:
115 host: apic
116 username: admin
117 password: SomeSecretPassword
118 domain: vmware_dom
119 vm_provider: vmware
120 state: query
121 delegate_to: localhost
122 register: query_result
123 """
124
125 RETURN = r"""
126 current:
127 description: The existing configuration from the APIC after the module has finished
128 returned: success
129 type: list
130 sample:
131 [
132 {
133 "fvTenant": {
134 "attributes": {
135 "descr": "Production environment",
136 "dn": "uni/tn-production",
137 "name": "production",
138 "nameAlias": "",
139 "ownerKey": "",
140 "ownerTag": ""
141 }
142 }
143 }
144 ]
145 error:
146 description: The error information as returned from the APIC
147 returned: failure
148 type: dict
149 sample:
150 {
151 "code": "122",
152 "text": "unknown managed object class foo"
153 }
154 raw:
155 description: The raw output returned by the APIC REST API (xml or json)
156 returned: parse error
157 type: str
158 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
159 sent:
160 description: The actual/minimal configuration pushed to the APIC
161 returned: info
162 type: list
163 sample:
164 {
165 "fvTenant": {
166 "attributes": {
167 "descr": "Production environment"
168 }
169 }
170 }
171 previous:
172 description: The original configuration from the APIC before the module has started
173 returned: info
174 type: list
175 sample:
176 [
177 {
178 "fvTenant": {
179 "attributes": {
180 "descr": "Production",
181 "dn": "uni/tn-production",
182 "name": "production",
183 "nameAlias": "",
184 "ownerKey": "",
185 "ownerTag": ""
186 }
187 }
188 }
189 ]
190 proposed:
191 description: The assembled configuration from the user-provided parameters
192 returned: info
193 type: dict
194 sample:
195 {
196 "fvTenant": {
197 "attributes": {
198 "descr": "Production environment",
199 "name": "production"
200 }
201 }
202 }
203 filter_string:
204 description: The filter string used for the request
205 returned: failure or debug
206 type: str
207 sample: ?rsp-prop-include=config-only
208 method:
209 description: The HTTP method used for the request to the APIC
210 returned: failure or debug
211 type: str
212 sample: POST
213 response:
214 description: The HTTP response from the APIC
215 returned: failure or debug
216 type: str
217 sample: OK (30 bytes)
218 status:
219 description: The HTTP status from the APIC
220 returned: failure or debug
221 type: int
222 sample: 200
223 url:
224 description: The HTTP url used for the request to the APIC
225 returned: failure or debug
226 type: str
227 sample: https://10.11.12.13/api/mo/uni/tn-production.json
228 """
229
230 from ansible.module_utils.basic import AnsibleModule
231 from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec, aci_owner_spec
232 from ansible_collections.cisco.aci.plugins.module_utils.constants import VM_PROVIDER_MAPPING
233
234
235 def main():
236 argument_spec = aci_argument_spec()
237 argument_spec.update(aci_annotation_spec())
238 argument_spec.update(aci_owner_spec())
239 argument_spec.update(
240 name=dict(type="str", aliases=["credential_name", "credential_profile"]),
241 credential_password=dict(type="str", no_log=True),
242 credential_username=dict(type="str"),
243 description=dict(type="str", aliases=["descr"]),
244 domain=dict(type="str", aliases=["domain_name", "domain_profile"]),
245 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
246 vm_provider=dict(type="str", choices=list(VM_PROVIDER_MAPPING)),
247 name_alias=dict(type="str"),
248 )
249
250 module = AnsibleModule(
251 argument_spec=argument_spec,
252 supports_check_mode=True,
253 required_if=[
254 ["state", "absent", ["domain"]],
255 ["state", "present", ["domain"]],
256 ],
257 )
258
259 name = module.params.get("name")
260 credential_password = module.params.get("credential_password")
261 credential_username = module.params.get("credential_username")
262 description = module.params.get("description")
263 domain = module.params.get("domain")
264 state = module.params.get("state")
265 vm_provider = module.params.get("vm_provider")
266 name_alias = module.params.get("name_alias")
267
268 credential_class = "vmmUsrAccP"
269 usracc_mo = "uni/vmmp-{0}/dom-{1}/usracc-{2}".format(VM_PROVIDER_MAPPING.get(vm_provider), domain, name)
270 usracc_rn = "vmmp-{0}/dom-{1}/usracc-{2}".format(VM_PROVIDER_MAPPING.get(vm_provider), domain, name)
271
272 # Ensure that querying all objects works when only domain is provided
273 if name is None:
274 usracc_mo = None
275
276 aci = ACIModule(module)
277 aci.construct_url(
278 root_class=dict(
279 aci_class=credential_class,
280 aci_rn=usracc_rn,
281 module_object=usracc_mo,
282 target_filter={"name": domain, "usracc": name},
283 ),
284 )
285
286 aci.get_existing()
287
288 if state == "present":
289 aci.payload(
290 aci_class=credential_class,
291 class_config=dict(
292 descr=description,
293 name=name,
294 pwd=credential_password,
295 usr=credential_username,
296 nameAlias=name_alias,
297 ),
298 )
299
300 aci.get_diff(aci_class=credential_class)
301
302 aci.post_config()
303
304 elif state == "absent":
305 aci.delete_config()
306
307 aci.exit_json()
308
309
310 if __name__ == "__main__":
311 main()