天翼云-物理机查询、编辑、删除
parent
76fa9721c6
commit
ed48babcea
|
@ -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<GridBean<PhysicalServer>> list(Pager pager,
|
||||
@Value(Common.REQ_CONTEXT) RequestContext context) {
|
||||
return physicalServerService.list(pager, context);
|
||||
}
|
||||
|
||||
|
||||
@Operation(tags = {"CMC", "CSC"}, summary = "添加标签")
|
||||
@PostMapping
|
||||
public GeneralResult<PhysicalServer> create(@RequestBody PhysicalServer physicalServer,
|
||||
@Value(Common.REQ_CONTEXT) RequestContext context) {
|
||||
return physicalServerService.create(physicalServer, context);
|
||||
}
|
||||
|
||||
|
||||
@Operation(tags = {"CMC", "CSC"}, summary = "修改标签")
|
||||
@PutMapping("/{id}")
|
||||
public GeneralResult<PhysicalServer> 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<PhysicalServer> detail(@PathVariable(value = Common.ID) Long id) {
|
||||
return physicalServerService.detail(id);
|
||||
}
|
||||
|
||||
}
|
|
@ -29,6 +29,8 @@ public class PhysicalServerModel {
|
|||
|
||||
private String name;
|
||||
|
||||
private String displayName;
|
||||
|
||||
private String remark;
|
||||
|
||||
private String systemVolumeRaidId;
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,6 +38,7 @@ public class PhyscicalConvertor implements BeanConvertor<PhysicalServerModel, JS
|
|||
model.setPrivateIp(info.getString("privateIp"));
|
||||
model.setPublicIp(info.getString("publicIp"));
|
||||
model.setEbmState(info.getString("ebmState"));
|
||||
model.setDisplayName(info.getString("displayName"));
|
||||
if (info.getJSONObject("flavor") != null) {
|
||||
model.setFlavor(info.getJSONObject("flavor").toJSONString());
|
||||
}
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
package com.bocloud.ctstack.plugin.repository;
|
||||
|
||||
import com.bocloud.cmp.entity.PhysicalServer;
|
||||
import com.megatron.common.model.Pager;
|
||||
import com.megatron.common.model.Param;
|
||||
import com.megatron.common.utils.Common;
|
||||
import com.megatron.common.utils.MapTools;
|
||||
import com.megatron.database.core.intf.impl.BasicGenericDao;
|
||||
import com.megatron.framework.core.CurrentService;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 物理服务器数据访问
|
||||
*
|
||||
* @author wangyu
|
||||
* @version 1.0
|
||||
* @since 2020年5月15日
|
||||
*/
|
||||
@Repository("serverRepository")
|
||||
public class PhysicalServerRepository extends BasicGenericDao<PhysicalServer, Long> {
|
||||
|
||||
|
||||
public PhysicalServerRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate, CurrentService service) {
|
||||
super(jdbcTemplate, npJdbcTemplate, service);
|
||||
}
|
||||
|
||||
public List<PhysicalServer> listByVendor(Long vendorId) {
|
||||
String sql = "select a.* from physical_server a where a.is_deleted = 0";
|
||||
Map<String, Object> 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<String, Object> 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<String, Object> params = MapTools.simpleMap("id", id);
|
||||
params.put("gmtModify", new Date());
|
||||
params.put("userId", userId);
|
||||
return this.execute(sql, params) > 0;
|
||||
}
|
||||
|
||||
public int count(List<Param> params) throws Exception {
|
||||
String sql = "select count(1) from physical_server where 1 = 1 ";
|
||||
sql = this.getQueryBuilder().build(sql, params, new HashMap<>(8), "");
|
||||
Map<String, Object> param = this.getQueryBuilder().getParam(params);
|
||||
return this.countQuery(sql, param).intValue();
|
||||
}
|
||||
|
||||
public List<PhysicalServer> list(int page, int rows, List<Param> params, Map<String, String> 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<String, Object> param = this.getQueryBuilder().getParam(params);
|
||||
return this.list(PhysicalServer.class, sql, param);
|
||||
}
|
||||
|
||||
}
|
|
@ -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<GridBean<PhysicalServer>> list(Pager pager, RequestContext context);
|
||||
|
||||
|
||||
GeneralResult<PhysicalServer> detail(Long id);
|
||||
|
||||
GeneralResult<PhysicalServer> create(PhysicalServer physicalServer, RequestContext context);
|
||||
|
||||
|
||||
GeneralResult<PhysicalServer> modify(PhysicalServer physicalServer, RequestContext context);
|
||||
|
||||
GeneralResult remove(Long id, RequestContext context);
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -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<GridBean<PhysicalServer>> list(Pager pager, RequestContext context) {
|
||||
List<PhysicalServer> list = null;
|
||||
int total = 0;
|
||||
GridBean gridBean = null;
|
||||
try {
|
||||
List<Param> params = Optional.ofNullable(pager.getParams()).orElse(new ArrayList<>());
|
||||
Map<String, String> 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<PhysicalServer> 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<PhysicalServer> create(PhysicalServer physicalServer, RequestContext context) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GeneralResult<PhysicalServer> 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, "删除任务下发失败");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue