天翼云添加容器资源纳管功能接口

iida
tanshaolong 2024-09-04 13:56:56 +08:00
parent dc0c56f1cb
commit a56102c285
12 changed files with 453 additions and 37 deletions

View File

@ -0,0 +1,35 @@
package com.bocloud.ctstack.plugin.domain.model;
import cn.hutool.core.date.DateTime;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ContainerClusterModel {
private String name;
private String remark;
private String clusterSpec;
private String status;
private String regionId;
private String clusterUuid;
private String clusterType;
private String version;
private Date expiredTime;
private String nodeCidr;
private String vpcUuid;
private Date created;
private String eipUuid;
private String loadBalancerUuid;
private String resourceGroupUuid;
private String serviceCidr;
private Long businessId;
}

View File

@ -0,0 +1,19 @@
package com.bocloud.ctstack.plugin.domain.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ContainerNodeModel {
private String nodeName;
private String nodeStatus;
private String regionId;
private String clusterName;
private String clusterUuid;
private Date created;
}

View File

@ -0,0 +1,35 @@
package com.bocloud.ctstack.plugin.domain.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ContainerNodePoolModel {
private String name;
private String nodepoolUuid;
private String remark;
private String status;
private String regionId;
private String clusterUuid;
private String clusterName;
private boolean isDefault;
private String type;
private Long totalNodes;
private Long healthyNodes;
private Long failedNodes;
private Date created;
private String kubernetesConfig;
private String scalingGroup;
// 天翼云的数据
private Long cpu;
private Long memory;
private String vmSpecName; // 节点规格
private String vmSpecType; // 节点规格类型
private String vmSpecId; // 节点规格id
}

View File

@ -1,13 +0,0 @@
package com.bocloud.ctstack.plugin.domain.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ContarinerClusterModel {
private String name;
}

View File

@ -34,7 +34,7 @@ public class TianyiSyncModel {
private List<HostModel> hostModels;
private ResourcePoolModel resourcePoolModel;
private List<RdsModel> rdsModels;
private List<ContarinerClusterModel> contarinerClusterModels;
private List<ContainerClusterModel> containerClusterModels;
/**
* @param serverModels
* @param imageModels

View File

@ -150,6 +150,20 @@ public class TianyiProvider {
return httpClient.post(header, params, uri);
}
public Result doPut(String url, Map<String, Object> headers, Map<String, Object> params) {
HttpClient httpClient = new HttpClient(60 * 1000, PostDataFormat.RAW);
String uri = buildUrl(url);
Map<String, Object> header = buildHeader(HttpRequestMethod.PUT, url, headers, params);
printCurl(HttpRequestMethod.PUT, uri, header, params);
return httpClient.post(header, params, uri);
}
public Result doDelete(String url, Map<String, Object> headers) {
HttpClient httpClient = new HttpClient(20 * 1000, PostDataFormat.RAW);
httpClient.setDataFormat(null);
return httpClient.get(buildHeader(HttpRequestMethod.DELETE, url, headers, null), null, buildUrl(url));
}
public String getCtUserId() {
return ctUserId;
}

View File

@ -290,7 +290,7 @@ public class TianyiLocationProvider extends TianyiProvider {
GeneralResult containerClusterList = tianYiContainerClusterProvider.list();
log.info("同步容器结果:{}",containerClusterList.isSuccess());
if (containerClusterList.isSuccess()) {
syncModel.setContarinerClusterModels((List<ContarinerClusterModel>) containerClusterList.getData());
syncModel.setContainerClusterModels((List<ContainerClusterModel>) containerClusterList.getData());
}
// 同步云硬盘

View File

@ -1,10 +1,9 @@
package com.bocloud.ctstack.plugin.provider.container;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.domain.Butler;
import com.bocloud.ctstack.plugin.domain.model.ContarinerClusterModel;
import com.bocloud.ctstack.plugin.domain.model.ContainerClusterModel;
import com.bocloud.ctstack.plugin.provider.TianyiProvider;
import com.bocloud.ctstack.plugin.provider.tianyiconvertor.BeanConvertor;
import com.bocloud.ctstack.plugin.provider.tianyiconvertor.ContainerClusterConvertor;
@ -12,6 +11,7 @@ import com.megatron.common.model.GeneralResult;
import com.megatron.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -20,7 +20,7 @@ import java.util.Map;
public class TianYiContainerClusterProvider extends TianyiProvider {
BeanConvertor<ContarinerClusterModel, JSONObject> containerClusterConvertor = new ContainerClusterConvertor();
BeanConvertor<ContainerClusterModel, JSONObject> containerClusterConvertor = new ContainerClusterConvertor();
Butler butler = null;
@ -37,18 +37,112 @@ public class TianYiContainerClusterProvider extends TianyiProvider {
public GeneralResult list() {
try {
String url = "/v1.1/ccse/clusters/page";
Integer pageNow = 1;
Integer pageSize = 100;
JSONObject body = new JSONObject();
body.put("pageSize", pageSize);
Map<String, Object> header = new HashMap<>();
header.put("regionId", this.getRegionId());
Result result = doGet(url, header);
log.info("result:{}", JSON.toJSONString(result));
JSONObject checkedResult = checkResult(result, "查询容器集群列表");
log.info("checkedResult:{}", JSON.toJSONString(checkedResult));
List<JSONObject> records = JSONObject.parseObject(checkedResult.getString("records"), List.class);
String url = "/v1.1/ccse/clusters/page";
List<JSONObject> records = new ArrayList<>();
while (true) {
body.put("pageNow", pageNow);
Result result = doGet(url, header, body);
JSONObject returnObj = checkResult(result, "查询容器集群列表");
log.info("checkedResult:{}", JSON.toJSONString(returnObj));
records.addAll(JSONObject.parseObject(returnObj.getString("records"), List.class));
pageNow++;
Integer current = returnObj.getInteger("current");
Integer pages = returnObj.getInteger("pages");
if (current == pages) {
break;
}
}
return new GeneralResult(true, containerClusterConvertor.convertList(records), "查询成功");
} catch (Exception e) {
log.error("查询容器集群列表失败 " + e);
return new GeneralResult(false, "查询容器集群列表失败", e.getMessage());
}
}
public GeneralResult create(ContainerClusterModel model) {
try {
String url = "/v1.1/ccse/clusters/createByPaas";
Map<String, Object> headers = new HashMap<>();
headers.put("regionId", this.getRegionId());
// 容器body参数
JSONObject body = new JSONObject();
body.put("reqType", "2"); // 2为天翼云 1桌面云
body.put("resPoolId", model.getResourceGroupUuid());
// clusterBaseV2VO参数
JSONObject clusterBaseV2VO = new JSONObject();
body.put("clusterBaseV2VO", clusterBaseV2VO);
// masterHost参数
JSONObject masterHost = new JSONObject();
body.put("masterHost", masterHost);
// slaveHost参数
JSONObject slaveHost = new JSONObject();
body.put("slaveHost", slaveHost);
Result result = doPost(url, headers, body);
JSONObject returnObj = checkResult(result, "创建容器集群");
// TODO: 等待订单完成
return detail(model.getName());
} catch (Exception e) {
log.error("创建容器集群失败 " + e);
return new GeneralResult(false, "创建容器集群失失败", e.getMessage());
}
}
public GeneralResult remove(String poolId, String InstanceId) {
try {
String url = "/v1.1/ccse/clusters/deleteByPaas";
JSONObject body = new JSONObject();
body.put("resPoolId", poolId);
body.put("prodInstId", InstanceId);
Map<String, Object> headers = new HashMap<>();
headers.put("regionId", this.getRegionId());
Result result = doPost(url, headers, body);
checkResult(result, "删除容器集群");
return new GeneralResult(true, "删除容器集群成功");
} catch (Exception e) {
log.error("删除容器集群失败 " + e);
return new GeneralResult(false, "删除容器集群失败", e.getMessage());
}
}
public GeneralResult detail(String clusterName) {
try {
String apiUrl = "/v1.1/ccse/clusters/" + clusterName;
Map<String, Object> headers = new HashMap<>();
headers.put("regionId", this.getRegionId());
Result result = doGet(apiUrl, headers);
JSONObject returnObj = checkResult(result, "查询容器集群详情");
// TODO:
ContainerClusterModel model = JSONObject.parseObject(JSONObject.toJSONString(returnObj), ContainerClusterModel.class);
return new GeneralResult(true, model, "查询容器集群详情成功");
} catch (Exception e) {
log.error("查询容器集群详情失败 " + e);
return new GeneralResult(false, null, "查询容器集群详情失败");
}
}
private GeneralResult modifyDesc(String clusterName, String clusterDesc) {
try {
String apiUrl = "/ccse/clusters/" + clusterName;
JSONObject body = new JSONObject();
body.put("clusterDesc", clusterDesc);
Map<String, Object> headers = new HashMap<>();
headers.put("regionId", this.getRegionId());
Result result = doPut(apiUrl, headers, body);
checkResult(result, "修改集群描述");
return detail(clusterName);
} catch (Exception e) {
log.error("修改容器集群失败 " + e);
return new GeneralResult(false, "修改容器集群失败", e.getMessage());
}
}
}

View File

@ -0,0 +1,137 @@
package com.bocloud.ctstack.plugin.provider.container;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.domain.Butler;
import com.bocloud.ctstack.plugin.domain.model.ContainerNodeModel;
import com.bocloud.ctstack.plugin.domain.model.ContainerNodePoolModel;
import com.bocloud.ctstack.plugin.provider.TianyiProvider;
import com.bocloud.ctstack.plugin.provider.tianyiconvertor.BeanConvertor;
import com.bocloud.ctstack.plugin.provider.tianyiconvertor.ContainerNodeConvertor;
import com.bocloud.ctstack.plugin.provider.tianyiconvertor.ContainerNodePoolConvertor;
import com.megatron.common.model.GeneralResult;
import com.megatron.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
public class TianYiContainerNodeProvider extends TianyiProvider {
BeanConvertor<ContainerNodeModel, JSONObject> nodeConvertor = new ContainerNodeConvertor();
BeanConvertor<ContainerNodePoolModel, JSONObject> nodePoolConvertor = new ContainerNodePoolConvertor();
Butler butler = null;
/**
*
*
* @param butler
*/
public TianYiContainerNodeProvider(Butler butler) {
super(butler);
this.butler = butler;
}
public GeneralResult listNodes(String clusterName) {
try {
String url = "/v1.1/ccse/clusters/" + clusterName + "/nodes/list";
Map<String, Object> 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 {
String url = "/ccse/clusters/" + poolModel.getClusterName() + "/nodePool";
Map<String, Object> headers = new HashMap<>();
headers.put("regionId", this.getRegionId());
// 容器body参数
JSONObject body = new JSONObject();
body.put("nodePoolName", poolModel.getName());
body.put("vmSpecName", poolModel.getVmSpecName());
body.put("vmSpecId", poolModel.getVmSpecId());
body.put("cpu", poolModel.getCpu());
body.put("memory", poolModel.getMemory());
Result result = doPost(url, headers, body);
JSONObject returnObj = checkResult(result, "创建容器集群");
return detailNodePool(poolModel.getClusterName(), poolModel.getName());
} catch (Exception e) {
log.error("创建容器集群节点池失败 " + e);
return new GeneralResult(false, "创建容器集群节点池失败", e.getMessage());
}
}
public GeneralResult detailNodePool(String clusterName, String poolName) {
try {
String apiUrl = "/v1.1/ccse/clusters/" + clusterName + "/nodePool/" + poolName;
Map<String, Object> headers = new HashMap<>();
headers.put("regionId", this.getRegionId());
Result result = doGet(apiUrl, headers);
JSONObject returnObj = checkResult(result, "查询容器节点池详情");
ContainerNodePoolModel poolModel = nodePoolConvertor.convertModel(returnObj);
return new GeneralResult(true, poolModel, "查询容器节点池详情成功");
} catch (Exception e) {
log.error("查询容器节点池详情失败 " + e);
return new GeneralResult(false, null, "查询容器节点池详情失败");
}
}
public GeneralResult listNodePools(String clusterName) {
try {
Integer pageNow = 1;
Integer pageSize = 100;
String url = "/v1.1/ccse/clusters/" + clusterName + "/nodePool/page";
JSONObject body = new JSONObject();
body.put("pageSize", pageSize);
Map<String, Object> header = new HashMap<>();
header.put("regionId", this.getRegionId());
List<JSONObject> records = new ArrayList<>();
while (true) {
body.put("pageNow", pageNow);
Result result = doGet(url, header, body);
JSONObject returnObj = checkResult(result, "容器集群节点池列表");
records.addAll(JSONObject.parseObject(returnObj.getString("records"), List.class));
pageNow++;
Integer current = returnObj.getInteger("current");
Integer pages = returnObj.getInteger("pages");
if (current == pages) {
break;
}
}
return new GeneralResult(true, nodePoolConvertor.convertList(records), "查询容器集群节点池列表成功");
} catch (Exception e) {
log.error("查询容器集群节点池列表失败 " + e);
return new GeneralResult(false, "查询容器集群节点池列表失败", e.getMessage());
}
}
public GeneralResult deleteNodePool(String clusterName, String poolName) {
try {
String url = "/ccse/clusters/" + clusterName + "/nodePool/" + poolName + "/delete";
Map<String, Object> headers = new HashMap<>();
headers.put("regionId", this.getRegionId());
Result result = doDelete(url, headers);
checkResult(result, "删除容器节点资源池");
return new GeneralResult(true, "删除容器节点资源池成功");
} catch (Exception e) {
log.error("删除容器节点资源池失败 " + e);
return new GeneralResult(false, "删除容器节点资源池失败", e.getMessage());
}
}
}

View File

@ -1,31 +1,45 @@
package com.bocloud.ctstack.plugin.provider.tianyiconvertor;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.ctstack.plugin.domain.model.ContarinerClusterModel;
import com.bocloud.ctstack.plugin.domain.model.ContainerClusterModel;
import java.util.ArrayList;
import java.util.List;
public class ContainerClusterConvertor implements BeanConvertor<ContarinerClusterModel, JSONObject> {
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class ContainerClusterConvertor implements BeanConvertor<ContainerClusterModel, JSONObject> {
@Override
public List<ContarinerClusterModel> convertList(List<? extends JSONObject> list) {
List<ContarinerClusterModel> contarinerClusterModels = new ArrayList<>();
public List<ContainerClusterModel> convertList(List<? extends JSONObject> list) {
List<ContainerClusterModel> containerClusterModels = new ArrayList<>();
for (JSONObject object : list) {
contarinerClusterModels.add(convertModel(object));
containerClusterModels.add(convertModel(object));
}
return contarinerClusterModels;
return containerClusterModels;
}
@Override
public ContarinerClusterModel convertModel(JSONObject jsonObject) {
public ContainerClusterModel convertModel(JSONObject jsonObject) {
if (null == jsonObject) return null;
if (jsonObject.isEmpty()) return null;
ContarinerClusterModel contarinerClusterModel = new ContarinerClusterModel();
ContainerClusterModel containerClusterModel = new ContainerClusterModel();
contarinerClusterModel.setName(jsonObject.getString("clusterName"));
containerClusterModel.setName(jsonObject.getString("clusterName"));
containerClusterModel.setRemark(jsonObject.getString("clusterDesc"));
containerClusterModel.setClusterSpec(jsonObject.getString("deployMode"));
containerClusterModel.setVersion(jsonObject.getString("clusterVersion"));
containerClusterModel.setStatus(jsonObject.getString("clusterStatus"));
containerClusterModel.setServiceCidr(jsonObject.getString("serviceCidr"));
containerClusterModel.setNodeCidr(jsonObject.getString("podCidr"));
containerClusterModel.setCreated(jsonObject.getDate("createdTime"));
containerClusterModel.setBusinessId(jsonObject.getLong("bizState"));
containerClusterModel.setResourceGroupUuid(jsonObject.getString("resPoolId"));
containerClusterModel.setEipUuid(jsonObject.getString("eip"));
containerClusterModel.setExpiredTime(jsonObject.getDate("expireTime"));
containerClusterModel.setLoadBalancerUuid(jsonObject.getString("masterSlbIp"));
containerClusterModel.setVpcUuid(jsonObject.getString("vpcId"));
return contarinerClusterModel;
return containerClusterModel;
}
}

View File

@ -0,0 +1,33 @@
package com.bocloud.ctstack.plugin.provider.tianyiconvertor;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.ctstack.plugin.domain.model.ContainerNodeModel;
import java.util.ArrayList;
import java.util.List;
public class ContainerNodeConvertor implements BeanConvertor<ContainerNodeModel, JSONObject> {
@Override
public List<ContainerNodeModel> convertList(List<? extends JSONObject> list) {
List<ContainerNodeModel> containerNodeModels = new ArrayList<>();
for (JSONObject object : list) {
containerNodeModels.add(convertModel(object));
}
return containerNodeModels;
}
@Override
public ContainerNodeModel convertModel(JSONObject jsonObject) {
if (null == jsonObject) return null;
if (jsonObject.isEmpty()) return null;
ContainerNodeModel containerNodeModel = new ContainerNodeModel();
containerNodeModel.setNodeName(jsonObject.getString("nodeName"));
containerNodeModel.setNodeStatus(jsonObject.getString("nodeStatus"));
containerNodeModel.setCreated(jsonObject.getDate("createdTime"));
return containerNodeModel;
}
}

View File

@ -0,0 +1,48 @@
package com.bocloud.ctstack.plugin.provider.tianyiconvertor;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.ctstack.plugin.domain.model.ContainerNodePoolModel;
import java.util.ArrayList;
import java.util.List;
public class ContainerNodePoolConvertor implements BeanConvertor<ContainerNodePoolModel, JSONObject> {
@Override
public List<ContainerNodePoolModel> convertList(List<? extends JSONObject> list) {
List<ContainerNodePoolModel> containerNodePoolModels = new ArrayList<>();
for (JSONObject object : list) {
containerNodePoolModels.add(convertModel(object));
}
return containerNodePoolModels;
}
@Override
public ContainerNodePoolModel convertModel(JSONObject jsonObject) {
if (null == jsonObject) return null;
if (jsonObject.isEmpty()) return null;
ContainerNodePoolModel containerNodePoolModel = new ContainerNodePoolModel();
containerNodePoolModel.setName(jsonObject.getString("nodePoolName"));
containerNodePoolModel.setNodepoolUuid(jsonObject.getString("nodePoolName"));
containerNodePoolModel.setRemark(jsonObject.getString("description"));
containerNodePoolModel.setStatus(jsonObject.getString("status"));
//containerNodePoolModel.setIsDefault(jsonObject.getBoolean("isDefault"));
containerNodePoolModel.setVmSpecType(jsonObject.getString("vmType"));
containerNodePoolModel.setVmSpecName(jsonObject.getString("vmSpecName"));
containerNodePoolModel.setVmSpecId(jsonObject.getString("vmSpecId"));
containerNodePoolModel.setCpu(jsonObject.getLong("cpu"));
containerNodePoolModel.setMemory(jsonObject.getLong("memory"));
containerNodePoolModel.setVmSpecId(jsonObject.getString("vmSpecId"));
containerNodePoolModel.setFailedNodes(jsonObject.getLong("unNormalNodeNum"));
containerNodePoolModel.setHealthyNodes(jsonObject.getLong("normalNodeNum"));
containerNodePoolModel.setTotalNodes(jsonObject.getLong("nodeTotalNum"));
containerNodePoolModel.setCreated(jsonObject.getDate("createdTime"));
containerNodePoolModel.setKubernetesConfig(jsonObject.getString("kubeletArgs"));
containerNodePoolModel.setScalingGroup(jsonObject.getString("nodeGroup"));
return containerNodePoolModel;
}
}