Repository URL to install this package:
|
Version:
5.1.2 ▾
|
"""
This module implements simple helper functions for managing service instance objects
"""
__author__ = "VMware, Inc."
import atexit
import ssl
from pyVim.connect import SmartConnect, Disconnect
def connect(args):
"""
Determine the most preferred API version supported by the specified server,
then connect to the specified server using that API version, login and return
the service instance object.
"""
service_instance = None
# form a connection...
try:
context = None
if hasattr(ssl, 'SSLContext'):
context = ssl.SSLContext(ssl.PROTOCOL_TLS)
if args.disable_ssl_verification:
context.verify_mode = ssl.CERT_NONE
else:
context.verify_mode = ssl.CERT_REQUIRED
context.load_verify_locations(args.vcenter_cert_path)
service_instance = SmartConnect(
host=args.host,
user=args.user,
pwd=args.password,
port=args.port,
sslContext=context)
# doing this means you don't need to remember to disconnect your script/objects
atexit.register(Disconnect, service_instance)
except IOError as io_error:
print(io_error)
if not service_instance:
raise SystemExit("Unable to connect to host with supplied credentials.")
return service_instance