Search in sources :

Example 6 with UserVmResponse

use of org.apache.cloudstack.api.response.UserVmResponse in project cloudstack by apache.

the class DetachIsoCmd method execute.

@Override
public void execute() {
    boolean result = _templateService.detachIso(virtualMachineId, isForced());
    if (result) {
        UserVm userVm = _entityMgr.findById(UserVm.class, virtualMachineId);
        UserVmResponse response = _responseGenerator.createUserVmResponse(getResponseView(), "virtualmachine", userVm).get(0);
        response.setResponseName(DeployVMCmd.getResultObjectName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to detach ISO");
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) ServerApiException(org.apache.cloudstack.api.ServerApiException) UserVmResponse(org.apache.cloudstack.api.response.UserVmResponse)

Example 7 with UserVmResponse

use of org.apache.cloudstack.api.response.UserVmResponse in project cloudstack by apache.

the class ScaleVMCmdTest method testCreateSuccess.

@Test
public void testCreateSuccess() {
    UserVmService userVmService = Mockito.mock(UserVmService.class);
    UserVm userVm = Mockito.mock(UserVm.class);
    try {
        Mockito.when(userVmService.upgradeVirtualMachine(scaleVMCmd)).thenReturn(userVm);
    } catch (Exception e) {
        Assert.fail("Received exception when success expected " + e.getMessage());
    }
    ResponseGenerator responseGenerator = Mockito.mock(ResponseGenerator.class);
    scaleVMCmd._responseGenerator = responseGenerator;
    UserVmResponse userVmResponse = Mockito.mock(UserVmResponse.class);
    // List<UserVmResponse> list = Mockito.mock(UserVmResponse.class);
    // list.add(userVmResponse);
    // LinkedList<UserVmResponse> mockedList = Mockito.mock(LinkedList.class);
    // Mockito.when(mockedList.get(0)).thenReturn(userVmResponse);
    List<UserVmResponse> list = new LinkedList<UserVmResponse>();
    list.add(userVmResponse);
    Mockito.when(responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", userVm)).thenReturn(list);
    scaleVMCmd._userVmService = userVmService;
    scaleVMCmd.execute();
}
Also used : UserVm(com.cloud.uservm.UserVm) UserVmService(com.cloud.vm.UserVmService) ResponseGenerator(org.apache.cloudstack.api.ResponseGenerator) UserVmResponse(org.apache.cloudstack.api.response.UserVmResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) ExpectedException(org.junit.rules.ExpectedException) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 8 with UserVmResponse

use of org.apache.cloudstack.api.response.UserVmResponse in project cloudstack by apache.

the class UserVmJoinDaoImpl method setUserVmResponse.

/**
 * The resulting Response attempts to be in line with what is returned from
 * @see com.cloud.api.ApiResponseHelper#createNicResponse(Nic)
 */
@Override
public UserVmResponse setUserVmResponse(ResponseView view, UserVmResponse userVmData, UserVmJoinVO uvo) {
    Long securityGroupId = uvo.getSecurityGroupId();
    if (securityGroupId != null && securityGroupId.longValue() != 0) {
        SecurityGroupResponse resp = new SecurityGroupResponse();
        resp.setId(uvo.getSecurityGroupUuid());
        resp.setName(uvo.getSecurityGroupName());
        resp.setDescription(uvo.getSecurityGroupDescription());
        resp.setObjectName("securitygroup");
        if (uvo.getAccountType() == Account.ACCOUNT_TYPE_PROJECT) {
            resp.setProjectId(uvo.getProjectUuid());
            resp.setProjectName(uvo.getProjectName());
        } else {
            resp.setAccountName(uvo.getAccountName());
        }
        userVmData.addSecurityGroup(resp);
    }
    long nic_id = uvo.getNicId();
    if (nic_id > 0) {
        NicResponse nicResponse = new NicResponse();
        // The numbered comments are to keep track of the data returned from here and ApiResponseHelper.createNicResponse()
        // the data can't be identical but some tidying up/unifying might be possible
        /*1: nicUuid*/
        nicResponse.setId(uvo.getNicUuid());
        /*2: networkUuid*/
        nicResponse.setNetworkid(uvo.getNetworkUuid());
        if (uvo.getTrafficType() != null) {
            /*4: trafficType*/
            nicResponse.setTrafficType(uvo.getTrafficType().toString());
        }
        if (uvo.getGuestType() != null) {
            /*5: guestType*/
            nicResponse.setType(uvo.getGuestType().toString());
        }
        /*6: ipAddress*/
        nicResponse.setIpaddress(uvo.getIpAddress());
        /*7: gateway*/
        nicResponse.setGateway(uvo.getGateway());
        /*8: netmask*/
        nicResponse.setNetmask(uvo.getNetmask());
        /*9: networkName*/
        nicResponse.setNetworkName(uvo.getNetworkName());
        /*10: macAddress*/
        nicResponse.setMacAddress(uvo.getMacAddress());
        /*11: IPv6Address*/
        nicResponse.setIp6Address(uvo.getIp6Address());
        /*12: IPv6Gateway*/
        nicResponse.setIp6Gateway(uvo.getIp6Gateway());
        /*13: IPv6Cidr*/
        nicResponse.setIp6Cidr(uvo.getIp6Cidr());
        /*15: broadcastURI*/
        if (uvo.getBroadcastUri() != null) {
            nicResponse.setBroadcastUri(uvo.getBroadcastUri().toString());
        }
        /*16: isolationURI*/
        if (uvo.getIsolationUri() != null) {
            nicResponse.setIsolationUri(uvo.getIsolationUri().toString());
        }
        /*17: default*/
        nicResponse.setIsDefault(uvo.isDefaultNic());
        nicResponse.setDeviceId(String.valueOf(uvo.getNicDeviceId()));
        List<NicSecondaryIpVO> secondaryIps = ApiDBUtils.findNicSecondaryIps(uvo.getNicId());
        if (secondaryIps != null) {
            List<NicSecondaryIpResponse> ipList = new ArrayList<NicSecondaryIpResponse>();
            for (NicSecondaryIpVO ip : secondaryIps) {
                NicSecondaryIpResponse ipRes = new NicSecondaryIpResponse();
                ipRes.setId(ip.getUuid());
                ApiResponseHelper.setResponseIpAddress(ip, ipRes);
                ipList.add(ipRes);
            }
            nicResponse.setSecondaryIps(ipList);
        }
        /* 18: extra dhcp options */
        nicResponse.setObjectName("nic");
        List<NicExtraDhcpOptionResponse> nicExtraDhcpOptionResponses = _nicExtraDhcpOptionDao.listByNicId(nic_id).stream().map(vo -> new NicExtraDhcpOptionResponse(Dhcp.DhcpOptionCode.valueOfInt(vo.getCode()).getName(), vo.getCode(), vo.getValue())).collect(Collectors.toList());
        nicResponse.setExtraDhcpOptions(nicExtraDhcpOptionResponses);
        userVmData.addNic(nicResponse);
    }
    long tag_id = uvo.getTagId();
    if (tag_id > 0 && !userVmData.containTag(tag_id)) {
        addTagInformation(uvo, userVmData);
    }
    if (userVmData.hasAnnotation() == null) {
        userVmData.setHasAnnotation(annotationDao.hasAnnotations(uvo.getUuid(), AnnotationService.EntityType.VM.name(), _accountMgr.isRootAdmin(CallContext.current().getCallingAccount().getId())));
    }
    Long affinityGroupId = uvo.getAffinityGroupId();
    if (affinityGroupId != null && affinityGroupId.longValue() != 0) {
        AffinityGroupResponse resp = new AffinityGroupResponse();
        resp.setId(uvo.getAffinityGroupUuid());
        resp.setName(uvo.getAffinityGroupName());
        resp.setDescription(uvo.getAffinityGroupDescription());
        resp.setObjectName("affinitygroup");
        resp.setAccountName(uvo.getAccountName());
        userVmData.addAffinityGroup(resp);
    }
    return userVmData;
}
Also used : UserVmManager(com.cloud.vm.UserVmManager) AnnotationDao(org.apache.cloudstack.annotation.dao.AnnotationDao) UserStatisticsDao(com.cloud.user.dao.UserStatisticsDao) UserVmJoinVO(com.cloud.api.query.vo.UserVmJoinVO) Logger(org.apache.log4j.Logger) Map(java.util.Map) NicExtraDhcpOptionDao(com.cloud.vm.dao.NicExtraDhcpOptionDao) EnumSet(java.util.EnumSet) CallContext(org.apache.cloudstack.context.CallContext) State(com.cloud.vm.VirtualMachine.State) Set(java.util.Set) ApiDBUtils(com.cloud.api.ApiDBUtils) Account(com.cloud.user.Account) QueryService(org.apache.cloudstack.query.QueryService) Collectors(java.util.stream.Collectors) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) List(java.util.List) NicExtraDhcpOptionResponse(org.apache.cloudstack.api.response.NicExtraDhcpOptionResponse) NicResponse(org.apache.cloudstack.api.response.NicResponse) GuestOS(com.cloud.storage.GuestOS) ResponseView(org.apache.cloudstack.api.ResponseObject.ResponseView) SearchBuilder(com.cloud.utils.db.SearchBuilder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) ServiceOfferingDetailsVO(com.cloud.service.ServiceOfferingDetailsVO) UserDao(com.cloud.user.dao.UserDao) SearchCriteria(com.cloud.utils.db.SearchCriteria) VMDetails(org.apache.cloudstack.api.ApiConstants.VMDetails) User(com.cloud.user.User) UserVmDetailsDao(com.cloud.vm.dao.UserVmDetailsDao) UserVmDetailVO(com.cloud.vm.UserVmDetailVO) VmStats(com.cloud.vm.VmStats) AffinityGroupResponse(org.apache.cloudstack.affinity.AffinityGroupResponse) Hashtable(java.util.Hashtable) Op(com.cloud.utils.db.SearchCriteria.Op) Dhcp(com.cloud.utils.net.Dhcp) UserVmResponse(org.apache.cloudstack.api.response.UserVmResponse) UserVm(com.cloud.uservm.UserVm) ApiResponseHelper(com.cloud.api.ApiResponseHelper) GPU(com.cloud.gpu.GPU) AnnotationService(org.apache.cloudstack.annotation.AnnotationService) DecimalFormat(java.text.DecimalFormat) ApiConstants(org.apache.cloudstack.api.ApiConstants) NicSecondaryIpResponse(org.apache.cloudstack.api.response.NicSecondaryIpResponse) Component(org.springframework.stereotype.Component) AccountManager(com.cloud.user.AccountManager) SecurityGroupResponse(org.apache.cloudstack.api.response.SecurityGroupResponse) ConfigurationDao(org.apache.cloudstack.framework.config.dao.ConfigurationDao) UserStatisticsVO(com.cloud.user.UserStatisticsVO) NicSecondaryIpVO(com.cloud.vm.dao.NicSecondaryIpVO) NicExtraDhcpOptionResponse(org.apache.cloudstack.api.response.NicExtraDhcpOptionResponse) ArrayList(java.util.ArrayList) SecurityGroupResponse(org.apache.cloudstack.api.response.SecurityGroupResponse) NicSecondaryIpResponse(org.apache.cloudstack.api.response.NicSecondaryIpResponse) NicSecondaryIpVO(com.cloud.vm.dao.NicSecondaryIpVO) AffinityGroupResponse(org.apache.cloudstack.affinity.AffinityGroupResponse) NicResponse(org.apache.cloudstack.api.response.NicResponse)

Example 9 with UserVmResponse

use of org.apache.cloudstack.api.response.UserVmResponse in project cloudstack by apache.

the class ListVMsCmd method updateVMResponse.

protected void updateVMResponse(List<UserVmResponse> response) {
    for (UserVmResponse vmResponse : response) {
        ResourceIcon resourceIcon = resourceIconManager.getByResourceTypeAndUuid(ResourceTag.ResourceObjectType.UserVm, vmResponse.getId());
        if (resourceIcon == null) {
            ResourceTag.ResourceObjectType type = ResourceTag.ResourceObjectType.Template;
            String uuid = vmResponse.getTemplateId();
            if (vmResponse.getIsoId() != null) {
                uuid = vmResponse.getIsoId();
                type = ResourceTag.ResourceObjectType.ISO;
            }
            resourceIcon = resourceIconManager.getByResourceTypeAndUuid(type, uuid);
            if (resourceIcon == null) {
                continue;
            }
        }
        ResourceIconResponse iconResponse = _responseGenerator.createResourceIconResponse(resourceIcon);
        vmResponse.setResourceIconResponse(iconResponse);
    }
}
Also used : ResourceTag(com.cloud.server.ResourceTag) ResourceIconResponse(org.apache.cloudstack.api.response.ResourceIconResponse) ResourceIcon(com.cloud.server.ResourceIcon) UserVmResponse(org.apache.cloudstack.api.response.UserVmResponse)

Example 10 with UserVmResponse

use of org.apache.cloudstack.api.response.UserVmResponse in project cloudstack by apache.

the class DestroyVMCmd method execute.

@Override
public void execute() throws ResourceUnavailableException, ConcurrentOperationException {
    CallContext.current().setEventDetails("Vm Id: " + this._uuidMgr.getUuid(VirtualMachine.class, getId()));
    UserVm result = _userVmService.destroyVm(this);
    UserVmResponse response = new UserVmResponse();
    if (result != null) {
        List<UserVmResponse> responses = _responseGenerator.createUserVmResponse(getResponseView(), "virtualmachine", result);
        if (responses != null && !responses.isEmpty()) {
            response = responses.get(0);
        }
        response.setResponseName("virtualmachine");
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to destroy vm");
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) ServerApiException(org.apache.cloudstack.api.ServerApiException) UserVmResponse(org.apache.cloudstack.api.response.UserVmResponse)

Aggregations

UserVmResponse (org.apache.cloudstack.api.response.UserVmResponse)56 UserVm (com.cloud.uservm.UserVm)49 ServerApiException (org.apache.cloudstack.api.ServerApiException)43 ArrayList (java.util.ArrayList)19 VMDetails (org.apache.cloudstack.api.ApiConstants.VMDetails)12 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)8 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)7 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)7 Account (com.cloud.user.Account)7 UserVmJoinVO (com.cloud.api.query.vo.UserVmJoinVO)6 VirtualMachine (com.cloud.vm.VirtualMachine)6 List (java.util.List)5 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)4 InsufficientServerCapacityException (com.cloud.exception.InsufficientServerCapacityException)4 ManagementServerException (com.cloud.exception.ManagementServerException)4 VirtualMachineMigrationException (com.cloud.exception.VirtualMachineMigrationException)4 Host (com.cloud.host.Host)4 ServiceOffering (com.cloud.offering.ServiceOffering)4 DiskOfferingVO (com.cloud.storage.DiskOfferingVO)4 ResponseView (org.apache.cloudstack.api.ResponseObject.ResponseView)4