VMware {code} Community
derevan007
Contributor
Contributor

How do I get the correct task from the Task List returned from vCloud Director REST API?

For many vCloud Director REST API requests, a task is generated. The response XML includes the HREF for this task in a <Task> node. Problem is that sometimes there is a list of tasks under <Tasks>. I have implemented logic to pull the most recently started task, but I question whether or not this will always work. Is there any guarantee that the most recent task corresponds to the original request. In other words, is it possible for another task that runs in close succession to the original task to show up as the most recently started, or does the REST response occur before that second task has a chance to even start?

Reply
0 Kudos
4 Replies
derevan007
Contributor
Contributor

Some observations:

   Any PUT or POST operation that operates on an existing object appears to only return just a <Task> response. Is there ever case where the object XML is returned with all tasks?

   Any POST operation that creates a new object such as a vApp or VM returns a <Tasks> section with a single <Task> representing the creation task.

   A GET operation on an object that has unexpired tasks (24 hour history by default) will show multiple tasks.

So I'm trying to get confirmation that a PUT and POST operation will only ever include at most one task. If that is not correct, under what circumstances would the response include multiple tasks?

Reply
0 Kudos
Todor_Todorov
Hot Shot
Hot Shot

Hi,

The <Tasks> element returned inside any object as a result of PUT or POST operation contains:

- the task that has been initiated as a result from the operation: this task is in "running" state

- any previous tasks that has failed and are in "error" state

- at any point of time there can be only one running task for a given object

You can change your algorithm to extract the only task in "running" state when you get multiple tasks inside <Tasks> element.

Regards,

Todor Todorov

Reply
0 Kudos
derevan007
Contributor
Contributor

Thanks for the response Todor. Some tasks run so quickly that the initial task status is NOT "running"'; the final "success" or "error" status is returned right away sometimes. This is the case I am most concerned about. In that case, when there is a response with multiple tasks and none of the tasks are running, is it safe to grab the one that has the most recent start time?

Reply
0 Kudos
Todor_Todorov
Hot Shot
Hot Shot

I think your assumption about the start time is correct. I'm surprised that sometimes the final status is returned right away.

I think that the response of a PUT or POST operation containing a <Tasks> element (and not a single <Task> object) should always return a "running" task which then reaches "success" or "error" state on the next GET. If this is the case, then you can remember the href (or UUID) of this running task and use it later when quering.

Regards,

Todor Todorov