From 9208d0f8f723ba952518b8e65b23ba195a2fa8e1 Mon Sep 17 00:00:00 2001 From: bayuzhen Date: Wed, 6 Nov 2024 16:00:11 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E6=A0=B9=E6=8D=AE=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=B8=AD=E5=BF=83=E6=9F=A5=E8=AF=A2=E8=99=9A=E6=8B=9F=E6=9C=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cmp/controller/CloudServerController.java | 13 ++ .../cmp/repository/CloudVendorRepository.java | 9 + .../cmp/service/CloudServerService.java | 1 + .../service/impl/CloudServerServiceImpl.java | 170 ++++++++++++++++++ 4 files changed, 193 insertions(+) diff --git a/src/main/java/com/bocloud/cmp/controller/CloudServerController.java b/src/main/java/com/bocloud/cmp/controller/CloudServerController.java index a86ae55..7111ec5 100644 --- a/src/main/java/com/bocloud/cmp/controller/CloudServerController.java +++ b/src/main/java/com/bocloud/cmp/controller/CloudServerController.java @@ -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> vmsOfDataCenter(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) { + return cloudServerService.vmsOfDataCenter(pager, context); + } + /** * 云主机列表查询 * diff --git a/src/main/java/com/bocloud/cmp/repository/CloudVendorRepository.java b/src/main/java/com/bocloud/cmp/repository/CloudVendorRepository.java index 6410e31..95abbfc 100644 --- a/src/main/java/com/bocloud/cmp/repository/CloudVendorRepository.java +++ b/src/main/java/com/bocloud/cmp/repository/CloudVendorRepository.java @@ -84,6 +84,15 @@ public class CloudVendorRepository extends BasicGenericDao { } return list.get(0); } + public List getByDcName(String dcName) { + String sql = "select * from cloud_vendor where dc_name = :dcName and is_deleted = 0"; + Map params = MapTools.simpleMap("dcName", dcName); + List list = this.list(CloudVendor.class, sql, params); + if (list.isEmpty()) { + return null; + } + return list; + } public List list(int page, int rows, List params, Map sorter, List vendorIds) { diff --git a/src/main/java/com/bocloud/cmp/service/CloudServerService.java b/src/main/java/com/bocloud/cmp/service/CloudServerService.java index 1c86484..366c88b 100644 --- a/src/main/java/com/bocloud/cmp/service/CloudServerService.java +++ b/src/main/java/com/bocloud/cmp/service/CloudServerService.java @@ -195,4 +195,5 @@ public interface CloudServerService { */ GeneralResult selectAllByVendorId(Long vendorId, RequestContext context); + GeneralResult> vmsOfDataCenter(Pager pager, RequestContext context); } diff --git a/src/main/java/com/bocloud/cmp/service/impl/CloudServerServiceImpl.java b/src/main/java/com/bocloud/cmp/service/impl/CloudServerServiceImpl.java index 923f7ad..55b6694 100644 --- a/src/main/java/com/bocloud/cmp/service/impl/CloudServerServiceImpl.java +++ b/src/main/java/com/bocloud/cmp/service/impl/CloudServerServiceImpl.java @@ -286,6 +286,176 @@ public class CloudServerServiceImpl implements CloudServerService { } } + @Override + public GeneralResult> vmsOfDataCenter(Pager pager, RequestContext context) { + GridBean gridBean; + try { + List params = Optional.ofNullable(pager.getParams()).orElse(new ArrayList<>()); + Map 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 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 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 list = Lists.newArrayList(); + if (context.getCatalog().equals(Catalog.Tenant)) { + if (simple) { + rows = Integer.MAX_VALUE; + } + // 租户能看到 1.属于当前租户的资源 2.租户下所有项目的资源 + List tenantProjects = smsProjectService.listTenantProjects(context.getTenant()); + List 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 userManagerProjects = smsProjectService.listUserManagerProjects(context.getTarget()); + List visibleProjects = smsProjectService.listUserVisibleProjects(context.getTarget()); + List projectIds = new ArrayList<>(); + List userManagerProjectsIds = userManagerProjects.stream().map(Project::getId) + .collect(Collectors.toList()); + if (!userManagerProjectsIds.isEmpty()) { + projectIds.addAll(userManagerProjectsIds); + } + List 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 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 tenantIds = new ArrayList<>(); + List userIds = new ArrayList<>(); + List businessIds = new ArrayList<>(); + List 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 userMap = smsUserService.listUsersByIds(userIds).stream() + .collect(Collectors.toMap(User::getId, User::getName)); + Map projectMap = smsProjectService.listProjectsByIds(projectIds).stream() + .collect(Collectors.toMap(Project::getId, Project::getName)); + Map businessMap = smsBusinessService.listBusinesssByIds(businessIds).stream() + .collect(Collectors.toMap(Business::getId, Business::getName)); + Map 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()) {