VMware {code} Community
acwang27
Contributor
Contributor

How to add an item to "Tasks (Console)" of vSphere Web Client

Hi all,

I am a total newbie in VMware SDK developing. I am trying to track action progress for my extension (plugin) using the Task Console but I could not find any sample code to help. I tried to piece the following code together but it was not working (no errors either).

        ServiceContent service = getServiceContent(serverGuid)

        // create task manager

        // ManagedObjectReference taskMgrRef = service.getTaskManager();

        // create scheduled task manager

        ManagedObjectReference scheduledTaskMgrRef = service.getScheduledTaskManager();

      

        // root folder

        ManagedObjectReference rootFolder = service.getRootFolder();

      

        // create scheduled task

        ScheduledTaskSpec scheduledTaskSpec = new ScheduledTaskSpec();

        scheduledTaskSpec.setName("TEST_SCHEDULE");

        scheduledTaskSpec.setEnabled(Boolean.TRUE);

        scheduledTaskSpec.setDescription("addAppliance-scheduledTask");

        // set action

        CreateTaskAction action = new CreateTaskAction();

        action.setTaskTypeId("TEST_SCHEDULE_sampletask");

        scheduledTaskSpec.setAction(action);

        // set scheduler

        OnceTaskScheduler onceTaskScheduler = new OnceTaskScheduler();

        scheduledTaskSpec.setScheduler(onceTaskScheduler);

        // set scheduler

        OnceTaskScheduler onceTaskScheduler = new OnceTaskScheduler();

        scheduledTaskSpec.setScheduler(onceTaskScheduler);

      

        ManagedObjectReference scheduledTask = null;

        try {

              scheduledTask = _vimPort.createObjectScheduledTask(cheduledTaskMgrRef, rootFolder, scheduledTaskSpec);

        } catch (DuplicateNameFaultMsg | InvalidNameFaultMsg | RuntimeFaultFaultMsg e) {

          String errorMessage = String.format(
"An Exception Occurred while getCustomFieldDefs " +
"in addDatastore call, details - %s.",
e.getMessage());
     logger.log(Level.SEVERE, errorMessage);
     throw new VMwareRequestException(errorMessage);
    }

Is there anything wrong in the code? I am also confused that should I use task manager or scheduled task manager?

Could anybody help me on that?

Thanks in advance!

0 Kudos
6 Replies
semerdzhievp
VMware Employee
VMware Employee

I don't see anything wrong with your code, although you could use the TaskManager (_vimPort.createTask() method) instead of the ScheduledTaskManager since you want to run the task immediately.

Also, if the version of vSphere you are using is 6.0 or prior, the Task Console in the Web Client will not update by itself and you'll have to refresh manually to view tasks that are not started through the Web Client (like the task in question).

0 Kudos
irahov
VMware Employee
VMware Employee

Hi, acwang27,

Creating a new task (vimPort functionality) is not part of the vSphere Client SDK but rather related to vSphere API itself. So you should use the vSphere Management SDK Forum .

A related question is when the progress of a task created with vimPort will visible in the vSphere Client? The answer is - it depends on the client version that you use, i.e. the availability of the 'live updates' functionality.

It is always ON for the latest HTML vSphere client version 6.5.

If you are using the flex client then the smallest version is 6.0 u1 (but the functionality is disabled by default so you have to enable it). Prior that version you can also schedule a task against the vSphere and the task will be executed but you will not be able to see its progress in the Tasks console.

6.0 documentation:

https://pubs.vmware.com/vsphere-60/index.jsp?topic=%2Fcom.vmware.vsphere.monitoring.doc%2FGUID-F80D0...

0 Kudos
acwang27
Contributor
Contributor

Thanks semerdzhievp!

I am using 6.0.

Do I need to refresh the view in the code somehow or just refresh the view in Web Client GUI?

Also could you please enlighten me of how to use createTask() method since I am not quite sure I passed in the correct parameters. Thanks!

0 Kudos
acwang27
Contributor
Contributor

Thanks irahov!

I am currently using vSphere Web Client v6.0.

Thanks for the doc link! I am looking into it...

Also could you please enlighten me of how to use createTask() method since I am not quite sure I passed in the correct parameters. Thanks!

0 Kudos
semerdzhievp
VMware Employee
VMware Employee

Hitting the refresh button in the top right of the vSphere Web Client should be enough to show the task.

See the API reference on createTask (it should be simpler than using createObjectScheduledTask) or ask in the vSphere Management SDK forum.

I suppose the usage in your case would be similar to:

_vimPort.createTask(taskMgrRef, rootFolder, "TEST_SCHEDULE_sampletask", null, false, null, null);

0 Kudos
acwang27
Contributor
Contributor

Thank you very much for your reply semerdzhievp! I will try it out.

0 Kudos