From c57a267dd9beb361b72e97ff043164ac8407daf4 Mon Sep 17 00:00:00 2001 From: danikov Date: Thu, 16 Feb 2012 00:59:54 +0000 Subject: [PATCH] import code/upgrade task to support predicate for awaiting completion + status --- .../vcloud/director/v1_5/domain/Task.java | 15 +++- .../domain/TaskInErrorStateException.java | 41 +++++++++++ .../director/v1_5/domain/TaskStatus.java | 73 +++++++++++++++++++ .../v1_5/features/TaskAsyncClient.java | 5 +- .../director/v1_5/features/TaskClient.java | 4 +- .../director/v1_5/predicates/TaskSuccess.java | 70 ++++++++++++++++++ 6 files changed, 201 insertions(+), 7 deletions(-) create mode 100644 labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/TaskInErrorStateException.java create mode 100644 labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/TaskStatus.java create mode 100644 labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/TaskSuccess.java diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/Task.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/Task.java index dfe569d62a..8c270c8441 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/Task.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/Task.java @@ -147,6 +147,13 @@ public class Task extends EntityType { this.status = status; return this; } + + /** + * @see Task#getStatus() + */ + public Builder status(TaskStatus status) { + return this.status(status.toString()); + } /** * @see Task#getOperation() @@ -411,8 +418,12 @@ public class Task extends EntityType { *
  • aborted - The task was aborted by an administrative action. * */ - public String getStatus() { - return status; + public TaskStatus getStatus() { + return TaskStatus.fromValue(status); + } + + public void setStatus(TaskStatus status) { + this.setStatus(status.toString()); } public void setStatus(String status) { diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/TaskInErrorStateException.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/TaskInErrorStateException.java new file mode 100644 index 0000000000..5b8fe5a272 --- /dev/null +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/TaskInErrorStateException.java @@ -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; + } + +} diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/TaskStatus.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/TaskStatus.java new file mode 100644 index 0000000000..ebef2f5218 --- /dev/null +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/domain/TaskStatus.java @@ -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; + } + } + +} \ No newline at end of file diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/TaskAsyncClient.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/TaskAsyncClient.java index 9eeb7340e7..1c39d7f7ef 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/TaskAsyncClient.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/TaskAsyncClient.java @@ -29,12 +29,11 @@ import org.jclouds.rest.annotations.EndpointParam; import org.jclouds.rest.annotations.ExceptionParser; import org.jclouds.rest.annotations.JAXBResponseParser; 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.TasksList; 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.ReferenceToEndpoint; import org.jclouds.vcloud.director.v1_5.functions.ThrowVCloudErrorOn4xx; import com.google.common.util.concurrent.ListenableFuture; @@ -53,7 +52,7 @@ public interface TaskAsyncClient { @Consumes @JAXBResponseParser @ExceptionParser(ThrowVCloudErrorOn4xx.class) - ListenableFuture getTaskList(@EndpointParam(parser = OrgReferenceToTaskListEndpoint.class) ReferenceType orgRef); + ListenableFuture getTaskList(@EndpointParam(parser = OrgReferenceToTaskListEndpoint.class) Reference orgRef); /** * @see TaskClient#getTask(URI) diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/TaskClient.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/TaskClient.java index 990ad8b613..c3790eb412 100644 --- a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/TaskClient.java +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/features/TaskClient.java @@ -22,7 +22,7 @@ import java.net.URI; import java.util.concurrent.TimeUnit; 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.TasksList; @@ -45,7 +45,7 @@ public interface TaskClient { * @param orgId the unique id for the organization * @return a list of tasks */ - TasksList getTaskList(ReferenceType orgRef); + TasksList getTaskList(Reference orgRef); /** * Retrieves a task. diff --git a/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/TaskSuccess.java b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/TaskSuccess.java new file mode 100644 index 0000000000..df0015def3 --- /dev/null +++ b/labs/vcloud-director/src/main/java/org/jclouds/vcloud/director/v1_5/predicates/TaskSuccess.java @@ -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 { + + private final TaskClient taskClient; + + @Resource + protected Logger logger = Logger.NULL; + + @Inject + public TaskSuccess(RestContext 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; + } + +}