add 根据数据中心查询虚拟机

develop
bayuzhen 2024-11-06 16:00:11 +08:00
parent 3a252bfafb
commit 9208d0f8f7
4 changed files with 193 additions and 0 deletions

View File

@ -38,6 +38,19 @@ public class CloudServerController {
return cloudServerService.list(pager, context);
}
/**
*
*
* @param pager
* @param context
* @return
*/
@Operation(tags = {"CMC", "CSC"}, summary = "查询云主机列表")
@GetMapping("/vmsOfDataCenter")
public GeneralResult<GridBean<CloudServer>> vmsOfDataCenter(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
return cloudServerService.vmsOfDataCenter(pager, context);
}
/**
*
*

View File

@ -84,6 +84,15 @@ public class CloudVendorRepository extends BasicGenericDao<CloudVendor, Long> {
}
return list.get(0);
}
public List<CloudVendor> getByDcName(String dcName) {
String sql = "select * from cloud_vendor where dc_name = :dcName and is_deleted = 0";
Map<String, Object> params = MapTools.simpleMap("dcName", dcName);
List<CloudVendor> list = this.list(CloudVendor.class, sql, params);
if (list.isEmpty()) {
return null;
}
return list;
}
public List<CloudVendor> list(int page, int rows, List<Param> params, Map<String, String> sorter,
List<Long> vendorIds) {

View File

@ -195,4 +195,5 @@ public interface CloudServerService {
*/
GeneralResult selectAllByVendorId(Long vendorId, RequestContext context);
GeneralResult<GridBean<CloudServer>> vmsOfDataCenter(Pager pager, RequestContext context);
}

View File

@ -286,6 +286,176 @@ public class CloudServerServiceImpl implements CloudServerService {
}
}
@Override
public GeneralResult<GridBean<CloudServer>> vmsOfDataCenter(Pager pager, RequestContext context) {
GridBean<CloudServer> gridBean;
try {
List<Param> params = Optional.ofNullable(pager.getParams()).orElse(new ArrayList<>());
Map<String, String> sorter = Optional.ofNullable(pager.getSorter()).orElse(Maps.newHashMap());
Boolean simple = Optional.ofNullable(pager.getSimple()).orElse(false);
sorter.put("gmtCreate", Common.ONE);
sorter.put("id", Common.ONE);
Integer page = Optional.ofNullable(pager.getPage()).orElse(1);
Integer rows = Optional.ofNullable(pager.getRows()).orElse(10);
Long clusterId = null;
String labelContent = null;
String dcName = null;
for (Param param : params) {
Map<String, Object> paramMap = param.getParam();
if (paramMap.containsKey("clusterId")) {
clusterId = Long.valueOf(paramMap.get("clusterId").toString());
paramMap.remove("clusterId");
}
if (paramMap.containsKey("labelContent")) {
labelContent = paramMap.get("labelContent").toString();
paramMap.remove("labelContent");
}
if (paramMap.containsKey("dcName")) {
dcName = paramMap.get("dcName").toString();
paramMap.remove("dcName");
// 查询相应云平台
List<CloudVendor> cloudVendors = cloudVendorRepository.getByDcName(dcName);
String ids = cloudVendors.stream().map(CloudVendor::getId).map(item -> String.valueOf(item)).collect(Collectors.joining(","));
paramMap.put("vendorId", ids);
param.setSign(Sign.IN);
}
}
int total = 0;
List<CloudServer> list = Lists.newArrayList();
if (context.getCatalog().equals(Catalog.Tenant)) {
if (simple) {
rows = Integer.MAX_VALUE;
}
// 租户能看到 1.属于当前租户的资源 2.租户下所有项目的资源
List<Project> tenantProjects = smsProjectService.listTenantProjects(context.getTenant());
List<Long> projectIds = tenantProjects.stream().map(Project::getId).collect(Collectors.toList());
total = this.cloudServerRepository.count(null, context.getTenant(), projectIds, params, labelContent);
list = this.cloudServerRepository.list(page, rows, null, context.getTenant(), projectIds, params,
sorter, labelContent);
gridBean = new GridBean<>(page, rows, total, list);
} else if (context.getCatalog().equals(Catalog.User)) {
if (simple) {
rows = Integer.MAX_VALUE;
}
// 用户能看到 1.自己申请的资源 2.自己所在的资源可见项目(资源对项目下所有成员可见)下创建的资源 3.自己管理的项目下的资源
List<Project> userManagerProjects = smsProjectService.listUserManagerProjects(context.getTarget());
List<Project> visibleProjects = smsProjectService.listUserVisibleProjects(context.getTarget());
List<Long> projectIds = new ArrayList<>();
List<Long> userManagerProjectsIds = userManagerProjects.stream().map(Project::getId)
.collect(Collectors.toList());
if (!userManagerProjectsIds.isEmpty()) {
projectIds.addAll(userManagerProjectsIds);
}
List<Long> visibleProjectsIds = visibleProjects.stream().map(Project::getId)
.collect(Collectors.toList());
if (!visibleProjectsIds.isEmpty()) {
projectIds.addAll(visibleProjectsIds);
}
total = this.cloudServerRepository.count(context.getTarget(), null, projectIds, params, labelContent);
list = this.cloudServerRepository.list(page, rows, context.getTarget(), null, projectIds, params,
sorter, labelContent);
gridBean = new GridBean<>(page, rows, total, list);
} else {
// 管理端搜索
if (null != clusterId) {
total = this.cloudServerRepository.count(clusterId, params);
list = this.cloudServerRepository.list(page, rows, clusterId, params, sorter);
gridBean = new GridBean<>(page, rows, total, list);
} else if (simple) {
total = this.cloudServerRepository.count(params, 0L);
List<SimpleBean> beans = this.cloudServerRepository.list(params, sorter);
gridBean = new GridBean(1, 1, total, beans);
} else {
total = this.cloudServerRepository.vmCount(params, labelContent);
list = this.cloudServerRepository.list(page, rows, params, sorter, labelContent);
gridBean = new GridBean<>(page, rows, total, list);
}
}
if (!simple) {
List<Long> tenantIds = new ArrayList<>();
List<Long> userIds = new ArrayList<>();
List<Long> businessIds = new ArrayList<>();
List<Long> projectIds = new ArrayList<>();
for (CloudServer cloudServer : list) {
if (cloudServer.getTenantId() != null) {
tenantIds.add(cloudServer.getTenantId());
}
if (cloudServer.getBusinessId() != null) {
businessIds.add(cloudServer.getBusinessId());
}
if (cloudServer.getProjectId() != null) {
projectIds.add(cloudServer.getProjectId());
}
if (cloudServer.getCreatorId() != null) {
userIds.add(cloudServer.getCreatorId());
}
if (cloudServer.getOwnerId() != null) {
userIds.add(cloudServer.getOwnerId());
}
if (cloudServer.getMenderId() != null) {
userIds.add(cloudServer.getMenderId());
}
}
Map<Long, String> userMap = smsUserService.listUsersByIds(userIds).stream()
.collect(Collectors.toMap(User::getId, User::getName));
Map<Long, String> projectMap = smsProjectService.listProjectsByIds(projectIds).stream()
.collect(Collectors.toMap(Project::getId, Project::getName));
Map<Long, String> businessMap = smsBusinessService.listBusinesssByIds(businessIds).stream()
.collect(Collectors.toMap(Business::getId, Business::getName));
Map<Long, String> tenantMap = smsTenantService.listTenantsByIds(tenantIds).stream()
.collect(Collectors.toMap(Tenant::getId, Tenant::getName));
for (CloudServer server : list) {
// 设置云主机归属信息
if (server.getTenantId() != null) {
server.setTenantName(tenantMap.get(server.getTenantId()));
}
if (server.getBusinessId() != null) {
server.setBusinessName(businessMap.get(server.getBusinessId()));
}
if (server.getProjectId() != null) {
server.setProjectName(projectMap.get(server.getProjectId()));
}
if (server.getCreatorId() != null) {
server.setCreatorName(userMap.get(server.getCreatorId()));
}
if (server.getOwnerId() != null) {
server.setOwnerName(userMap.get(server.getOwnerId()));
}
if (server.getMenderId() != null) {
server.setMenderName(userMap.get(server.getMenderId()));
}
// 设置主机名称
Server host = serverRepository.basic(server.getHostId());
host = Optional.ofNullable(host).orElse(new Server());
server.setHostName(host.getName());
server.setClusterName(host.getClusterName());
server.setVdcName(host.getVdcName());
server.setDisk((null == server.getDisk() || 0 == server.getDisk()) ? server.getTotalDisks()
: server.getDisk());
if (!StringUtils.isEmpty(server.getResourceGroup())) {
String resourceGroupName = dictionaryRepository.getNameByValue(server.getResourceGroup());
server.setResourceGroupName(resourceGroupName);
}
if (server.getVendorType().equalsIgnoreCase("CTSTACK")) {
FloatingIp floatingIp = floatingIpRepository.getByInstanceId(server.getInstanceId());
if (null != floatingIp) {
server.setFipId(floatingIp.getId());
//server.setFloatingIp(floatingIp.getFloatingIpAddress());
if (StringUtils.isEmpty(server.getPublicIps())) {
server.setPublicIps(JSON.toJSONString(Collections.singletonList(floatingIp.getFloatingIpAddress())));
}
}
}
}
gridBean.setRows(list);
}
return new GeneralResult<>(true, gridBean, "查询成功");
} catch (Exception e) {
log.error("Query vm list fail:{}", e.getMessage());
return new GeneralResult<>(false, "查询失败");
}
}
public GeneralResult snapshotQuotasCallBack(RequestContext context, Action action) {
try {
if (null == context.getCatalog()) {