Merge pull request #44865 from nmadhok/develop

[develop][salt-cloud][vmware] - Adding capability to specify datastore cluster/datastore for hard disks
This commit is contained in:
Nicole Thomas 2017-12-11 13:34:48 -05:00 committed by GitHub
commit 3c5b5914ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 12 deletions

View File

@ -292,12 +292,36 @@ def _add_new_hard_disk_helper(disk_label, size_gb, unit_number, controller_key=1
disk_spec.device.backing.diskMode = 'persistent'
if datastore:
ds_ref = salt.utils.vmware.get_datastore_ref(_get_si(), datastore)
if not ds_ref:
raise SaltCloudSystemExit('Requested {0} disk in datastore {1}, but no such datastore found.'.format(disk_label, datastore))
datastore_path = '[' + str(ds_ref.name) + '] ' + vm_name
datastore_ref = salt.utils.vmware.get_mor_using_container_view(_get_si(), vim.Datastore, datastore)
if not datastore_ref:
# check if it is a datastore cluster instead
datastore_cluster_ref = salt.utils.vmware.get_mor_using_container_view(_get_si(), vim.StoragePod, datastore)
if not datastore_cluster_ref:
# datastore/datastore cluster specified does not exist
raise SaltCloudSystemExit("Specified datastore/datastore cluster ({0}) for disk ({1}) does not exist".format(datastore, disk_label))
# datastore cluster has been specified
# find datastore with most free space available
#
# TODO: Get DRS Recommendations instead of finding datastore with most free space
datastore_list = salt.utils.vmware.get_datastores(_get_si(), datastore_cluster_ref, get_all_datastores=True)
datastore_free_space = 0
for ds_ref in datastore_list:
log.trace("Found datastore ({0}) with free space ({1}) in datastore cluster ({2})".format(ds_ref.name, ds_ref.summary.freeSpace, datastore))
if ds_ref.summary.accessible and ds_ref.summary.freeSpace > datastore_free_space:
datastore_free_space = ds_ref.summary.freeSpace
datastore_ref = ds_ref
if not datastore_ref:
# datastore cluster specified does not have any accessible datastores
raise SaltCloudSystemExit("Specified datastore cluster ({0}) for disk ({1}) does not have any accessible datastores available".format(datastore, disk_label))
datastore_path = '[' + str(datastore_ref.name) + '] ' + vm_name
disk_spec.device.backing.fileName = datastore_path + '/' + disk_label + '.vmdk'
disk_spec.device.backing.datastore = ds_ref
disk_spec.device.backing.datastore = datastore_ref
log.trace("Using datastore ({0}) for disk ({1}), vm_name ({2})".format(datastore_ref.name, disk_label, vm_name))
disk_spec.device.controllerKey = controller_key
disk_spec.device.unitNumber = unit_number

View File

@ -296,21 +296,24 @@ def get_customizationspec_ref(si, customization_spec_name):
return customization_spec_name
def get_datastore_ref(si, datastore_name):
def get_mor_using_container_view(si, obj_type, obj_name):
'''
Get a reference to a VMware datastore for the purposes of adding/removing disks
Get reference to an object of specified object type and name
si
ServiceInstance for the vSphere or ESXi server (see get_service_instance)
datastore_name
Name of the datastore
obj_type
Type of the object (vim.StoragePod, vim.Datastore, etc)
obj_name
Name of the object
'''
inventory = get_inventory(si)
container = inventory.viewManager.CreateContainerView(inventory.rootFolder, [vim.Datastore], True)
container = inventory.viewManager.CreateContainerView(inventory.rootFolder, [obj_type], True)
for item in container.view:
if item.name == datastore_name:
if item.name == obj_name:
return item
return None
@ -2073,12 +2076,19 @@ def get_datastores(service_instance, reference, datastore_names=None,
skip=False,
type=vim.ClusterComputeResource)
elif isinstance(reference, vim.Datacenter):
# Traversal spec for clusters
# Traversal spec for datacenter
traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(
name='datacenter_datastore_traversal',
path='datastore',
skip=False,
type=vim.Datacenter)
elif isinstance(reference, vim.StoragePod):
# Traversal spec for datastore clusters
traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(
name='datastore_cluster_traversal',
path='childEntity',
skip=False,
type=vim.StoragePod)
elif isinstance(reference, vim.Folder) and \
get_managed_object_name(reference) == 'Datacenters':
# Traversal of root folder (doesn't support multiple levels of Folders)