diff --git a/server/src/com/cloud/api/commands/QueryAsyncJobResultCmd.java b/server/src/com/cloud/api/commands/QueryAsyncJobResultCmd.java index d897536585e..ecb0613625b 100644 --- a/server/src/com/cloud/api/commands/QueryAsyncJobResultCmd.java +++ b/server/src/com/cloud/api/commands/QueryAsyncJobResultCmd.java @@ -18,32 +18,20 @@ package com.cloud.api.commands; -import java.util.ArrayList; import java.util.Date; -import java.util.List; -import java.util.Map; import org.apache.log4j.Logger; import com.cloud.api.BaseCmd; import com.cloud.api.Parameter; -import com.cloud.api.ServerApiException; +import com.cloud.api.response.AsyncJobResponse; import com.cloud.async.AsyncJobResult; -import com.cloud.async.executor.IngressRuleResultObject; -import com.cloud.async.executor.NetworkGroupResultObject; -import com.cloud.exception.PermissionDeniedException; import com.cloud.serializer.SerializerHelper; -import com.cloud.utils.Pair; public class QueryAsyncJobResultCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(QueryAsyncJobResultCmd.class.getName()); private static final String s_name = "queryasyncjobresultresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.JOB_ID, Boolean.TRUE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -64,57 +52,62 @@ public class QueryAsyncJobResultCmd extends BaseCmd { /////////////// API Implementation/////////////////// ///////////////////////////////////////////////////// + @Override public String getName() { return s_name; } - public List> getProperties() { - return s_properties; - } - - @Override - public List> execute(Map params) { - Long jobId = (Long)params.get(BaseCmd.Properties.JOB_ID.getName()); - AsyncJobResult result; - - try { - result = getManagementServer().queryAsyncJobResult(jobId); - } catch (PermissionDeniedException e) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Permission denied"); - } - - List> returnValues = new ArrayList>(); - returnValues.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), jobId)); - returnValues.add(new Pair(BaseCmd.Properties.JOB_STATUS.getName(), Integer.valueOf(result.getJobStatus()))); - returnValues.add(new Pair(BaseCmd.Properties.JOB_PROCESS_STATUS.getName(), Integer.valueOf(result.getProcessStatus()))); - returnValues.add(new Pair(BaseCmd.Properties.JOB_RESULT_CODE.getName(), Integer.valueOf(result.getResultCode()))); - - Object resultObject = result.getResultObject(); - if(resultObject != null) { - - Class clz = resultObject.getClass(); - if(clz.isPrimitive() || clz.getSuperclass() == Number.class || clz == String.class || clz == Date.class) { - returnValues.add(new Pair(BaseCmd.Properties.JOB_RESULT_TYPE.getName(), "text")); - SerializerHelper.appendPairList(returnValues, resultObject, BaseCmd.Properties.JOB_RESULT.getName()); - } else { - returnValues.add(new Pair(BaseCmd.Properties.JOB_RESULT_TYPE.getName(), "object")); - - if(result.getCmdOriginator() != null && !result.getCmdOriginator().isEmpty()) { - List> resultValues = new ArrayList>(); - if (resultObject instanceof NetworkGroupResultObject) { - serializeNetworkGroupResults(resultValues, (NetworkGroupResultObject)resultObject); - } else { + @Override + public String getResponse() { + AsyncJobResult result = (AsyncJobResult)getResponseObject(); + + AsyncJobResponse response = new AsyncJobResponse(); + response.setId(result.getJobId()); + response.setJobStatus(result.getJobStatus()); + response.setJobProcStatus(result.getProcessStatus()); + response.setJobResultCode(result.getResultCode()); + response.setJobResult(result.getResult()); + + Object resultObject = result.getResultObject(); + if (resultObject != null) { + Class clz = resultObject.getClass(); + if(clz.isPrimitive() || clz.getSuperclass() == Number.class || clz == String.class || clz == Date.class) { + response.setJobResultType("text"); + } else { + response.setJobResultType("object"); + } + } + + /* + Object resultObject = result.getResultObject(); + if (resultObject != null) { + + Class clz = resultObject.getClass(); + if(clz.isPrimitive() || clz.getSuperclass() == Number.class || clz == String.class || clz == Date.class) { + returnValues.add(new Pair(BaseCmd.Properties.JOB_RESULT_TYPE.getName(), "text")); + SerializerHelper.appendPairList(returnValues, resultObject, BaseCmd.Properties.JOB_RESULT.getName()); + } else { + returnValues.add(new Pair(BaseCmd.Properties.JOB_RESULT_TYPE.getName(), "object")); + + if(result.getCmdOriginator() != null && !result.getCmdOriginator().isEmpty()) { + List> resultValues = new ArrayList>(); + if (resultObject instanceof NetworkGroupResultObject) { + serializeNetworkGroupResults(resultValues, (NetworkGroupResultObject)resultObject); + } else { SerializerHelper.appendPairList(resultValues, resultObject, BaseCmd.Properties.JOB_RESULT.getName()); - } - returnValues.add(new Pair(result.getCmdOriginator(), new Object[] { resultValues } )); - } - } - } - return returnValues; - } + } + returnValues.add(new Pair(result.getCmdOriginator(), new Object[] { resultValues } )); + } + } + } + */ + + return SerializerHelper.toSerializedString(response); + } // For now network groups are the only objects with nested objects inside, so we special case serialization to handle this one case. // In the future, if a generic serialization that handles nested objects is implemented then this special case can be removed. + /* private void serializeNetworkGroupResults(List> resultValues, NetworkGroupResultObject resultObject) { if (resultObject != null) { resultValues.add(new Pair(BaseCmd.Properties.ID.getName(), resultObject.getId().toString())); @@ -152,5 +145,6 @@ public class QueryAsyncJobResultCmd extends BaseCmd { resultValues.add(new Pair("ingressrule", ingressDataArray)); } } - } + } + */ } diff --git a/server/src/com/cloud/api/response/AsyncJobResponse.java b/server/src/com/cloud/api/response/AsyncJobResponse.java index bd9471a5070..65f67b9c902 100644 --- a/server/src/com/cloud/api/response/AsyncJobResponse.java +++ b/server/src/com/cloud/api/response/AsyncJobResponse.java @@ -44,6 +44,9 @@ public class AsyncJobResponse implements ResponseObject { @Param(name="jobresultcode") private Integer jobResultCode; + @Param(name="jobresulttype") + private String jobResultType; + @Param(name="jobresult") private String jobResult; @@ -112,6 +115,14 @@ public class AsyncJobResponse implements ResponseObject { this.jobResultCode = jobResultCode; } + public String getJobResultType() { + return jobResultType; + } + + public void setJobResultType(String jobResultType) { + this.jobResultType = jobResultType; + } + public String getJobResult() { return jobResult; } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index d8f19fe122b..003106701d1 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -68,6 +68,7 @@ import com.cloud.api.commands.ListVolumesCmd; import com.cloud.api.commands.ListZonesByCmd; import com.cloud.api.commands.LockAccountCmd; import com.cloud.api.commands.LockUserCmd; +import com.cloud.api.commands.QueryAsyncJobResultCmd; import com.cloud.api.commands.RebootSystemVmCmd; import com.cloud.api.commands.RegisterCmd; import com.cloud.api.commands.RemovePortForwardingServiceCmd; @@ -1604,6 +1605,15 @@ public interface ManagementServer { * @return async-call result object */ AsyncJobResult queryAsyncJobResult(long jobId) throws PermissionDeniedException; + + /** + * Queries for the status or final result of an async job. + * @param cmd the command that specifies the job id + * @return an async-call result object + * @throws PermissionDeniedException + */ + AsyncJobResult queryAsyncJobResult(QueryAsyncJobResultCmd cmd) throws PermissionDeniedException; + AsyncJobVO findInstancePendingAsyncJob(String instanceType, long instanceId); AsyncJobVO findAsyncJobById(long jobId); diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 8251c265a7a..037fb9362a5 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -33,7 +33,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.StringTokenizer; import java.util.TimeZone; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -103,6 +102,7 @@ import com.cloud.api.commands.ListVolumesCmd; import com.cloud.api.commands.ListZonesByCmd; import com.cloud.api.commands.LockAccountCmd; import com.cloud.api.commands.LockUserCmd; +import com.cloud.api.commands.QueryAsyncJobResultCmd; import com.cloud.api.commands.RebootSystemVmCmd; import com.cloud.api.commands.RegisterCmd; import com.cloud.api.commands.RemovePortForwardingServiceCmd; @@ -123,7 +123,6 @@ import com.cloud.async.AsyncJobResult; import com.cloud.async.AsyncJobVO; import com.cloud.async.BaseAsyncJobExecutor; import com.cloud.async.dao.AsyncJobDao; -import com.cloud.async.executor.CreateOrUpdateRuleParam; import com.cloud.async.executor.DeleteDomainParam; import com.cloud.async.executor.DeployVMParam; import com.cloud.async.executor.NetworkGroupIngressParam; @@ -7181,6 +7180,11 @@ public class ManagementServerImpl implements ManagementServer { return _diskOfferingDao.search(sc, searchFilter); } + @Override + public AsyncJobResult queryAsyncJobResult(QueryAsyncJobResultCmd cmd) throws PermissionDeniedException { + return queryAsyncJobResult(cmd.getId()); + } + @Override public AsyncJobResult queryAsyncJobResult(long jobId) throws PermissionDeniedException { AsyncJobVO job = _asyncMgr.getAsyncJob(jobId);