Debian Codegraph @

ansible_collections/dellemc/openmanage/plugins/module_utils/dellemc_idrac.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2
3 # Dell OpenManage Ansible Modules
4 # Version 9.1.0
5 # Copyright (C) 2019-2025 Dell Inc. or its subsidiaries. All Rights Reserved.
6
7 # Redistribution and use in source and binary forms, with or without modification,
8 # are permitted provided that the following conditions are met:
9
10 # * Redistributions of source code must retain the above copyright notice,
11 # this list of conditions and the following disclaimer.
12
13 # * Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 # IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
25 # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #
27
28
29 from __future__ import (absolute_import, division, print_function)
30 __metaclass__ = type
31 import os
32 from ansible.module_utils.common.parameters import env_fallback
33 from ansible_collections.dellemc.openmanage.plugins.module_utils.utils import compress_ipv6
34 try:
35 from omsdk.sdkinfra import sdkinfra
36 from omsdk.sdkcreds import UserCredentials
37 from omsdk.sdkprotopref import ProtoPreference, ProtocolEnum
38 from omsdk.http.sdkwsmanbase import WsManOptions
39 HAS_OMSDK = True
40 except ImportError:
41 HAS_OMSDK = False
42
43
44 idrac_auth_params = {
45 "idrac_ip": {"required": True, "type": 'str'},
46 "idrac_user": {"required": True, "type": 'str', "fallback": (env_fallback, ['IDRAC_USERNAME'])},
47 "idrac_password": {"required": True, "type": 'str', "aliases": ['idrac_pwd'], "no_log": True, "fallback": (env_fallback, ['IDRAC_PASSWORD'])},
48 "idrac_port": {"required": False, "default": 443, "type": 'int'},
49 "validate_certs": {"type": "bool", "default": True},
50 "ca_path": {"type": "path"},
51 "timeout": {"type": "int", "default": 30},
52 }
53
54
55 class iDRACConnection:
56
57 def __init__(self, module_params):
58 if not HAS_OMSDK:
59 raise ImportError("Dell OMSDK library is required for this module")
60 self.idrac_ip = compress_ipv6(module_params['idrac_ip'])
61 self.idrac_user = module_params['idrac_user']
62 self.idrac_pwd = module_params['idrac_password']
63 self.idrac_port = module_params['idrac_port']
64 if not all((self.idrac_ip, self.idrac_user, self.idrac_pwd)):
65 raise ValueError("hostname, username and password required")
66 self.handle = None
67 self.creds = UserCredentials(self.idrac_user, self.idrac_pwd)
68 self.validate_certs = module_params.get("validate_certs", False)
69 self.ca_path = module_params.get("ca_path")
70 verify_ssl = False
71 if self.validate_certs is True:
72 if self.ca_path is None:
73 self.ca_path = self._get_omam_ca_env()
74 verify_ssl = self.ca_path
75 timeout = module_params.get("timeout", 30)
76 if not timeout or not isinstance(timeout, int):
77 timeout = 30
78 self.pOp = WsManOptions(port=self.idrac_port, read_timeout=timeout, verify_ssl=verify_ssl)
79 self.sdk = sdkinfra()
80 if self.sdk is None:
81 msg = "Could not initialize iDRAC drivers."
82 raise RuntimeError(msg)
83
84 def __enter__(self):
85 self.idrac_ip = self.idrac_ip.strip('[]')
86 self.sdk.importPath()
87 protopref = ProtoPreference(ProtocolEnum.WSMAN)
88 protopref.include_only(ProtocolEnum.WSMAN)
89 self.handle = self.sdk.get_driver(self.sdk.driver_enum.iDRAC, self.idrac_ip, self.creds,
90 protopref=protopref, pOptions=self.pOp)
91 if self.handle is None:
92 msg = "Unable to communicate with iDRAC {0}. This may be due to one of the following: " \
93 "Incorrect username or password, unreachable iDRAC IP or " \
94 "a failure in TLS/SSL handshake.".format(self.idrac_ip)
95 raise RuntimeError(msg)
96 return self.handle
97
98 def __exit__(self, exc_type, exc_val, exc_tb):
99 self.handle.disconnect()
100 return False
101
102 def _get_omam_ca_env(self):
103 """Check if the value is set in REQUESTS_CA_BUNDLE or CURL_CA_BUNDLE or OMAM_CA_BUNDLE or True as ssl has to
104 be validated from omsdk with single param and is default to false in omsdk"""
105 return (os.environ.get("REQUESTS_CA_BUNDLE") or os.environ.get("CURL_CA_BUNDLE")
106 or os.environ.get("OMAM_CA_BUNDLE") or True)