use of org.apache.cloudstack.api.response.UserVmResponse in project cloudstack by apache.
the class UserVmJoinDaoImpl method newUserVmResponse.
@Override
public UserVmResponse newUserVmResponse(ResponseView view, String objectName, UserVmJoinVO userVm, EnumSet<VMDetails> details, Account caller) {
UserVmResponse userVmResponse = new UserVmResponse();
if (userVm.getHypervisorType() != null) {
userVmResponse.setHypervisor(userVm.getHypervisorType().toString());
}
userVmResponse.setId(userVm.getUuid());
userVmResponse.setName(userVm.getName());
if (userVm.getDisplayName() != null) {
userVmResponse.setDisplayName(userVm.getDisplayName());
} else {
userVmResponse.setDisplayName(userVm.getName());
}
if (userVm.getAccountType() == Account.ACCOUNT_TYPE_PROJECT) {
userVmResponse.setProjectId(userVm.getProjectUuid());
userVmResponse.setProjectName(userVm.getProjectName());
} else {
userVmResponse.setAccountName(userVm.getAccountName());
}
User user = _userDao.getUser(userVm.getUserId());
if (user != null) {
userVmResponse.setUserId(user.getUuid());
userVmResponse.setUserName(user.getUsername());
}
userVmResponse.setDomainId(userVm.getDomainUuid());
userVmResponse.setDomainName(userVm.getDomainName());
userVmResponse.setCreated(userVm.getCreated());
userVmResponse.setLastUpdated(userVm.getLastUpdated());
userVmResponse.setDisplayVm(userVm.isDisplayVm());
if (userVm.getState() != null) {
userVmResponse.setState(userVm.getState().toString());
}
userVmResponse.setHaEnable(userVm.isHaEnabled());
if (details.contains(VMDetails.all) || details.contains(VMDetails.group)) {
userVmResponse.setGroupId(userVm.getInstanceGroupUuid());
userVmResponse.setGroup(userVm.getInstanceGroupName());
}
userVmResponse.setZoneId(userVm.getDataCenterUuid());
userVmResponse.setZoneName(userVm.getDataCenterName());
if (view == ResponseView.Full) {
userVmResponse.setInstanceName(userVm.getInstanceName());
userVmResponse.setHostId(userVm.getHostUuid());
userVmResponse.setHostName(userVm.getHostName());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.tmpl)) {
userVmResponse.setTemplateId(userVm.getTemplateUuid());
userVmResponse.setTemplateName(userVm.getTemplateName());
userVmResponse.setTemplateDisplayText(userVm.getTemplateDisplayText());
userVmResponse.setPasswordEnabled(userVm.isPasswordEnabled());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.iso)) {
userVmResponse.setIsoId(userVm.getIsoUuid());
userVmResponse.setIsoName(userVm.getIsoName());
userVmResponse.setIsoDisplayText(userVm.getIsoDisplayText());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.servoff)) {
userVmResponse.setServiceOfferingId(userVm.getServiceOfferingUuid());
userVmResponse.setServiceOfferingName(userVm.getServiceOfferingName());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.diskoff)) {
DiskOfferingVO diskOfferingVO = ApiDBUtils.findDiskOfferingById(userVm.getDiskOfferingId());
if (diskOfferingVO != null) {
userVmResponse.setDiskOfferingId(userVm.getDiskOfferingUuid());
userVmResponse.setDiskOfferingName(userVm.getDiskOfferingName());
}
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.backoff)) {
userVmResponse.setBackupOfferingId(userVm.getBackupOfferingUuid());
userVmResponse.setBackupOfferingName(userVm.getBackupOfferingName());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.servoff) || details.contains(VMDetails.stats)) {
userVmResponse.setCpuNumber(userVm.getCpu());
userVmResponse.setCpuSpeed(userVm.getSpeed());
userVmResponse.setMemory(userVm.getRamSize());
ServiceOfferingDetailsVO serviceOfferingDetail = ApiDBUtils.findServiceOfferingDetail(userVm.getServiceOfferingId(), GPU.Keys.vgpuType.toString());
if (serviceOfferingDetail != null) {
userVmResponse.setVgpu(serviceOfferingDetail.getValue());
}
}
userVmResponse.setGuestOsId(userVm.getGuestOsUuid());
if (details.contains(VMDetails.all) || details.contains(VMDetails.volume)) {
userVmResponse.setRootDeviceId(userVm.getVolumeDeviceId());
if (userVm.getVolumeType() != null) {
userVmResponse.setRootDeviceType(userVm.getVolumeType().toString());
}
}
userVmResponse.setPassword(userVm.getPassword());
if (userVm.getJobId() != null) {
userVmResponse.setJobId(userVm.getJobUuid());
userVmResponse.setJobStatus(userVm.getJobStatus());
}
// userVmResponse.setForVirtualNetwork(userVm.getForVirtualNetwork());
userVmResponse.setPublicIpId(userVm.getPublicIpUuid());
userVmResponse.setPublicIp(userVm.getPublicIpAddress());
userVmResponse.setKeyPairName(userVm.getKeypairName());
userVmResponse.setOsTypeId(userVm.getGuestOsUuid());
GuestOS guestOS = ApiDBUtils.findGuestOSById(userVm.getGuestOsId());
if (guestOS != null) {
userVmResponse.setOsDisplayName(guestOS.getDisplayName());
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.stats)) {
// stats calculation
VmStats vmStats = ApiDBUtils.getVmStatistics(userVm.getId());
if (vmStats != null) {
userVmResponse.setCpuUsed(new DecimalFormat("#.##").format(vmStats.getCPUUtilization()) + "%");
userVmResponse.setNetworkKbsRead((long) vmStats.getNetworkReadKBs());
userVmResponse.setNetworkKbsWrite((long) vmStats.getNetworkWriteKBs());
userVmResponse.setDiskKbsRead((long) vmStats.getDiskReadKBs());
userVmResponse.setDiskKbsWrite((long) vmStats.getDiskWriteKBs());
userVmResponse.setDiskIORead((long) vmStats.getDiskReadIOs());
userVmResponse.setDiskIOWrite((long) vmStats.getDiskWriteIOs());
long totalMemory = (long) vmStats.getMemoryKBs();
long freeMemory = (long) vmStats.getIntFreeMemoryKBs();
long correctedFreeMemory = freeMemory >= totalMemory ? 0 : freeMemory;
userVmResponse.setMemoryKBs(totalMemory);
userVmResponse.setMemoryIntFreeKBs(correctedFreeMemory);
userVmResponse.setMemoryTargetKBs((long) vmStats.getTargetMemoryKBs());
}
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.secgrp)) {
Long securityGroupId = userVm.getSecurityGroupId();
if (securityGroupId != null && securityGroupId.longValue() != 0) {
SecurityGroupResponse resp = new SecurityGroupResponse();
resp.setId(userVm.getSecurityGroupUuid());
resp.setName(userVm.getSecurityGroupName());
resp.setDescription(userVm.getSecurityGroupDescription());
resp.setObjectName("securitygroup");
if (userVm.getAccountType() == Account.ACCOUNT_TYPE_PROJECT) {
resp.setProjectId(userVm.getProjectUuid());
resp.setProjectName(userVm.getProjectName());
} else {
resp.setAccountName(userVm.getAccountName());
}
userVmResponse.addSecurityGroup(resp);
}
}
if (details.contains(VMDetails.all) || details.contains(VMDetails.nics)) {
long nic_id = userVm.getNicId();
if (nic_id > 0) {
NicResponse nicResponse = new NicResponse();
nicResponse.setId(userVm.getNicUuid());
nicResponse.setIpaddress(userVm.getIpAddress());
nicResponse.setGateway(userVm.getGateway());
nicResponse.setNetmask(userVm.getNetmask());
nicResponse.setNetworkid(userVm.getNetworkUuid());
nicResponse.setNetworkName(userVm.getNetworkName());
nicResponse.setMacAddress(userVm.getMacAddress());
nicResponse.setIp6Address(userVm.getIp6Address());
nicResponse.setIp6Gateway(userVm.getIp6Gateway());
nicResponse.setIp6Cidr(userVm.getIp6Cidr());
if (userVm.getBroadcastUri() != null) {
nicResponse.setBroadcastUri(userVm.getBroadcastUri().toString());
}
if (userVm.getIsolationUri() != null) {
nicResponse.setIsolationUri(userVm.getIsolationUri().toString());
}
if (userVm.getTrafficType() != null) {
nicResponse.setTrafficType(userVm.getTrafficType().toString());
}
if (userVm.getGuestType() != null) {
nicResponse.setType(userVm.getGuestType().toString());
}
nicResponse.setIsDefault(userVm.isDefaultNic());
nicResponse.setDeviceId(String.valueOf(userVm.getNicDeviceId()));
List<NicSecondaryIpVO> secondaryIps = ApiDBUtils.findNicSecondaryIps(userVm.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);
}
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);
userVmResponse.addNic(nicResponse);
}
}
// update tag information
long tag_id = userVm.getTagId();
if (tag_id > 0 && !userVmResponse.containTag(tag_id)) {
addTagInformation(userVm, userVmResponse);
}
userVmResponse.setHasAnnotation(annotationDao.hasAnnotations(userVm.getUuid(), AnnotationService.EntityType.VM.name(), _accountMgr.isRootAdmin(caller.getId())));
if (details.contains(VMDetails.all) || details.contains(VMDetails.affgrp)) {
Long affinityGroupId = userVm.getAffinityGroupId();
if (affinityGroupId != null && affinityGroupId.longValue() != 0) {
AffinityGroupResponse resp = new AffinityGroupResponse();
resp.setId(userVm.getAffinityGroupUuid());
resp.setName(userVm.getAffinityGroupName());
resp.setDescription(userVm.getAffinityGroupDescription());
resp.setObjectName("affinitygroup");
resp.setAccountName(userVm.getAccountName());
userVmResponse.addAffinityGroup(resp);
}
}
// set resource details map
// Allow passing details to end user
// Honour the display field and only return if display is set to true
List<UserVmDetailVO> vmDetails = _userVmDetailsDao.listDetails(userVm.getId(), true);
if (vmDetails != null) {
Map<String, String> resourceDetails = new HashMap<String, String>();
for (UserVmDetailVO userVmDetailVO : vmDetails) {
if (!userVmDetailVO.getName().startsWith(ApiConstants.PROPERTIES) || (UserVmManager.DisplayVMOVFProperties.value() && userVmDetailVO.getName().startsWith(ApiConstants.PROPERTIES))) {
resourceDetails.put(userVmDetailVO.getName(), userVmDetailVO.getValue());
}
if ((ApiConstants.BootType.UEFI.toString()).equalsIgnoreCase(userVmDetailVO.getName())) {
userVmResponse.setBootType("Uefi");
userVmResponse.setBootMode(userVmDetailVO.getValue().toLowerCase());
}
}
if (vmDetails.size() == 0) {
userVmResponse.setBootType("Bios");
userVmResponse.setBootMode("legacy");
}
if (userVm.getPoolType() != null) {
userVmResponse.setPoolType(userVm.getPoolType().toString());
}
// Remove deny listed settings if user is not admin
if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
String[] userVmSettingsToHide = QueryService.UserVMDeniedDetails.value().split(",");
for (String key : userVmSettingsToHide) {
resourceDetails.remove(key.trim());
}
}
userVmResponse.setDetails(resourceDetails);
if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) {
userVmResponse.setReadOnlyDetails(QueryService.UserVMReadOnlyDetails.value());
}
}
userVmResponse.setObjectName(objectName);
if (userVm.isDynamicallyScalable() == null) {
userVmResponse.setDynamicallyScalable(false);
} else {
userVmResponse.setDynamicallyScalable(userVm.isDynamicallyScalable());
}
addVmRxTxDataToResponse(userVm, userVmResponse);
return userVmResponse;
}
use of org.apache.cloudstack.api.response.UserVmResponse in project cloudstack by apache.
the class ApiResponseHelper method createNicResponse.
/**
* The resulting Response attempts to be in line with what is returned from
* @see com.cloud.api.query.dao.UserVmJoinDaoImpl#setUserVmResponse(ResponseView, UserVmResponse, UserVmJoinVO)
*/
@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());
List<NicExtraDhcpOptionVO> nicExtraDhcpOptionVOs = _nicExtraDhcpOptionDao.listByNicId(result.getId());
// The numbered comments are to keep track of the data returned from here and UserVmJoinDaoImpl.setUserVmResponse()
// the data can't be identical but some tidying up/unifying might be possible
/*1: nicUuid*/
response.setId(result.getUuid());
/*2: networkUuid*/
response.setNetworkid(network.getUuid());
/*3: vmId*/
if (vm != null) {
response.setVmId(vm.getUuid());
}
if (network.getTrafficType() != null) {
/*4: trafficType*/
response.setTrafficType(network.getTrafficType().toString());
}
if (network.getGuestType() != null) {
/*5: guestType*/
response.setType(network.getGuestType().toString());
}
/*6: ipAddress*/
response.setIpaddress(result.getIPv4Address());
/*7: gateway*/
response.setGateway(result.getIPv4Gateway());
/*8: netmask*/
response.setNetmask(result.getIPv4Netmask());
/*9: networkName*/
response.setNetworkName(network.getName());
/*10: macAddress*/
response.setMacAddress(result.getMacAddress());
/*11: IPv6Address*/
if (result.getIPv6Address() != null) {
response.setIp6Address(result.getIPv6Address());
}
/*12: IPv6Gateway*/
if (result.getIPv6Gateway() != null) {
response.setIp6Gateway(result.getIPv6Gateway());
}
/*13: IPv6Cidr*/
if (result.getIPv6Cidr() != null) {
response.setIp6Cidr(result.getIPv6Cidr());
}
/*14: deviceId*/
response.setDeviceId(String.valueOf(result.getDeviceId()));
/*15: broadcastURI*/
if (result.getBroadcastUri() != null) {
response.setBroadcastUri(result.getBroadcastUri().toString());
}
/*16: isolationURI*/
if (result.getIsolationUri() != null) {
response.setIsolationUri(result.getIsolationUri().toString());
}
/*17: default*/
response.setIsDefault(result.isDefaultNic());
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());
setResponseIpAddress(ip, ipRes);
ipList.add(ipRes);
}
response.setSecondaryIps(ipList);
}
}
/*18: extra dhcp options */
List<NicExtraDhcpOptionResponse> nicExtraDhcpOptionResponses = nicExtraDhcpOptionVOs.stream().map(vo -> new NicExtraDhcpOptionResponse(Dhcp.DhcpOptionCode.valueOfInt(vo.getCode()).getName(), vo.getCode(), vo.getValue())).collect(Collectors.toList());
response.setExtraDhcpOptions(nicExtraDhcpOptionResponses);
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;
}
use of org.apache.cloudstack.api.response.UserVmResponse in project cloudstack by apache.
the class KeystoreTest method testUuid.
/*
public void testKeystoreSave() throws Exception {
KeystoreVO ksVo;
ComponentLocator locator = ComponentLocator.getCurrentLocator();
KeystoreDao ksDao = locator.getDao(KeystoreDao.class);
ksDao.save("CPVMCertificate", "CPVMCertificate", "KeyForCertificate", "realhostip.com");
ksVo = ksDao.findByName("CPVMCertificate");
assertTrue(ksVo != null);
assertTrue(ksVo.getCertificate().equals("CPVMCertificate"));
assertTrue(ksVo.getKey().equals("KeyForCertificate"));
assertTrue(ksVo.getDomainSuffix().equals("realhostip.com"));
ksDao.save("CPVMCertificate", "CPVMCertificate Again", "KeyForCertificate Again", "again.realhostip.com");
ksVo = ksDao.findByName("CPVMCertificate");
assertTrue(ksVo != null);
assertTrue(ksVo.getCertificate().equals("CPVMCertificate Again"));
assertTrue(ksVo.getKey().equals("KeyForCertificate Again"));
assertTrue(ksVo.getDomainSuffix().equals("again.realhostip.com"));
ksDao.expunge(ksVo.getId());
}
public void testStripeKey() throws Exception {
Pattern regex = Pattern.compile("(^[\\-]+[^\\-]+[\\-]+[\\n]?)([^\\-]+)([\\-]+[^\\-]+[\\-]+$)");
Matcher m = regex.matcher("-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAm4bLUORp9oM65GV9XrPrbs+K563DjUR1M8mP1HaE+Y4lX5pk\nvQjC/xoEqSs5pxDDWXAkoexvxij8A4AWcsKU1Q+ep2E+GcytBoz8XINGvgb8cQNn\n/4PlVWKp7j5SDDNCfleYvmiRn8k6P4mxVJOHKzwb/IwQcKghyqAF1w==\n-----END RSA PRIVATE KEY-----");
if(m.find()) {
String content = m.group(2);
assertTrue(content.startsWith("MIIEpAIBAAKCAQE"));
assertTrue(content.endsWith("KghyqAF1w==\n"));
} else {
assertTrue(false);
}
}
public void testKeystoreManager() throws Exception {
ComponentLocator locator = ComponentLocator.getCurrentLocator();
KeystoreManagerImpl ksMgr = ComponentLocator.inject(KeystoreManagerImpl.class);
assertTrue(ksMgr.configure("TaskManager", new HashMap<String, Object>()));
assertTrue(ksMgr.start());
ksMgr.saveCertificate("CPVMCertificate", certContent, keyContent, "realhostip.com");
byte[] ksBits = ksMgr.getKeystoreBits("CPVMCertificate", "realhostip", "vmops.com");
assertTrue(ksBits != null);
try {
KeyStore ks = CertificateHelper.loadKeystore(ksBits, "vmops.com");
assertTrue(ks != null);
} catch(Exception e) {
assertTrue(false);
}
KeystoreDao ksDao = locator.getDao(KeystoreDao.class);
KeystoreVO ksVo = ksDao.findByName("CPVMCertificate");
ksDao.expunge(ksVo.getId());
}
*/
public void testUuid() {
UserVmResponse vm = new UserVmResponse();
vm.setId(Long.toString(3L));
/*
vm.setAccountName("admin");
vm.setName("i-2-3-KY");
vm.setDisplayName("i-2-3-KY");
vm.setDomainId(1L);
vm.setDomainName("ROOT");
vm.setCreated(new Date());
vm.setState("Running");
vm.setZoneId(1L);
vm.setZoneName("KY");
vm.setHostId(1L);
vm.setObjectName("virtualmachine");
*/
String result = ApiSerializerHelper.toSerializedString(vm);
System.out.println(result);
// Object obj = ApiSerializerHelper.fromSerializedString(result);
AlertResponse alert = new AlertResponse();
alert.setId("100");
alert.setDescription("Hello");
result = ApiSerializerHelper.toSerializedString(alert);
System.out.println(result);
ApiSerializerHelper.fromSerializedString(result);
}
use of org.apache.cloudstack.api.response.UserVmResponse 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;
Account caller = CallContext.current().getCallingAccount();
if (_accountMgr.isRootAdmin(caller.getId())) {
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;
}
use of org.apache.cloudstack.api.response.UserVmResponse 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());
}
Aggregations