I have purestorage volume and vcenter in my environment. As i want to add/provision purestorage volume as a datastore in vmware vcenter using python script using python pyvmomi package. Its not working as expected.
I tried the below using pyvmomi python package, but it is not working as expected
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim
import purestorage
import requests
from urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
import ssl
s=ssl.SSLContext(ssl.PROTOCOL_TLSv1)
s.verify_mode=ssl.CERT_NONE
def add_pure_volume_as_datastore():
# Pure Storage array details
pure_array = '192.168.0.106'
pure_api_token = '125cfec6-560a-ca26-e7af-27be4234d3959'
pure_array_name = 'purefa1.lab.local'
pure_vol_name = 'ds-vm-01'
# VMware vCenter details
vcenter_ip = '192.168.0.185'
vcenter_user = '[email protected]'
vcenter_password = 'admin@123'
datacenter_name = 'dc'
cluster_name = 'lab'
esxi_hostname = 'esxi1.lab.local'
port=443
# Connect to Pure Storage array
pure = purestorage.FlashArray(pure_array, api_token=pure_api_token)
# Get volume information
volume = pure.get_volume(pure_vol_name)
# Connect to vCenter
service_instance = SmartConnect(host=vcenter_ip, user=vcenter_user, pwd=vcenter_password, port=port,sslContext=s,disableSslCertValidation=True)
print(vcenter_ip)
# Get datacenter
content = service_instance.RetrieveContent()
datacenter = None
for dc in content.rootFolder.childEntity:
if isinstance(dc, vim.Datacenter) and dc.name == datacenter_name:
datacenter = dc
break
if not datacenter:
print(f"Datacenter '{datacenter_name}' not found.")
return
# Find the cluster
cluster = None
for c in datacenter.hostFolder.childEntity:
if isinstance(c, vim.ClusterComputeResource) and c.name == cluster_name:
cluster = c
break
if not cluster:
print(f"Cluster '{cluster_name}' not found.")
return
# Find the host
host = None
for h in cluster.host:
if h.name == esxi_hostname:
host = h
break
if not host:
print(f"ESXi host '{esxi_hostname}' not found in cluster '{cluster_name}'.")
return
# Create a spec for the new datastore
spec = vim.host.DatastoreSystem.CreateVmfsDatastoreSpec()
spec.vmfs = vim.host.VmfsDatastoreCreateSpec()
spec.vmfs.volumeName = volume['name']
spec.vmfs.extent = [vim.host.VmfsDatastoreExpandSpec(
devicePath=f'/vmfs/devices/disks/{volume["source"]["serial"]}:1',
partition=None
)]
# Create the datastore
host.configManager.datastoreSystem.CreateVmfsDatastore(spec=spec)
# Disconnect from Pure Storage array and vCenter
pure.invalidate_cookie()
connect.Disconnect(service_instance)
if __name__ == "__main__":
add_pure_volume_as_datastore()
while i am executing the above code . i am getting the error like below :
Traceback (most recent call last):
File "add_pure_datastore.py", line 91, in <module>
add_pure_volume_as_datastore()
File "add_pure_datastore.py", line 75, in add_pure_volume_as_datastore
spec = vim.host.DatastoreSystem.CreateVmfsDatastoreSpec()
File "/home/student/venv/ansible_pure/lib64/python3.6/site-packages/pyVmomi/VmomiSupport.py", line 202, in __getattr__
return super(LazyType, self).__getattribute__(attr)
AttributeError: type object 'vim.host.DatastoreSystem' has no attribute 'CreateVmfsDatastoreSpec'