From ed48babcea8b79cba9ec5c38167a6f586aca273e Mon Sep 17 00:00:00 2001 From: yuemian <--list> Date: Mon, 2 Sep 2024 16:56:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A9=E7=BF=BC=E4=BA=91-=E7=89=A9=E7=90=86?= =?UTF-8?q?=E6=9C=BA=E6=9F=A5=E8=AF=A2=E3=80=81=E7=BC=96=E8=BE=91=E3=80=81?= =?UTF-8?q?=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/PhysicalServerController.java | 75 +++++++++ .../domain/model/PhysicalServerModel.java | 2 + .../compute/TianyiPhysicalProvider.java | 39 +++++ .../tianyiconvertor/PhyscicalConvertor.java | 1 + .../repository/PhysicalServerRepository.java | 73 ++++++++ .../plugin/service/PhysicalServerService.java | 26 +++ .../impl/PhysicalServerServiceImpl.java | 156 ++++++++++++++++++ .../service/impl/TianyiTransporter.java | 3 +- 8 files changed, 374 insertions(+), 1 deletion(-) create mode 100644 bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/controller/PhysicalServerController.java create mode 100644 bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/repository/PhysicalServerRepository.java create mode 100644 bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/PhysicalServerService.java create mode 100644 bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/PhysicalServerServiceImpl.java diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/controller/PhysicalServerController.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/controller/PhysicalServerController.java new file mode 100644 index 00000000..6189c85e --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/controller/PhysicalServerController.java @@ -0,0 +1,75 @@ +package com.bocloud.ctstack.plugin.controller; + +import com.bocloud.cmp.entity.PhysicalServer; +import com.bocloud.ctstack.plugin.service.PhysicalServerService; +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.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author zhangdi + * @since 2018/7/2 + */ +@RestController +@RequestMapping("/v1/physical/servers") +@Tag(name = "命名规则") +public class PhysicalServerController { + + @Autowired + private PhysicalServerService physicalServerService; + + + @Operation(tags = {"CMC", "CSC"}, summary = "获取命名规则列表") + @GetMapping + public GeneralResult> list(Pager pager, + @Value(Common.REQ_CONTEXT) RequestContext context) { + return physicalServerService.list(pager, context); + } + + + @Operation(tags = {"CMC", "CSC"}, summary = "添加标签") + @PostMapping + public GeneralResult create(@RequestBody PhysicalServer physicalServer, + @Value(Common.REQ_CONTEXT) RequestContext context) { + return physicalServerService.create(physicalServer, context); + } + + + @Operation(tags = {"CMC", "CSC"}, summary = "修改标签") + @PutMapping("/{id}") + public GeneralResult modify(@PathVariable(value = Common.ID) Long id, + @RequestBody PhysicalServer physicalServer, + @Value(Common.REQ_CONTEXT) RequestContext context) { + physicalServer.setId(id); + return physicalServerService.modify(physicalServer, context); + } + + @Operation(tags = {"CMC", "CSC"}, summary = "删除标签") + @DeleteMapping("/{id}") + public GeneralResult remove(@PathVariable(value = Common.ID) Long id, + @Value(Common.REQ_CONTEXT) RequestContext context) { + return physicalServerService.remove(id, context); + } + + + @Operation(tags = {"CMC", "CSC"}, summary = "查看标签详情") + @GetMapping("/{id}") + public GeneralResult detail(@PathVariable(value = Common.ID) Long id) { + return physicalServerService.detail(id); + } + +} \ No newline at end of file diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/PhysicalServerModel.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/PhysicalServerModel.java index ace7f1df..aaeba42f 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/PhysicalServerModel.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/domain/model/PhysicalServerModel.java @@ -29,6 +29,8 @@ public class PhysicalServerModel { private String name; + private String displayName; + private String remark; private String systemVolumeRaidId; diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/compute/TianyiPhysicalProvider.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/compute/TianyiPhysicalProvider.java index 1b5e95e9..8bad275b 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/compute/TianyiPhysicalProvider.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/compute/TianyiPhysicalProvider.java @@ -11,6 +11,7 @@ import com.megatron.common.model.GeneralResult; import com.megatron.common.model.Result; import com.megatron.common.utils.ListTool; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.util.CollectionUtils; import java.util.ArrayList; @@ -77,4 +78,42 @@ public class TianyiPhysicalProvider extends TianyiProvider { return null; } } + + public GeneralResult modify(PhysicalServerModel model) { + try { + String apiUrl = "/v4/ebm/update"; + JSONObject headers = new JSONObject(); + headers.put("regionId", this.getRegionId()); + headers.put("instanceUUID", model.getInstanceUuid()); + if (StringUtils.isNotBlank(model.getRemark())) { + headers.put("description", model.getRemark()); + } + if (StringUtils.isNotBlank(model.getDisplayName())) { + headers.put("displayName", model.getDisplayName()); + } + + Result result = doPost(apiUrl, headers); + checkResult(result, "修改物理主机"); + return new GeneralResult(true, "修改物理机成功"); + } catch (Exception e) { + log.error("修改物理机失败 : " + e); + return new GeneralResult(false, "修改物理机失败", e.getMessage()); + } + } + + public GeneralResult remove(PhysicalServerModel model) { + try { + String apiUrl = "/v4/ebm/delete"; + JSONObject jsonObject = new JSONObject(); + jsonObject.put("regionID", this.getRegionId()); + jsonObject.put("azName", model.getZoneId()); + jsonObject.put("instanceUUID", model.getInstanceUuid()); + Result result = doPost(apiUrl, jsonObject); + checkResultArray(result, "删除物理机"); + return new GeneralResult(true, "删除物理机成功"); + } catch (Exception e) { + log.error("删除物理机失败 : " + e.getMessage(), e); + return new GeneralResult(false, "删除物理机失败", e.getMessage()); + } + } } diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/PhyscicalConvertor.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/PhyscicalConvertor.java index 366ecf89..0bcdbd0c 100644 --- a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/PhyscicalConvertor.java +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/provider/tianyiconvertor/PhyscicalConvertor.java @@ -38,6 +38,7 @@ public class PhyscicalConvertor implements BeanConvertor { + + + public PhysicalServerRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate, CurrentService service) { + super(jdbcTemplate, npJdbcTemplate, service); + } + + public List listByVendor(Long vendorId) { + String sql = "select a.* from physical_server a where a.is_deleted = 0"; + Map params = new HashMap<>(8); + if (null != vendorId) { + sql += " and a.vendor_id = :vendorId"; + params.put(Common.VENDORID, vendorId); + } + return this.list(PhysicalServer.class, sql, params); + } + + public boolean removeByVendor(Long vendorId, Long userId) { + String sql = "update physical_server set is_deleted = true , gmt_modify = :gmtModify where is_deleted = 0 and vendor_id = :vendorId"; + Map params = MapTools.simpleMap(Common.VENDORID, vendorId); + params.put("gmtModify", new Date()); + return this.execute(sql, params) > 0; + } + + public boolean remove(Long id, Long userId) { + String sql = "update physical_server set is_deleted = true,gmt_modify = :gmtModify, mender_id = :userId where is_deleted = 0 and id = :id"; + Map params = MapTools.simpleMap("id", id); + params.put("gmtModify", new Date()); + params.put("userId", userId); + return this.execute(sql, params) > 0; + } + + public int count(List params) throws Exception { + String sql = "select count(1) from physical_server where 1 = 1 "; + sql = this.getQueryBuilder().build(sql, params, new HashMap<>(8), ""); + Map param = this.getQueryBuilder().getParam(params); + return this.countQuery(sql, param).intValue(); + } + + public List list(int page, int rows, List params, Map sorter) throws Exception { + String sql = "select a.* from physical_server a where 1 = 1 "; + sql = this.getQueryBuilder().build(sql, new Pager(page, rows, params, sorter), "a"); + Map param = this.getQueryBuilder().getParam(params); + return this.list(PhysicalServer.class, sql, param); + } + +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/PhysicalServerService.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/PhysicalServerService.java new file mode 100644 index 00000000..b9fbea75 --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/PhysicalServerService.java @@ -0,0 +1,26 @@ +package com.bocloud.ctstack.plugin.service; + +import com.bocloud.cmp.entity.PhysicalServer; +import com.megatron.common.model.GeneralResult; +import com.megatron.common.model.GridBean; +import com.megatron.common.model.Pager; +import com.megatron.common.model.RequestContext; + +public interface PhysicalServerService { + + + GeneralResult> list(Pager pager, RequestContext context); + + + GeneralResult detail(Long id); + + GeneralResult create(PhysicalServer physicalServer, RequestContext context); + + + GeneralResult modify(PhysicalServer physicalServer, RequestContext context); + + GeneralResult remove(Long id, RequestContext context); + + + +} diff --git a/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/PhysicalServerServiceImpl.java b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/PhysicalServerServiceImpl.java new file mode 100644 index 00000000..faa8e3d1 --- /dev/null +++ b/bocloud.ctstack.plugin/src/main/java/com/bocloud/ctstack/plugin/service/impl/PhysicalServerServiceImpl.java @@ -0,0 +1,156 @@ +package com.bocloud.ctstack.plugin.service.impl; + +import com.bocloud.cmp.entity.CloudVendor; +import com.bocloud.cmp.entity.PhysicalServer; +import com.bocloud.cmp.repository.CloudVendorRepository; +import com.bocloud.ctstack.plugin.repository.PhysicalServerRepository; +import com.bocloud.ctstack.plugin.config.ButlerConfig; +import com.bocloud.ctstack.plugin.domain.model.PhysicalServerModel; +import com.bocloud.ctstack.plugin.entity.Label; +import com.bocloud.ctstack.plugin.provider.compute.TianyiPhysicalProvider; +import com.bocloud.ctstack.plugin.repository.ResourceEventRepository; +import com.bocloud.ctstack.plugin.repository.VolumeRepository; +import com.bocloud.ctstack.plugin.repository.VpcRepository; +import com.bocloud.ctstack.plugin.service.PhysicalServerService; +import com.google.common.collect.Maps; +import com.megatron.common.model.GeneralResult; +import com.megatron.common.model.GridBean; +import com.megatron.common.model.Pager; +import com.megatron.common.model.Param; +import com.megatron.common.model.RequestContext; +import com.megatron.common.utils.GridHelper; +import com.megatron.framework.lock.AutoCloseLock; +import com.megatron.framework.lock.LockFactory; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.TimeUnit; + +@Slf4j +@Service("physicalServerService") +public class PhysicalServerServiceImpl implements PhysicalServerService { + + @Autowired + private LockFactory lockFactory; + @Autowired + private ResourceEventRepository resourceEventRepository; + @Autowired + private CloudVendorRepository cloudVendorRepository; + @Autowired + private VpcRepository vpcRepository; + @Autowired + private VolumeRepository volumeRepository; + @Autowired + private PhysicalServerRepository physicalServerRepository; + @Autowired + private ButlerConfig butlerConfig; + + @Override + public GeneralResult> list(Pager pager, RequestContext context) { + List list = null; + int total = 0; + GridBean gridBean = null; + 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); + Integer page = Optional.ofNullable(pager.getPage()).orElse(1); + Integer rows = Optional.ofNullable(pager.getRows()).orElse(10); + total = physicalServerRepository.count(params); + list = physicalServerRepository.list(page, rows, params, sorter); + gridBean = GridHelper.getBean(page, rows, total, list); + return new GeneralResult<>(true, gridBean, "查询成功"); + } catch (Exception e) { + log.error("list physical server failure:", e); + return new GeneralResult<>(false, "查询失败"); + } + } + + @Override + public GeneralResult detail(Long id) { + try { + PhysicalServer physicalServer = physicalServerRepository.get(PhysicalServer.class, id); + return new GeneralResult<>(true, physicalServer, "查询成功"); + } catch (Exception e) { + log.error("Get error message:", e); + return new GeneralResult<>(false, "查询失败"); + } + } + + @Override + public GeneralResult create(PhysicalServer physicalServer, RequestContext context) { + return null; + } + + @Override + public GeneralResult modify(PhysicalServer physicalServer, RequestContext context) { + String path = PhysicalServer.class.getSimpleName() + "_" + physicalServer.getId(); + try (AutoCloseLock lock = lockFactory.getACLock(path)) { + Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时"); + PhysicalServer oldServer = this.physicalServerRepository.get(PhysicalServer.class, physicalServer.getId()); + if (null == oldServer) { + log.warn("physical server does not exist!"); + return new GeneralResult<>(false, "数据不存在"); + } + CloudVendor vendor = cloudVendorRepository.query(oldServer.getVendorId()); + if (vendor == null) { + return GeneralResult.FAILED("云平台不存在"); + } + new Thread(() -> { + PhysicalServerModel model = new PhysicalServerModel(); + model.setInstanceUuid(oldServer.getInstanceUuid()); + model.setRegionId(oldServer.getRegionId()); + model.setZoneId(oldServer.getZoneId()); + model.setRemark(physicalServer.getRemark()); + TianyiPhysicalProvider physicalProvider = new TianyiPhysicalProvider(butlerConfig.regionButler(vendor.getUuid(), oldServer.getRegionId())); + GeneralResult result = physicalProvider.modify(model); + if (result.isSuccess()) { + oldServer.setRemark(physicalServer.getRemark()); + physicalServerRepository.update(oldServer); + } + }).start(); + return new GeneralResult<>(true, "修改任务已下发"); + } catch (Exception e) { + log.error("Modify label error:", e); + return new GeneralResult<>(false, "修改失败"); + } + } + + @Override + public GeneralResult remove(Long id, RequestContext context) { + String path = Label.class.getSimpleName() + "_" + id; + try (AutoCloseLock lock = lockFactory.getACLock(path)) { + Assert.isTrue(lock.acquire(10, TimeUnit.SECONDS), "请求超时"); + PhysicalServer oldServer = this.physicalServerRepository.get(PhysicalServer.class, id); + if (null == oldServer) { + log.warn("physical server does not exist!"); + return new GeneralResult<>(false, "数据不存在"); + } + CloudVendor vendor = cloudVendorRepository.query(oldServer.getVendorId()); + if (vendor == null) { + return GeneralResult.FAILED("云平台不存在"); + } + new Thread(() -> { + PhysicalServerModel model = new PhysicalServerModel(); + model.setInstanceUuid(oldServer.getInstanceUuid()); + model.setRegionId(oldServer.getRegionId()); + model.setZoneId(oldServer.getZoneId()); + TianyiPhysicalProvider physicalProvider = new TianyiPhysicalProvider(butlerConfig.regionButler(vendor.getUuid(), oldServer.getRegionId())); + GeneralResult result = physicalProvider.remove(model); + if (result.isSuccess()) { + physicalServerRepository.remove(id, context.getTarget()); + } + }).start(); + return new GeneralResult<>(true, "删除任务下发"); + } catch (Exception e) { + log.error("Remove physical server error:", e); + return new GeneralResult<>(false, "删除任务下发失败"); + } + } +} 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 714bfa9f..02dc5680 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 @@ -8,7 +8,7 @@ import com.bocloud.cmp.entity.PhysicalServer; import com.bocloud.cmp.entity.Server; import com.bocloud.cmp.entity.ServerConfig; import com.bocloud.cmp.enums.ResourceCategory; -import com.bocloud.cmp.repository.PhysicalServerRepository; +import com.bocloud.ctstack.plugin.repository.PhysicalServerRepository; import com.bocloud.cmp.repository.ServerRepository; import com.bocloud.ctstack.plugin.config.ButlerConfig; import com.bocloud.ctstack.plugin.domain.model.*; @@ -439,6 +439,7 @@ public class TianyiTransporter { server = serverMap.get(physicalServerModel.getInstanceUuid()); if (server != null) { BeanUtils.copyProperties(physicalServerModel, server, new String[]{"id", "tenantId", "creatorId", "projectId", "isDeleted", "gmtCreate"}); + physicalServerRepository.update(server); serverMap.remove(server.getInstanceUuid()); continue; }