mirror of https://github.com/apache/cloudstack.git
bug 11311: change list vm call to provide less info
This commit is contained in:
parent
a572205e3c
commit
07e44e3a23
|
|
@ -129,9 +129,11 @@ public interface ResponseGenerator {
|
|||
SnapshotResponse createSnapshotResponse(Snapshot snapshot);
|
||||
|
||||
SnapshotPolicyResponse createSnapshotPolicyResponse(SnapshotPolicy policy);
|
||||
|
||||
|
||||
List<UserVmResponse> createUserVmResponse(String objectName, UserVm... userVms);
|
||||
|
||||
List<UserVmResponse> createUserVmResponse(String objectName, int details, UserVm... userVms);
|
||||
|
||||
SystemVmResponse createSystemVmResponse(VirtualMachine systemVM);
|
||||
|
||||
DomainRouterResponse createDomainRouterResponse(VirtualRouter router);
|
||||
|
|
|
|||
|
|
@ -84,6 +84,9 @@ public class ListVMsCmd extends BaseListCmd {
|
|||
|
||||
@Parameter(name=ApiConstants.PROJECT_ID, type=CommandType.LONG, description="list vms by project")
|
||||
private Long projectId;
|
||||
|
||||
@Parameter(name=ApiConstants.DETAILS, type=CommandType.INTEGER, description="bits for querying required vm details")
|
||||
private Integer details_mask;
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
/////////////////// Accessors ///////////////////////
|
||||
|
|
@ -153,6 +156,9 @@ public class ListVMsCmd extends BaseListCmd {
|
|||
return projectId;
|
||||
}
|
||||
|
||||
public int getDetailsMask() {
|
||||
return details_mask == null ? 0 : details_mask;
|
||||
}
|
||||
/////////////////////////////////////////////////////
|
||||
/////////////// API Implementation///////////////////
|
||||
/////////////////////////////////////////////////////
|
||||
|
|
@ -169,7 +175,7 @@ public class ListVMsCmd extends BaseListCmd {
|
|||
public void execute(){
|
||||
List<? extends UserVm> result = _userVmService.searchForUserVMs(this);
|
||||
ListResponse<UserVmResponse> response = new ListResponse<UserVmResponse>();
|
||||
List<UserVmResponse> vmResponses = _responseGenerator.createUserVmResponse("virtualmachine", result.toArray(new UserVm[result.size()]));
|
||||
List<UserVmResponse> vmResponses = _responseGenerator.createUserVmResponse("virtualmachine", getDetailsMask(), result.toArray(new UserVm[result.size()]));
|
||||
response.setResponses(vmResponses);
|
||||
response.setResponseName(getCommandName());
|
||||
this.setResponseObject(response);
|
||||
|
|
|
|||
|
|
@ -638,8 +638,8 @@ public class ApiDBUtils {
|
|||
return _firewallCidrsDao.getSourceCidrs(id);
|
||||
}
|
||||
|
||||
public static Hashtable<Long, UserVmData> listVmDetails(Hashtable<Long, UserVmData> vmData){
|
||||
return _userVmDao.listVmDetails(vmData);
|
||||
public static Hashtable<Long, UserVmData> listVmDetails(Hashtable<Long, UserVmData> vmData, int details){
|
||||
return _userVmDao.listVmDetails(vmData, details);
|
||||
}
|
||||
|
||||
public static Account getProjectOwner(long projectId) {
|
||||
|
|
|
|||
|
|
@ -1128,9 +1128,15 @@ public class ApiResponseHelper implements ResponseGenerator {
|
|||
response.setObjectName("ipforwardingrule");
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<UserVmResponse> createUserVmResponse(String objectName, UserVm... userVms) {
|
||||
return createUserVmResponse(objectName, 0, userVms);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserVmResponse> createUserVmResponse(String objectName, int details, UserVm... userVms) {
|
||||
Account caller = UserContext.current().getCaller();
|
||||
boolean caller_is_admin = ((caller == null) || (caller.getType() == Account.ACCOUNT_TYPE_ADMIN));
|
||||
|
||||
|
|
@ -1141,7 +1147,7 @@ public class ApiResponseHelper implements ResponseGenerator {
|
|||
vmDataList.put(userVm.getId(), userVmData);
|
||||
}
|
||||
|
||||
vmDataList = ApiDBUtils.listVmDetails(vmDataList);
|
||||
vmDataList = ApiDBUtils.listVmDetails(vmDataList, details);
|
||||
|
||||
//initialize vmresponse from vmdatalist
|
||||
List<UserVmResponse> vmResponses = new ArrayList<UserVmResponse>();
|
||||
|
|
|
|||
|
|
@ -72,5 +72,5 @@ public interface UserVmDao extends GenericDao<UserVmVO, Long> {
|
|||
List<Long> listPodIdsHavingVmsforAccount(long zoneId, long accountId);
|
||||
public Long countAllocatedVMsForAccount(long accountId);
|
||||
|
||||
Hashtable<Long, UserVmData> listVmDetails(Hashtable<Long, UserVmData> userVmData);
|
||||
Hashtable<Long, UserVmData> listVmDetails(Hashtable<Long, UserVmData> userVmData, int details);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -75,33 +75,7 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
|
|||
"GROUP BY pod_id HAVING count(id) > 0 ORDER BY count(id) DESC";
|
||||
|
||||
private static final int VM_DETAILS_BATCH_SIZE=100;
|
||||
private static final String VM_DETAILS = "select vm_instance.id, " +
|
||||
"account.id, account.account_name, account.type, domain.name, instance_group.id, instance_group.name," +
|
||||
"data_center.id, data_center.name, data_center.is_security_group_enabled, host.id, host.name, " +
|
||||
"vm_template.id, vm_template.name, vm_template.display_text, iso.id, iso.name, " +
|
||||
"vm_template.enable_password, service_offering.id, disk_offering.name, storage_pool.id, storage_pool.pool_type, " +
|
||||
"service_offering.cpu, service_offering.speed, service_offering.ram_size, volumes.id, volumes.device_id, volumes.volume_type, security_group.id, security_group.name, " +
|
||||
"security_group.description, nics.id, nics.ip4_address, nics.gateway, nics.network_id, nics.netmask, nics.mac_address, nics.broadcast_uri, nics.isolation_uri, " +
|
||||
"networks.traffic_type, networks.guest_type, networks.is_default from vm_instance " +
|
||||
"left join account on vm_instance.account_id=account.id " +
|
||||
"left join domain on vm_instance.domain_id=domain.id " +
|
||||
"left join instance_group_vm_map on vm_instance.id=instance_group_vm_map.instance_id " +
|
||||
"left join instance_group on instance_group_vm_map.group_id=instance_group.id " +
|
||||
"left join data_center on vm_instance.data_center_id=data_center.id " +
|
||||
"left join host on vm_instance.host_id=host.id " +
|
||||
"left join vm_template on vm_instance.vm_template_id=vm_template.id " +
|
||||
"left join user_vm on vm_instance.id=user_vm.id " +
|
||||
"left join vm_template iso on iso.id=user_vm.iso_id " +
|
||||
"left join service_offering on vm_instance.service_offering_id=service_offering.id " +
|
||||
"left join disk_offering on vm_instance.service_offering_id=disk_offering.id " +
|
||||
"left join volumes on vm_instance.id=volumes.instance_id " +
|
||||
"left join storage_pool on volumes.pool_id=storage_pool.id " +
|
||||
"left join security_group_vm_map on vm_instance.id=security_group_vm_map.instance_id " +
|
||||
"left join security_group on security_group_vm_map.security_group_id=security_group.id " +
|
||||
"left join nics on vm_instance.id=nics.instance_id " +
|
||||
"left join networks on nics.network_id=networks.id " +
|
||||
"where vm_instance.id in (";
|
||||
|
||||
|
||||
protected final UserVmDetailsDaoImpl _detailsDao = ComponentLocator.inject(UserVmDetailsDaoImpl.class);
|
||||
protected final NicDaoImpl _nicDao = ComponentLocator.inject(NicDaoImpl.class);
|
||||
|
||||
|
|
@ -344,9 +318,11 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
|
|||
}
|
||||
|
||||
@Override
|
||||
public Hashtable<Long, UserVmData> listVmDetails(Hashtable<Long, UserVmData> userVmDataHash){
|
||||
public Hashtable<Long, UserVmData> listVmDetails(Hashtable<Long, UserVmData> userVmDataHash, int details){
|
||||
Transaction txn = Transaction.currentTxn();
|
||||
PreparedStatement pstmt = null;
|
||||
DetailSql sql = new DetailSql();
|
||||
|
||||
|
||||
try {
|
||||
int curr_index=0;
|
||||
|
|
@ -354,7 +330,7 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
|
|||
List<UserVmData> userVmDataList = new ArrayList(userVmDataHash.values());
|
||||
|
||||
if (userVmDataList.size() > VM_DETAILS_BATCH_SIZE){
|
||||
pstmt = txn.prepareStatement(VM_DETAILS + getQueryBatchAppender(VM_DETAILS_BATCH_SIZE));
|
||||
pstmt = txn.prepareStatement(sql.getSql(details) + getQueryBatchAppender(VM_DETAILS_BATCH_SIZE));
|
||||
while ( (curr_index + VM_DETAILS_BATCH_SIZE) <= userVmDataList.size()){
|
||||
// set the vars value
|
||||
for (int k=1,j=curr_index;j<curr_index+VM_DETAILS_BATCH_SIZE;j++,k++){
|
||||
|
|
@ -380,7 +356,7 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
|
|||
|
||||
if (curr_index < userVmDataList.size()){
|
||||
int batch_size = (userVmDataList.size() - curr_index);
|
||||
pstmt = txn.prepareStatement(VM_DETAILS + getQueryBatchAppender(batch_size));
|
||||
pstmt = txn.prepareStatement(sql.getSql(details) + getQueryBatchAppender(batch_size));
|
||||
// set the vars value
|
||||
for (int k=1,j=curr_index;j<curr_index+batch_size;j++,k++){
|
||||
pstmt.setLong(k, userVmDataList.get(j).getId());
|
||||
|
|
@ -403,9 +379,9 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
|
|||
if (pstmt!=null)pstmt.close();
|
||||
return userVmDataHash;
|
||||
} catch (SQLException e) {
|
||||
throw new CloudRuntimeException("DB Exception on: " + VM_DETAILS, e);
|
||||
throw new CloudRuntimeException("DB Exception on: " + sql.getSql(details), e);
|
||||
} catch (Throwable e) {
|
||||
throw new CloudRuntimeException("Caught: " + VM_DETAILS, e);
|
||||
throw new CloudRuntimeException("Caught: " + sql.getSql(details), e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -540,4 +516,47 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
|
|||
sc.setParameters("state", new Object[] {State.Destroyed, State.Error, State.Expunging});
|
||||
return customSearch(sc, null).get(0);
|
||||
}
|
||||
|
||||
|
||||
public static class DetailSql {
|
||||
private String VM_DETAILS = "select vm_instance.id, " +
|
||||
"account.id, account.account_name, account.type, domain.name, instance_group.id, instance_group.name," +
|
||||
"data_center.id, data_center.name, data_center.is_security_group_enabled, host.id, host.name, " +
|
||||
"vm_template.id, vm_template.name, vm_template.display_text, iso.id, iso.name, " +
|
||||
"vm_template.enable_password, service_offering.id, disk_offering.name, storage_pool.id, storage_pool.pool_type, " +
|
||||
"service_offering.cpu, service_offering.speed, service_offering.ram_size, volumes.id, volumes.device_id, volumes.volume_type, security_group.id, security_group.name, " +
|
||||
"security_group.description, nics.id, nics.ip4_address, nics.gateway, nics.network_id, nics.netmask, nics.mac_address, nics.broadcast_uri, nics.isolation_uri, " +
|
||||
"networks.traffic_type, networks.guest_type, networks.is_default from vm_instance " +
|
||||
"left join account on vm_instance.account_id=account.id " +
|
||||
"left join domain on vm_instance.domain_id=domain.id " +
|
||||
"left join instance_group_vm_map on vm_instance.id=instance_group_vm_map.instance_id " +
|
||||
"left join instance_group on instance_group_vm_map.group_id=instance_group.id " +
|
||||
"left join data_center on vm_instance.data_center_id=data_center.id " +
|
||||
"left join host on vm_instance.host_id=host.id " +
|
||||
"left join vm_template on vm_instance.vm_template_id=vm_template.id " +
|
||||
"left join user_vm on vm_instance.id=user_vm.id " +
|
||||
"left join vm_template iso on iso.id=user_vm.iso_id " +
|
||||
"left join service_offering on vm_instance.service_offering_id=service_offering.id " +
|
||||
"left join disk_offering on vm_instance.service_offering_id=disk_offering.id " +
|
||||
"left join volumes on vm_instance.id=volumes.instance_id " +
|
||||
"left join storage_pool on volumes.pool_id=storage_pool.id " +
|
||||
"left join security_group_vm_map on vm_instance.id=security_group_vm_map.instance_id " +
|
||||
"left join security_group on security_group_vm_map.security_group_id=security_group.id " +
|
||||
"left join nics on vm_instance.id=nics.instance_id " +
|
||||
"left join networks on nics.network_id=networks.id " +
|
||||
"where vm_instance.id in (";
|
||||
|
||||
public DetailSql(){
|
||||
|
||||
}
|
||||
|
||||
public String getSql(int details){
|
||||
return VM_DETAILS;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@ public class UserVmData {
|
|||
public SecurityGroupData newSecurityGroupData(){
|
||||
return new SecurityGroupData();
|
||||
}
|
||||
|
||||
|
||||
public String getHypervisor() {
|
||||
return hypervisor;
|
||||
}
|
||||
|
|
@ -676,5 +676,110 @@ public class UserVmData {
|
|||
public void setAccountId(long accountId) {
|
||||
this.accountId = accountId;
|
||||
}
|
||||
|
||||
|
||||
public class DetailsMask {
|
||||
public final static int FULL_DETAILS=0;
|
||||
|
||||
public final static int NICS_DETAILS=1;
|
||||
public final static int STATISTICS_DETAILS=2;
|
||||
public final static int SECURITY_GROUP_DETAILS=4;
|
||||
public final static int TEMPLATE_DETAILS=8;
|
||||
|
||||
public final static int SERVICE_OFFERING_DETAILS=16;
|
||||
public final static int ISO_DETAILS=32;
|
||||
public final static int VOLUME_DETAILS=64;
|
||||
|
||||
int _mask;
|
||||
|
||||
public DetailsMask(int i){
|
||||
_mask = i;
|
||||
}
|
||||
|
||||
private final String[] strings = new String[] {
|
||||
"nics", "stats", "secgrp", "tmpl",
|
||||
"servoff", "iso", "volume"
|
||||
};
|
||||
|
||||
private final int[] values = new int[] {
|
||||
1, 2, 4, 8,
|
||||
16, 32, 64, 128,
|
||||
256, 512, 1024, 2048,
|
||||
4096, 8192, 16384, 32768,
|
||||
65536, 131072, 262144, 524288
|
||||
};
|
||||
|
||||
public int intValue(String str) {
|
||||
for (int i = 0; i < strings.length; i++) {
|
||||
if (str.equals(strings[i])) {
|
||||
return values[i];
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public String stringValue(int mvId) {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
if (mvId == values[i]) {
|
||||
return strings[i];
|
||||
}
|
||||
else if ( (mvId & values[i]) == values[i]) {
|
||||
buf.append(strings[i]).append("|");
|
||||
}
|
||||
}
|
||||
if (buf.length() == 0) {
|
||||
return "";
|
||||
}
|
||||
return buf.deleteCharAt(buf.length() - 1).toString();
|
||||
}
|
||||
|
||||
public boolean idValid(int mvId) {
|
||||
if (mvId > 128 || mvId < 0) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public boolean isAllDetails(){
|
||||
return (_mask == 0);
|
||||
}
|
||||
|
||||
|
||||
public boolean isNicsDetails(){
|
||||
return (_mask & NICS_DETAILS ) > 0;
|
||||
}
|
||||
|
||||
public boolean isStatsDetails(){
|
||||
return (_mask & STATISTICS_DETAILS ) > 0;
|
||||
}
|
||||
|
||||
public boolean isSecurityGroupDetails(){
|
||||
return (_mask & STATISTICS_DETAILS ) > 0;
|
||||
}
|
||||
|
||||
public boolean isTemplateDetails(){
|
||||
return (_mask & TEMPLATE_DETAILS ) > 0;
|
||||
}
|
||||
|
||||
public boolean isServiceOfferingDetails(){
|
||||
return (_mask & SERVICE_OFFERING_DETAILS ) > 0;
|
||||
}
|
||||
|
||||
public boolean isIsoDetails(){
|
||||
return (_mask & ISO_DETAILS ) > 0;
|
||||
}
|
||||
|
||||
public boolean isVolumeDetails(){
|
||||
return (_mask & VOLUME_DETAILS ) > 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue