bug 11311: vm summary command initial impl

This commit is contained in:
Abhinandan Prateek 2011-10-28 14:07:19 +05:30
parent 3cb1426a6c
commit f456462443
6 changed files with 336 additions and 0 deletions

View File

@ -131,6 +131,8 @@ public interface ResponseGenerator {
SnapshotPolicyResponse createSnapshotPolicyResponse(SnapshotPolicy policy);
List<UserVmResponse> createUserVmResponse(String objectName, UserVm... userVms);
List<UserVmResponse> createUserVmSummaryResponse(String objectName, UserVm... userVms);
SystemVmResponse createSystemVmResponse(VirtualMachine systemVM);

View File

@ -0,0 +1,178 @@
/**
* Copyright (C) 2011 Citrix, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.api.commands;
import java.util.List;
import org.apache.log4j.Logger;
import com.cloud.api.ApiConstants;
import com.cloud.api.BaseListCmd;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.response.ListResponse;
import com.cloud.api.response.UserVmResponse;
import com.cloud.async.AsyncJob;
import com.cloud.uservm.UserVm;
@Implementation(description="List summary of the virtual machines owned by the account.", responseObject=UserVmResponse.class)
public class VMsSummaryCmd extends BaseListCmd {
public static final Logger s_logger = Logger.getLogger(VMsSummaryCmd.class.getName());
private static final String s_name = "listvirtualmachinessummaryresponse";
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
@Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="account. Must be used with the domainId parameter.")
private String accountName;
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="the domain ID. If used with the account parameter, lists virtual machines for the specified account in this domain.")
private Long domainId;
@Parameter(name=ApiConstants.IS_RECURSIVE, type=CommandType.BOOLEAN, description="Must be used with domainId parameter. Defaults to false, but if true, lists all vms from the parent specified by the domain id till leaves.")
private Boolean recursive;
@Parameter(name=ApiConstants.GROUP_ID, type=CommandType.LONG, description="the group ID")
private Long groupId;
@Parameter(name=ApiConstants.HOST_ID, type=CommandType.LONG, description="the host ID")
private Long hostId;
@Parameter(name=ApiConstants.ID, type=CommandType.LONG, description="the ID of the virtual machine")
private Long id;
@Parameter(name=ApiConstants.NAME, type=CommandType.STRING, description="name of the virtual machine")
private String instanceName;
@Parameter(name=ApiConstants.POD_ID, type=CommandType.LONG, description="the pod ID")
private Long podId;
@Parameter(name=ApiConstants.STATE, type=CommandType.STRING, description="state of the virtual machine")
private String state;
@Parameter(name=ApiConstants.ZONE_ID, type=CommandType.LONG, description="the availability zone ID")
private Long zoneId;
@Parameter(name=ApiConstants.FOR_VIRTUAL_NETWORK, type=CommandType.BOOLEAN, description="list by network type; true if need to list vms using Virtual Network, false otherwise")
private Boolean forVirtualNetwork;
@Parameter(name=ApiConstants.NETWORK_ID, type=CommandType.LONG, description="list by network id")
private Long networkId;
@Parameter(name=ApiConstants.HYPERVISOR, type=CommandType.STRING, description="the target hypervisor for the template")
private String hypervisor;
@Parameter(name=ApiConstants.STORAGE_ID, type=CommandType.LONG, description="the storage ID where vm's volumes belong to")
private Long storageId;
@Parameter(name=ApiConstants.PROJECT_ID, type=CommandType.LONG, description="list vms by project")
private Long projectId;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
public String getAccountName() {
return accountName;
}
public Long getDomainId() {
return domainId;
}
public Long getGroupId() {
return groupId;
}
public Long getHostId() {
return hostId;
}
public Long getId() {
return id;
}
public String getInstanceName() {
return instanceName;
}
public Long getPodId() {
return podId;
}
public String getState() {
return state;
}
public Long getZoneId() {
return zoneId;
}
public Boolean getForVirtualNetwork() {
return forVirtualNetwork;
}
public void setForVirtualNetwork(Boolean forVirtualNetwork) {
this.forVirtualNetwork = forVirtualNetwork;
}
public Long getNetworkId() {
return networkId;
}
public Boolean isRecursive() {
return recursive;
}
public String getHypervisor() {
return hypervisor;
}
public Long getStorageId() {
return storageId;
}
public Long getProjectId() {
return projectId;
}
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@Override
public String getCommandName() {
return s_name;
}
public AsyncJob.Type getInstanceType() {
return AsyncJob.Type.VirtualMachine;
}
@Override
public void execute(){
List<? extends UserVm> result = _userVmService.searchForUserVMs(this);
ListResponse<UserVmResponse> response = new ListResponse<UserVmResponse>();
List<UserVmResponse> vmResponses = _responseGenerator.createUserVmSummaryResponse("virtualmachine", result.toArray(new UserVm[result.size()]));
response.setResponses(vmResponses);
response.setResponseName(getCommandName());
this.setResponseObject(response);
}
}

View File

@ -38,6 +38,7 @@ import com.cloud.api.commands.RestoreVMCmd;
import com.cloud.api.commands.StartVMCmd;
import com.cloud.api.commands.UpdateVMCmd;
import com.cloud.api.commands.UpgradeVMCmd;
import com.cloud.api.commands.VMsSummaryCmd;
import com.cloud.dc.DataCenter;
import com.cloud.exception.ConcurrentOperationException;
import com.cloud.exception.InsufficientCapacityException;
@ -349,6 +350,7 @@ public interface UserVmService {
* @return List of UserVMs.
*/
List<? extends UserVm> searchForUserVMs(ListVMsCmd cmd);
List<? extends UserVm> searchForUserVMs(VMsSummaryCmd cmd);
HypervisorType getHypervisorTypeOfUserVM(long vmid);

View File

@ -46,6 +46,7 @@ changeServiceForVirtualMachine=com.cloud.api.commands.UpgradeVMCmd;15
updateVirtualMachine=com.cloud.api.commands.UpdateVMCmd;15
recoverVirtualMachine=com.cloud.api.commands.RecoverVMCmd;7
listVirtualMachines=com.cloud.api.commands.ListVMsCmd;15
listVMSummary=com.cloud.api.commands.VMsSummaryCmd;15
getVMPassword=com.cloud.api.commands.GetVMPasswordCmd;15
migrateVirtualMachine=com.cloud.api.commands.MigrateVMCmd;1
moveVirtualMachine=com.cloud.api.commands.MoveUserVMCmd;15

View File

@ -1169,6 +1169,44 @@ public class ApiResponseHelper implements ResponseGenerator {
}
return vmResponses;
}
@Override
public List<UserVmResponse> createUserVmSummaryResponse(String objectName, UserVm... userVms) {
Account caller = UserContext.current().getCaller();
boolean caller_is_admin = ((caller == null) || (caller.getType() == Account.ACCOUNT_TYPE_ADMIN));
//initialize vmresponse from vmdatalist
List<UserVmResponse> vmResponses = new ArrayList<UserVmResponse>();
for (UserVm userVm : userVms) {
UserVmResponse userVmResponse = new UserVmResponse();
userVmResponse.setHypervisor(userVm.getHypervisorType().toString());
userVmResponse.setId(userVm.getId());
userVmResponse.setName(userVm.getInstanceName());
userVmResponse.setDisplayName(userVm.getDisplayName());
userVmResponse.setIpAddress(userVm.getPrivateIpAddress());
userVmResponse.setHaEnable(userVm.isHaEnabled());
populateAccount(userVmResponse, userVm.getAccountId());
populateDomain(userVmResponse, userVm.getDomainId());
userVmResponse.setCreated(userVm.getCreated());
userVmResponse.setState(userVm.getState().toString());
userVmResponse.setZoneId(userVm.getDataCenterIdToDeployIn());
if (caller_is_admin){
userVmResponse.setHostId(userVm.getHostId());
//userVmResponse.setHostName(userVm.getHostName());
}
userVmResponse.setTemplateId(userVm.getTemplateId());
userVmResponse.setIsoId(userVm.getIsoId());
userVmResponse.setServiceOfferingId(userVm.getServiceOfferingId());
userVmResponse.setPassword(userVm.getPassword());
userVmResponse.setObjectName(objectName);
vmResponses.add(userVmResponse);
}
return vmResponses;
}
@Override

View File

@ -71,6 +71,7 @@ import com.cloud.api.commands.RestoreVMCmd;
import com.cloud.api.commands.StartVMCmd;
import com.cloud.api.commands.UpdateVMCmd;
import com.cloud.api.commands.UpgradeVMCmd;
import com.cloud.api.commands.VMsSummaryCmd;
import com.cloud.async.AsyncJobExecutor;
import com.cloud.async.AsyncJobManager;
import com.cloud.async.AsyncJobVO;
@ -2884,6 +2885,119 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
throw new CloudRuntimeException("Failed to destroy vm with id " + vmId);
}
}
@Override
public List<UserVmVO> searchForUserVMs(VMsSummaryCmd cmd) {
Account caller = UserContext.current().getCaller();
Long domainId = cmd.getDomainId();
String accountName = cmd.getAccountName();
Boolean isRecursive = cmd.isRecursive();
String hypervisor = cmd.getHypervisor();
List<Long> permittedAccounts = new ArrayList<Long>();
String path = null;
Long projectId = cmd.getProjectId();
if (isRecursive != null && isRecursive && domainId == null) {
throw new InvalidParameterValueException("Please enter a parent domain id for listing vms recursively");
}
if (domainId != null) {
// Verify if user is authorized to see instances belonging to the domain
DomainVO domain = _domainDao.findById(domainId);
if (domain == null) {
throw new InvalidParameterValueException("Domain id=" + domainId + " doesn't exist");
}
_accountMgr.checkAccess(caller, domain);
}
boolean isAdmin = false;
if (_accountMgr.isAdmin(caller.getType())) {
isAdmin = true;
if (accountName != null && domainId != null) {
Account account = _accountDao.findActiveAccount(accountName, domainId);
if (account == null) {
throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId);
}
permittedAccounts.add(caller.getId());
}
if (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) {
if (isRecursive == null) {
DomainVO domain = _domainDao.findById(caller.getDomainId());
path = domain.getPath();
}
}
} else {
//regular user can't specify any other domain rather than his own
if (domainId != null && domainId.longValue() != caller.getDomainId()) {
throw new PermissionDeniedException("Caller is not authorised to see domain id=" + domainId + " entries");
}
permittedAccounts.add(caller.getId());
}
if (isRecursive != null && isRecursive && isAdmin) {
if (isRecursive) {
DomainVO domain = _domainDao.findById(domainId);
path = domain.getPath();
domainId = null;
}
}
//set project information
if (projectId != null) {
permittedAccounts.clear();
Project project = _projectMgr.getProject(projectId);
if (project == null) {
throw new InvalidParameterValueException("Unable to find project by id " + projectId);
}
if (!_projectMgr.canAccessProjectAccount(caller, project.getProjectAccountId())) {
throw new InvalidParameterValueException("Account " + caller + " can't access project id=" + projectId);
}
permittedAccounts.add(project.getProjectAccountId());
} else {
permittedAccounts.addAll(_projectMgr.listPermittedProjectAccounts(caller.getId()));
}
Criteria c = new Criteria("id", Boolean.TRUE, cmd.getStartIndex(), cmd.getPageSizeVal());
c.addCriteria(Criteria.KEYWORD, cmd.getKeyword());
c.addCriteria(Criteria.ID, cmd.getId());
c.addCriteria(Criteria.NAME, cmd.getInstanceName());
c.addCriteria(Criteria.STATE, cmd.getState());
c.addCriteria(Criteria.DATACENTERID, cmd.getZoneId());
c.addCriteria(Criteria.GROUPID, cmd.getGroupId());
c.addCriteria(Criteria.FOR_VIRTUAL_NETWORK, cmd.getForVirtualNetwork());
c.addCriteria(Criteria.NETWORKID, cmd.getNetworkId());
if (domainId != null) {
c.addCriteria(Criteria.DOMAINID, domainId);
}
if (path != null) {
c.addCriteria(Criteria.PATH, path);
}
if (HypervisorType.getType(hypervisor) != HypervisorType.None) {
c.addCriteria(Criteria.HYPERVISOR, hypervisor);
} else if (hypervisor != null) {
throw new InvalidParameterValueException("Invalid HypervisorType " + hypervisor);
}
// ignore these search requests if it's not an admin
if (isAdmin) {
c.addCriteria(Criteria.PODID, cmd.getPodId());
c.addCriteria(Criteria.HOSTID, cmd.getHostId());
c.addCriteria(Criteria.STORAGE_ID, cmd.getStorageId());
}
if (!permittedAccounts.isEmpty()) {
c.addCriteria(Criteria.ACCOUNTID, permittedAccounts.toArray());
}
c.addCriteria(Criteria.ISADMIN, isAdmin);
return searchForUserVMs(c);
}
@Override
public List<UserVmVO> searchForUserVMs(ListVMsCmd cmd) {
@ -3479,4 +3593,5 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager
s_logger.debug("Restore VM " + vmId + " with template " + root.getTemplateId() + " successfully");
return vm;
}
}