vmware / pyvmomi

VMware vSphere API Python Bindings
Apache License 2.0
2.21k stars 764 forks source link

NameError: global name 'Iso8601' is not defined in VmomiSupport.py and SoapAdapter.py #112

Closed sduehr closed 10 years ago

sduehr commented 10 years ago

In https://github.com/vmware/pyvmomi/blob/master/pyVmomi/VmomiSupport.py line 26 is

import pyVmomi.Iso8601

but line 313 is

result = Iso8601.ISO8601Format(val)

leading to the error NameError: global name 'Iso8601' is not defined

eg. when calling vm.config

To fix this, I think line 313 should be

result = pyVmomi.Iso8601.ISO8601Format(val)

The same issue seems to exist in https://github.com/vmware/pyvmomi/blob/master/pyVmomi/SoapAdapter.py line 374:

result = Iso8601.ISO8601Format(val)
hartsock commented 10 years ago

Watching to see if Travis-CI verifies https://github.com/vmware/pyvmomi/pull/115

hartsock commented 10 years ago

Note: This sample find_by_uuid.py#L88 would exercise the same line of code. I have observed that this sample appears to work properly and reports a boot time.

We do not currently have a test to validate this, however. So, we need to verify.

hartsock commented 10 years ago

I'm looking for a test that sends a date-time from python into vSphere. I'll want to include that additional test before I'm 100% satisfied that this is a not a bug.

sduehr commented 10 years ago

The sample works for me. However, here's how I can reproduce the exception:

[root@vmw5-bareos-centos6-64-devel ~]# python
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> site.addsitedir('/opt/pyvmomi/lib/python2.6/site-packages')
>>> from pyVim.connect import SmartConnect, Disconnect
>>> from pyVmomi import vim
>>> from pyVmomi import vmodl
>>> 
>>> si = SmartConnect(host='10.20.40.102', user='xxxxx', pwd='xxxxxxxxxx', port=443)
>>> content = si.content
>>> objView = content.viewManager.CreateContainerView(content.rootFolder,
...                                                           [vim.VirtualMachine],
...                                                           True)
>>> vmList = objView.view
>>> objView.Destroy()
>>> matched_vms = [vm for vm in vmList if vm.name == 'stephand-test01']
>>> matched_vms
['vim.VirtualMachine:vm-47']
>>> vm = matched_vms[0]
>>> vm.summary.config.name
'stephand-test01'
>>> vm.summary.config.instanceUuid
'502dcc71-adf9-e432-303c-e52f8f1ebe2d'
>>> vm.summary.config.uuid
'422d6fa7-df63-12d0-9863-6f001673355f'
>>> vm.summary.config.vmPathName
'[datastore1] stephand-test01/stephand-test01.vmx'
>>> vm.summary.config.guestId
'rhel6_64Guest'
>>> vm.summary.config.guestFullName
'Red Hat Enterprise Linux 6 (64-bit)'
>>> vm.runtime.bootTime
datetime.datetime(2014, 6, 5, 11, 35, 33, 501265, tzinfo=<pyVmomi.Iso8601.TZInfo object at 0x1e4fa90>)
>>> vm.config
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/pyvmomi/lib/python2.6/site-packages/pyvmomi-5.5.0_2014.dev-py2.6.egg/pyVmomi/VmomiSupport.py", line 290, in FormatObject
    for prop in val._GetPropertyList()]), indent * "   ")
  File "/opt/pyvmomi/lib/python2.6/site-packages/pyvmomi-5.5.0_2014.dev-py2.6.egg/pyVmomi/VmomiSupport.py", line 313, in FormatObject
    result = Iso8601.ISO8601Format(val)
NameError: global name 'Iso8601' is not defined
>>> vm.runtime
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/pyvmomi/lib/python2.6/site-packages/pyvmomi-5.5.0_2014.dev-py2.6.egg/pyVmomi/VmomiSupport.py", line 290, in FormatObject
    for prop in val._GetPropertyList()]), indent * "   ")
  File "/opt/pyvmomi/lib/python2.6/site-packages/pyvmomi-5.5.0_2014.dev-py2.6.egg/pyVmomi/VmomiSupport.py", line 313, in FormatObject
    result = Iso8601.ISO8601Format(val)
NameError: global name 'Iso8601' is not defined

When I change the line in VmomiSupport.py like this:

--- VmomiSupport.py.orig    2014-08-15 12:20:09.951001359 +0200
+++ VmomiSupport.py 2014-08-16 10:15:15.102715215 +0200
@@ -310,7 +310,7 @@
    elif isinstance(val, bool):
       result = val and "true" or "false"
    elif isinstance(val, datetime):
-      result = Iso8601.ISO8601Format(val)
+      result = pyVmomi.Iso8601.ISO8601Format(val)
    elif isinstance(val, binary):
       result = base64.b64encode(val)
    else:

Then the output of the whole object works:

>>> vm.summary.config.name
'stephand-test01'
>>> vm.runtime.bootTime
datetime.datetime(2014, 6, 5, 11, 35, 33, 501265, tzinfo=<pyVmomi.Iso8601.TZInfo object at 0x1706a90>)
>>> vm.runtime
(vim.vm.RuntimeInfo) {
   dynamicType = <unset>,
   dynamicProperty = (vmodl.DynamicProperty) [],
...
   suspendTime = <unset>,
   bootTime = 2014-06-05T11:35:33.501265Z,
   suspendInterval = 0L,
...
   vFlashCacheAllocation = <unset>
}

So this is probably only occurs when generating the output representation.

hartsock commented 10 years ago

I'm going to try to hit this scenario today.

hartsock commented 10 years ago

It looks like the import was malformed. We should have said

from pyVmomi import Iso8601

hartsock commented 10 years ago

I've verified that this problem happens on date serialization. I'm trying to come up with a test.

hartsock commented 10 years ago

I have a test, but it will not survive translation to Travis CI. I will be working on the infrastructure libs to figure out why it's not carrying over properly.

hartsock commented 10 years ago

Note: The fix is a one-liner

from pyVmomi import Iso8601

The rest of the commit is testing to validate that the fix works.

I've marked this for 2014.1.1 bug fix release.

hartsock commented 10 years ago

I've broken out the XML testing part. This turned out to be a bit of a rat-hole. Instead I'm just testing for the one key scenario we care about here. Is the date-time stamp getting turned into a string of the correct format? The code to validate this is now in the new test, however, it does not use the standard asserts but instead uses custom matchers in vcrpy.

hartsock commented 10 years ago

The fix for this merged here: https://github.com/vmware/pyvmomi/pull/117