import code/upgrade task to support predicate for awaiting completion + status

This commit is contained in:
danikov 2012-02-16 00:59:54 +00:00
parent 56b9600d7b
commit c57a267dd9
6 changed files with 201 additions and 7 deletions

View File

@ -147,6 +147,13 @@ public class Task extends EntityType<Task> {
this.status = status; this.status = status;
return this; return this;
} }
/**
* @see Task#getStatus()
*/
public Builder status(TaskStatus status) {
return this.status(status.toString());
}
/** /**
* @see Task#getOperation() * @see Task#getOperation()
@ -411,8 +418,12 @@ public class Task extends EntityType<Task> {
* <li>aborted - The task was aborted by an administrative action. * <li>aborted - The task was aborted by an administrative action.
* </ul> * </ul>
*/ */
public String getStatus() { public TaskStatus getStatus() {
return status; return TaskStatus.fromValue(status);
}
public void setStatus(TaskStatus status) {
this.setStatus(status.toString());
} }
public void setStatus(String status) { public void setStatus(String status) {

View File

@ -0,0 +1,41 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.vcloud.director.v1_5.domain;
/**
*
* @author Adrian Cole
*
*/
public class TaskInErrorStateException extends RuntimeException {
private static final long serialVersionUID = 1L;
private final Task task;
public TaskInErrorStateException(Task task) {
super("error on task: " + task + " error: " + task.getError());
this.task = task;
}
public Task getTask() {
return task;
}
}

View File

@ -0,0 +1,73 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.vcloud.director.v1_5.domain;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* @author Adrian Cole
*/
public enum TaskStatus {
/**
* The task has completed and returned a value indicating success.
*/
SUCCESS,
/**
* The task is running.
*/
RUNNING,
/**
* The task has been queued for execution.
*/
QUEUED,
/**
* The task has completed and returned a value indicating an error.
*/
ERROR,
/**
* not an official status, temporarily in.
*/
CANCELLED, UNRECOGNIZED;
public String value() {
return name().toLowerCase();
}
@Override
public String toString() {
return value();
}
public static TaskStatus fromValue(String status) {
if ("CANCELED".equals(status.toUpperCase())) {
// TODO: ecloud hack
status = "CANCELLED";
} else if ("FAILED".equals(status.toUpperCase())) {
status = "ERROR";
} else if ("COMPLETED".equals(status.toUpperCase())) {
status = "SUCCESS";
}
try {
return valueOf(checkNotNull(status, "status").toUpperCase());
} catch (IllegalArgumentException e) {
return UNRECOGNIZED;
}
}
}

View File

@ -29,12 +29,11 @@ import org.jclouds.rest.annotations.EndpointParam;
import org.jclouds.rest.annotations.ExceptionParser; import org.jclouds.rest.annotations.ExceptionParser;
import org.jclouds.rest.annotations.JAXBResponseParser; import org.jclouds.rest.annotations.JAXBResponseParser;
import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.vcloud.director.v1_5.domain.ReferenceType; import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.domain.Task; import org.jclouds.vcloud.director.v1_5.domain.Task;
import org.jclouds.vcloud.director.v1_5.domain.TasksList; import org.jclouds.vcloud.director.v1_5.domain.TasksList;
import org.jclouds.vcloud.director.v1_5.filters.AddVCloudAuthorizationToRequest; import org.jclouds.vcloud.director.v1_5.filters.AddVCloudAuthorizationToRequest;
import org.jclouds.vcloud.director.v1_5.functions.OrgReferenceToTaskListEndpoint; import org.jclouds.vcloud.director.v1_5.functions.OrgReferenceToTaskListEndpoint;
import org.jclouds.vcloud.director.v1_5.functions.ReferenceToEndpoint;
import org.jclouds.vcloud.director.v1_5.functions.ThrowVCloudErrorOn4xx; import org.jclouds.vcloud.director.v1_5.functions.ThrowVCloudErrorOn4xx;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
@ -53,7 +52,7 @@ public interface TaskAsyncClient {
@Consumes @Consumes
@JAXBResponseParser @JAXBResponseParser
@ExceptionParser(ThrowVCloudErrorOn4xx.class) @ExceptionParser(ThrowVCloudErrorOn4xx.class)
ListenableFuture<TasksList> getTaskList(@EndpointParam(parser = OrgReferenceToTaskListEndpoint.class) ReferenceType<?> orgRef); ListenableFuture<TasksList> getTaskList(@EndpointParam(parser = OrgReferenceToTaskListEndpoint.class) Reference orgRef);
/** /**
* @see TaskClient#getTask(URI) * @see TaskClient#getTask(URI)

View File

@ -22,7 +22,7 @@ import java.net.URI;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.jclouds.concurrent.Timeout; import org.jclouds.concurrent.Timeout;
import org.jclouds.vcloud.director.v1_5.domain.ReferenceType; import org.jclouds.vcloud.director.v1_5.domain.Reference;
import org.jclouds.vcloud.director.v1_5.domain.Task; import org.jclouds.vcloud.director.v1_5.domain.Task;
import org.jclouds.vcloud.director.v1_5.domain.TasksList; import org.jclouds.vcloud.director.v1_5.domain.TasksList;
@ -45,7 +45,7 @@ public interface TaskClient {
* @param orgId the unique id for the organization * @param orgId the unique id for the organization
* @return a list of tasks * @return a list of tasks
*/ */
TasksList getTaskList(ReferenceType<?> orgRef); TasksList getTaskList(Reference orgRef);
/** /**
* Retrieves a task. * Retrieves a task.

View File

@ -0,0 +1,70 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.vcloud.director.v1_5.predicates;
import java.net.URI;
import javax.annotation.Resource;
import javax.inject.Singleton;
import org.jclouds.logging.Logger;
import org.jclouds.rest.RestContext;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorAsyncClient;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient;
import org.jclouds.vcloud.director.v1_5.domain.Task;
import org.jclouds.vcloud.director.v1_5.domain.TaskInErrorStateException;
import org.jclouds.vcloud.director.v1_5.domain.TaskStatus;
import org.jclouds.vcloud.director.v1_5.features.TaskClient;
import com.google.common.base.Predicate;
import com.google.inject.Inject;
/**
*
* Tests to see if a task succeeds.
*
* @author Adrian Cole
*/
@Singleton
public class TaskSuccess implements Predicate<URI> {
private final TaskClient taskClient;
@Resource
protected Logger logger = Logger.NULL;
@Inject
public TaskSuccess(RestContext<VCloudDirectorClient, VCloudDirectorAsyncClient> context) {
this.taskClient = context.getApi().getTaskClient();
}
public boolean apply(URI taskUri) {
logger.trace("looking for status on task %s", taskUri);
Task task = taskClient.getTask(taskUri);
// perhaps task isn't available, yet
if (task == null)
return false;
logger.trace("%s: looking for status %s: currently: %s", task, TaskStatus.SUCCESS, task.getStatus());
if (task.getStatus() == TaskStatus.ERROR)
throw new TaskInErrorStateException(task);
return task.getStatus() == TaskStatus.SUCCESS;
}
}