mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
Refactor listAsyncJobs to new API framework, consolidating parameter and criteria packing/unpacking into just command unpacking to simplify building the search criteria.
This commit is contained in:
parent
53892037c4
commit
68275debc0
@ -15,38 +15,23 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.cloud.api.commands;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.cloud.api.BaseCmd;
|
||||
import com.cloud.api.BaseListCmd;
|
||||
import com.cloud.api.Implementation;
|
||||
import com.cloud.api.Parameter;
|
||||
import com.cloud.api.ServerApiException;
|
||||
import com.cloud.api.response.AsyncJobResponse;
|
||||
import com.cloud.async.AsyncJobVO;
|
||||
import com.cloud.server.Criteria;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.utils.Pair;
|
||||
|
||||
public class ListAsyncJobsCmd extends BaseCmd {
|
||||
import com.cloud.serializer.SerializerHelper;
|
||||
|
||||
@Implementation(method="searchForAsyncJobs")
|
||||
public class ListAsyncJobsCmd extends BaseListCmd {
|
||||
private static final String s_name = "listasyncjobsresponse";
|
||||
|
||||
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
|
||||
|
||||
static {
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE));
|
||||
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_ID, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.START_TZDATE, Boolean.FALSE));
|
||||
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.PAGE, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.PAGESIZE, Boolean.FALSE));
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
//////////////// API parameters /////////////////////
|
||||
/////////////////////////////////////////////////////
|
||||
@ -81,80 +66,33 @@ public class ListAsyncJobsCmd extends BaseCmd {
|
||||
/////////////// API Implementation///////////////////
|
||||
/////////////////////////////////////////////////////
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return s_name;
|
||||
}
|
||||
|
||||
public List<Pair<Enum, Boolean>> getProperties() {
|
||||
return s_properties;
|
||||
}
|
||||
|
||||
public List<Pair<String, Object>> execute(Map<String, Object> params) {
|
||||
Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
|
||||
Integer page = (Integer)params.get(BaseCmd.Properties.PAGE.getName());
|
||||
Integer pageSize = (Integer)params.get(BaseCmd.Properties.PAGESIZE.getName());
|
||||
Long accountId = (Long)params.get(BaseCmd.Properties.ACCOUNT_ID.getName());
|
||||
Date startDate = (Date)params.get(BaseCmd.Properties.START_TZDATE.getName());
|
||||
|
||||
Long startIndex = Long.valueOf(0);
|
||||
int pageSizeNum = 50;
|
||||
if (pageSize != null) {
|
||||
pageSizeNum = pageSize.intValue();
|
||||
}
|
||||
if (page != null) {
|
||||
int pageNum = page.intValue();
|
||||
if (pageNum > 0) {
|
||||
startIndex = Long.valueOf(pageSizeNum * (pageNum-1));
|
||||
}
|
||||
}
|
||||
|
||||
Criteria c = new Criteria("id", Boolean.TRUE, startIndex, Long.valueOf(pageSizeNum));
|
||||
if(account == null) {
|
||||
if(accountId != null)
|
||||
c.addCriteria(Criteria.ACCOUNTID, accountId.longValue());
|
||||
} else {
|
||||
if(account.getType() == Account.ACCOUNT_TYPE_ADMIN) {
|
||||
// for root admins, can take arbitrary account id from input
|
||||
if(accountId != null)
|
||||
c.addCriteria(Criteria.ACCOUNTID, accountId.longValue());
|
||||
} else {
|
||||
// for normal accounts, they can only query jobs of their own
|
||||
c.addCriteria(Criteria.ACCOUNTID, account.getId());
|
||||
}
|
||||
|
||||
@Override @SuppressWarnings("unchecked")
|
||||
public String getResponse() {
|
||||
List<AsyncJobVO> jobs = (List<AsyncJobVO>)getResponseObject();
|
||||
|
||||
List<AsyncJobResponse> response = new ArrayList<AsyncJobResponse>();
|
||||
for (AsyncJobVO job : jobs) {
|
||||
AsyncJobResponse jobResponse = new AsyncJobResponse();
|
||||
jobResponse.setAccountId(job.getAccountId());
|
||||
jobResponse.setCmd(job.getCmd());
|
||||
jobResponse.setCreated(job.getCreated());
|
||||
jobResponse.setId(job.getId());
|
||||
jobResponse.setJobInstanceId(job.getInstanceId());
|
||||
jobResponse.setJobInstanceType(job.getInstanceType());
|
||||
jobResponse.setJobProcStatus(job.getProcessStatus());
|
||||
jobResponse.setJobResult(job.getResult());
|
||||
jobResponse.setJobResultCode(job.getResultCode());
|
||||
jobResponse.setJobStatus(job.getStatus());
|
||||
jobResponse.setUserId(job.getUserId());
|
||||
|
||||
response.add(jobResponse);
|
||||
}
|
||||
|
||||
if(startDate != null) {
|
||||
c.addCriteria(Criteria.STARTDATE, startDate);
|
||||
}
|
||||
|
||||
List<AsyncJobVO> jobs = getManagementServer().searchForAsyncJobs(c);
|
||||
if (jobs == null) {
|
||||
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "unable to find async jobs");
|
||||
}
|
||||
|
||||
List<Pair<String, Object>> jobTags = new ArrayList<Pair<String, Object>>();
|
||||
Object[] sTag = new Object[jobs.size()];
|
||||
int i = 0;
|
||||
for(AsyncJobVO job : jobs) {
|
||||
List<Pair<String, Object>> jobData = new ArrayList<Pair<String, Object>>();
|
||||
jobData.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), job.getId().toString()));
|
||||
jobData.add(new Pair<String, Object>(BaseCmd.Properties.ACCOUNT_ID.getName(), String.valueOf(job.getAccountId())));
|
||||
jobData.add(new Pair<String, Object>(BaseCmd.Properties.USER_ID.getName(), String.valueOf(job.getUserId())));
|
||||
jobData.add(new Pair<String, Object>(BaseCmd.Properties.CMD.getName(), job.getCmd()));
|
||||
jobData.add(new Pair<String, Object>(BaseCmd.Properties.JOB_STATUS.getName(), String.valueOf(job.getStatus())));
|
||||
jobData.add(new Pair<String, Object>(BaseCmd.Properties.JOB_PROCESS_STATUS.getName(), String.valueOf(job.getProcessStatus())));
|
||||
jobData.add(new Pair<String, Object>(BaseCmd.Properties.JOB_RESULT_CODE.getName(), String.valueOf(job.getResultCode())));
|
||||
jobData.add(new Pair<String, Object>(BaseCmd.Properties.JOB_RESULT.getName(), job.getResult()));
|
||||
jobData.add(new Pair<String, Object>(BaseCmd.Properties.JOB_INSTANCE_TYPE.getName(), job.getInstanceType()));
|
||||
jobData.add(new Pair<String, Object>(BaseCmd.Properties.JOB_INSTANCE_ID.getName(), String.valueOf(job.getInstanceId())));
|
||||
jobData.add(new Pair<String, Object>(BaseCmd.Properties.CREATED.getName(), getDateString(job.getCreated())));
|
||||
|
||||
sTag[i++] = jobData;
|
||||
}
|
||||
|
||||
Pair<String, Object> jobTag = new Pair<String, Object>("asyncjobs", sTag);
|
||||
jobTags.add(jobTag);
|
||||
|
||||
return jobTags;
|
||||
|
||||
return SerializerHelper.toSerializedString(response);
|
||||
}
|
||||
}
|
||||
|
||||
129
server/src/com/cloud/api/response/AsyncJobResponse.java
Normal file
129
server/src/com/cloud/api/response/AsyncJobResponse.java
Normal file
@ -0,0 +1,129 @@
|
||||
package com.cloud.api.response;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.cloud.api.ResponseObject;
|
||||
import com.cloud.serializer.Param;
|
||||
|
||||
public class AsyncJobResponse implements ResponseObject {
|
||||
@Param(name="id")
|
||||
private Long id;
|
||||
|
||||
@Param(name="accountid")
|
||||
private Long accountId;
|
||||
|
||||
@Param(name="userid")
|
||||
private Long userId;
|
||||
|
||||
@Param(name="cmd")
|
||||
private String cmd;
|
||||
|
||||
@Param(name="jobstatus")
|
||||
private Integer jobStatus;
|
||||
|
||||
@Param(name="jobprocstatus")
|
||||
private Integer jobProcStatus;
|
||||
|
||||
@Param(name="jobresultcode")
|
||||
private Integer jobResultCode;
|
||||
|
||||
@Param(name="jobresult")
|
||||
private String jobResult;
|
||||
|
||||
@Param(name="jobinstancetype")
|
||||
private String jobInstanceType;
|
||||
|
||||
@Param(name="jobinstanceid")
|
||||
private Long jobInstanceId;
|
||||
|
||||
@Param(name="created")
|
||||
private Date created;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getAccountId() {
|
||||
return accountId;
|
||||
}
|
||||
|
||||
public void setAccountId(Long accountId) {
|
||||
this.accountId = accountId;
|
||||
}
|
||||
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(Long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getCmd() {
|
||||
return cmd;
|
||||
}
|
||||
|
||||
public void setCmd(String cmd) {
|
||||
this.cmd = cmd;
|
||||
}
|
||||
|
||||
public Integer getJobStatus() {
|
||||
return jobStatus;
|
||||
}
|
||||
|
||||
public void setJobStatus(Integer jobStatus) {
|
||||
this.jobStatus = jobStatus;
|
||||
}
|
||||
|
||||
public Integer getJobProcStatus() {
|
||||
return jobProcStatus;
|
||||
}
|
||||
|
||||
public void setJobProcStatus(Integer jobProcStatus) {
|
||||
this.jobProcStatus = jobProcStatus;
|
||||
}
|
||||
|
||||
public Integer getJobResultCode() {
|
||||
return jobResultCode;
|
||||
}
|
||||
|
||||
public void setJobResultCode(Integer jobResultCode) {
|
||||
this.jobResultCode = jobResultCode;
|
||||
}
|
||||
|
||||
public String getJobResult() {
|
||||
return jobResult;
|
||||
}
|
||||
|
||||
public void setJobResult(String jobResult) {
|
||||
this.jobResult = jobResult;
|
||||
}
|
||||
|
||||
public String getJobInstanceType() {
|
||||
return jobInstanceType;
|
||||
}
|
||||
|
||||
public void setJobInstanceType(String jobInstanceType) {
|
||||
this.jobInstanceType = jobInstanceType;
|
||||
}
|
||||
|
||||
public Long getJobInstanceId() {
|
||||
return jobInstanceId;
|
||||
}
|
||||
|
||||
public void setJobInstanceId(Long jobInstanceId) {
|
||||
this.jobInstanceId = jobInstanceId;
|
||||
}
|
||||
|
||||
public Date getCreated() {
|
||||
return created;
|
||||
}
|
||||
|
||||
public void setCreated(Date created) {
|
||||
this.created = created;
|
||||
}
|
||||
}
|
||||
@ -32,6 +32,7 @@ import com.cloud.api.commands.EnableAccountCmd;
|
||||
import com.cloud.api.commands.EnableUserCmd;
|
||||
import com.cloud.api.commands.GetCloudIdentifierCmd;
|
||||
import com.cloud.api.commands.ListAlertsCmd;
|
||||
import com.cloud.api.commands.ListAsyncJobsCmd;
|
||||
import com.cloud.api.commands.LockAccountCmd;
|
||||
import com.cloud.api.commands.LockUserCmd;
|
||||
import com.cloud.api.commands.RebootSystemVmCmd;
|
||||
@ -1649,9 +1650,13 @@ public interface ManagementServer {
|
||||
AsyncJobResult queryAsyncJobResult(long jobId) throws PermissionDeniedException;
|
||||
AsyncJobVO findInstancePendingAsyncJob(String instanceType, long instanceId);
|
||||
AsyncJobVO findAsyncJobById(long jobId);
|
||||
|
||||
List<AsyncJobVO> searchForAsyncJobs(Criteria c);
|
||||
|
||||
|
||||
/**
|
||||
* Search for async jobs by account and/or startDate
|
||||
* @param cmd the command specifying the account and start date parameters
|
||||
* @return the list of async jobs that match the criteria
|
||||
*/
|
||||
List<AsyncJobVO> searchForAsyncJobs(ListAsyncJobsCmd cmd) throws InvalidParameterValueException, PermissionDeniedException;
|
||||
|
||||
/**
|
||||
* Assign a security group to a VM
|
||||
|
||||
@ -71,6 +71,7 @@ import com.cloud.api.commands.EnableAccountCmd;
|
||||
import com.cloud.api.commands.EnableUserCmd;
|
||||
import com.cloud.api.commands.GetCloudIdentifierCmd;
|
||||
import com.cloud.api.commands.ListAlertsCmd;
|
||||
import com.cloud.api.commands.ListAsyncJobsCmd;
|
||||
import com.cloud.api.commands.LockAccountCmd;
|
||||
import com.cloud.api.commands.LockUserCmd;
|
||||
import com.cloud.api.commands.PrepareForMaintenanceCmd;
|
||||
@ -7643,32 +7644,75 @@ public class ManagementServerImpl implements ManagementServer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AsyncJobVO> searchForAsyncJobs(Criteria c) {
|
||||
Filter searchFilter = new Filter(AsyncJobVO.class, c.getOrderBy(), c
|
||||
.getAscending(), c.getOffset(), c.getLimit());
|
||||
public List<AsyncJobVO> searchForAsyncJobs(ListAsyncJobsCmd cmd) throws InvalidParameterValueException, PermissionDeniedException {
|
||||
Integer pageSize = cmd.getPageSize();
|
||||
Integer page = cmd.getPage();
|
||||
Long startIndex = Long.valueOf(0);
|
||||
int pageSizeNum = 50;
|
||||
if (pageSize != null) {
|
||||
pageSizeNum = pageSize.intValue();
|
||||
}
|
||||
if (page != null) {
|
||||
int pageNum = page.intValue();
|
||||
if (pageNum > 0) {
|
||||
startIndex = Long.valueOf(pageSizeNum * (pageNum-1));
|
||||
}
|
||||
}
|
||||
|
||||
Filter searchFilter = new Filter(AsyncJobVO.class, "id", true, startIndex, Long.valueOf(pageSizeNum));
|
||||
SearchBuilder<AsyncJobVO> sb = _jobDao.createSearchBuilder();
|
||||
|
||||
Object accountId = null;
|
||||
Long domainId = cmd.getDomainId();
|
||||
Account account = (Account)UserContext.current().getAccountObject();
|
||||
if ((account == null) || isAdmin(account.getType())) {
|
||||
String accountName = cmd.getAccountName();
|
||||
|
||||
if ((accountName != null) && (domainId != null)) {
|
||||
Account userAccount = _accountDao.findActiveAccount(accountName, domainId);
|
||||
if (userAccount != null) {
|
||||
accountId = userAccount.getId();
|
||||
} else {
|
||||
throw new InvalidParameterValueException("Failed to list async jobs for account " + accountName + " in domain " + domainId + "; account not found.");
|
||||
}
|
||||
} else if (domainId != null) {
|
||||
if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(), domainId)) {
|
||||
throw new PermissionDeniedException("Failed to list async jobs for domain " + domainId + "; permission denied.");
|
||||
}
|
||||
|
||||
// we can do a domain match for the admin case
|
||||
SearchBuilder<DomainVO> domainSearch = _domainDao.createSearchBuilder();
|
||||
domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE);
|
||||
|
||||
SearchBuilder<AccountVO> accountSearch = _accountDao.createSearchBuilder();
|
||||
accountSearch.join("domainSearch", domainSearch, accountSearch.entity().getDomainId(), domainSearch.entity().getId());
|
||||
|
||||
sb.join("accountSearch", accountSearch, sb.entity().getAccountId(), accountSearch.entity().getId());
|
||||
}
|
||||
} else {
|
||||
accountId = account.getId();
|
||||
}
|
||||
|
||||
Object keyword = cmd.getKeyword();
|
||||
Object startDate = cmd.getStartDate();
|
||||
|
||||
|
||||
SearchCriteria<AsyncJobVO> sc = _jobDao.createSearchCriteria();
|
||||
|
||||
Object accountId = c.getCriteria(Criteria.ACCOUNTID);
|
||||
Object status = c.getCriteria(Criteria.STATUS);
|
||||
Object keyword = c.getCriteria(Criteria.KEYWORD);
|
||||
Object startDate = c.getCriteria(Criteria.STARTDATE);
|
||||
|
||||
if (keyword != null) {
|
||||
sc.addAnd("cmd", SearchCriteria.Op.LIKE, "%" + keyword+ "%");
|
||||
sc.addAnd("cmd", SearchCriteria.Op.LIKE, "%" + keyword + "%");
|
||||
}
|
||||
|
||||
if (accountId != null) {
|
||||
sc.addAnd("accountId", SearchCriteria.Op.EQ, accountId);
|
||||
} else if (domainId != null) {
|
||||
DomainVO domain = _domainDao.findById(domainId);
|
||||
sc.setJoinParameters("domainSearch", "path", domain.getPath() + "%");
|
||||
}
|
||||
|
||||
if(status != null) {
|
||||
sc.addAnd("status", SearchCriteria.Op.EQ, status);
|
||||
}
|
||||
|
||||
if(startDate != null) {
|
||||
if (startDate != null) {
|
||||
sc.addAnd("created", SearchCriteria.Op.GTEQ, startDate);
|
||||
}
|
||||
|
||||
|
||||
return _jobDao.search(sc, searchFilter);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user