(self)
Creates the connection object to the vSphere API using parameters supplied from the configuration.

        Docs for vim.ServiceInstance:
            https://vdc-download.vmware.com/vmwb-repository/dcr-public/b525fb12-61bb-4ede-b9e3-c4a1f8171510/99ba073a-60e9-4933-8690-149860ce8754/doc/vim.ServiceInstance.html
Creates the connection object to the vSphere API using parameters supplied from the configuration.
Creates the connection object to the vSphere API using parameters supplied from the configuration .
def smart_connect(self):
        # type: () -> None
        """
        Creates the connection object to the vSphere API using parameters supplied from the configuration.

        Docs for vim.ServiceInstance:
            https://vdc-download.vmware.com/vmwb-repository/dcr-public/b525fb12-61bb-4ede-b9e3-c4a1f8171510/99ba073a-60e9-4933-8690-149860ce8754/doc/vim.ServiceInstance.html
        """
        context = None
        if not self.config.ssl_verify:
            # Remove type ignore when this is merged https://github.com/python/typeshed/pull/3855
            context = ssl.SSLContext(ssl.PROTOCOL_TLS)  # type: ignore
            context.verify_mode = ssl.CERT_NONE
        elif self.config.ssl_capath:
            # Remove type ignore when this is merged https://github.com/python/typeshed/pull/3855
            context = ssl.SSLContext(ssl.PROTOCOL_TLS)  # type: ignore
            context.verify_mode = ssl.CERT_REQUIRED
            # `check_hostname` must be enabled as well to verify the authenticity of a cert.
            context.check_hostname = True
            context.load_verify_locations(capath=self.config.ssl_capath)

        try:
            # Object returned by SmartConnect is a ServerInstance
            # https://www.vmware.com/support/developer/vc-sdk/visdk2xpubs/ReferenceGuide/vim.ServiceInstance.html
            conn = connect.SmartConnect(
                host=self.config.hostname, user=self.config.username, pwd=self.config.password, sslContext=context
            )
            # Next line tries a simple API call to check the health of the connection.
            version_info = VersionInfo(conn.content.about)
        except Exception as e:
            err_msg = "Connection to {} failed: {}".format(self.config.hostname, e)
            raise APIConnectionError(err_msg)

        if not version_info.is_vcenter():
            # Connection was successful but to something that is not a VirtualCenter instance. The check won't
            # run correctly.
            # TODO: Raise an exception and stop execution here
            self.log.error(
                "%s is not a valid VirtualCenter (vCenter) instance, the vSphere API reports '%s'. "
                "Do not try to connect to ESXi hosts directly.",
                self.config.hostname,
                version_info.api_type,
            )

        if self._conn:
            connect.Disconnect(self._conn)

        self._conn = conn
        self.log.debug("Connected to %s", version_info.fullName)
def smart_connect ( self ) : # type: () -> None context = None if not self . config . ssl_verify : # Remove type ignore when this is merged https://github.com/python/typeshed/pull/3855 context = ssl . SSLContext ( ssl . PROTOCOL_TLS ) # type: ignore context . verify_mode = ssl . CERT_NONE elif self . config . ssl_capath : # Remove type ignore when this is merged https://github.com/python/typeshed/pull/3855 context = ssl . SSLContext ( ssl . PROTOCOL_TLS ) # type: ignore context . verify_mode = ssl . CERT_REQUIRED # `check_hostname` must be enabled as well to verify the authenticity of a cert. context . check_hostname = True context . load_verify_locations ( capath = self . config . ssl_capath ) try : # Object returned by SmartConnect is a ServerInstance # https://www.vmware.com/support/developer/vc-sdk/visdk2xpubs/ReferenceGuide/vim.ServiceInstance.html conn = connect . SmartConnect ( host = self . config . hostname , user = self . config . username , pwd = self . config . password , sslContext = context ) # Next line tries a simple API call to check the health of the connection. version_info = VersionInfo ( conn . content . about ) except Exception as e : err_msg = "Connection to {} failed: {}" . format ( self . config . hostname , e ) raise APIConnectionError ( err_msg ) if not version_info . is_vcenter ( ) : # Connection was successful but to something that is not a VirtualCenter instance. The check won't # run correctly. # TODO: Raise an exception and stop execution here self . log . error ( "%s is not a valid VirtualCenter (vCenter) instance, the vSphere API reports '%s'. " "Do not try to connect to ESXi hosts directly." , self . config . hostname , version_info . api_type , ) if self . _conn : connect . Disconnect ( self . _conn ) self . _conn = conn self . log . debug ( "Connected to %s" , version_info . fullName )