0

If I run the below code in pycharm, I get this error:

--error--

C:\Python33\python.exe B:/Python/untitled3/working_test.py
'vim.VirtualMachine:vm-65063'
Traceback (most recent call last):
  File "B:/Python/untitled3/working_test.py", line 47, in <module>
    main()
  File "B:/Python/untitled3/working_test.py", line 37, in main
    filterspec = vim.TaskFilterSpec(vim.TaskFilterSpec.ByEntity(entity=source_machine))
TypeError: __init__() takes 1 positional argument but 2 were given

Process finished with exit code 1

--error--

I've tried using self, creating a class etc, but I just can't get my head around what I'm doing wrong. Any help is appreciated. I'm basically, trying to get task information on an entity (virtual machine) within vsphere.

Thanks!

import ssl

from pyVim import connect
from pyVmomi import vmodl, vim


def main():

    try:
        context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
        context.verify_mode = ssl.CERT_NONE
        si = connect.SmartConnect(host='vcenter name',
                                  user='user name',
                                  pwd='password',
                                  port=443,
                                  sslContext=context)

        if not si:
            print("Could not connect to the specified host using specified "
                  "username and password")
            return -1

        content = si.RetrieveContent()

        def getobject(vimtype, name):
            obj = None
            container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
            for c in container.view:
                if c.name == name:
                    obj = c
                    break
            return obj

        source_machine = getobject([vim.VirtualMachine], 'virtual machine name')
        print(source_machine)
        taskManager = content.taskManager
        filterspec = vim.TaskFilterSpec(vim.TaskFilterSpec.ByEntity(entity=source_machine))
        collector = taskManager.CreateCollectorForTasks(filterspec)

    except vmodl.MethodFault as e:
        print("Caught vmodl fault : {}".format(e.msg))
        return -1

    return 0

if __name__ == "__main__":
    main()
romainl
  • 186,200
  • 21
  • 280
  • 313
markc
  • 1
  • 3
  • I don't know this specific module, but what it's saying, is that this class expects in fact no arguments upon instantiation. – enedil Jun 12 '17 at 15:30

2 Answers2

0

vim.TaskFilterSpec() accepts no positional arguments. A minimal reproduction of the exception can be had with:

>>> vim.TaskFilterSpec(vim.TaskFilterSpec.ByEntity(entity=vim.ManagedEntity('1')))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() takes 1 positional argument but 2 were given

The class constructor vim.TaskFilterSpec() wants to be invoked with a entity named parameter. In your example code above, this would mean altering line 37 to read:

filterspec = vim.TaskFilterSpec(entity=vim.TaskFilterSpec.ByEntity(entity=source_machine))

When invoked with a placebo ManagedEntity, this results in a filter-spec similar to:

>>> source_machine=vim.ManagedEntity('1')
>>> filterspec = vim.TaskFilterSpec(entity=vim.TaskFilterSpec.ByEntity(entity=source_machine))
>>> filterspec
(vim.TaskFilterSpec) {
   dynamicType = <unset>,
   dynamicProperty = (vmodl.DynamicProperty) [],
   entity = (vim.TaskFilterSpec.ByEntity) {
      dynamicType = <unset>,
      dynamicProperty = (vmodl.DynamicProperty) [],
      entity = 'vim.ManagedEntity:1',
      recursion = <unset>
   },
   time = <unset>,
   userName = <unset>,
   activationId = (str) [],
   state = (vim.TaskInfo.State) [],
   alarm = <unset>,
   scheduledTask = <unset>,
   eventChainId = (int) [],
   tag = (str) [],
   parentTaskKey = (str) [],
   rootTaskKey = (str) []
}
0

thank you for the help.

i've adjusted this portion of the code and it's returned back without errors, but looking into now, why it only seems to be returning the query task itself instead of the tasks pertaining to the vm. I think it may have something to do with the tasks being at a vcenter level, but working throug it now.

source_machine = getobject([vim.VirtualMachine], 'virtual machine name)
taskManager = content.taskManager
filterspec = vim.TaskFilterSpec()
filterspec.entity = vim.TaskFilterSpec.ByEntity(entity=source_machine,recursion='all')
collector = taskManager.CreateCollectorForTasks(filterspec)
print(collector)

output returned:

C:\Python33\python.exe B:/Python/untitled3/working_test.py
'vim.TaskHistoryCollector:session[52b617f0-0f65-705c-7462-814d8b648fdd]52081175-cb98-a09f-f9f6-f6787f68d3b7'

Process finished with exit code 0
markc
  • 1
  • 3