From 05825ffd921ee5c932dbf04f763ec905c4beb2da Mon Sep 17 00:00:00 2001 From: tanshaolong Date: Thu, 5 Sep 2024 15:45:50 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=E5=AE=B9=E5=99=A8=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=9F=9F=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ContainerController.java | 50 ++++++++++++++++++ .../plugin/provider/TianyiProvider.java | 10 ++++ .../TianYiContainerClusterProvider.java | 2 +- .../ContainerClusterRepository.java | 35 ++++++++++++- .../plugin/service/ContainerService.java | 11 ++++ .../service/impl/ContainerServiceImpl.java | 52 +++++++++++++++++++ 6 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/controller/ContainerController.java create mode 100644 bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/ContainerService.java create mode 100644 bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/ContainerServiceImpl.java 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 new file mode 100644 index 00000000..03a64145 --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/controller/ContainerController.java @@ -0,0 +1,50 @@ +package com.bocloud.ctstack.plugin.controller; + +import com.bocloud.ctstack.plugin.domain.model.ContainerClusterModel; +import com.bocloud.ctstack.plugin.entity.ContainerCluster; +import com.bocloud.ctstack.plugin.service.ContainerService; +import com.megatron.common.model.GeneralResult; +import com.megatron.common.model.GridBean; +import com.megatron.common.model.Pager; +import com.megatron.common.model.RequestContext; +import com.megatron.common.utils.Common; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.*; + +@Tag(name = "容器集群管理") +@RestController +@RequestMapping("/v1/containers") +public class ContainerController { + + @Autowired + private ContainerService containerService; + + /** + * 查看集群列表 + * + * @param pager + * @return + */ + @Operation(tags = {"CMC", "CSC"}, summary = "查询集群列表") + @GetMapping + public GeneralResult> list(Pager pager) { + return containerService.list(pager); + } + + /** + * 添加集群 + * + * @param model + * @param context + * @return + */ +// @Operation(tags = {"CMC", "CSC"}, summary = "创建集群") +// @PostMapping +// public GeneralResult create(@RequestBody ContainerClusterModel model, +// @Value(Common.REQ_CONTEXT) RequestContext context) { +// return containerService.create(model, context); +// } +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/TianyiProvider.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/TianyiProvider.java index b602dd0d..111b73e1 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/TianyiProvider.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/TianyiProvider.java @@ -100,6 +100,16 @@ public class TianyiProvider { return httpClient.get(header, params, uri); } + public Result doGet2(String url, Map headers, Map params) { + HttpClient httpClient = new HttpClient(60 * 1000, PostDataFormat.RAW); + httpClient.setDataFormat(null); + + String uri = "http://ccse-global.ctapi.ctyun.local:31167"; + Map header = buildHeader(HttpRequestMethod.GET, url, headers, params); + printCurl(HttpRequestMethod.GET, uri, header, params); + return httpClient.get(header, params, uri); + } + public Result doGetWithBody(String url, Map headers, Map params) { try { //创建httpclient对象 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 2f97801a..90de90de 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 @@ -48,7 +48,7 @@ public class TianYiContainerClusterProvider extends TianyiProvider { while (true) { body.put("pageNow", pageNow); - Result result = doGet(url, header, body); + Result result = doGet2(url, header, body); JSONObject returnObj = checkResult(result, "查询容器集群列表"); log.info("checkedResult:{}", JSON.toJSONString(returnObj)); records.addAll(JSONObject.parseObject(returnObj.getString("records"), List.class)); diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerClusterRepository.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerClusterRepository.java index 1d1dcfc2..698fc087 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerClusterRepository.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/ContainerClusterRepository.java @@ -1,6 +1,9 @@ package com.bocloud.ctstack.plugin.repository; import com.bocloud.ctstack.plugin.entity.ContainerCluster; +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,24 @@ public class ContainerClusterRepository extends BasicGenericDao params) { + String sql = "select count(1) from container_cluster 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 vdc_name,c.name vendor_name , r.name region_name from container_cluster a " + + " left join vdc b on a.vdc_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(ContainerCluster.class, sql, paramMap); + } + public boolean removeByVid(Long vendorId, String regionId, Long userId) throws Exception { String sql = "update container_cluster set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId " + " and region_id = :regionId "; @@ -38,12 +60,23 @@ public class ContainerClusterRepository extends BasicGenericDao list(List params, Map sorter) { + String sql = "select a.id,a.name from container_cluster 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 clusters = this.list(ContainerCluster.class, sql, paramMap); + List beans = new ArrayList(); + for (ContainerCluster cluster : clusters) { + beans.add(new SimpleBean(cluster.getId(), cluster.getName())); + } + return beans; + } + public boolean remove(Long id, Long userId) throws Exception { String sql = "update container_cluster set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id = :id"; Map params = MapTools.simpleMap("id", id); params.put(Common.MENDER_ID, userId); params.put("gmtModify", new Date()); return this.execute(sql, params) > 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 new file mode 100644 index 00000000..eb6e9245 --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/ContainerService.java @@ -0,0 +1,11 @@ +package com.bocloud.ctstack.plugin.service; + +import com.bocloud.ctstack.plugin.entity.ContainerCluster; +import com.megatron.common.model.GeneralResult; +import com.megatron.common.model.GridBean; +import com.megatron.common.model.Pager; + +public interface ContainerService { + + GeneralResult> list(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 new file mode 100644 index 00000000..4f7a4c8d --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/ContainerServiceImpl.java @@ -0,0 +1,52 @@ +package com.bocloud.ctstack.plugin.service.impl; + +import com.bocloud.cmp.entity.Server; +import com.bocloud.cmp.entity.ServerConfig; +import com.bocloud.ctstack.plugin.entity.Cluster; +import com.bocloud.ctstack.plugin.entity.ContainerCluster; +import com.bocloud.ctstack.plugin.repository.ContainerClusterRepository; +import com.bocloud.ctstack.plugin.service.ContainerService; +import com.megatron.common.model.GeneralResult; +import com.megatron.common.model.GridBean; +import com.megatron.common.model.Pager; +import com.megatron.common.model.SimpleBean; +import com.megatron.common.utils.Common; +import com.megatron.common.utils.GridHelper; +import com.megatron.common.utils.ListTool; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Slf4j +@Service("containerService") +public class ContainerServiceImpl implements ContainerService { + + @Autowired + private ContainerClusterRepository containerClusterRepository; + + @Override + public GeneralResult> list(Pager pager) { + GridBean gridBean = null; + try { + pager.getSorter().put("gmtCreate", Common.ONE); + int total = this.containerClusterRepository.count(pager.getParams()); + if (pager.getSimple()) { + List beans = this.containerClusterRepository.list(pager.getParams(), pager.getSorter()); + gridBean = new GridBean(1, 1, total, beans); + } else { + List list = this.containerClusterRepository.list(pager.getPage(), pager.getRows(), pager.getParams(), pager.getSorter()); + gridBean = GridHelper.getBean(pager.getPage(), pager.getRows(), total, list); + } + return new GeneralResult<>(true, gridBean, "查询成功"); + } catch (Exception e) { + log.error("Query container cluster list fail:", e); + return new GeneralResult<>(false, "查询容器集群失败"); + } + } +}