物理机创建、查询优化
parent
c81027e127
commit
ce82e9271e
|
@ -220,7 +220,7 @@ public class TianyiLocationProvider extends TianyiProvider {
|
|||
}
|
||||
log.info("获取物理机信息...");
|
||||
TianyiPhysicalProvider physicalProvider = new TianyiPhysicalProvider(this.getButler());
|
||||
GeneralResult<List<PhysicalServerModel>> physicalResult = physicalProvider.list();
|
||||
GeneralResult<List<PhysicalServerModel>> physicalResult = physicalProvider.list(null);
|
||||
log.info("同步物理机结果:{}", physicalResult.isSuccess());
|
||||
if(physicalResult.isSuccess()){
|
||||
syncModel.setPhysicalModels(physicalResult.getData());
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.bocloud.ctstack.plugin.provider.compute;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.bocloud.cmp.domain.Butler;
|
||||
|
@ -55,14 +56,14 @@ public class TianyiPhysicalProvider extends TianyiProvider {
|
|||
physicalModels.add(physcicalConvertor.convertModel(model));
|
||||
}
|
||||
}
|
||||
return new GeneralResult(true, physicalModels, "查询宿主机列表成功");
|
||||
return new GeneralResult(true, physicalModels, "查询物理机列表成功");
|
||||
} catch (Exception e) {
|
||||
log.error("查询宿主机列表失败 :", e);
|
||||
return new GeneralResult(false, "查询宿主机列表失败" + e.getMessage());
|
||||
log.error("查询物理机列表失败 :", e);
|
||||
return new GeneralResult(false, "查询物理机列表失败" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/*public GeneralResult create(PhysicalServerModel model) {
|
||||
public GeneralResult create(PhysicalServerModel model) {
|
||||
try {
|
||||
String apiUrl = "/v4/ebm/create";
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
@ -95,30 +96,28 @@ public class TianyiPhysicalProvider extends TianyiProvider {
|
|||
Result result = doPost(apiUrl, jsonObject);
|
||||
JSONObject returnObj = checkResult(result, "创建物理机");
|
||||
List<JSONObject> resources = returnObj.getJSONArray("resources").toJavaList(JSONObject.class);
|
||||
String resourceID = resources.get(0).getString("resourceID");
|
||||
if (resourceID == null) {
|
||||
return new GeneralResult(false, "创建物理机失败!");
|
||||
if (CollectionUtils.isEmpty(resources)) {
|
||||
return GeneralResult.FAILED("物理机创建失败:无创建resource信息返回");
|
||||
}
|
||||
Long startTime = System.currentTimeMillis();
|
||||
while (true) {
|
||||
sleepSomeTime(5000);
|
||||
PhysicalServerModel physicalServerModel = (PhysicalServerModel) detail(resourceID, model.getRegionId(), false).getData();
|
||||
if (null != physicalServerModel) {
|
||||
return new GeneralResult<>(true, physicalServerModel, "success");
|
||||
} else {
|
||||
log.warn("根据ResourceId[{}]查询云主机信息返回空数据", resourceID);
|
||||
}
|
||||
Long endTime = System.currentTimeMillis();
|
||||
// 等待三分钟
|
||||
if (endTime - startTime > 15 * 60 * 1000) {
|
||||
return GeneralResult.FAILED("查询资源详情超时!");
|
||||
}
|
||||
List<PhysicalServerModel> models = new ArrayList<>();
|
||||
List<String> resourceIds = new ArrayList<>();
|
||||
for (JSONObject resource : resources) {
|
||||
String resourceId = resource.getString("resourceID");
|
||||
resourceIds.add(resourceId);
|
||||
}
|
||||
GeneralResult modelResult = listByResourceId(resourceIds, model.getZoneId());
|
||||
Object modelObject = modelResult.getData();
|
||||
if (modelObject != null) {
|
||||
models = JSONArray.parseArray(JSON.toJSONString(modelObject), PhysicalServerModel.class);
|
||||
} else {
|
||||
return GeneralResult.FAILED("物理主机批量创建完全失败");
|
||||
}
|
||||
return new GeneralResult<>(true, models, "success");
|
||||
} catch (Exception e) {
|
||||
log.error("创建物理机失败 : " + e.getMessage(), e);
|
||||
return GeneralResult.FAILED("创建物理机失败:" + e.getMessage());
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
public List<JSONObject> listZones(String regionId) {
|
||||
try {
|
||||
|
@ -176,4 +175,41 @@ public class TianyiPhysicalProvider extends TianyiProvider {
|
|||
}
|
||||
|
||||
|
||||
public GeneralResult<List<PhysicalServerModel>> listByResourceId(List<String> resourceIds, String zoneId) {
|
||||
try {
|
||||
List<PhysicalServerModel> physicalModels = new ArrayList<>();
|
||||
for (String resourceId : resourceIds) {
|
||||
long startTime = System.currentTimeMillis();
|
||||
Boolean flag = true;
|
||||
while (flag) {
|
||||
if (System.currentTimeMillis() - startTime > 3 * 60 * 1000) {
|
||||
flag = false;
|
||||
}
|
||||
String apiUrl = "/v4/ebm/list";
|
||||
JSONObject body = new JSONObject();
|
||||
body.put("regionID", this.getRegionId());
|
||||
body.put("azName", zoneId);
|
||||
body.put("resourceID", resourceId);
|
||||
Result result = doGet(apiUrl, null, body);
|
||||
JSONObject returnObj = checkResult(result, "查询物理机列表");
|
||||
List<JSONObject> results = new ArrayList<>();
|
||||
results.addAll(JSONArray.parseArray(returnObj.getString("results"), JSONObject.class));
|
||||
if (!CollectionUtils.isEmpty(results)) {
|
||||
for (JSONObject model : results) {
|
||||
physicalModels.add(physcicalConvertor.convertModel(model));
|
||||
}
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (physicalModels.size() != resourceIds.size()) {
|
||||
return new GeneralResult<>(false, physicalModels, "部分成功");
|
||||
}
|
||||
return new GeneralResult(true, physicalModels, "查询物理机列表成功");
|
||||
} catch (Exception e) {
|
||||
log.error("查询物理机列表失败 :", e);
|
||||
return new GeneralResult(false, "查询物理机列表失败" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -64,7 +64,11 @@ public class PhysicalServerRepository extends BasicGenericDao<PhysicalServer, Lo
|
|||
}
|
||||
|
||||
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 ";
|
||||
String sql = "select a.*, b.name vpc_name, c.name subnet_name from physical_server a " +
|
||||
"left join vpc b on b.vpc_id = a.vpc_uuid " +
|
||||
"left join subnet c on c.subnet_uuid = a.subnet_uuid " +
|
||||
"left join zone d on d.zone_id = a.zone_id " +
|
||||
"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);
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
package com.bocloud.ctstack.plugin.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
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.PhysicalServerRepository;
|
||||
import com.bocloud.ctstack.plugin.repository.ResourceEventRepository;
|
||||
import com.bocloud.ctstack.plugin.repository.VolumeRepository;
|
||||
import com.bocloud.ctstack.plugin.repository.VpcRepository;
|
||||
|
@ -22,9 +24,11 @@ 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.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -85,7 +89,39 @@ public class PhysicalServerServiceImpl implements PhysicalServerService {
|
|||
|
||||
@Override
|
||||
public GeneralResult<PhysicalServer> create(PhysicalServer physicalServer, RequestContext context) {
|
||||
return null;
|
||||
// 云平台ID
|
||||
Long vendorId = physicalServer.getVendorId();
|
||||
String path = CloudVendor.class.getSimpleName() + "_" + vendorId + "_SYNC";
|
||||
try (AutoCloseLock lock = lockFactory.getACLock(path)) {
|
||||
if (!lock.acquire(10, TimeUnit.SECONDS)) {
|
||||
log.warn("当前云平台存在同步任务,稍后创建");
|
||||
return GeneralResult.FAILED("当前云平台存在同步任务,稍后创建!");
|
||||
}
|
||||
CloudVendor vendor = cloudVendorRepository.query(vendorId);
|
||||
Assert.notNull(vendor, "云平台不存在");
|
||||
new Thread(() -> {
|
||||
TianyiPhysicalProvider physicalProvider = new TianyiPhysicalProvider(butlerConfig.regionButler(vendor.getUuid(), physicalServer.getRegionId()));
|
||||
PhysicalServerModel model = new PhysicalServerModel();
|
||||
BeanUtils.copyProperties(physicalServer, model);
|
||||
GeneralResult result = physicalProvider.create(model);
|
||||
if (result.isSuccess()) {
|
||||
if (result.getData() != null) {
|
||||
List<PhysicalServerModel> models = JSONArray.parseArray(JSON.toJSONString(result.getData()), PhysicalServerModel.class);
|
||||
if (!CollectionUtils.isEmpty(models)) {
|
||||
for (PhysicalServerModel serverModel: models) {
|
||||
PhysicalServer server = new PhysicalServer();
|
||||
BeanUtils.copyProperties(serverModel, server);
|
||||
physicalServerRepository.save(server);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
return new GeneralResult<>(true, "裸金属创建任务下发成功!");
|
||||
} catch (Exception e) {
|
||||
log.error("Get error message:" + e.getMessage(), e);
|
||||
return GeneralResult.FAILED("裸金属创建失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -108,10 +144,12 @@ public class PhysicalServerServiceImpl implements PhysicalServerService {
|
|||
model.setRegionId(oldServer.getRegionId());
|
||||
model.setZoneId(oldServer.getZoneId());
|
||||
model.setRemark(physicalServer.getRemark());
|
||||
model.setDisplayName(physicalServer.getDisplayName());
|
||||
TianyiPhysicalProvider physicalProvider = new TianyiPhysicalProvider(butlerConfig.regionButler(vendor.getUuid(), oldServer.getRegionId()));
|
||||
GeneralResult result = physicalProvider.modify(model);
|
||||
if (result.isSuccess()) {
|
||||
oldServer.setRemark(physicalServer.getRemark());
|
||||
oldServer.setDisplayName(physicalServer.getDisplayName());
|
||||
physicalServerRepository.update(oldServer);
|
||||
}
|
||||
}).start();
|
||||
|
|
Loading…
Reference in New Issue