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.");
}
}
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.");
}
}
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();
}
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);
}
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;
}
Aggregations