Search in sources :

Example 46 with UserVmResponse

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

the class UpdateDefaultNicForVMCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("Vm Id: " + this._uuidMgr.getUuid(VirtualMachine.class, getVmId()) + " Nic Id: " + this._uuidMgr.getUuid(Nic.class, getNicId()));
    UserVm result = _userVmService.updateDefaultNicForVirtualMachine(this);
    ArrayList<VMDetails> dc = new ArrayList<VMDetails>();
    dc.add(VMDetails.valueOf("nics"));
    EnumSet<VMDetails> details = EnumSet.copyOf(dc);
    if (result != null) {
        UserVmResponse response = _responseGenerator.createUserVmResponse(getResponseView(), "virtualmachine", details, result).get(0);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to set default nic for VM. Refer to server logs for details.");
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) VMDetails(org.apache.cloudstack.api.ApiConstants.VMDetails) ServerApiException(org.apache.cloudstack.api.ServerApiException) ArrayList(java.util.ArrayList) UserVmResponse(org.apache.cloudstack.api.response.UserVmResponse)

Example 47 with UserVmResponse

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

the class UpdateVmNicIpCmd method execute.

@Override
public void execute() throws ResourceUnavailableException, ResourceAllocationException, ConcurrentOperationException, InsufficientCapacityException {
    CallContext.current().setEventDetails("Nic Id: " + getNicId());
    String ip;
    if ((ip = getIpaddress()) != null) {
        if (!NetUtils.isValidIp4(ip)) {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Invalid ip address " + ip);
        }
    }
    UserVm vm = _userVmService.updateNicIpForVirtualMachine(this);
    ArrayList<VMDetails> dc = new ArrayList<VMDetails>();
    dc.add(VMDetails.valueOf("nics"));
    EnumSet<VMDetails> details = EnumSet.copyOf(dc);
    if (vm != null) {
        UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", details, vm).get(0);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update ip address on vm NIC. Refer to server logs for details.");
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) VMDetails(org.apache.cloudstack.api.ApiConstants.VMDetails) ServerApiException(org.apache.cloudstack.api.ServerApiException) ArrayList(java.util.ArrayList) UserVmResponse(org.apache.cloudstack.api.response.UserVmResponse)

Example 48 with UserVmResponse

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

the class UpdateVmNicIpTest method testSuccess.

@Test
public void testSuccess() throws ResourceAllocationException, ResourceUnavailableException, ConcurrentOperationException, InsufficientCapacityException {
    UserVmService userVmService = Mockito.mock(UserVmService.class);
    updateVmNicIpCmd = Mockito.mock(UpdateVmNicIpCmd.class);
    UserVm userVm = Mockito.mock(UserVm.class);
    Mockito.when(userVmService.updateNicIpForVirtualMachine(Mockito.any(UpdateVmNicIpCmd.class))).thenReturn(userVm);
    updateVmNicIpCmd._userVmService = userVmService;
    responseGenerator = Mockito.mock(ResponseGenerator.class);
    List<UserVmResponse> list = new LinkedList<UserVmResponse>();
    UserVmResponse userVmResponse = Mockito.mock(UserVmResponse.class);
    list.add(userVmResponse);
    Mockito.when(responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", userVm)).thenReturn(list);
    updateVmNicIpCmd._responseGenerator = responseGenerator;
    updateVmNicIpCmd.execute();
}
Also used : UserVm(com.cloud.uservm.UserVm) UserVmService(com.cloud.vm.UserVmService) ResponseGenerator(org.apache.cloudstack.api.ResponseGenerator) UpdateVmNicIpCmd(org.apache.cloudstack.api.command.user.vm.UpdateVmNicIpCmd) UserVmResponse(org.apache.cloudstack.api.response.UserVmResponse) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 49 with UserVmResponse

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

the class UnmanagedVMsManagerImplTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    AccountVO account = new AccountVO("admin", 1L, "", Account.ACCOUNT_TYPE_ADMIN, "uuid");
    UserVO user = new UserVO(1, "adminuser", "password", "firstname", "lastName", "email", "timezone", UUID.randomUUID().toString(), User.Source.UNKNOWN);
    CallContext.register(user, account);
    UnmanagedInstanceTO instance = new UnmanagedInstanceTO();
    instance.setName("TestInstance");
    instance.setCpuCores(2);
    instance.setCpuCoresPerSocket(1);
    instance.setCpuSpeed(1000);
    instance.setMemory(1024);
    instance.setOperatingSystem("CentOS 7");
    List<UnmanagedInstanceTO.Disk> instanceDisks = new ArrayList<>();
    UnmanagedInstanceTO.Disk instanceDisk = new UnmanagedInstanceTO.Disk();
    instanceDisk.setDiskId("1000-1");
    instanceDisk.setLabel("DiskLabel");
    instanceDisk.setController("scsi");
    instanceDisk.setImagePath("[b6ccf44a1fa13e29b3667b4954fa10ee] TestInstance/ROOT-1.vmdk");
    instanceDisk.setCapacity(5242880L);
    instanceDisk.setDatastoreName("Test");
    instanceDisk.setDatastoreHost("Test");
    instanceDisk.setDatastorePath("Test");
    instanceDisk.setDatastoreType("NFS");
    instanceDisks.add(instanceDisk);
    instance.setDisks(instanceDisks);
    List<UnmanagedInstanceTO.Nic> instanceNics = new ArrayList<>();
    UnmanagedInstanceTO.Nic instanceNic = new UnmanagedInstanceTO.Nic();
    instanceNic.setNicId("NIC 1");
    instanceNic.setAdapterType("VirtualE1000E");
    instanceNic.setMacAddress("02:00:2e:0f:00:02");
    instanceNic.setVlan(1024);
    instanceNics.add(instanceNic);
    instance.setNics(instanceNics);
    instance.setPowerState(UnmanagedInstanceTO.PowerState.PowerOn);
    ClusterVO clusterVO = new ClusterVO(1L, 1L, "Cluster");
    clusterVO.setHypervisorType(Hypervisor.HypervisorType.VMware.toString());
    when(clusterDao.findById(Mockito.anyLong())).thenReturn(clusterVO);
    when(configurationDao.getValue(Mockito.anyString())).thenReturn(null);
    doNothing().when(resourceLimitService).checkResourceLimit(any(Account.class), any(Resource.ResourceType.class), anyLong());
    List<HostVO> hosts = new ArrayList<>();
    HostVO hostVO = Mockito.mock(HostVO.class);
    when(hostVO.isInMaintenanceStates()).thenReturn(false);
    hosts.add(hostVO);
    when(hostVO.checkHostServiceOfferingTags(Mockito.any())).thenReturn(true);
    when(resourceManager.listHostsInClusterByStatus(Mockito.anyLong(), Mockito.any(Status.class))).thenReturn(hosts);
    List<VMTemplateStoragePoolVO> templates = new ArrayList<>();
    when(templatePoolDao.listAll()).thenReturn(templates);
    List<VolumeVO> volumes = new ArrayList<>();
    when(volumeDao.findIncludingRemovedByZone(Mockito.anyLong())).thenReturn(volumes);
    List<VMInstanceVO> vms = new ArrayList<>();
    when(vmDao.listByHostId(Mockito.anyLong())).thenReturn(vms);
    when(vmDao.listByLastHostIdAndStates(Mockito.anyLong())).thenReturn(vms);
    GetUnmanagedInstancesCommand cmd = Mockito.mock(GetUnmanagedInstancesCommand.class);
    HashMap<String, UnmanagedInstanceTO> map = new HashMap<>();
    map.put(instance.getName(), instance);
    Answer answer = new GetUnmanagedInstancesAnswer(cmd, "", map);
    when(agentManager.easySend(Mockito.anyLong(), Mockito.any(GetUnmanagedInstancesCommand.class))).thenReturn(answer);
    DataCenterVO zone = Mockito.mock(DataCenterVO.class);
    when(zone.getId()).thenReturn(1L);
    when(dataCenterDao.findById(Mockito.anyLong())).thenReturn(zone);
    when(accountService.getActiveAccountById(Mockito.anyLong())).thenReturn(Mockito.mock(Account.class));
    List<UserVO> users = new ArrayList<>();
    users.add(Mockito.mock(UserVO.class));
    when(userDao.listByAccount(Mockito.anyLong())).thenReturn(users);
    VMTemplateVO template = Mockito.mock(VMTemplateVO.class);
    when(template.getId()).thenReturn(1L);
    when(template.getName()).thenReturn("Template");
    when(templateDao.findById(Mockito.anyLong())).thenReturn(template);
    when(templateDao.findByName(Mockito.anyString())).thenReturn(template);
    ServiceOfferingVO serviceOffering = Mockito.mock(ServiceOfferingVO.class);
    when(serviceOffering.getId()).thenReturn(1L);
    when(serviceOffering.isDynamic()).thenReturn(false);
    when(serviceOffering.getCpu()).thenReturn(instance.getCpuCores());
    when(serviceOffering.getRamSize()).thenReturn(instance.getMemory());
    when(serviceOffering.getSpeed()).thenReturn(instance.getCpuSpeed());
    when(serviceOfferingDao.findById(Mockito.anyLong())).thenReturn(serviceOffering);
    DiskOfferingVO diskOfferingVO = Mockito.mock(DiskOfferingVO.class);
    when(diskOfferingVO.getTags()).thenReturn("");
    when(diskOfferingVO.isCustomized()).thenReturn(false);
    when(diskOfferingVO.getDiskSize()).thenReturn(Long.MAX_VALUE);
    when(diskOfferingDao.findById(Mockito.anyLong())).thenReturn(diskOfferingVO);
    UserVmVO userVm = Mockito.mock(UserVmVO.class);
    when(userVm.getAccountId()).thenReturn(1L);
    when(userVm.getDataCenterId()).thenReturn(1L);
    when(userVm.getHostName()).thenReturn(instance.getName());
    when(userVm.getTemplateId()).thenReturn(1L);
    when(userVm.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.VMware);
    when(userVm.getUuid()).thenReturn("abcd");
    when(userVm.isDisplayVm()).thenReturn(true);
    // Skip usage publishing and resource increment for test
    when(userVm.getType()).thenReturn(VirtualMachine.Type.Instance);
    userVm.setInstanceName(instance.getName());
    userVm.setHostName(instance.getName());
    StoragePoolVO poolVO = Mockito.mock(StoragePoolVO.class);
    when(poolVO.getDataCenterId()).thenReturn(1L);
    when(poolVO.getClusterId()).thenReturn(clusterVO.getId());
    List<StoragePoolVO> pools = new ArrayList<>();
    pools.add(poolVO);
    when(primaryDataStoreDao.listPoolByHostPath(Mockito.anyString(), Mockito.anyString())).thenReturn(pools);
    when(userVmManager.importVM(nullable(DataCenter.class), nullable(Host.class), nullable(VirtualMachineTemplate.class), nullable(String.class), nullable(String.class), nullable(Account.class), nullable(String.class), nullable(Account.class), nullable(Boolean.class), nullable(String.class), nullable(Long.class), nullable(Long.class), nullable(ServiceOffering.class), nullable(String.class), nullable(String.class), nullable(Hypervisor.HypervisorType.class), nullable(Map.class), nullable(VirtualMachine.PowerState.class))).thenReturn(userVm);
    when(volumeApiService.doesTargetStorageSupportDiskOffering(Mockito.any(StoragePool.class), Mockito.anyString())).thenReturn(true);
    NetworkVO networkVO = Mockito.mock(NetworkVO.class);
    when(networkVO.getGuestType()).thenReturn(Network.GuestType.L2);
    when(networkVO.getBroadcastUri()).thenReturn(URI.create(String.format("vlan://%d", instanceNic.getVlan())));
    when(networkVO.getDataCenterId()).thenReturn(1L);
    when(networkDao.findById(Mockito.anyLong())).thenReturn(networkVO);
    List<NetworkVO> networks = new ArrayList<>();
    networks.add(networkVO);
    when(networkDao.listByZone(Mockito.anyLong())).thenReturn(networks);
    doNothing().when(networkModel).checkNetworkPermissions(Mockito.any(Account.class), Mockito.any(Network.class));
    doNothing().when(networkModel).checkRequestedIpAddresses(Mockito.anyLong(), Mockito.any(Network.IpAddresses.class));
    NicProfile profile = Mockito.mock(NicProfile.class);
    Integer deviceId = 100;
    Pair<NicProfile, Integer> pair = new Pair<NicProfile, Integer>(profile, deviceId);
    when(networkOrchestrationService.importNic(nullable(String.class), nullable(Integer.class), nullable(Network.class), nullable(Boolean.class), nullable(VirtualMachine.class), nullable(Network.IpAddresses.class), anyBoolean())).thenReturn(pair);
    when(volumeManager.importVolume(Mockito.any(Volume.Type.class), Mockito.anyString(), Mockito.any(DiskOffering.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyLong(), Mockito.any(VirtualMachine.class), Mockito.any(VirtualMachineTemplate.class), Mockito.any(Account.class), Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString(), Mockito.anyString())).thenReturn(Mockito.mock(DiskProfile.class));
    when(volumeDao.findByInstance(Mockito.anyLong())).thenReturn(volumes);
    List<UserVmResponse> userVmResponses = new ArrayList<>();
    UserVmResponse userVmResponse = new UserVmResponse();
    userVmResponse.setInstanceName(instance.getName());
    userVmResponses.add(userVmResponse);
    when(responseGenerator.createUserVmResponse(Mockito.any(ResponseObject.ResponseView.class), Mockito.anyString(), Mockito.any(UserVm.class))).thenReturn(userVmResponses);
    when(vmDao.findById(virtualMachineId)).thenReturn(virtualMachine);
    when(virtualMachine.getState()).thenReturn(VirtualMachine.State.Running);
    when(virtualMachine.getInstanceName()).thenReturn("i-2-7-VM");
    when(virtualMachine.getId()).thenReturn(virtualMachineId);
    VolumeVO volumeVO = mock(VolumeVO.class);
    when(volumeDao.findByInstance(virtualMachineId)).thenReturn(Collections.singletonList(volumeVO));
    when(volumeVO.getInstanceId()).thenReturn(virtualMachineId);
    when(volumeVO.getId()).thenReturn(virtualMachineId);
    when(nicDao.listByVmId(virtualMachineId)).thenReturn(Collections.singletonList(nicVO));
    when(nicVO.getNetworkId()).thenReturn(1L);
    when(networkDao.findById(1L)).thenReturn(networkVO);
}
Also used : Account(com.cloud.user.Account) StoragePool(com.cloud.storage.StoragePool) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AccountVO(com.cloud.user.AccountVO) GetUnmanagedInstancesCommand(com.cloud.agent.api.GetUnmanagedInstancesCommand) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO) UserVm(com.cloud.uservm.UserVm) VolumeVO(com.cloud.storage.VolumeVO) GetUnmanagedInstancesAnswer(com.cloud.agent.api.GetUnmanagedInstancesAnswer) Network(com.cloud.network.Network) VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) Status(com.cloud.host.Status) DataCenterVO(com.cloud.dc.DataCenterVO) NetworkVO(com.cloud.network.dao.NetworkVO) VirtualMachineTemplate(com.cloud.template.VirtualMachineTemplate) ServiceOffering(com.cloud.offering.ServiceOffering) VMInstanceVO(com.cloud.vm.VMInstanceVO) DiskProfile(com.cloud.vm.DiskProfile) UserVmResponse(org.apache.cloudstack.api.response.UserVmResponse) HostVO(com.cloud.host.HostVO) Matchers.anyLong(org.mockito.Matchers.anyLong) Map(java.util.Map) HashMap(java.util.HashMap) VirtualMachine(com.cloud.vm.VirtualMachine) UserVmVO(com.cloud.vm.UserVmVO) DiskOffering(com.cloud.offering.DiskOffering) VMTemplateVO(com.cloud.storage.VMTemplateVO) DiskOfferingVO(com.cloud.storage.DiskOfferingVO) Pair(com.cloud.utils.Pair) ClusterVO(com.cloud.dc.ClusterVO) Host(com.cloud.host.Host) NicProfile(com.cloud.vm.NicProfile) VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) Answer(com.cloud.agent.api.Answer) GetUnmanagedInstancesAnswer(com.cloud.agent.api.GetUnmanagedInstancesAnswer) DataCenter(com.cloud.dc.DataCenter) UserVO(com.cloud.user.UserVO) Before(org.junit.Before)

Example 50 with UserVmResponse

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

the class MetricsServiceImpl method listVmMetrics.

@Override
public List<VmMetricsResponse> listVmMetrics(List<UserVmResponse> vmResponses) {
    final List<VmMetricsResponse> metricsResponses = new ArrayList<>();
    for (final UserVmResponse vmResponse : vmResponses) {
        VmMetricsResponse metricsResponse = new VmMetricsResponse();
        try {
            BeanUtils.copyProperties(metricsResponse, vmResponse);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate vm metrics response");
        }
        metricsResponse.setHasAnnotation(vmResponse.hasAnnotation());
        metricsResponse.setIpAddress(vmResponse.getNics());
        metricsResponse.setCpuTotal(vmResponse.getCpuNumber(), vmResponse.getCpuSpeed());
        metricsResponse.setMemTotal(vmResponse.getMemory());
        metricsResponse.setNetworkRead(vmResponse.getNetworkKbsRead());
        metricsResponse.setNetworkWrite(vmResponse.getNetworkKbsWrite());
        metricsResponse.setDiskRead(vmResponse.getDiskKbsRead());
        metricsResponse.setDiskWrite(vmResponse.getDiskKbsWrite());
        metricsResponse.setDiskIopsTotal(vmResponse.getDiskIORead(), vmResponse.getDiskIOWrite());
        metricsResponse.setLastUpdated(vmResponse.getLastUpdated());
        metricsResponses.add(metricsResponse);
    }
    return metricsResponses;
}
Also used : VmMetricsResponse(org.apache.cloudstack.response.VmMetricsResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) ArrayList(java.util.ArrayList) UserVmResponse(org.apache.cloudstack.api.response.UserVmResponse) InvocationTargetException(java.lang.reflect.InvocationTargetException)

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