From 387a702d177c0a76628e22720992f54357846da5 Mon Sep 17 00:00:00 2001 From: tanshaolong Date: Mon, 9 Sep 2024 15:36:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=A9=E7=BF=BC=E4=BA=91?= =?UTF-8?q?=E5=AE=B9=E5=99=A8=E9=9B=86=E7=BE=A4=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ContainerController.java | 67 +++++++ .../container/ContainerClusterModel.java | 2 + .../container/ContainerNodePoolModel.java | 4 +- .../plugin/entity/ContainerCluster.java | 6 + .../TianYiContainerClusterProvider.java | 4 +- .../TianYiContainerNodeProvider.java | 4 + .../ContainerNodePoolConvertor.java | 3 +- .../ContainerClusterRepository.java | 16 ++ .../ContainerNodePoolRepository.java | 47 +++++ .../repository/ContainerNodeRepository.java | 45 +++++ .../plugin/service/ContainerService.java | 15 ++ .../service/impl/ContainerServiceImpl.java | 182 +++++++++++++++++- 12 files changed, 387 insertions(+), 8 deletions(-) diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/controller/ContainerController.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/controller/ContainerController.java index c65466e7..5af21417 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/controller/ContainerController.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/controller/ContainerController.java @@ -1,7 +1,10 @@ package com.bocloud.ctstack.plugin.controller; import com.bocloud.ctstack.plugin.domain.model.container.ContainerClusterModel; +import com.bocloud.ctstack.plugin.domain.model.container.ContainerNodePoolModel; import com.bocloud.ctstack.plugin.entity.ContainerCluster; +import com.bocloud.ctstack.plugin.entity.ContainerClusterNode; +import com.bocloud.ctstack.plugin.entity.ContainerClusterNodePool; import com.bocloud.ctstack.plugin.service.ContainerService; import com.megatron.common.model.GeneralResult; import com.megatron.common.model.GridBean; @@ -34,6 +37,42 @@ public class ContainerController { return containerService.list(pager); } + /** + * 查看集群详情 + * + * @param id + * @return + */ + @Operation(tags = {"CMC", "CSC"}, summary = "查询集群详情") + @GetMapping("/{id}") + public GeneralResult detail(@PathVariable(value = Common.ID) Long id) { + return containerService.detail(id); + } + + /** + * 查看集群下节点列表 + * + * @param pager + * @return + */ + @Operation(tags = {"CMC", "CSC"}, summary = "查询集群节点列表") + @GetMapping("/nodes") + public GeneralResult> listClusterNodes(Pager pager) { + return containerService.listClusterNodes(pager); + } + + /** + * 查看集群下节点池列表 + * + * @param pager + * @return + */ + @Operation(tags = {"CMC", "CSC"}, summary = "查询集群节点池列表") + @GetMapping("/nodepools") + public GeneralResult> listClusterNodePools(Pager pager) { + return containerService.listClusterNodePools(pager); + } + /** * 添加集群 * @@ -47,4 +86,32 @@ public class ContainerController { @Value(Common.REQ_CONTEXT) RequestContext context) { return containerService.create(model, context); } + + @Operation(tags = {"CMC", "CSC"}, summary = "删除容器集群") + @DeleteMapping("/{id}") + public GeneralResult remove(@PathVariable(value = Common.ID) Long id, + @Value(Common.REQ_CONTEXT) RequestContext context) { + return containerService.remove(id, context); + } + + /** + * 集群创建节点池 + * + * @param model + * @param context + * @return + */ + @Operation(tags = {"CMC", "CSC"}, summary = "创建集群节点池") + @PostMapping("/nodepool") + public GeneralResult createNodePool(@RequestBody ContainerNodePoolModel model, + @Value(Common.REQ_CONTEXT) RequestContext context) { + return containerService.createNodePool(model, context); + } + + @Operation(tags = {"CMC", "CSC"}, summary = "删除容器集群节点池") + @DeleteMapping("nodepool/{id}") + public GeneralResult removeNodePool(@PathVariable(value = Common.ID) Long id, + @Value(Common.REQ_CONTEXT) RequestContext context) { + return containerService.removeNodePool(id, context); + } } diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/container/ContainerClusterModel.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/container/ContainerClusterModel.java index 6ad68505..3beef87e 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/container/ContainerClusterModel.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/container/ContainerClusterModel.java @@ -29,6 +29,8 @@ public class ContainerClusterModel { private String resourceGroupUuid; private String serviceCidr; private Long businessId; + private String prodInstId; // 平台实例ID + private String resPoolId; // 资源池ID private List nodeModels; private List nodePoolModels; diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/container/ContainerNodePoolModel.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/container/ContainerNodePoolModel.java index 143e06f2..f510e1a8 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/container/ContainerNodePoolModel.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/container/ContainerNodePoolModel.java @@ -25,11 +25,13 @@ public class ContainerNodePoolModel { private Date created; private String kubernetesConfig; private String scalingGroup; + private Long vendorId; + // 天翼云的数据 private Long cpu; private Long memory; private String vmSpecName; // 节点规格 private String vmSpecType; // 节点规格类型 - private String vmSpecId; // 节点规格id + private Long vmSpecId; // 节点规格id } diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerCluster.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerCluster.java index 2c37c80d..6ac535ca 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerCluster.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerCluster.java @@ -72,4 +72,10 @@ public class ContainerCluster extends GenericEntity { @Column("is_recycle") private Boolean isRecycle; + + @Column("resource_pool_id") + private String resPoolId; // 资源池ID + + @Column("prod_instance_id") + private String prodInstId; // 平台实例ID } diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/container/TianYiContainerClusterProvider.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/container/TianYiContainerClusterProvider.java index 40631b93..0d5bc64d 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/container/TianYiContainerClusterProvider.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/container/TianYiContainerClusterProvider.java @@ -163,8 +163,8 @@ public class TianYiContainerClusterProvider extends TianyiProvider { headers.put("regionId", this.getRegionId()); Result result = doGet(apiUrl, headers); JSONObject returnObj = checkResult(result, "查询容器集群详情"); - // TODO: - ContainerClusterModel model = JSONObject.parseObject(JSONObject.toJSONString(returnObj), ContainerClusterModel.class); + + ContainerClusterModel model = containerClusterConvertor.convertModel(returnObj); return new GeneralResult(true, model, "查询容器集群详情成功"); } catch (Exception e) { log.error("查询容器集群详情失败 : " + e); diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/container/TianYiContainerNodeProvider.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/container/TianYiContainerNodeProvider.java index add15ca3..81e7a637 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/container/TianYiContainerNodeProvider.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/container/TianYiContainerNodeProvider.java @@ -12,6 +12,7 @@ import com.bocloud.ctstack.plugin.provider.tianyiconvertor.ContainerNodePoolConv import com.megatron.common.model.GeneralResult; import com.megatron.common.model.Result; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; import java.util.HashMap; @@ -64,6 +65,9 @@ public class TianYiContainerNodeProvider extends TianyiProvider { body.put("vmSpecId", poolModel.getVmSpecId()); body.put("cpu", poolModel.getCpu()); body.put("memory", poolModel.getMemory()); + if (!StringUtils.isEmpty(poolModel.getRemark())) { + body.put("description", poolModel.getRemark()); + } Result result = doPost(url, headers, body); JSONObject returnObj = checkResult(result, "创建容器集群"); diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/ContainerNodePoolConvertor.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/ContainerNodePoolConvertor.java index 817ecaf6..0a82264d 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/ContainerNodePoolConvertor.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/ContainerNodePoolConvertor.java @@ -31,10 +31,9 @@ public class ContainerNodePoolConvertor implements BeanConvertor 0; } + + public ContainerCluster query(Long id) { + String sql = "select a.*, v.name vpc_name, s.name subnet_name, s.cidr subnet_cidr, s.subnet_uuid subnet_uuid, g.name security_group_name, os.name enterprise_project_name, r.name region_name from container_cluster a " + + "left join vpc v on v.id = a.vpc_id and v.is_deleted = 0 " + + "left join subnet s on s.id = a.subnet_id and s.is_deleted = 0 " + + "left join security_group g on g.id = a.security_group_id and g.is_deleted = 0 " + + "left join os_tenant os on a.vendor_id = os.vendor_id and a.enterprise_project_id = os.id " + + "left join region r on r.vendor_id = a.vendor_id and r.region_id = a.region_id " + + "where a.is_deleted = 0 and a.id = :id"; + Map params = MapTools.simpleMap(Common.ID, id); + List list = this.list(ContainerCluster.class, sql, params); + if (list.isEmpty()) { + return null; + } + return list.get(0); + } } diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerNodePoolRepository.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerNodePoolRepository.java index cc164fc3..b5b7fa63 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerNodePoolRepository.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerNodePoolRepository.java @@ -1,6 +1,9 @@ package com.bocloud.ctstack.plugin.repository; import com.bocloud.ctstack.plugin.entity.ContainerClusterNodePool; +import com.megatron.common.model.Pager; +import com.megatron.common.model.Param; +import com.megatron.common.model.SimpleBean; import com.megatron.common.utils.Common; import com.megatron.common.utils.MapTools; import com.megatron.database.core.intf.impl.BasicGenericDao; @@ -9,6 +12,7 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.stereotype.Component; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; @@ -20,6 +24,36 @@ public class ContainerNodePoolRepository extends BasicGenericDao params) { + String sql = "select count(1) from container_cluster_node_pool a where a.is_deleted = 0 "; + sql = this.getQueryBuilder().buildRaw(sql, params, null, "a"); + Map paramMap = this.getQueryBuilder().getParam(params); + return this.countQuery(sql, paramMap).intValue(); + } + + public List list(List params, Map sorter) { + String sql = "select a.id, a.name from container_cluster_node_pool a where a.is_deleted = 0 "; + sql = this.getQueryBuilder().build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a"); + Map paramMap = this.getQueryBuilder().getParam(params); + List pools = this.list(ContainerClusterNodePool.class, sql, paramMap); + List beans = new ArrayList(); + for (ContainerClusterNodePool pool : pools) { + beans.add(new SimpleBean(pool.getId(), pool.getName())); + } + return beans; + } + + public List list(int page, int rows, List params, Map sorter) { + String sql = "select a.*,b.name cluster_name,c.name vendor_name, r.name region_name from container_cluster_node_pool a " + + " left join container_cluster b on a.cluster_id = b.id " + + " left join cloud_vendor c on a.vendor_id = c.id " + + " left join region r on a.region_id = r.id and a.vendor_id = r.vendor_id " + + " where a.is_deleted = 0 "; + sql = this.getQueryBuilder().buildRaw(sql, new Pager(page, rows, params, sorter), "a"); + Map paramMap = this.getQueryBuilder().getParam(params); + return this.list(ContainerClusterNodePool.class, sql, paramMap); + } + public boolean removeByVid(Long vendorId, String regionId, Long userId) throws Exception { String sql = "update container_cluster_node set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId " + " and region_id = :regionId "; @@ -57,4 +91,17 @@ public class ContainerNodePoolRepository extends BasicGenericDao 0; } + + public ContainerClusterNodePool query(Long id) { + String sql = "select a.*, os.name enterprise_project_name, r.name region_name from container_cluster_node_pool a " + + "left join os_tenant os on a.vendor_id = os.vendor_id and a.enterprise_project_id = os.id " + + "left join region r on r.vendor_id = a.vendor_id and r.region_id = a.region_id " + + "where a.is_deleted = 0 and a.id = :id"; + Map params = MapTools.simpleMap(Common.ID, id); + List list = this.list(ContainerClusterNodePool.class, sql, params); + if (list.isEmpty()) { + return null; + } + return list.get(0); + } } diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerNodeRepository.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerNodeRepository.java index 08c263fa..6b9079c4 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerNodeRepository.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerNodeRepository.java @@ -1,6 +1,9 @@ package com.bocloud.ctstack.plugin.repository; import com.bocloud.ctstack.plugin.entity.ContainerClusterNode; +import com.megatron.common.model.Pager; +import com.megatron.common.model.Param; +import com.megatron.common.model.SimpleBean; import com.megatron.common.utils.Common; import com.megatron.common.utils.MapTools; import com.megatron.database.core.intf.impl.BasicGenericDao; @@ -9,6 +12,7 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.stereotype.Component; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; @@ -20,6 +24,36 @@ public class ContainerNodeRepository extends BasicGenericDao params) { + String sql = "select count(1) from container_cluster_node a where a.is_deleted = 0 "; + sql = this.getQueryBuilder().buildRaw(sql, params, null, "a"); + Map paramMap = this.getQueryBuilder().getParam(params); + return this.countQuery(sql, paramMap).intValue(); + } + + public List list(int page, int rows, List params, Map sorter) { + String sql = "select a.*,b.name cluster_name,c.name vendor_name, r.name region_name from container_cluster_node a " + + " left join container_cluster b on a.cluster_id = b.id " + + " left join cloud_vendor c on a.vendor_id = c.id " + + " left join region r on a.region_id = r.id and a.vendor_id = r.vendor_id " + + " where a.is_deleted = 0 "; + sql = this.getQueryBuilder().buildRaw(sql, new Pager(page, rows, params, sorter), "a"); + Map paramMap = this.getQueryBuilder().getParam(params); + return this.list(ContainerClusterNode.class, sql, paramMap); + } + + public List list(List params, Map sorter) { + String sql = "select a.id,a.name from container_cluster_node a where a.is_deleted = 0 "; + sql = this.getQueryBuilder().build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a"); + Map paramMap = this.getQueryBuilder().getParam(params); + List nodes = this.list(ContainerClusterNode.class, sql, paramMap); + List beans = new ArrayList(); + for (ContainerClusterNode node : nodes) { + beans.add(new SimpleBean(node.getId(), node.getName())); + } + return beans; + } + public boolean removeByVid(Long vendorId, String regionId, Long userId) throws Exception { String sql = "update container_cluster_node_pool set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId " + " and region_id = :regionId "; @@ -57,4 +91,15 @@ public class ContainerNodeRepository extends BasicGenericDao 0; } + + public ContainerClusterNode query(Long id) { + String sql = "select a.* from container_cluster_node a " + + "where a.is_deleted = 0 and a.id = :id"; + Map params = MapTools.simpleMap(Common.ID, id); + List list = this.list(ContainerClusterNode.class, sql, params); + if (list.isEmpty()) { + return null; + } + return list.get(0); + } } diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/ContainerService.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/ContainerService.java index 0cb1db22..af8ab381 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/ContainerService.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/ContainerService.java @@ -1,7 +1,10 @@ package com.bocloud.ctstack.plugin.service; import com.bocloud.ctstack.plugin.domain.model.container.ContainerClusterModel; +import com.bocloud.ctstack.plugin.domain.model.container.ContainerNodePoolModel; import com.bocloud.ctstack.plugin.entity.ContainerCluster; +import com.bocloud.ctstack.plugin.entity.ContainerClusterNode; +import com.bocloud.ctstack.plugin.entity.ContainerClusterNodePool; import com.megatron.common.model.GeneralResult; import com.megatron.common.model.GridBean; import com.megatron.common.model.Pager; @@ -11,5 +14,17 @@ public interface ContainerService { GeneralResult> list(Pager pager); + GeneralResult detail(Long id); + GeneralResult create(ContainerClusterModel model, RequestContext context); + + GeneralResult remove(Long id, RequestContext context); + + GeneralResult createNodePool(ContainerNodePoolModel model, RequestContext context); + + GeneralResult removeNodePool(Long id, RequestContext context); + + GeneralResult> listClusterNodes(Pager pager); + + GeneralResult> listClusterNodePools(Pager pager); } diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/ContainerServiceImpl.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/ContainerServiceImpl.java index a8df79df..a474f0f1 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/ContainerServiceImpl.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/ContainerServiceImpl.java @@ -2,17 +2,23 @@ package com.bocloud.ctstack.plugin.service.impl; import com.alibaba.fastjson.JSONObject; import com.bocloud.cmp.entity.CloudVendor; +import com.bocloud.cmp.entity.Server; +import com.bocloud.cmp.entity.ServerConfig; import com.bocloud.cmp.repository.CloudVendorRepository; import com.bocloud.cmp.util.ResourceEventPublisher; import com.bocloud.ctstack.plugin.config.ButlerConfig; import com.bocloud.ctstack.plugin.domain.model.VpcModel; import com.bocloud.ctstack.plugin.domain.model.container.ContainerClusterModel; -import com.bocloud.ctstack.plugin.domain.model.database.CreateRdsModel; -import com.bocloud.ctstack.plugin.domain.model.database.RdsModel; +import com.bocloud.ctstack.plugin.domain.model.container.ContainerNodePoolModel; +import com.bocloud.ctstack.plugin.entity.Cluster; import com.bocloud.ctstack.plugin.entity.ContainerCluster; +import com.bocloud.ctstack.plugin.entity.ContainerClusterNode; +import com.bocloud.ctstack.plugin.entity.ContainerClusterNodePool; import com.bocloud.ctstack.plugin.provider.container.TianYiContainerClusterProvider; -import com.bocloud.ctstack.plugin.provider.database.TianyiRdsProvider; +import com.bocloud.ctstack.plugin.provider.container.TianYiContainerNodeProvider; import com.bocloud.ctstack.plugin.repository.ContainerClusterRepository; +import com.bocloud.ctstack.plugin.repository.ContainerNodePoolRepository; +import com.bocloud.ctstack.plugin.repository.ContainerNodeRepository; import com.bocloud.ctstack.plugin.service.ContainerService; import com.megatron.common.model.*; import com.megatron.common.utils.Common; @@ -27,7 +33,11 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Service; import org.springframework.util.Assert; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; @Slf4j @Service("containerService") @@ -36,6 +46,12 @@ public class ContainerServiceImpl implements ContainerService { @Autowired private ContainerClusterRepository containerClusterRepository; + @Autowired + private ContainerNodePoolRepository containerNodePoolRepository; + + @Autowired + private ContainerNodeRepository containerNodeRepository; + @Autowired private ResourceEventPublisher resourceEventPublisher; @@ -71,6 +87,20 @@ public class ContainerServiceImpl implements ContainerService { } } + @Override + public GeneralResult detail(Long id) { + try { + ContainerCluster cluster = this.containerClusterRepository.query(id); + if (null == cluster) { + return new GeneralResult<>(false, "容器集群不存在"); + } + return new GeneralResult<>(true, cluster, "查询详情成功"); + } catch (Exception e) { + log.error("Get container cluster detail error:", e); + return new GeneralResult<>(false, "查询详情失败"); + } + } + public GeneralResult create(ContainerClusterModel model, RequestContext context) { try { CloudVendor vendor = cloudVendorRepository.query(model.getVendorId()); @@ -122,4 +152,150 @@ public class ContainerServiceImpl implements ContainerService { return new GeneralResult(false, "创建容器集群失败", null, null); } } + + public GeneralResult remove(Long id, RequestContext context) { + try { + ContainerCluster cluster = containerClusterRepository.query(id); + Assert.notNull(cluster, "容器集群不存在"); + CloudVendor vendor = cloudVendorRepository.query(cluster.getVendorId()); + Assert.notNull(vendor, "指定的地域下云平台不存在"); + + TianYiContainerClusterProvider provider = + new TianYiContainerClusterProvider(butlerConfig.regionButler(vendor.getUuid(), cluster.getRegionId())); + + provider.remove(cluster.getResPoolId(), cluster.getProdInstId()); + containerClusterRepository.remove(cluster.getId(), context.getTarget()); + return new GeneralResult<>(true, "删除成功"); + } catch (Exception e) { + log.error("delete cloud rds failure:", e); + return new GeneralResult<>(false, "删除失败", null, null); + } + } + + public GeneralResult createNodePool(ContainerNodePoolModel model, RequestContext context) { + try { + CloudVendor vendor = cloudVendorRepository.query(model.getVendorId()); + Assert.notNull(vendor, "指定云平台不存在"); + + taskExecutor.submit(() -> { + String operate = vendor.getType().toLowerCase() + ".cloudrds"; + String content = "容器集群节点池创建成功!"; + boolean result = true; + try { + TianYiContainerNodeProvider provider = + new TianYiContainerNodeProvider(butlerConfig.regionButler(vendor.getUuid(), model.getRegionId())); + GeneralResult createResult = provider.createNodePool(model); + if (createResult.isFailed()) { + result = false; + content = createResult.getMessage(); + } else { + // 保存新创建的容器节点池集群信息。 + ContainerNodePoolModel nodePoolModel = + JSONObject.parseObject(JSONObject.toJSONString(createResult.getData()), ContainerNodePoolModel.class); + ContainerClusterNodePool containerClusterNodePool = new ContainerClusterNodePool(); + BeanUtils.copyProperties(nodePoolModel, containerClusterNodePool); + containerClusterNodePool.setVendorId(vendor.getId()); + containerClusterNodePool.setRegionId(model.getRegionId()); + containerClusterNodePool.setMenderId(context.getTarget()); + containerClusterNodePool.setCreatorId(context.getTarget()); + containerClusterNodePool.setTenantId(context.getTenant()); + containerClusterNodePool.setProjectId(context.getProject()); + containerNodePoolRepository.save(containerClusterNodePool); + } + } catch (Exception e) { + log.error("create cloud container failure:", e); + result = false; + content = e.getMessage(); + } + resourceEventPublisher.send(new OperateResult(result, content, operate, + MapTools.simpleMap(Common.VENDORID, vendor.getId()), + OperateResult.OperateCategory.ResourceEvent, context)); + }); + return new GeneralResult<>(true, "任务已下发,正在执行..."); + } catch (Exception e) { + log.error("create container node pool failure:", e); + return new GeneralResult(false, "创建容器集群节点池失败", null, null); + } + } + + public GeneralResult removeNodePool(Long id, RequestContext context) { + try { + ContainerClusterNodePool nodePool = containerNodePoolRepository.query(id); + Assert.notNull(nodePool, "容器集群节点池不存在"); + CloudVendor vendor = cloudVendorRepository.query(nodePool.getVendorId()); + Assert.notNull(vendor, "指定的地域下云平台不存在"); + + TianYiContainerNodeProvider provider = + new TianYiContainerNodeProvider(butlerConfig.regionButler(vendor.getUuid(), nodePool.getRegionId())); + + provider.deleteNodePool(nodePool.getClusterUuid(), nodePool.getName()); + containerNodePoolRepository.remove(nodePool.getId(), context.getTarget()); + return new GeneralResult<>(true, "删除成功"); + } catch (Exception e) { + log.error("delete cloud rds failure:", e); + return new GeneralResult<>(false, "删除失败", null, null); + } + } + + @Override + public GeneralResult> listClusterNodes(Pager pager) { + GridBean gridBean = null; + try { + pager.getSorter().put("gmtCreate", Common.ONE); + List params = pager.getParams(); +// for (Param param : params) { +// Map paramMap = param.getParam(); +// if (paramMap.containsKey("clusterId")) { +// Long clusterId = Long.valueOf(paramMap.get("cluserId").toString()); +// // 检查容器集群是否存在 +// ContainerCluster cluster = containerClusterRepository.query(clusterId); +// Assert.notNull(cluster, "指定容器集群不存在"); +// paramMap.remove("cluserId"); +// } +// } + int total = this.containerNodeRepository.count(params); + if (pager.getSimple()) { + List beans = this.containerNodeRepository.list(params, pager.getSorter()); + gridBean = new GridBean(1, 1, total, beans); + } else { + List list = this.containerNodeRepository.list(pager.getPage(), pager.getRows(), params, pager.getSorter()); + gridBean = GridHelper.getBean(pager.getPage(), pager.getRows(), total, list); + } + return new GeneralResult<>(true, gridBean, "查询成功"); + } catch (Exception e) { + log.error("Query container cluster nodes fail:", e); + return new GeneralResult<>(false, "查询容器集群节点失败"); + } + } + + @Override + public GeneralResult> listClusterNodePools(Pager pager) { + GridBean gridBean = null; + try { + pager.getSorter().put("gmtCreate", Common.ONE); + List params = pager.getParams(); +// for (Param param : params) { +// Map paramMap = param.getParam(); +// if (paramMap.containsKey("clusterId")) { +// Long clusterId = Long.valueOf(paramMap.get("cluserId").toString()); +// // 检查容器集群是否存在 +// ContainerCluster cluster = containerClusterRepository.query(clusterId); +// Assert.notNull(cluster, "指定容器集群不存在"); +// paramMap.remove("cluserId"); +// } +// } + int total = this.containerNodePoolRepository.count(params); + if (pager.getSimple()) { + List beans = this.containerNodePoolRepository.list(params, pager.getSorter()); + gridBean = new GridBean(1, 1, total, beans); + } else { + List list = this.containerNodePoolRepository.list(pager.getPage(), pager.getRows(), params, pager.getSorter()); + gridBean = GridHelper.getBean(pager.getPage(), pager.getRows(), total, list); + } + return new GeneralResult<>(true, gridBean, "查询成功"); + } catch (Exception e) { + log.error("Query container cluster node pools fail:", e); + return new GeneralResult<>(false, "查询容器集群节点池失败"); + } + } }