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 5af21417..ea98461a 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 @@ -94,6 +94,15 @@ public class ContainerController { return containerService.remove(id, context); } + @Operation(tags = {"CMC", "CSC"}, summary = "修改容器集群") + @PutMapping("/{id}") + public GeneralResult modify(@PathVariable(value = Common.ID) Long id, + @RequestBody ContainerClusterModel containerClusterModel, + @Value(Common.REQ_CONTEXT) RequestContext context) { + containerClusterModel.setId(id); + return containerService.modify(containerClusterModel, 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 3beef87e..1780f3ea 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 @@ -12,6 +12,7 @@ import java.util.List; @NoArgsConstructor public class ContainerClusterModel { + private Long id; private String name; private String remark; private String clusterSpec; @@ -31,7 +32,6 @@ public class ContainerClusterModel { 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 f510e1a8..dfca22cf 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 @@ -5,6 +5,7 @@ import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; +import java.util.List; @Data @AllArgsConstructor @@ -34,4 +35,5 @@ public class ContainerNodePoolModel { private String vmSpecType; // 节点规格类型 private Long vmSpecId; // 节点规格id + private List nodeModels; } diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerClusterNode.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerClusterNode.java index 34f3241a..2c3462ab 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerClusterNode.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/entity/ContainerClusterNode.java @@ -34,6 +34,9 @@ public class ContainerClusterNode extends GenericEntity { @Column("node_status") private String nodeStatus; + @Column("node_pool_uuid") + private String nodePoolUuid; + @Column("created") private Date created; // 创建时间 } diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/common/TianyiLocationProvider.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/common/TianyiLocationProvider.java index 0c5bd435..cdcae912 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/common/TianyiLocationProvider.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/common/TianyiLocationProvider.java @@ -304,17 +304,9 @@ public class TianyiLocationProvider extends TianyiProvider { if (containerClusterList.isSuccess()) { List containerClusterModels = (List) containerClusterList.getData(); for (ContainerClusterModel clusterModel : containerClusterModels) { - // 同步容器节点信息 - log.info("获取容器节点信息..."); - TianYiContainerNodeProvider tianYiContainerNodeProvider = new TianYiContainerNodeProvider(this.getButler()); - GeneralResult clusterNodeList = tianYiContainerNodeProvider.listNodes(clusterModel.getName()); - log.info("同步容器节点结果:{}",clusterNodeList.isSuccess()); - if (clusterNodeList.isSuccess()) { - clusterModel.setNodeModels((List) clusterNodeList.getData()); - } - // 同步容器节点池信息 log.info("获取容器节点池信息..."); + TianYiContainerNodeProvider tianYiContainerNodeProvider = new TianYiContainerNodeProvider(this.getButler()); GeneralResult clusterNodePoolList = tianYiContainerNodeProvider.listNodePools(clusterModel.getName()); log.info("同步容器节点池结果:{}",clusterNodePoolList.isSuccess()); if (clusterNodePoolList.isSuccess()) { 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 0d5bc64d..11dcda05 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 @@ -10,6 +10,7 @@ import com.bocloud.ctstack.plugin.provider.tianyiconvertor.ContainerClusterConve 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; @@ -172,6 +173,19 @@ public class TianYiContainerClusterProvider extends TianyiProvider { } } + public GeneralResult modify(ContainerClusterModel model) { + try { + if(!StringUtils.isEmpty(model.getRemark())) { + return modifyDesc(model.getName(), model.getRemark()); + } else { + return new GeneralResult(false, null, "容器集群没有可修改的内容"); + } + } catch (Exception e) { + log.error("修改容器集群失败 : " + e); + return new GeneralResult(false, null, "修改容器集群失败"); + } + } + private GeneralResult modifyDesc(String clusterName, String clusterDesc) { try { String apiUrl = "/ccse/clusters/" + clusterName; 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 81e7a637..1fd3b61f 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 @@ -37,20 +37,20 @@ public class TianYiContainerNodeProvider extends TianyiProvider { this.apiDomain = "http://ccse-global.ctapi.ctyun.local:" + this.apiPort; } - public GeneralResult listNodes(String clusterName) { - try { - String url = "/v1.1/ccse/clusters/" + clusterName + "/nodes/list"; - Map header = new HashMap<>(); - header.put("regionId", this.getRegionId()); - - Result result = doGet(url, header); - JSONObject returnObj = checkResult(result, "容器集群节点列表"); - return new GeneralResult(true, nodeConvertor.convertList(JSONArray.parseArray(returnObj.toJSONString(), JSONObject.class)), "查询容器集群节点列表成功"); - } catch (Exception e) { - log.error("查询容器集群节点列表失败 : " + e); - return new GeneralResult(false, "查询容器集群节点列表失败", e.getMessage()); - } - } +// public GeneralResult listNodes(String clusterName) { +// try { +// String url = "/v1.1/ccse/clusters/" + clusterName + "/nodes/list"; +// Map header = new HashMap<>(); +// header.put("regionId", this.getRegionId()); +// +// Result result = doGet(url, header); +// JSONObject returnObj = checkResult(result, "容器集群节点列表"); +// return new GeneralResult(true, nodeConvertor.convertList(JSONArray.parseArray(returnObj.toJSONString(), JSONObject.class)), "查询容器集群节点列表成功"); +// } catch (Exception e) { +// log.error("查询容器集群节点列表失败 : " + e); +// return new GeneralResult(false, "查询容器集群节点列表失败", e.getMessage()); +// } +// } public GeneralResult createNodePool(ContainerNodePoolModel poolModel) { try { 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 0a82264d..968a83fd 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 @@ -1,6 +1,7 @@ package com.bocloud.ctstack.plugin.provider.tianyiconvertor; import com.alibaba.fastjson.JSONObject; +import com.bocloud.ctstack.plugin.domain.model.container.ContainerNodeModel; import com.bocloud.ctstack.plugin.domain.model.container.ContainerNodePoolModel; import java.util.ArrayList; @@ -41,6 +42,21 @@ public class ContainerNodePoolConvertor implements BeanConvertor nodeModels = new ArrayList<>(); + List nodes = jsonObject.getJSONArray("nodes").toJavaList(JSONObject.class); + if (nodes != null) { + for (JSONObject nodeObject : nodes) { + ContainerNodeModel nodeModel = new ContainerNodeModel(); + nodeModel.setNodeName(nodeObject.getString("nodeName")); + nodeModel.setNodeStatus(nodeObject.getString("nodeStatus")); + nodeModel.setCreated(jsonObject.getDate("createdTime")); + + nodeModels.add(nodeModel); + } + containerNodePoolModel.setNodeModels(nodeModels); + } + return containerNodePoolModel; } } 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 6b9079c4..b1575e55 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 @@ -55,7 +55,7 @@ public class ContainerNodeRepository extends BasicGenericDao params = MapTools.simpleMap(Common.VENDORID, vendorId); params.put(Common.REGIONID, regionId); @@ -65,8 +65,8 @@ public class ContainerNodeRepository extends BasicGenericDao params = MapTools.simpleMap(Common.VENDORID, vendorId); params.put(Common.REGIONID, regionId); params.put(Common.MENDER_ID, userId); @@ -75,12 +75,23 @@ public class ContainerNodeRepository extends BasicGenericDao 0; } - public List listByClusterUuid(Long vendorId, String regionId, String clusterUuid) throws Exception { - String sql = "select a.*,t.name tenant_name from container_cluster_node a left join tenant t on a.tenant_id = t.id where a.is_deleted = 0 and a.vendor_id = :vendorId" - + " and a.region_id = :regionId and a.cluster_uuid = :clusterUuid"; + public boolean removeByNodePoolUuid(Long vendorId, String regionId, String nodePoolUuid, 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 and node_pool_uuid = :nodePoolUuid "; Map params = MapTools.simpleMap(Common.VENDORID, vendorId); params.put(Common.REGIONID, regionId); - params.put("clusterUuid", clusterUuid); + params.put(Common.MENDER_ID, userId); + params.put("nodePoolUuid", nodePoolUuid); + params.put("gmtModify", new Date()); + return this.execute(sql, params) > 0; + } + + public List listByNodePoolUuid(Long vendorId, String regionId, String nodePoolUuid) throws Exception { + String sql = "select a.*,t.name tenant_name from container_cluster_node a left join tenant t on a.tenant_id = t.id where a.is_deleted = 0 and a.vendor_id = :vendorId" + + " and a.region_id = :regionId and a.node_pool_uuid = :nodePoolUuid"; + Map params = MapTools.simpleMap(Common.VENDORID, vendorId); + params.put(Common.REGIONID, regionId); + params.put("nodePoolUuid", nodePoolUuid); return this.list(ContainerClusterNode.class, sql, params); } 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 af8ab381..4a07c798 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 @@ -20,6 +20,8 @@ public interface ContainerService { GeneralResult remove(Long id, RequestContext context); + GeneralResult modify(ContainerClusterModel model, RequestContext context); + GeneralResult createNodePool(ContainerNodePoolModel model, RequestContext context); GeneralResult removeNodePool(Long id, RequestContext context); 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 a474f0f1..751305b1 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 @@ -172,6 +172,32 @@ public class ContainerServiceImpl implements ContainerService { } } + public GeneralResult modify(ContainerClusterModel model, RequestContext context) { + try { + ContainerCluster queryCluser = containerClusterRepository.query(model.getId()); + Assert.notNull(queryCluser, "容器集群不存在"); + CloudVendor vendor = cloudVendorRepository.query(queryCluser.getVendorId()); + Assert.notNull(vendor, "指定的地域下云平台不存在"); + + TianYiContainerClusterProvider provider = + new TianYiContainerClusterProvider(butlerConfig.regionButler(vendor.getUuid(), queryCluser.getRegionId())); + + model.setName(queryCluser.getName()); + GeneralResult result = provider.modify(model); + if (result.isSuccess()) { + // 保存新创建的容器节点池集群信息。 + ContainerClusterModel newModel = (ContainerClusterModel) result.getData(); + BeanUtils.copyProperties(newModel, queryCluser, new String[]{"id", "tenantId", "creatorId", "projectId", "isDeleted", "gmtCreate"}); + queryCluser.setVendorId(vendor.getId()); + containerClusterRepository.update(queryCluser); + } + 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()); diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/TianyiTransporter.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/TianyiTransporter.java index ac5dcd41..787e912f 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/TianyiTransporter.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/TianyiTransporter.java @@ -613,8 +613,7 @@ public class TianyiTransporter { containerClusterRepository.update(containerCluster); clusterMap.remove(model.getClusterUuid()); - // 同步这个集群下的节点和节点池信息 - syncContainerNodes(containerCluster, model.getNodeModels(), vendor, userId, region); + // 同步这个集群下的节点池信息 syncContainerNodePools(containerCluster, model.getNodePoolModels(), vendor, userId, region); continue; } @@ -629,30 +628,29 @@ public class TianyiTransporter { containerCluster.setVendorId(vendorId); containerClusterRepository.save(containerCluster); - // 同步这个集群下的节点和节点池信息 - syncContainerNodes(containerCluster, model.getNodeModels(), vendor, userId, region); + // 同步这个集群下的节点池信息 syncContainerNodePools(containerCluster, model.getNodePoolModels(), vendor, userId, region); } for (Entry entry : clusterMap.entrySet()) { containerClusterRepository.remove(entry.getValue().getId(), userId); // 删除这个容器云下面的节点和节点池 - containerNodeRepository.removeByClusterUuid(vendorId, region.getRegionId(), entry.getValue().getClusterUuid(), userId); containerNodePoolRepository.removeByClusterUuid(vendorId, region.getRegionId(), entry.getValue().getClusterUuid(), userId); + containerNodeRepository.removeByClusterUuid(vendorId, region.getRegionId(), entry.getValue().getClusterUuid(), userId); } } - private void syncContainerNodes(ContainerCluster containerCluster, List nodeModels, CloudVendor vendor, Long userId, Region region) throws Exception { + private void syncContainerNodes(ContainerClusterNodePool nodePool, List nodeModels, CloudVendor vendor, Long userId, Region region) throws Exception { if (null == nodeModels) { return; } Long vendorId = vendor.getId(); - // 当容器集群下节点为空时,清空数据库 + // 当容器节点池下节点为空时,清空数据库 if (ListTool.isEmpty(nodeModels)) { - containerNodeRepository.removeByClusterUuid(vendorId, region.getRegionId(), containerCluster.getClusterUuid(), userId); + containerNodeRepository.removeByNodePoolUuid(vendorId, region.getRegionId(), nodePool.getNodePoolUuid(), userId); return; } - List nodes = containerNodeRepository.listByClusterUuid(vendorId, region.getRegionId(), - containerCluster.getClusterUuid()); + List nodes = containerNodeRepository.listByNodePoolUuid(vendorId, region.getRegionId(), + nodePool.getNodePoolUuid()); Map nodeMap = Maps.newHashMap(); for (ContainerClusterNode containerClusterNode : nodes) { nodeMap.put(containerClusterNode.getNodeName(), containerClusterNode); @@ -664,7 +662,7 @@ public class TianyiTransporter { // 更新操作 containerClusterNode = nodeMap.get(model.getNodeName()); BeanUtils.copyProperties(model, containerClusterNode); - containerClusterNode.setClusterUuid(containerCluster.getClusterUuid()); + containerClusterNode.setNodePoolUuid(nodePool.getNodePoolUuid()); containerClusterNode.setMenderId(userId); containerClusterNode.setRegionId(region.getRegionId()); containerClusterNode.setGmtModify(new Date()); @@ -676,7 +674,7 @@ public class TianyiTransporter { } containerClusterNode = new ContainerClusterNode(); BeanUtils.copyProperties(model, containerClusterNode); - containerClusterNode.setClusterUuid(containerCluster.getClusterUuid()); + containerClusterNode.setNodePoolUuid(nodePool.getNodePoolUuid()); containerClusterNode.setMenderId(userId); containerClusterNode.setRegionId(region.getRegionId()); containerClusterNode.setGmtModify(new Date()); @@ -697,6 +695,7 @@ public class TianyiTransporter { // 当容器集群下节点为空时,清空数据库 if (ListTool.isEmpty(nodePoolModels)) { containerNodePoolRepository.removeByClusterUuid(vendorId, region.getRegionId(), containerCluster.getClusterUuid(), userId); + containerNodeRepository.removeByClusterUuid(vendorId, region.getRegionId(), containerCluster.getClusterUuid(), userId); return; } List nodePools = containerNodePoolRepository.listByClusterUuid(vendorId, region.getRegionId(), @@ -720,6 +719,8 @@ public class TianyiTransporter { containerClusterNodePool.setVendorId(vendorId); containerNodePoolRepository.update(containerClusterNodePool); nodePoolMap.remove(model.getClusterUuid()); + + syncContainerNodes(containerClusterNodePool, model.getNodeModels(), vendor, userId, region); continue; } containerClusterNodePool = new ContainerClusterNodePool(); @@ -733,9 +734,13 @@ public class TianyiTransporter { containerClusterNodePool.setCreatorId(userId); containerClusterNodePool.setVendorId(vendorId); containerNodePoolRepository.save(containerClusterNodePool); + + syncContainerNodes(containerClusterNodePool, model.getNodeModels(), vendor, userId, region); } for (Entry entry : nodePoolMap.entrySet()) { containerNodePoolRepository.remove(entry.getValue().getId(), userId); + // 清除这个节点池下面的节点信息 + containerNodeRepository.removeByNodePoolUuid(vendorId, region.getRegionId(), entry.getValue().getNodePoolUuid(), userId); } }