Connecting to vCenter Server and Selecting a Cluster for vSAN

Before configuring a vSAN cluster by using the vSAN Management API, you must establish a secure connection with vCenter Server and filter the clusters on which you want to enable vSAN.

In the following example, a secure connection is established with the vCenter Server using the user name and password authentication passed as arguments to the Python script. Later, the getClusterInstance function is called by passing the cluster name as an argument.

if sys.version_info[:3] > (2, 7, 8):
    context = ssl.create_default_context()
    context.check_hostname = False
    context.verify_mode = ssl.CERT_NONE

# Connect to vCenter Server
si = SmartConnect(
    host=args.host,
    user=args.user,
    pwd=password,
    port=int(args.port),
    sslContext=context)

# Disconnect from vSAN upon exit
atexit.register(Disconnect, si)

# Connect to the cluster passed as an argument
cluster = getClusterInstance(args.clusterName, si)

After establishing a secure connection with the vCenter Server and identifying the cluster, a connection is made to that cluster. The getClusterInstance function can be reused across the client application to connect to the clusters on which you want to configure vSAN.

def getClusterInstance(clusterName, serviceInstance):
    content = serviceInstance.RetrieveContent()
    searchIndex = content.searchIndex
    datacenters = content.rootFolder.childEntity

# Look for the cluster in each datacenter attached to vCenter Server
for datacenter in datacenters:
    cluster = searchIndex.FindChild(datacenter.hostFolder, clusterName)
    if cluster is not None:
        return Cluster
    else:
        return None