Added enums with all the known error & status codes for async jobs

This commit is contained in:
andreisavu 2011-11-29 23:49:33 +02:00
parent 13f1a68b67
commit 3b785994c7
5 changed files with 121 additions and 10 deletions

View File

@ -0,0 +1,40 @@
/**
* 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.cloudstack;
/**
* @author Andrei Savu
*/
public class AsyncJobException extends RuntimeException {
public AsyncJobException() {
}
public AsyncJobException(String s) {
super(s);
}
public AsyncJobException(String s, Throwable throwable) {
super(s, throwable);
}
public AsyncJobException(Throwable throwable) {
super(throwable);
}
}

View File

@ -28,6 +28,41 @@ import com.google.gson.annotations.SerializedName;
*/ */
public class AsyncJob<T> { public class AsyncJob<T> {
/**
* Valid job result codes
*/
public static enum ResultCode {
SUCCESS (0),
FAIL (530),
UNKNOWN (-1);
private final int code;
private ResultCode(int code) {
this.code = code;
}
public int code() { return this.code; }
}
/**
* Valid async job statuses
*/
public static enum Status {
IN_PROGRESS (0),
SUCCEEDED (1),
FAILED (2),
UNKNOWN (-1);
private final int code;
private Status(int code) {
this.code = code;
}
public int code() { return this.code; }
}
public static <T> Builder<T> builder() { public static <T> Builder<T> builder() {
return new Builder<T>(); return new Builder<T>();
} }

View File

@ -25,6 +25,30 @@ import com.google.gson.annotations.SerializedName;
* @author Adrian Cole * @author Adrian Cole
*/ */
public class AsyncJobError { public class AsyncJobError {
/**
* Error codes for job errors
*/
public static enum Code {
INTERNAL_ERROR (530),
ACCOUNT_ERROR (531),
ACCOUNT_RESOURCE_LIMIT_ERROR(532),
INSUFFICIENT_CAPACITY_ERROR (533),
RESOURCE_UNAVAILABLE_ERROR (534),
RESOURCE_ALLOCATION_ERROR (535),
RESOURCE_IN_USE_ERROR (536),
NETWORK_RULE_CONFLICT_ERROR (537),
UNKNOWN (-1);
private final int code;
private Code(int code) {
this.code = code;
}
public int code() { return this.code; }
}
@SerializedName("errorcode") @SerializedName("errorcode")
private int errorCode; private int errorCode;
@SerializedName("errortext") @SerializedName("errortext")

View File

@ -19,10 +19,15 @@
package org.jclouds.cloudstack.predicates; package org.jclouds.cloudstack.predicates;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.cloudstack.domain.AsyncJob.ResultCode.FAIL;
import static org.jclouds.cloudstack.domain.AsyncJob.ResultCode.SUCCESS;
import static org.jclouds.cloudstack.domain.AsyncJob.Status.FAILED;
import static org.jclouds.cloudstack.domain.AsyncJob.Status.SUCCEEDED;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.cloudstack.AsyncJobException;
import org.jclouds.cloudstack.CloudStackClient; import org.jclouds.cloudstack.CloudStackClient;
import org.jclouds.cloudstack.domain.AsyncJob; import org.jclouds.cloudstack.domain.AsyncJob;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
@ -50,17 +55,21 @@ public class JobComplete implements Predicate<Long> {
} }
public boolean apply(Long jobId) { public boolean apply(Long jobId) {
logger.trace("looking for status on job %s", checkNotNull(jobId, "jobId")); logger.trace(">> looking for status on job %s", checkNotNull(jobId, "jobId"));
AsyncJob<?> job = refresh(jobId); AsyncJob<?> job = refresh(jobId);
if (job == null) if (job == null) {
return false; return false;
logger.trace("%s: looking for job status %s: currently: %s", job.getId(), 1, job.getStatus());
if (job.getError() != null) {
// TODO: create a typed error
throw new RuntimeException(String.format("job %s failed with exception %s", job.getId(), job.getError()
.toString()));
} }
return job.getStatus() > 0; logger.trace("%s: looking for job status %s: currently: %s", job.getId(), 1, job.getStatus());
if (job.getError() != null ||
job.getStatus() == FAILED.code() ||
job.getResultCode() == FAIL.code()) {
throw new AsyncJobException(String.format("job %s failed with exception %s",
job.toString(), job.getError().toString()));
}
return job.getStatus() == SUCCEEDED.code() &&
job.getResultCode() == SUCCESS.code();
} }
private AsyncJob<?> refresh(Long jobId) { private AsyncJob<?> refresh(Long jobId) {

View File

@ -27,12 +27,14 @@ import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify; import static org.easymock.EasyMock.verify;
import static org.jclouds.cloudstack.domain.AsyncJob.ResultCode.SUCCESS;
import static org.jclouds.cloudstack.domain.AsyncJob.Status.SUCCEEDED;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
/** /**
* @author Andrei Savu * @author Andrei Savu
*/ */
@Test(groups = "unit") @Test(groups = "unit", singleThreaded = true)
public class JobCompleteTest { public class JobCompleteTest {
@Test @Test
@ -42,7 +44,8 @@ public class JobCompleteTest {
expect(client.getAsyncJobClient()).andReturn(asyncJobClient); expect(client.getAsyncJobClient()).andReturn(asyncJobClient);
AsyncJob job = AsyncJob.builder().id(100L).status(1).build(); AsyncJob job = AsyncJob.builder().id(100L)
.status(SUCCEEDED.code()).resultCode(SUCCESS.code()).build();
expect(asyncJobClient.getAsyncJob(job.getId())).andReturn(job); expect(asyncJobClient.getAsyncJob(job.getId())).andReturn(job);
replay(client, asyncJobClient); replay(client, asyncJobClient);