Search in sources :

Example 1 with UserVmJoinVO

use of com.cloud.api.query.vo.UserVmJoinVO in project cloudstack by apache.

the class UserVmJoinDaoImpl method newUserVmView.

@Override
public List<UserVmJoinVO> newUserVmView(UserVm... userVms) {
    Hashtable<Long, UserVm> userVmDataHash = new Hashtable<Long, UserVm>();
    for (UserVm vm : userVms) {
        if (!userVmDataHash.containsKey(vm.getId())) {
            userVmDataHash.put(vm.getId(), vm);
        }
    }
    Set<Long> vmIdSet = userVmDataHash.keySet();
    List<UserVmJoinVO> uvms = searchByIds(vmIdSet.toArray(new Long[vmIdSet.size()]));
    // populate transit password field from UserVm
    if (uvms != null) {
        for (UserVmJoinVO uvm : uvms) {
            UserVm v = userVmDataHash.get(uvm.getId());
            uvm.setPassword(v.getPassword());
        }
    }
    return uvms;
}
Also used : UserVm(com.cloud.uservm.UserVm) Hashtable(java.util.Hashtable) UserVmJoinVO(com.cloud.api.query.vo.UserVmJoinVO)

Example 2 with UserVmJoinVO

use of com.cloud.api.query.vo.UserVmJoinVO in project cloudstack by apache.

the class QueryManagerImpl method searchForUserVMsInternal.

private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsInternal(ListVMsCmd cmd) {
    Account caller = CallContext.current().getCallingAccount();
    List<Long> permittedAccounts = new ArrayList<Long>();
    boolean listAll = cmd.listAll();
    Long id = cmd.getId();
    Long userId = cmd.getUserId();
    Map<String, String> tags = cmd.getTags();
    Boolean display = cmd.getDisplay();
    Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
    _accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, listAll, false);
    Long domainId = domainIdRecursiveListProject.first();
    Boolean isRecursive = domainIdRecursiveListProject.second();
    ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
    Filter searchFilter = new Filter(UserVmJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
    List<Long> ids = null;
    if (cmd.getId() != null) {
        if (cmd.getIds() != null && !cmd.getIds().isEmpty()) {
            throw new InvalidParameterValueException("Specify either id or ids but not both parameters");
        }
        ids = new ArrayList<Long>();
        ids.add(cmd.getId());
    } else {
        ids = cmd.getIds();
    }
    // first search distinct vm id by using query criteria and pagination
    SearchBuilder<UserVmJoinVO> sb = _userVmJoinDao.createSearchBuilder();
    // select distinct ids
    sb.select(null, Func.DISTINCT, sb.entity().getId());
    _accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    String hypervisor = cmd.getHypervisor();
    Object name = cmd.getName();
    String state = cmd.getState();
    Object zoneId = cmd.getZoneId();
    Object keyword = cmd.getKeyword();
    boolean isAdmin = false;
    boolean isRootAdmin = false;
    if (_accountMgr.isAdmin(caller.getId())) {
        isAdmin = true;
    }
    if (_accountMgr.isRootAdmin(caller.getId())) {
        isRootAdmin = true;
    }
    Object groupId = cmd.getGroupId();
    Object networkId = cmd.getNetworkId();
    if (HypervisorType.getType(hypervisor) == HypervisorType.None && hypervisor != null) {
        // invalid hypervisor type input
        throw new InvalidParameterValueException("Invalid HypervisorType " + hypervisor);
    }
    Object templateId = cmd.getTemplateId();
    Object isoId = cmd.getIsoId();
    Object vpcId = cmd.getVpcId();
    Object affinityGroupId = cmd.getAffinityGroupId();
    Object keyPairName = cmd.getKeyPairName();
    Object serviceOffId = cmd.getServiceOfferingId();
    Object pod = null;
    Object hostId = null;
    Object storageId = null;
    if (cmd instanceof ListVMsCmdByAdmin) {
        ListVMsCmdByAdmin adCmd = (ListVMsCmdByAdmin) cmd;
        pod = adCmd.getPodId();
        hostId = adCmd.getHostId();
        storageId = adCmd.getStorageId();
    }
    sb.and("displayName", sb.entity().getDisplayName(), SearchCriteria.Op.LIKE);
    sb.and("idIN", sb.entity().getId(), SearchCriteria.Op.IN);
    sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
    sb.and("stateEQ", sb.entity().getState(), SearchCriteria.Op.EQ);
    sb.and("stateNEQ", sb.entity().getState(), SearchCriteria.Op.NEQ);
    sb.and("stateNIN", sb.entity().getState(), SearchCriteria.Op.NIN);
    sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
    sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
    sb.and("hypervisorType", sb.entity().getHypervisorType(), SearchCriteria.Op.EQ);
    sb.and("hostIdEQ", sb.entity().getHostId(), SearchCriteria.Op.EQ);
    sb.and("templateId", sb.entity().getTemplateId(), SearchCriteria.Op.EQ);
    sb.and("isoId", sb.entity().getIsoId(), SearchCriteria.Op.EQ);
    sb.and("instanceGroupId", sb.entity().getInstanceGroupId(), SearchCriteria.Op.EQ);
    if (serviceOffId != null) {
        sb.and("serviceOfferingId", sb.entity().getServiceOfferingId(), SearchCriteria.Op.EQ);
    }
    if (display != null) {
        sb.and("display", sb.entity().isDisplayVm(), SearchCriteria.Op.EQ);
    }
    if (groupId != null && (Long) groupId != -1) {
        sb.and("instanceGroupId", sb.entity().getInstanceGroupId(), SearchCriteria.Op.EQ);
    }
    if (userId != null) {
        sb.and("userId", sb.entity().getUserId(), SearchCriteria.Op.EQ);
    }
    if (networkId != null) {
        sb.and("networkId", sb.entity().getNetworkId(), SearchCriteria.Op.EQ);
    }
    if (vpcId != null && networkId == null) {
        sb.and("vpcId", sb.entity().getVpcId(), SearchCriteria.Op.EQ);
    }
    if (storageId != null) {
        sb.and("poolId", sb.entity().getPoolId(), SearchCriteria.Op.EQ);
    }
    if (affinityGroupId != null) {
        sb.and("affinityGroupId", sb.entity().getAffinityGroupId(), SearchCriteria.Op.EQ);
    }
    if (keyPairName != null) {
        sb.and("keyPairName", sb.entity().getKeypairName(), SearchCriteria.Op.EQ);
    }
    if (!isRootAdmin) {
        sb.and("displayVm", sb.entity().isDisplayVm(), SearchCriteria.Op.EQ);
    }
    // populate the search criteria with the values passed in
    SearchCriteria<UserVmJoinVO> sc = sb.create();
    // building ACL condition
    _accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
    if (tags != null && !tags.isEmpty()) {
        SearchCriteria<UserVmJoinVO> tagSc = _userVmJoinDao.createSearchCriteria();
        for (Map.Entry<String, String> entry : tags.entrySet()) {
            SearchCriteria<UserVmJoinVO> tsc = _userVmJoinDao.createSearchCriteria();
            tsc.addAnd("tagKey", SearchCriteria.Op.EQ, entry.getKey());
            tsc.addAnd("tagValue", SearchCriteria.Op.EQ, entry.getValue());
            tagSc.addOr("tagKey", SearchCriteria.Op.SC, tsc);
        }
        sc.addAnd("tagKey", SearchCriteria.Op.SC, tagSc);
    }
    if (groupId != null && (Long) groupId != -1) {
        sc.setParameters("instanceGroupId", groupId);
    }
    if (keyword != null) {
        SearchCriteria<UserVmJoinVO> ssc = _userVmJoinDao.createSearchCriteria();
        ssc.addOr("displayName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("instanceName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
        ssc.addOr("state", SearchCriteria.Op.EQ, keyword);
        sc.addAnd("displayName", SearchCriteria.Op.SC, ssc);
    }
    if (serviceOffId != null) {
        sc.setParameters("serviceOfferingId", serviceOffId);
    }
    if (display != null) {
        sc.setParameters("display", display);
    }
    if (ids != null && !ids.isEmpty()) {
        sc.setParameters("idIN", ids.toArray());
    }
    if (templateId != null) {
        sc.setParameters("templateId", templateId);
    }
    if (isoId != null) {
        sc.setParameters("isoId", isoId);
    }
    if (userId != null) {
        sc.setParameters("userId", userId);
    }
    if (networkId != null) {
        sc.setParameters("networkId", networkId);
    }
    if (vpcId != null && networkId == null) {
        sc.setParameters("vpcId", vpcId);
    }
    if (name != null) {
        sc.setParameters("name", "%" + name + "%");
    }
    if (state != null) {
        if (state.equalsIgnoreCase("present")) {
            sc.setParameters("stateNIN", "Destroyed", "Expunging");
        } else {
            sc.setParameters("stateEQ", state);
        }
    }
    if (hypervisor != null) {
        sc.setParameters("hypervisorType", hypervisor);
    }
    // Don't show Destroyed and Expunging vms to the end user if the AllowUserViewDestroyedVM flag is not set.
    if (!isAdmin && !AllowUserViewDestroyedVM.valueIn(caller.getAccountId())) {
        sc.setParameters("stateNIN", "Destroyed", "Expunging");
    }
    if (zoneId != null) {
        sc.setParameters("dataCenterId", zoneId);
    }
    if (affinityGroupId != null) {
        sc.setParameters("affinityGroupId", affinityGroupId);
    }
    if (keyPairName != null) {
        sc.setParameters("keyPairName", keyPairName);
    }
    if (cmd instanceof ListVMsCmdByAdmin) {
        ListVMsCmdByAdmin aCmd = (ListVMsCmdByAdmin) cmd;
        if (aCmd.getPodId() != null) {
            sc.setParameters("podId", pod);
            if (state == null) {
                sc.setParameters("stateNEQ", "Destroyed");
            }
        }
        if (hostId != null) {
            sc.setParameters("hostIdEQ", hostId);
        }
        if (storageId != null) {
            sc.setParameters("poolId", storageId);
        }
    }
    if (!isRootAdmin) {
        sc.setParameters("displayVm", 1);
    }
    // search vm details by ids
    Pair<List<UserVmJoinVO>, Integer> uniqueVmPair = _userVmJoinDao.searchAndDistinctCount(sc, searchFilter);
    Integer count = uniqueVmPair.second();
    if (count.intValue() == 0) {
        // handle empty result cases
        return uniqueVmPair;
    }
    List<UserVmJoinVO> uniqueVms = uniqueVmPair.first();
    Long[] vmIds = new Long[uniqueVms.size()];
    int i = 0;
    for (UserVmJoinVO v : uniqueVms) {
        vmIds[i++] = v.getId();
    }
    List<UserVmJoinVO> vms = _userVmJoinDao.searchByIds(vmIds);
    return new Pair<List<UserVmJoinVO>, Integer>(vms, count);
}
Also used : Account(com.cloud.user.Account) ArrayList(java.util.ArrayList) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ArrayList(java.util.ArrayList) List(java.util.List) Pair(com.cloud.utils.Pair) Ternary(com.cloud.utils.Ternary) UserVmJoinVO(com.cloud.api.query.vo.UserVmJoinVO) ListProjectResourcesCriteria(com.cloud.projects.Project.ListProjectResourcesCriteria) TemplateFilter(com.cloud.template.VirtualMachineTemplate.TemplateFilter) Filter(com.cloud.utils.db.Filter) ListVMsCmdByAdmin(org.apache.cloudstack.api.command.admin.vm.ListVMsCmdByAdmin) Map(java.util.Map)

Example 3 with UserVmJoinVO

use of com.cloud.api.query.vo.UserVmJoinVO in project cloudstack by apache.

the class ApiResponseHelper method createNicResponse.

@Override
public NicResponse createNicResponse(Nic result) {
    NicResponse response = new NicResponse();
    NetworkVO network = _entityMgr.findById(NetworkVO.class, result.getNetworkId());
    VMInstanceVO vm = _entityMgr.findById(VMInstanceVO.class, result.getInstanceId());
    UserVmJoinVO userVm = _entityMgr.findById(UserVmJoinVO.class, result.getInstanceId());
    response.setId(result.getUuid());
    response.setNetworkid(network.getUuid());
    if (vm != null) {
        response.setVmId(vm.getUuid());
    }
    if (userVm != null) {
        if (userVm.getTrafficType() != null) {
            response.setTrafficType(userVm.getTrafficType().toString());
        }
        if (userVm.getGuestType() != null) {
            response.setType(userVm.getGuestType().toString());
        }
    }
    response.setIpaddress(result.getIPv4Address());
    if (result.getSecondaryIp()) {
        List<NicSecondaryIpVO> secondaryIps = ApiDBUtils.findNicSecondaryIps(result.getId());
        if (secondaryIps != null) {
            List<NicSecondaryIpResponse> ipList = new ArrayList<NicSecondaryIpResponse>();
            for (NicSecondaryIpVO ip : secondaryIps) {
                NicSecondaryIpResponse ipRes = new NicSecondaryIpResponse();
                ipRes.setId(ip.getUuid());
                ipRes.setIpAddr(ip.getIp4Address());
                ipList.add(ipRes);
            }
            response.setSecondaryIps(ipList);
        }
    }
    response.setGateway(result.getIPv4Gateway());
    response.setNetmask(result.getIPv4Netmask());
    response.setMacAddress(result.getMacAddress());
    if (result.getIPv6Address() != null) {
        response.setIp6Address(result.getIPv6Address());
    }
    if (result.getIPv6Cidr() != null) {
        response.setIp6Cidr(result.getIPv6Cidr());
    }
    response.setDeviceId(String.valueOf(result.getDeviceId()));
    response.setIsDefault(result.isDefaultNic());
    if (result instanceof NicVO) {
        if (((NicVO) result).getNsxLogicalSwitchUuid() != null) {
            response.setNsxLogicalSwitch(((NicVO) result).getNsxLogicalSwitchUuid());
        }
        if (((NicVO) result).getNsxLogicalSwitchPortUuid() != null) {
            response.setNsxLogicalSwitchPort(((NicVO) result).getNsxLogicalSwitchPortUuid());
        }
    }
    return response;
}
Also used : NicSecondaryIpResponse(org.apache.cloudstack.api.response.NicSecondaryIpResponse) NicSecondaryIpVO(com.cloud.vm.dao.NicSecondaryIpVO) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NetworkVO(com.cloud.network.dao.NetworkVO) ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) NicVO(com.cloud.vm.NicVO) UserVmJoinVO(com.cloud.api.query.vo.UserVmJoinVO) NicResponse(org.apache.cloudstack.api.response.NicResponse)

Example 4 with UserVmJoinVO

use of com.cloud.api.query.vo.UserVmJoinVO in project cloudstack by apache.

the class ViewResponseHelper method createUserVmResponse.

public static List<UserVmResponse> createUserVmResponse(ResponseView view, String objectName, EnumSet<VMDetails> details, UserVmJoinVO... userVms) {
    Account caller = CallContext.current().getCallingAccount();
    Hashtable<Long, UserVmResponse> vmDataList = new Hashtable<Long, UserVmResponse>();
    for (UserVmJoinVO userVm : userVms) {
        UserVmResponse userVmData = vmDataList.get(userVm.getId());
        if (userVmData == null) {
            // first time encountering this vm
            userVmData = ApiDBUtils.newUserVmResponse(view, objectName, userVm, details, caller);
        } else {
            // update nics, securitygroups, tags, affinitygroups for 1 to many mapping fields
            userVmData = ApiDBUtils.fillVmDetails(view, userVmData, userVm);
        }
        vmDataList.put(userVm.getId(), userVmData);
    }
    return new ArrayList<UserVmResponse>(vmDataList.values());
}
Also used : Account(com.cloud.user.Account) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) UserVmResponse(org.apache.cloudstack.api.response.UserVmResponse) UserVmJoinVO(com.cloud.api.query.vo.UserVmJoinVO)

Example 5 with UserVmJoinVO

use of com.cloud.api.query.vo.UserVmJoinVO in project cloudstack by apache.

the class QueryManagerImpl method searchForUserVMs.

@Override
public ListResponse<UserVmResponse> searchForUserVMs(ListVMsCmd cmd) {
    Pair<List<UserVmJoinVO>, Integer> result = searchForUserVMsInternal(cmd);
    ListResponse<UserVmResponse> response = new ListResponse<UserVmResponse>();
    ResponseView respView = ResponseView.Restricted;
    if (cmd instanceof ListVMsCmdByAdmin) {
        respView = ResponseView.Full;
    }
    List<UserVmResponse> vmResponses = ViewResponseHelper.createUserVmResponse(respView, "virtualmachine", cmd.getDetails(), result.first().toArray(new UserVmJoinVO[result.first().size()]));
    response.setResponses(vmResponses, result.second());
    return response;
}
Also used : ResponseView(org.apache.cloudstack.api.ResponseObject.ResponseView) ListResponse(org.apache.cloudstack.api.response.ListResponse) ListVMsCmdByAdmin(org.apache.cloudstack.api.command.admin.vm.ListVMsCmdByAdmin) ArrayList(java.util.ArrayList) List(java.util.List) UserVmResponse(org.apache.cloudstack.api.response.UserVmResponse) UserVmJoinVO(com.cloud.api.query.vo.UserVmJoinVO)

Aggregations

UserVmJoinVO (com.cloud.api.query.vo.UserVmJoinVO)5 ArrayList (java.util.ArrayList)4 Account (com.cloud.user.Account)2 Hashtable (java.util.Hashtable)2 List (java.util.List)2 ListVMsCmdByAdmin (org.apache.cloudstack.api.command.admin.vm.ListVMsCmdByAdmin)2 UserVmResponse (org.apache.cloudstack.api.response.UserVmResponse)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 NetworkVO (com.cloud.network.dao.NetworkVO)1 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)1 ListProjectResourcesCriteria (com.cloud.projects.Project.ListProjectResourcesCriteria)1 TemplateFilter (com.cloud.template.VirtualMachineTemplate.TemplateFilter)1 UserVm (com.cloud.uservm.UserVm)1 Pair (com.cloud.utils.Pair)1 Ternary (com.cloud.utils.Ternary)1 Filter (com.cloud.utils.db.Filter)1 NicVO (com.cloud.vm.NicVO)1 VMInstanceVO (com.cloud.vm.VMInstanceVO)1 NicSecondaryIpVO (com.cloud.vm.dao.NicSecondaryIpVO)1 Map (java.util.Map)1