VMware {code} Community
mwaack
Contributor
Contributor

Creating custom attributes using pyvmomi

Creating a custom attribute using the perl sdk looks like this:

$customFieldMgr->AddCustomFieldDef(name => $cfkey, moType => 'VirtualMachine'); 


Doing the same in python using pyvmomi creates a traceback:


>>> customFieldMgr.AddCustomFieldDef(name = cfkey, moType = 'VirtualMachine')

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "pyVmomi/VmomiSupport.py", line 553, in <lambda>

    self.f(*(self.args + (obj,) + args), **kwargs)

  File "pyVmomi/VmomiSupport.py", line 361, in _InvokeMethod

    map(CheckField, info.params, args)

  File "pyVmomi/VmomiSupport.py", line 936, in CheckField

    % (info.name, info.type.__name__, valType.__name__))

TypeError: For "moType" expected type type, but got str

So whats wrong here?

BTW: does someone know a source of examples using pyvmomi?

Mathias

0 Kudos
4 Replies
Steve_Jin
Expert
Expert

I had written a simple sample before, but not the same as you expected:

http://www.doublecloud.org/2013/11/hacking-vmware-private-python-api-for-vsphere-with-a-quick-sample...

Good luck!

Steve (http://www.doublecloud.org)

Steve JIN Author of VMware VI and vSphere SDK; Creator of open source VI Java API (http://vijava.sf.net); Blogger at http://www.doublecloud.org
0 Kudos
tbarn
Contributor
Contributor

Hi Matthias did you ever get an answer for this?  Steve Jin I see your link's contents but it doesn't address the issue, could you be more specific if you know how to access the methods of the CustomFieldsManager object?  I'm trying to use the SetField method myself and can't get it to work.

0 Kudos
stumpr
Virtuoso
Virtuoso

In PyVmomi you'll need to pass in the vim class type, not the string.  I ran into this while doing some Ansible modules.  I ended up needing something that worked from the entity type string ('VirtualMachine', 'HostSystem', etc); but generally the ManagedEntity types are hanging off the vim object

# Include this with your other pyVmomi imports

from pyVmomi import vim


customFieldMgr.AddCustomFieldDef(name = cfkey, moType = vim.VirtualMachine)

See if that does the trick.

Reuben Stump | http://www.virtuin.com | @ReubenStump
0 Kudos
gm0meyl0ve
Contributor
Contributor

That works great.

without moType = vim.VirtualMachine it just creates it as a "Global" Type.


Thanks


0 Kudos