|
54 | 54 | import com.cloud.configuration.ResourceCount.ResourceType; |
55 | 55 | import com.cloud.configuration.ResourceLimit; |
56 | 56 | import com.cloud.dc.ClusterVO; |
| 57 | +import com.cloud.dc.DataCenter; |
57 | 58 | import com.cloud.dc.DataCenterVO; |
58 | 59 | import com.cloud.dc.HostPodVO; |
59 | 60 | import com.cloud.dc.Vlan.VlanType; |
|
67 | 68 | import com.cloud.network.FirewallRuleVO; |
68 | 69 | import com.cloud.network.IPAddressVO; |
69 | 70 | import com.cloud.network.LoadBalancerVO; |
| 71 | +import com.cloud.network.Network.TrafficType; |
| 72 | +import com.cloud.network.NetworkConfiguration; |
70 | 73 | import com.cloud.offering.NetworkOffering.GuestIpType; |
71 | 74 | import com.cloud.offering.ServiceOffering; |
72 | 75 | import com.cloud.server.Criteria; |
|
91 | 94 | import com.cloud.vm.ConsoleProxyVO; |
92 | 95 | import com.cloud.vm.DomainRouter; |
93 | 96 | import com.cloud.vm.InstanceGroupVO; |
| 97 | +import com.cloud.vm.Nic; |
94 | 98 | import com.cloud.vm.SecondaryStorageVmVO; |
95 | 99 | import com.cloud.vm.State; |
96 | 100 | import com.cloud.vm.SystemVm; |
@@ -377,7 +381,6 @@ public static UserVmResponse createUserVmResponse (UserVm userVm) { |
377 | 381 | userVmResponse.setState(userVm.getState().toString()); |
378 | 382 | } |
379 | 383 |
|
380 | | - |
381 | 384 | userVmResponse.setHaEnable(userVm.isHaEnabled()); |
382 | 385 |
|
383 | 386 | if (userVm.getDisplayName() != null) { |
@@ -985,4 +988,276 @@ public static FirewallRuleResponse createFirewallRuleResponse(FirewallRuleVO fwR |
985 | 988 | return response; |
986 | 989 | } |
987 | 990 |
|
| 991 | + |
| 992 | + |
| 993 | + public static UserVmResponse createUserVm2Response (UserVm userVm) { |
| 994 | + UserVmResponse userVmResponse = new UserVmResponse(); |
| 995 | + Account acct = ApiDBUtils.findAccountById(Long.valueOf(userVm.getAccountId())); |
| 996 | + //FIXME - this check should be done in searchForUserVm method in ManagementServerImpl; |
| 997 | + //otherwise the number of vms returned is not going to match pageSize request parameter |
| 998 | + if ((acct != null) && (acct.getRemoved() == null)) { |
| 999 | + userVmResponse.setAccountName(acct.getAccountName()); |
| 1000 | + userVmResponse.setDomainId(acct.getDomainId()); |
| 1001 | + userVmResponse.setDomainName(ApiDBUtils.findDomainById(acct.getDomainId()).getName()); |
| 1002 | + } else { |
| 1003 | + return null; // the account has been deleted, skip this VM in the response |
| 1004 | + } |
| 1005 | + |
| 1006 | + userVmResponse.setId(userVm.getId()); |
| 1007 | + AsyncJobVO asyncJob = ApiDBUtils.findInstancePendingAsyncJob("vm_instance", userVm.getId()); |
| 1008 | + if (asyncJob != null) { |
| 1009 | + userVmResponse.setJobId(asyncJob.getId()); |
| 1010 | + userVmResponse.setJobStatus(asyncJob.getStatus()); |
| 1011 | + } |
| 1012 | + |
| 1013 | + userVmResponse.setName(userVm.getHostName()); |
| 1014 | + userVmResponse.setCreated(userVm.getCreated()); |
| 1015 | + |
| 1016 | + if (userVm.getState() != null) { |
| 1017 | + userVmResponse.setState(userVm.getState().toString()); |
| 1018 | + } |
| 1019 | + |
| 1020 | + userVmResponse.setHaEnable(userVm.isHaEnabled()); |
| 1021 | + |
| 1022 | + if (userVm.getDisplayName() != null) { |
| 1023 | + userVmResponse.setDisplayName(userVm.getDisplayName()); |
| 1024 | + } else { |
| 1025 | + userVmResponse.setDisplayName(userVm.getHostName()); |
| 1026 | + } |
| 1027 | + |
| 1028 | + InstanceGroupVO group = ApiDBUtils.findInstanceGroupForVM(userVm.getId()); |
| 1029 | + if (group != null) { |
| 1030 | + userVmResponse.setGroup(group.getName()); |
| 1031 | + userVmResponse.setGroupId(group.getId()); |
| 1032 | + } |
| 1033 | + |
| 1034 | + // Data Center Info |
| 1035 | + userVmResponse.setZoneId(userVm.getDataCenterId()); |
| 1036 | + userVmResponse.setZoneName(ApiDBUtils.findZoneById(userVm.getDataCenterId()).getName()); |
| 1037 | + |
| 1038 | + Account account = UserContext.current().getAccount(); |
| 1039 | + //if user is an admin, display host id |
| 1040 | + if (((account == null) || (account.getType() == Account.ACCOUNT_TYPE_ADMIN)) && (userVm.getHostId() != null)) { |
| 1041 | + userVmResponse.setHostId(userVm.getHostId()); |
| 1042 | + userVmResponse.setHostName(ApiDBUtils.findHostById(userVm.getHostId()).getName()); |
| 1043 | + } |
| 1044 | + |
| 1045 | + // Template Info |
| 1046 | + VMTemplateVO template = ApiDBUtils.findTemplateById(userVm.getTemplateId()); |
| 1047 | + if (template != null) { |
| 1048 | + userVmResponse.setTemplateId(userVm.getTemplateId()); |
| 1049 | + userVmResponse.setTemplateName(template.getName()); |
| 1050 | + userVmResponse.setTemplateDisplayText(template.getDisplayText()); |
| 1051 | + userVmResponse.setPasswordEnabled(template.getEnablePassword()); |
| 1052 | + } else { |
| 1053 | + userVmResponse.setTemplateId(-1L); |
| 1054 | + userVmResponse.setTemplateName("ISO Boot"); |
| 1055 | + userVmResponse.setTemplateDisplayText("ISO Boot"); |
| 1056 | + userVmResponse.setPasswordEnabled(false); |
| 1057 | + } |
| 1058 | + |
| 1059 | + if (userVm.getPassword() != null) { |
| 1060 | + userVmResponse.setPassword(userVm.getPassword()); |
| 1061 | + } |
| 1062 | + |
| 1063 | + // ISO Info |
| 1064 | + if (userVm.getIsoId() != null) { |
| 1065 | + VMTemplateVO iso = ApiDBUtils.findTemplateById(userVm.getIsoId().longValue()); |
| 1066 | + if (iso != null) { |
| 1067 | + userVmResponse.setIsoId(userVm.getIsoId()); |
| 1068 | + userVmResponse.setIsoName(iso.getName()); |
| 1069 | + } |
| 1070 | + } |
| 1071 | + |
| 1072 | + // Service Offering Info |
| 1073 | + ServiceOffering offering = ApiDBUtils.findServiceOfferingById(userVm.getServiceOfferingId()); |
| 1074 | + userVmResponse.setServiceOfferingId(userVm.getServiceOfferingId()); |
| 1075 | + userVmResponse.setServiceOfferingName(offering.getName()); |
| 1076 | + userVmResponse.setCpuNumber(offering.getCpu()); |
| 1077 | + userVmResponse.setCpuSpeed(offering.getSpeed()); |
| 1078 | + userVmResponse.setMemory(offering.getRamSize()); |
| 1079 | + |
| 1080 | + VolumeVO rootVolume = ApiDBUtils.findRootVolume(userVm.getId()); |
| 1081 | + if (rootVolume != null) { |
| 1082 | + userVmResponse.setRootDeviceId(rootVolume.getDeviceId()); |
| 1083 | + String rootDeviceType = "Not created"; |
| 1084 | + if (rootVolume.getPoolId() != null){ |
| 1085 | + StoragePoolVO storagePool = ApiDBUtils.findStoragePoolById(rootVolume.getPoolId()); |
| 1086 | + rootDeviceType = storagePool.getPoolType().toString(); |
| 1087 | + } |
| 1088 | + userVmResponse.setRootDeviceType(rootDeviceType); |
| 1089 | + } |
| 1090 | + |
| 1091 | + //stats calculation |
| 1092 | + DecimalFormat decimalFormat = new DecimalFormat("#.##"); |
| 1093 | + String cpuUsed = null; |
| 1094 | + VmStats vmStats = ApiDBUtils.getVmStatistics(userVm.getId()); |
| 1095 | + if (vmStats != null) { |
| 1096 | + float cpuUtil = (float) vmStats.getCPUUtilization(); |
| 1097 | + cpuUsed = decimalFormat.format(cpuUtil) + "%"; |
| 1098 | + userVmResponse.setCpuUsed(cpuUsed); |
| 1099 | + |
| 1100 | + long networkKbRead = (long)vmStats.getNetworkReadKBs(); |
| 1101 | + userVmResponse.setNetworkKbsRead(networkKbRead); |
| 1102 | + |
| 1103 | + long networkKbWrite = (long)vmStats.getNetworkWriteKBs(); |
| 1104 | + userVmResponse.setNetworkKbsWrite(networkKbWrite); |
| 1105 | + } |
| 1106 | + |
| 1107 | + userVmResponse.setGuestOsId(userVm.getGuestOSId()); |
| 1108 | + //network groups |
| 1109 | + userVmResponse.setNetworkGroupList(ApiDBUtils.getNetworkGroupsNamesForVm(userVm.getId())); |
| 1110 | + |
| 1111 | + List<? extends Nic> nics = BaseCmd._networkMgr.getNics(userVm); |
| 1112 | + for (Nic singleNic : nics) { |
| 1113 | + long configId = singleNic.getNetworkConfigurationId(); |
| 1114 | + NetworkConfiguration networkConf = BaseCmd._networkMgr.getNetworkConfiguration(configId); |
| 1115 | + if (networkConf.getTrafficType() == TrafficType.Guest) { |
| 1116 | + userVmResponse.setIpAddress(singleNic.getIp4Address()); |
| 1117 | + } |
| 1118 | + } |
| 1119 | + |
| 1120 | + userVmResponse.setObjectName("virtualmachine"); |
| 1121 | + return userVmResponse; |
| 1122 | + } |
| 1123 | + |
| 1124 | + |
| 1125 | + public static DomainRouterResponse createDomainRouter2Response (DomainRouter router) { |
| 1126 | + DomainRouterResponse routerResponse = new DomainRouterResponse(); |
| 1127 | + routerResponse.setId(router.getId()); |
| 1128 | + routerResponse.setZoneId(router.getDataCenterId()); |
| 1129 | + routerResponse.setName(router.getHostName()); |
| 1130 | + routerResponse.setPodId(router.getPodId()); |
| 1131 | + routerResponse.setTemplateId(router.getTemplateId()); |
| 1132 | + routerResponse.setCreated(router.getCreated()); |
| 1133 | + routerResponse.setState(router.getState()); |
| 1134 | + routerResponse.setNetworkDomain(router.getDomain()); |
| 1135 | + |
| 1136 | + if (router.getHostId() != null) { |
| 1137 | + routerResponse.setHostId(router.getHostId()); |
| 1138 | + routerResponse.setHostName(ApiDBUtils.findHostById(router.getHostId()).getName()); |
| 1139 | + } |
| 1140 | + |
| 1141 | + Account accountTemp = ApiDBUtils.findAccountById(router.getAccountId()); |
| 1142 | + if (accountTemp != null) { |
| 1143 | + routerResponse.setAccountName(accountTemp.getAccountName()); |
| 1144 | + routerResponse.setDomainId(accountTemp.getDomainId()); |
| 1145 | + routerResponse.setDomainName(ApiDBUtils.findDomainById(accountTemp.getDomainId()).getName()); |
| 1146 | + } |
| 1147 | + |
| 1148 | + AsyncJobVO asyncJob = ApiDBUtils.findInstancePendingAsyncJob("domain_router", router.getId()); |
| 1149 | + if (asyncJob != null) { |
| 1150 | + routerResponse.setJobId(asyncJob.getId()); |
| 1151 | + routerResponse.setJobStatus(asyncJob.getStatus()); |
| 1152 | + } |
| 1153 | + |
| 1154 | + List<? extends Nic> nics = BaseCmd._networkMgr.getNics(router); |
| 1155 | + for (Nic singleNic : nics) { |
| 1156 | + long configId = singleNic.getNetworkConfigurationId(); |
| 1157 | + NetworkConfiguration networkConf = BaseCmd._networkMgr.getNetworkConfiguration(configId); |
| 1158 | + |
| 1159 | + if (networkConf.getTrafficType() == TrafficType.Guest) { |
| 1160 | + routerResponse.setGuestIpAddress(singleNic.getIp4Address()); |
| 1161 | + routerResponse.setGuestMacAddress(singleNic.getMacAddress()); |
| 1162 | + routerResponse.setGuestNetmask(singleNic.getNetmask()); |
| 1163 | + } |
| 1164 | + |
| 1165 | + if (networkConf.getTrafficType() == TrafficType.Control) { |
| 1166 | + routerResponse.setPrivateIp(singleNic.getIp4Address()); |
| 1167 | + routerResponse.setPrivateMacAddress(singleNic.getMacAddress()); |
| 1168 | + routerResponse.setPrivateNetmask(singleNic.getNetmask()); |
| 1169 | + } |
| 1170 | + |
| 1171 | + if (networkConf.getTrafficType() == TrafficType.Public) { |
| 1172 | + routerResponse.setPublicIp(singleNic.getIp4Address()); |
| 1173 | + routerResponse.setPublicMacAddress(singleNic.getMacAddress()); |
| 1174 | + routerResponse.setPublicNetmask(singleNic.getNetmask()); |
| 1175 | + routerResponse.setGateway(singleNic.getGateway()); |
| 1176 | + } |
| 1177 | + |
| 1178 | + DataCenter zone = ApiDBUtils.findZoneById(router.getDataCenterId()); |
| 1179 | + if (zone != null) { |
| 1180 | + routerResponse.setZoneName(zone.getName()); |
| 1181 | + routerResponse.setDns1(zone.getDns1()); |
| 1182 | + routerResponse.setDns2(zone.getDns2()); |
| 1183 | + } |
| 1184 | + } |
| 1185 | + |
| 1186 | + routerResponse.setObjectName("domainrouter"); |
| 1187 | + return routerResponse; |
| 1188 | + } |
| 1189 | + |
| 1190 | + |
| 1191 | + public static SystemVmResponse createSystemVm2Response (VMInstanceVO systemVM) { |
| 1192 | + SystemVmResponse vmResponse = new SystemVmResponse(); |
| 1193 | + if (systemVM instanceof SystemVm) { |
| 1194 | + SystemVm vm = (SystemVm)systemVM; |
| 1195 | + |
| 1196 | + vmResponse.setId(vm.getId()); |
| 1197 | + vmResponse.setSystemVmType(vm.getType().toString().toLowerCase()); |
| 1198 | + vmResponse.setZoneId(vm.getDataCenterId()); |
| 1199 | + |
| 1200 | + vmResponse.setNetworkDomain(vm.getDomain()); |
| 1201 | + vmResponse.setName(vm.getHostName()); |
| 1202 | + vmResponse.setPodId(vm.getPodId()); |
| 1203 | + vmResponse.setTemplateId(vm.getTemplateId()); |
| 1204 | + vmResponse.setCreated(vm.getCreated()); |
| 1205 | + |
| 1206 | + if (vm.getHostId() != null) { |
| 1207 | + vmResponse.setHostId(vm.getHostId()); |
| 1208 | + vmResponse.setHostName(ApiDBUtils.findHostById(vm.getHostId()).getName()); |
| 1209 | + } |
| 1210 | + |
| 1211 | + if (vm.getState() != null) { |
| 1212 | + vmResponse.setState(vm.getState().toString()); |
| 1213 | + } |
| 1214 | + |
| 1215 | + String instanceType = "console_proxy"; |
| 1216 | + if (systemVM instanceof SecondaryStorageVmVO) { |
| 1217 | + instanceType = "sec_storage_vm"; // FIXME: this should be a constant so that the async jobs get updated with the correct instance type, they are using |
| 1218 | + // different instance types at the moment |
| 1219 | + } |
| 1220 | + |
| 1221 | + AsyncJobVO asyncJob = ApiDBUtils.findInstancePendingAsyncJob(instanceType, vm.getId()); |
| 1222 | + if (asyncJob != null) { |
| 1223 | + vmResponse.setJobId(asyncJob.getId()); |
| 1224 | + vmResponse.setJobStatus(asyncJob.getStatus()); |
| 1225 | + } |
| 1226 | + |
| 1227 | + // for console proxies, add the active sessions |
| 1228 | + if (systemVM instanceof ConsoleProxyVO) { |
| 1229 | + ConsoleProxyVO proxy = (ConsoleProxyVO)systemVM; |
| 1230 | + vmResponse.setActiveViewerSessions(proxy.getActiveSession()); |
| 1231 | + } |
| 1232 | + |
| 1233 | + DataCenter zone = ApiDBUtils.findZoneById(vm.getDataCenterId()); |
| 1234 | + if (zone != null) { |
| 1235 | + vmResponse.setZoneName(zone.getName()); |
| 1236 | + vmResponse.setDns1(zone.getDns1()); |
| 1237 | + vmResponse.setDns2(zone.getDns2()); |
| 1238 | + } |
| 1239 | + |
| 1240 | + List<? extends Nic> nics = BaseCmd._networkMgr.getNics(systemVM); |
| 1241 | + for (Nic singleNic : nics) { |
| 1242 | + long configId = singleNic.getNetworkConfigurationId(); |
| 1243 | + NetworkConfiguration networkConf = BaseCmd._networkMgr.getNetworkConfiguration(configId); |
| 1244 | + |
| 1245 | + if (networkConf.getTrafficType() == TrafficType.Management) { |
| 1246 | + vmResponse.setPrivateIp(singleNic.getIp4Address()); |
| 1247 | + vmResponse.setPrivateMacAddress(singleNic.getMacAddress()); |
| 1248 | + vmResponse.setPrivateNetmask(singleNic.getNetmask()); |
| 1249 | + } |
| 1250 | + |
| 1251 | + if (networkConf.getTrafficType() == TrafficType.Public) { |
| 1252 | + vmResponse.setPublicIp(singleNic.getIp4Address()); |
| 1253 | + vmResponse.setPublicMacAddress(singleNic.getMacAddress()); |
| 1254 | + vmResponse.setPublicNetmask(singleNic.getNetmask()); |
| 1255 | + vmResponse.setGateway(singleNic.getGateway()); |
| 1256 | + } |
| 1257 | + } |
| 1258 | + } |
| 1259 | + |
| 1260 | + vmResponse.setObjectName("systemvm"); |
| 1261 | + return vmResponse; |
| 1262 | + } |
988 | 1263 | } |
0 commit comments