Debian Codegraph @

ansible_collections/vmware/vmware_rest/plugins/module_utils/vmware_rest.py

raw
1 # This file is maintained in the vmware_rest_code_generator project
2 # https://github.com/ansible-collections/vmware_rest_code_generator
3 # Copyright (c) 2021 Ansible Project
4 #
5 # This code is part of Ansible, but is an independent component.
6 # This particular file snippet, and this file snippet only, is BSD licensed.
7 # Modules you write using this snippet, which is embedded dynamically by Ansible
8 # still belong to the author of the module, and may assign their own license
9 # to the complete work.
10 #
11 # Redistribution and use in source and binary forms, with or without modification,
12 # are permitted provided that the following conditions are met:
13 #
14 # * Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
16 # * Redistributions in binary form must reproduce the above copyright notice,
17 # this list of conditions and the following disclaimer in the documentation
18 # and/or other materials provided with the distribution.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
28 # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #
30
31 import hashlib
32 import importlib
33 import json
34 import re
35 import urllib.parse
36
37 from ansible.module_utils.basic import missing_required_lib
38 from ansible.module_utils.parsing.convert_bool import boolean
39
40 try:
41 from ansible_collections.cloud.common.plugins.module_utils.turbo.exceptions import (
42 EmbeddedModuleFailure as ModuleFailureException,
43 )
44 except ImportError:
45 ModuleFailureException = Exception
46
47
48 async def open_session(
49 vcenter_hostname=None,
50 vcenter_username=None,
51 vcenter_password=None,
52 validate_certs=True,
53 log_file=None,
54 ):
55 validate_certs = boolean(validate_certs)
56 m = hashlib.sha256()
57 m.update(vcenter_hostname.encode())
58 m.update(vcenter_username.encode())
59 m.update(vcenter_password.encode())
60 if log_file:
61 m.update(log_file.encode())
62 m.update(b"yes" if validate_certs else b"no")
63 digest = m.hexdigest()
64 # TODO: Handle session timeout
65 if digest in open_session._pool:
66 return open_session._pool[digest]
67
68 try:
69 aiohttp = importlib.import_module("aiohttp")
70 except ImportError:
71 raise ModuleFailureException(missing_required_lib("aiohttp"))
72
73 if not aiohttp:
74 raise ModuleFailureException("Failed to import aiohttp")
75
76 if log_file:
77 trace_config = aiohttp.TraceConfig()
78
79 async def on_request_end(session, trace_config_ctx, params):
80 with open(log_file, "a+", encoding="utf-8") as fd:
81 answer = await params.response.text()
82 fd.write(
83 f"{params.method}: {params.url}\n"
84 f"headers: {params.headers}\n"
85 f" status: {params.response.status}\n"
86 f" answer: {answer}\n\n"
87 )
88
89 trace_config.on_request_end.append(on_request_end)
90 trace_configs = [trace_config]
91 else:
92 trace_configs = []
93
94 auth = aiohttp.BasicAuth(vcenter_username, vcenter_password)
95 if validate_certs:
96 connector = aiohttp.TCPConnector(limit=20)
97 else:
98 connector = aiohttp.TCPConnector(limit=20, ssl=False)
99 async with aiohttp.ClientSession(
100 connector=connector, connector_owner=False, trace_configs=trace_configs
101 ) as session:
102 try:
103 async with session.post(
104 "https://{hostname}/rest/com/vmware/cis/session".format(
105 hostname=vcenter_hostname
106 ),
107 auth=auth,
108 ) as resp:
109 if resp.status != 200:
110 raise ModuleFailureException(
111 "Authentication failure. code: {0}, json: {1}".format(
112 resp.status, await resp.text()
113 )
114 )
115 json = await resp.json()
116 except aiohttp.client_exceptions.ClientConnectorError as e:
117 raise ModuleFailureException(f"Authentication failure: {e}")
118
119 session_id = json["value"]
120 session = aiohttp.ClientSession(
121 connector=connector,
122 headers={
123 "vmware-api-session-id": session_id,
124 "content-type": "application/json",
125 },
126 connector_owner=False,
127 trace_configs=trace_configs,
128 )
129 open_session._pool[digest] = session
130 return session
131
132
133 open_session._pool = {}
134
135
136 def gen_args(params, in_query_parameter):
137 elements = []
138 for i in in_query_parameter:
139 if i.startswith("filter."): # < 7.0.2
140 v = params.get("filter_" + i[7:])
141 else:
142 v = params.get(i)
143 if not v:
144 continue
145 if isinstance(v, list):
146 for j in v:
147 elements += [(i, j)]
148 elif isinstance(v, bool) and v:
149 elements += [(i, str(v).lower())]
150 else:
151 elements += [(i, str(v))]
152 if not elements:
153 return ""
154 return "?" + urllib.parse.urlencode(elements, quote_via=urllib.parse.quote)
155
156
157 def session_timeout(params):
158 try:
159 aiohttp = importlib.import_module("aiohttp")
160 except ImportError:
161 raise ModuleFailureException(missing_required_lib("aiohttp"))
162
163 if not aiohttp:
164 raise ModuleFailureException("Failed to import aiohttp")
165 out = {}
166 if params.get("session_timeout"):
167 out["timeout"] = aiohttp.ClientTimeout(total=params.get("session_timeout"))
168 return out
169
170
171 async def update_changed_flag(data, status, operation):
172 if data is None:
173 data = {"value": {}}
174 elif isinstance(data, list): # e.g: appliance_infraprofile_configs_info
175 data = {"value": data}
176 elif isinstance(data, str):
177 data = {"value": data}
178 elif isinstance(data, dict) and "value" not in data: # 7.0.2+
179 data = {"value": data}
180 elif isinstance(data, bool):
181 data = {"value": data}
182
183 if isinstance(data["value"], str) and data["value"][0] in [
184 "{",
185 "]",
186 ]: # e.g: appliance_infraprofile_configs
187 data["value"] == json.loads(data["value"])
188
189 if status == 500:
190 data["failed"] = True
191 data["changed"] = False
192 elif operation in ["create", "clone", "instant_clone"] and status in [200, 201]:
193 data["failed"] = False
194 data["changed"] = True
195 elif operation == "update" and status in [200, 204]:
196 data["failed"] = False
197 data["changed"] = True
198 elif operation in ["upgrade", "create_temporary"] and status == 200:
199 data["failed"] = False
200 data["changed"] = True
201 elif operation == "set" and status in [200, 204]:
202 data["failed"] = False
203 data["changed"] = True
204 elif operation == "delete" and status in [200, 204]:
205 data["failed"] = False
206 data["changed"] = True
207 elif operation in ["create", "move"] and status in [204]:
208 data["failed"] = False
209 data["changed"] = True
210 elif operation == "delete" and status == 404:
211 data["failed"] = False
212 data["changed"] = False
213 elif operation in ["get", "list"] and status in [200]:
214 data["failed"] = False
215 data["changed"] = False
216 elif operation in ["get", "list"] and status in [404]:
217 data["failed"] = True
218 data["changed"] = False
219
220 elif status >= 400:
221 data["failed"] = True
222 data["changed"] = False
223
224 if not isinstance(data["value"], dict):
225 pass
226 elif data.get("type") == "com.vmware.vapi.std.errors.not_found":
227 if operation == "delete":
228 data["failed"] = False
229 data["changed"] = False
230 else:
231 data["failed"] = True
232 data["changed"] = False
233 elif data.get("type") == "com.vmware.vapi.std.errors.already_in_desired_state":
234 data["failed"] = False
235 data["changed"] = False
236 elif data.get("type") == "com.vmware.vapi.std.errors.already_exists":
237 data["failed"] = False
238 data["changed"] = False
239 elif (
240 data.get("value", {}).get("error_type") in ["NOT_FOUND"]
241 and operation == "delete"
242 ):
243 data["failed"] = False
244 data["changed"] = False
245 elif data.get("value", {}).get("error_type") in [
246 "ALREADY_EXISTS",
247 "ALREADY_IN_DESIRED_STATE",
248 ]:
249 data["failed"] = False
250 data["changed"] = False
251 elif data.get("type") == "com.vmware.vapi.std.errors.resource_in_use":
252 # NOTE: this is a shortcut/hack. We get this issue if a CDROM already exists
253 data["failed"] = False
254 data["changed"] = False
255 elif (
256 data.get("type") == "com.vmware.vapi.std.errors.internal_server_error"
257 and data["value"]
258 and data["value"]["messages"]
259 and data["value"]["messages"][0]["args"]
260 == [
261 "com.vmware.vim.binding.vim.fault.DuplicateName cannot be cast to com.vmware.vim.binding.vim.fault.AlreadyConnected"
262 ]
263 ):
264 # NOTE: another one for vcenter_host
265 data["failed"] = False
266 data["changed"] = False
267 elif data.get("type", "").startswith("com.vmware.vapi.std.errors"):
268 data["failed"] = True
269 # 7.0.3, vcenter_ovf_libraryitem returns status 200 on failure
270 elif data.get("value", {}).get("error", {}).get("errors", []):
271 data["failed"] = True
272
273 return data
274
275
276 async def list_devices(session, url):
277 pass
278
279 async with session.get(url) as resp:
280 _json = await resp.json()
281 return _json
282
283
284 async def build_full_device_list(session, url, device_list):
285 import asyncio
286
287 device_ids = []
288
289 if isinstance(device_list, list):
290 value = device_list
291 else: # 7.0.2 <
292 value = device_list["value"]
293 for i in value:
294 # Content library returns string {"value": "library_id"}
295 if isinstance(i, str):
296 device_ids.append(i)
297 continue
298 fields = list(i.values())
299 if len(fields) != 1:
300 # The list already comes with all the details
301 return device_list
302 device_ids.append(fields[0])
303
304 tasks = [
305 asyncio.ensure_future(get_device_info(session, url, _id)) for _id in device_ids
306 ]
307
308 return [await i for i in tasks]
309
310
311 async def get_device_info(session, url, _id):
312 # remove the action=foo from the URL
313 m = re.search("(.+)(action=[-a-z]+)(.*)", url)
314 if m:
315 url = f"{m.group(1)}{m.group(3)}"
316 url = url.rstrip("?")
317
318 # workaround for content_library_item_info
319 if "item?library_id=" in url:
320 item_url = url.split("?")[0] + "/" + _id
321 else:
322 item_url = url + "/" + _id
323
324 async with session.get(item_url) as resp:
325 if resp.status == 200:
326 _json = await resp.json()
327 if "value" not in _json: # 7.0.2+
328 _json = {"value": _json}
329 _json["id"] = str(_id)
330 return _json
331
332
333 async def exists(
334 params, session, url, uniquity_keys=None, per_id_url=None, comp_func=None
335 ):
336 if not uniquity_keys:
337 uniquity_keys = []
338 if not per_id_url:
339 per_id_url = url
340
341 def default_comp_func(device):
342 for k in uniquity_keys:
343 if not params.get(k):
344 continue
345 if isinstance(device, dict): # 7.0.2 <
346 v = device["value"].get(k)
347 elif isinstance(device, list):
348 v = device
349 else:
350 raise ModuleFailureException("Unexpect type")
351
352 if isinstance(k, int) or isinstance(v, str):
353 k = str(k)
354 v = str(v)
355 if v == params.get(k):
356 return device
357
358 if not comp_func:
359 comp_func = default_comp_func
360
361 uniquity_keys += ["label", "pci_slot_number", "sata"]
362
363 devices = await list_devices(session, url)
364 full_devices = await build_full_device_list(session, per_id_url, devices)
365
366 for device in full_devices:
367 if comp_func(device):
368 return device
369
370
371 def set_subkey(root, path, value):
372 cur_loc = root
373 splitted = path.split("/")
374 for j in splitted[:-1]:
375 if j not in cur_loc:
376 cur_loc[j] = {}
377 cur_loc = cur_loc[j]
378 cur_loc[splitted[-1]] = value
379
380
381 def prepare_payload(params, payload_format):
382 payload = {}
383 for i in payload_format["body"].keys():
384 if params[i] is None:
385 continue
386
387 path = payload_format["body"][i]
388 set_subkey(payload, path, params[i])
389 return payload
390
391
392 def get_subdevice_type(url):
393 """If url needs a subkey, return its name."""
394 candidates = []
395 for i in url.split("/"):
396 if i.startswith("{"):
397 candidates.append(i[1:-1])
398 if len(candidates) != 2:
399 return
400 return candidates[-1].split("}")[0]
401
402
403 def get_device_type(url):
404 device_type = url.split("/")[-1]
405 # NOTE: This mapping can be extracted from the delete end-point of the
406 # resource, e.g:
407 # /rest/vcenter/vm/{vm}/hardware/ethernet/{nic} -> nic
408 # Also, it sounds like we can use "list_index" instead
409 if device_type == "ethernet":
410 return "nic"
411 elif device_type in ["sata", "scsi"]:
412 return "adapter"
413 elif device_type in ["parallel", "serial"]:
414 return "port"
415 else:
416 return device_type