|
|
@ -0,0 +1,279 @@
|
|
|
|
|
|
|
|
package net.educoder.util;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
|
|
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
|
|
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
import net.educoder.exception.BridgeException;
|
|
|
|
|
|
|
|
import net.educoder.model.param.CreateCloudHostParam;
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @Author: youys
|
|
|
|
|
|
|
|
* @Date: 2022/7/14
|
|
|
|
|
|
|
|
* @Description: 交大云工具类
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
|
|
|
public class JCloudUtil {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取云主机列表详情
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param token
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static String getCloudListDetail(String token) {
|
|
|
|
|
|
|
|
String url = "https://home.jcloud.sjtu.edu.cn:8774/v2.1/servers/detail";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
|
|
String result = HttpRequest.get(url).header("Content-Type", "application/json").header("X-Auth-Token", token).execute().body();
|
|
|
|
|
|
|
|
long end = System.currentTimeMillis();
|
|
|
|
|
|
|
|
log.info("交大云获取云主机列表详情返回结果:{},耗时:{}ms", result, (end - start));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取云主机详情
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param token
|
|
|
|
|
|
|
|
* @param serverId
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static JSONObject getCloudDetail(String token, String serverId) {
|
|
|
|
|
|
|
|
String url = "https://home.jcloud.sjtu.edu.cn:8774/v2.1/servers/{server_id}".replaceAll("\\{server_id}", serverId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
|
|
String result = HttpRequest.get(url).header("Content-Type", "application/json").header("X-Auth-Token", token).execute().body();
|
|
|
|
|
|
|
|
long end = System.currentTimeMillis();
|
|
|
|
|
|
|
|
log.info("交大云获取云主机serverId:{}详情返回结果:{},耗时:{}ms", serverId, result, (end - start));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(result)) {
|
|
|
|
|
|
|
|
return JSONObject.parseObject(result);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new BridgeException("获取云主机详情失败");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 创建云主机
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param createCloudHost
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static String createCloudHost(String token, CreateCloudHostParam createCloudHost) {
|
|
|
|
|
|
|
|
String url = "https://home.jcloud.sjtu.edu.cn:8774/v2.1/servers";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 默认
|
|
|
|
|
|
|
|
String body = "{\n" +
|
|
|
|
|
|
|
|
" \"server\":{\n" +
|
|
|
|
|
|
|
|
" \"name\": \"" + createCloudHost.getName() + "\",\n" +
|
|
|
|
|
|
|
|
" \"adminPass\": \"Educoder123\",\n" +
|
|
|
|
|
|
|
|
" \"imageRef\": \"" + createCloudHost.getImageRef() + "\",\n" +
|
|
|
|
|
|
|
|
" \"flavorRef\": \"" + createCloudHost.getFlavorRef() + "\",\n" +
|
|
|
|
|
|
|
|
" \"networks\":[\n" +
|
|
|
|
|
|
|
|
" {\n" +
|
|
|
|
|
|
|
|
" \"uuid\": \"" + createCloudHost.getNetworkId() + "\"\n" +
|
|
|
|
|
|
|
|
" }\n" +
|
|
|
|
|
|
|
|
" ],\n" +
|
|
|
|
|
|
|
|
" \"security_groups\": [\n" +
|
|
|
|
|
|
|
|
" {\n" +
|
|
|
|
|
|
|
|
" \"name\": \"default\"\n" +
|
|
|
|
|
|
|
|
" }\n" +
|
|
|
|
|
|
|
|
" ]" +
|
|
|
|
|
|
|
|
" }\n" +
|
|
|
|
|
|
|
|
"}";
|
|
|
|
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
|
|
String result = HttpRequest.post(url).header("Content-Type", "application/json").header("X-Auth-Token", token).body(body).execute().body();
|
|
|
|
|
|
|
|
long end = System.currentTimeMillis();
|
|
|
|
|
|
|
|
log.info("交大云创建云主机返回结果:{},耗时:{}ms", result, (end - start));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 更改密码
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param token
|
|
|
|
|
|
|
|
* @param serverId
|
|
|
|
|
|
|
|
* @param password
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static boolean changePassword(String token, String serverId, String password) {
|
|
|
|
|
|
|
|
String url = "https://home.jcloud.sjtu.edu.cn:8774/v2.1/servers/{server_id}/action".replaceAll("\\{server_id}", serverId);
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* {
|
|
|
|
|
|
|
|
* "changePassword" : {
|
|
|
|
|
|
|
|
* "adminPass" : "foo"
|
|
|
|
|
|
|
|
* }
|
|
|
|
|
|
|
|
* }
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
JSONObject changePassword = new JSONObject();
|
|
|
|
|
|
|
|
changePassword.put("adminPass", password);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject requestBody = new JSONObject();
|
|
|
|
|
|
|
|
requestBody.put("changePassword", changePassword);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
|
|
String result = HttpRequest.post(url).header("Content-Type", "application/json").header("X-Auth-Token", token).body(requestBody.toJSONString()).execute().body();
|
|
|
|
|
|
|
|
long end = System.currentTimeMillis();
|
|
|
|
|
|
|
|
log.info("交大云更改云主机密码返回结果:{},耗时:{}ms", result, (end - start));
|
|
|
|
|
|
|
|
return StringUtils.isNotBlank(result);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 重置云主机
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param token
|
|
|
|
|
|
|
|
* @param serverId
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static boolean rebuildCloud(String token, String serverId) {
|
|
|
|
|
|
|
|
String url = "https://home.jcloud.sjtu.edu.cn:8774/v2.1/servers/{server_id}/action".replaceAll("\\{server_id}", serverId);
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* {
|
|
|
|
|
|
|
|
* "rebuild" : {
|
|
|
|
|
|
|
|
* "imageRef" : "70a599e0-31e7-49b7-b260-868f441e862b",
|
|
|
|
|
|
|
|
* "adminPass" : "seekr3t"
|
|
|
|
|
|
|
|
* }
|
|
|
|
|
|
|
|
* }
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
JSONObject rebuild = new JSONObject();
|
|
|
|
|
|
|
|
rebuild.put("imageRef", "31a54313-bcfe-4c6e-9e19-dcad8360ef56");
|
|
|
|
|
|
|
|
rebuild.put("adminPass", "Educoder123");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject requestBody = new JSONObject();
|
|
|
|
|
|
|
|
requestBody.put("rebuild", rebuild);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
|
|
String result = HttpRequest.post(url).header("Content-Type", "application/json").header("X-Auth-Token", token).body(requestBody.toJSONString()).execute().body();
|
|
|
|
|
|
|
|
long end = System.currentTimeMillis();
|
|
|
|
|
|
|
|
log.info("交大云重新构建云主机密码返回结果:{},耗时:{}ms", result, (end - start));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 删除云主机
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param serverId
|
|
|
|
|
|
|
|
* @param token
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static boolean deleteCloudHost(String serverId, String token) {
|
|
|
|
|
|
|
|
String url = "https://home.jcloud.sjtu.edu.cn:8774/v2.1/servers/{server_id}".replaceAll("\\{server_id}", serverId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
|
|
String result = HttpRequest.delete(url).header("Content-Type", "application/json").header("X-Auth-Token", token).execute().body();
|
|
|
|
|
|
|
|
long end = System.currentTimeMillis();
|
|
|
|
|
|
|
|
log.info("交大云删除云主机返回结果:{},耗时:{}ms", result, (end - start));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return StringUtils.isBlank(result);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取api token
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return token
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static String getApiToken(String username, String password) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* {
|
|
|
|
|
|
|
|
* "auth": {
|
|
|
|
|
|
|
|
* "identity": {
|
|
|
|
|
|
|
|
* "methods": [
|
|
|
|
|
|
|
|
* "password"
|
|
|
|
|
|
|
|
* ],
|
|
|
|
|
|
|
|
* "password": {
|
|
|
|
|
|
|
|
* "user": {
|
|
|
|
|
|
|
|
* "name": "admin",
|
|
|
|
|
|
|
|
* "domain": {
|
|
|
|
|
|
|
|
* "name": "Default"
|
|
|
|
|
|
|
|
* },
|
|
|
|
|
|
|
|
* "password": "devstacker"
|
|
|
|
|
|
|
|
* }
|
|
|
|
|
|
|
|
* }
|
|
|
|
|
|
|
|
* }
|
|
|
|
|
|
|
|
* }
|
|
|
|
|
|
|
|
* }
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
JSONObject domain = new JSONObject();
|
|
|
|
|
|
|
|
domain.put("name", "Default");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject user = new JSONObject();
|
|
|
|
|
|
|
|
user.put("name", username);
|
|
|
|
|
|
|
|
user.put("password", password);
|
|
|
|
|
|
|
|
user.put("domain", domain);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject passwordJson = new JSONObject();
|
|
|
|
|
|
|
|
passwordJson.put("user", user);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject identity = new JSONObject();
|
|
|
|
|
|
|
|
identity.put("methods", Arrays.asList("password"));
|
|
|
|
|
|
|
|
identity.put("password", passwordJson);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject auth = new JSONObject();
|
|
|
|
|
|
|
|
auth.put("identity", identity);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject requestBody = new JSONObject();
|
|
|
|
|
|
|
|
requestBody.put("auth", auth);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HttpResponse response = HttpRequest.post("https://home.jcloud.sjtu.edu.cn:5000/v3/auth/tokens").header("Content-Type", "application/json").body(requestBody.toJSONString()).execute();
|
|
|
|
|
|
|
|
if (response.isOk()) {
|
|
|
|
|
|
|
|
String token = response.header("X-Subject-Token");
|
|
|
|
|
|
|
|
log.info("交我算平台获取token成功,token: {}", token);
|
|
|
|
|
|
|
|
return token;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.info("交我算平台获取token失败,body:{}", response.body());
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
String username = "educoder01";
|
|
|
|
|
|
|
|
String password = "Educoder123";
|
|
|
|
|
|
|
|
String apiToken = getApiToken(username, password);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// createCloudHost(apiToken, defaultCreateCloudHostParam());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String serverId = "7c91c97b-37a4-4cf2-85be-fdddaef44f2b";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 重置云主机,并且需要修改密码
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// rebuildCloud(apiToken, serverId);
|
|
|
|
|
|
|
|
// changePassword(apiToken, serverId, "Educoder123");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// getCloudDetail(apiToken, serverId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// deleteCloudHost("902d0100-745c-4ef5-af44-edca4b1410d9",apiToken);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 默认创建云主机参数
|
|
|
|
|
|
|
|
* 镜像:centos7.9
|
|
|
|
|
|
|
|
* 规格:4核8G
|
|
|
|
|
|
|
|
* 内网网络:提前创建好
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static CreateCloudHostParam defaultCreateCloudHostParam() {
|
|
|
|
|
|
|
|
CreateCloudHostParam createCloudHost = new CreateCloudHostParam();
|
|
|
|
|
|
|
|
createCloudHost.setName(RandomUtil.randomString(10));
|
|
|
|
|
|
|
|
createCloudHost.setImageRef("31a54313-bcfe-4c6e-9e19-dcad8360ef56");
|
|
|
|
|
|
|
|
createCloudHost.setFlavorRef("02723f5f-5518-4031-bd5e-dfecae675606");
|
|
|
|
|
|
|
|
createCloudHost.setNetworkId("f41ece4b-ff14-4444-8d1d-c730779a0df2");
|
|
|
|
|
|
|
|
return createCloudHost;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|