argos83 / pysphere

Automatically exported from code.google.com/p/pysphere
88 stars 76 forks source link

pysphere.resources.vi_exception.VIException: [Task Error]: The object has already been deleted or has not been completely created #52

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Test that code
2.
3.

What is the expected output? What do you see instead?
The Virtual machine getting deployed from template

What version of the product are you using? On what operating system?
vSphere 5.5

Please provide any additional information below.

#Create new Virtual machine from template
from pysphere import VIServer
server = VIServer()
server.connect("host", "username", "passwd", trace_file="debug.txt")

#Set Variables
sHost = "host"
sDataStore = "ovh4-2-HDD2TB"

#Get Resource Pools
resource_pools = server.get_resource_pools()
a_rp = resource_pools.keys()[0]

#Define Template
template = server.get_vm_by_name("Windows Server 2008 R2 Standard #2")

#Define new Virtual machine name
newMachineName = "Test Machine"

#Deploy new virtual machine
newVirtualMachine = template.clone(newMachineName, resourcepool=a_rp, 
host=sHost)

#Get Status
print newVirtualMachine.get_status()

#Close server connection
server.disconnect()

It gives the following output: 
pysphere.resources.vi_exception.VIException: [Task Error]: The object has 
already been deleted or has not been completely created

Original issue reported on code.google.com by dfrankes...@gmail.com on 19 Feb 2014 at 6:38

GoogleCodeExporter commented 9 years ago
yes, I also get the same problem, how do you reslove this proble, can you tell 
me.

my e-mai: john_xsj@hotmail.com

Original comment by xiaoshij...@gmail.com on 10 Jun 2014 at 8:04

GoogleCodeExporter commented 9 years ago
hello, I had resolve this problem, my code as follow:

import pysphere

def getHostByName(server, name):
    mor = None
    for host_mor, host_name in server.get_hosts().items():
        if host_name == name: mor = host_mor; break
    return mor

def getResourcePoolByName(server, resourcepool):
    mor = None
    for rp_mor, rp_path in server.get_resource_pools().items():
        if resourcepool in rp_path : mor = rp_mor; break
    return mor

def getDatastoreByName(server, name):
    mor = None
    for ds_mor, ds_path in server.get_datastores().items():
        if ds_path == name: mor = ds_mor; break
    return mor

def vmClone(name, vmTemplateName, resourcepool, host, datastore, sync):
    #name: name of cloned VM;
    #vmTemplateName: name of source template;
    #labName: VM''s belong to a training lab;
    #host: to deploy the VM on;
    #labgroup: sequence number of lab system to use;
    #sync: synchronous mode or not

    vm = vCenterServer.get_vm_by_name(name=vmTemplateName)
    host_mor = getHostByName(vCenterServer, host)   #locate destination host MOR
    rp_mor = getResourcePoolByName(vCenterServer,resourcepool)   # locate default resource pool of destination host (the resource pool which parent folder is equal to the parent folder of the host)
    ds_mor = getDatastoreByName(vCenterServer, datastore)   # destination data store
    if sync:  # be careful,  vm.clone returns different objects based on whether sync is true or false
        clonedVM = vm.clone(name, sync_run=True, resourcepool=rp_mor, datastore=ds_mor, host=host_mor, power_on=False) # returns a VM object
        return
    else:
        return vm.clone(name, sync_run=False, resourcepool=rp_mor, datastore=ds_mor, host=host_mor, power_on=False)  # returns a VITask object

vCenterServer = pysphere.VIServer()
vCenterServer.connect('10.21.134.19', 'root', 'vmware')
print 
vmClone("test_vm4","WIN7-Template-Neil-14","TIS17-2","10.21.120.14","tis17-bs-ti
ered",1)

Original comment by xiaoshij...@gmail.com on 10 Jun 2014 at 9:43