Better error checking

This commit is contained in:
Nitin Madhok 2015-05-04 05:23:08 -04:00
parent ace1d58947
commit 8a9e9eeb03

View file

@ -1798,50 +1798,78 @@ def create(vm_):
if 'clonefrom' in vm_:
# Clone VM/template from specified VM/template
object_ref = _get_mor_by_property(vim.VirtualMachine, vm_['clonefrom'])
if object_ref.config.template:
clone_type = "template"
if object_ref:
clone_type = "template" if object_ref.config.template else "vm"
else:
clone_type = "vm"
raise SaltCloudSystemExit(
'The VM/template that you have specified under clonefrom does not exist'
)
# Either a cluster, or a resource pool must be specified when cloning from template.
if resourcepool:
resourcepool_ref = _get_mor_by_property(vim.ResourcePool, resourcepool)
if not resourcepool_ref:
log.error('Specified resource pool: {0} does not exist'.format(resourcepool))
if clone_type == "template":
raise SaltCloudSystemExit('You must specify a resource pool that exists')
elif cluster:
cluster_ref = _get_mor_by_property(vim.ClusterComputeResource, cluster)
resourcepool_ref = cluster_ref.resourcePool
if not cluster_ref:
log.error('Specified cluster: {0} does not exist'.format(cluster))
if clone_type == "template":
raise SaltCloudSystemExit('You must specify a cluster that exists')
else:
resourcepool_ref = cluster_ref.resourcePool
elif clone_type == "template":
raise SaltCloudSystemExit(
'You must either specify a cluster, a host or a resource pool'
'You must either specify a cluster or a resource pool when cloning from a template'
)
else:
log.debug("Using resource pool used by the {0} {1}".format(clone_type, vm_['clonefrom']))
# Either a datacenter or a folder can be optionally specified
# If not specified, the existing VM/template\'s parent folder is used.
if folder:
folder_ref = _get_mor_by_property(vim.Folder, folder)
if not folder_ref:
log.error('Specified folder: {0} does not exist. '.format(folder))
log.debug("Using folder in which {0} {1} is present".format(clone_type, vm_['clonefrom']))
folder_ref = object_ref.parent
elif datacenter:
datacenter_ref = _get_mor_by_property(vim.Datacenter, datacenter)
folder_ref = datacenter_ref.vmFolder
if not datacenter_ref:
log.error('Specified datacenter: {0} does not exist'.format(datacenter))
log.debug"Using datacenter folder in which {0} {1} is present".format(clone_type, vm_['clonefrom']))
folder_ref = object_ref.parent
else:
folder_ref = datacenter_ref.vmFolder
else:
log.debug("Using folder in which {0} {1} is present".format(clone_type, vm_['clonefrom']))
folder_ref = object_ref.parent
# Create the relocation specs
reloc_spec = vim.vm.RelocateSpec()
if resourcepool or cluster:
if (resourcepool and resourcepool_ref) or (cluster and cluster_ref):
reloc_spec.pool = resourcepool_ref
# Either a datastore/datastore cluster can be optionally specified.
# If not specified, the current datastore is used.
if datastore:
datastore_ref = _get_mor_by_property(vim.Datastore, datastore)
if datastore_ref:
reloc_spec.datastore = datastore_ref
else:
log.error("Specified datastore: {0} does not exist".format(datastore))
log.debug("Using datastore used by the {0} {1}".format(clone_type, vm_['clonefrom']))
if host:
host_ref = _get_mor_by_property(vim.HostSystem, host)
if host_ref:
reloc_spec.host = host_ref
else:
log.warning("Specified host: {0} does not exist".format(host))
log.warning("Using host used by the {0} {1}".format(clone_type, vm_['clonefrom']))
log.error("Specified host: {0} does not exist".format(host))
log.debug("Using host used by the {0} {1}".format(clone_type, vm_['clonefrom']))
# Create the config specs
config_spec = vim.vm.ConfigSpec()