diff --git a/pom.xml b/pom.xml
index 5fdd642..212195e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -46,6 +46,12 @@
3.12.0
+
+ com.google.guava
+ guava
+ 30.1-jre
+
+
diff --git a/src/main/java/net/educoder/config/CacheConfig.java b/src/main/java/net/educoder/config/CacheConfig.java
new file mode 100644
index 0000000..d4078c5
--- /dev/null
+++ b/src/main/java/net/educoder/config/CacheConfig.java
@@ -0,0 +1,52 @@
+package net.educoder.config;
+
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import lombok.extern.slf4j.Slf4j;
+import net.educoder.constant.CommonConstants;
+import net.educoder.util.JCloudUtil;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import javax.annotation.PostConstruct;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @Author: youys
+ * @Date: 2022/8/17
+ * @Description: 缓存配置类
+ */
+@Slf4j
+@Configuration
+public class CacheConfig {
+
+ @Value("${jcloud.password}")
+ private String username;
+
+ @Value("${jcloud.password}")
+ private String password;
+
+
+ @PostConstruct
+ public void init() {
+ Cache cache = guavaCache();
+ String apiToken = JCloudUtil.getApiToken(username, password);
+ log.info("token init--------{}", apiToken);
+ cache.put(CommonConstants.API_TOKEN, apiToken);
+ }
+
+ @Bean
+ public Cache guavaCache() {
+
+ return CacheBuilder.newBuilder()
+ .concurrencyLevel(8)
+ .initialCapacity(100)
+ .maximumSize(100)
+ .expireAfterWrite(1, TimeUnit.HOURS)
+ .removalListener(notify -> {
+ log.info("缓存key:{}, value:{}, 被移除原因:{}", notify.getKey(), notify.getValue(), notify.getCause());
+ })
+ .build();
+ }
+}
diff --git a/src/main/java/net/educoder/constant/CommonConstants.java b/src/main/java/net/educoder/constant/CommonConstants.java
new file mode 100644
index 0000000..c15ee32
--- /dev/null
+++ b/src/main/java/net/educoder/constant/CommonConstants.java
@@ -0,0 +1,18 @@
+package net.educoder.constant;
+
+/**
+ * @Author: youys
+ * @Date: 2022/8/17
+ * @Description: 常量
+ */
+public class CommonConstants {
+
+ public static final String SUCCESS = "success";
+
+ public static final String FAILED = "failed";
+
+ public static final String API_TOKEN = "api_token";
+
+
+ public static final String DEFAULT_PASSWORD = "Educoder_123123";
+}
diff --git a/src/main/java/net/educoder/controller/CloudController.java b/src/main/java/net/educoder/controller/CloudController.java
index 1439fc4..9952b77 100644
--- a/src/main/java/net/educoder/controller/CloudController.java
+++ b/src/main/java/net/educoder/controller/CloudController.java
@@ -1,5 +1,14 @@
package net.educoder.controller;
+import com.google.common.cache.Cache;
+import lombok.extern.slf4j.Slf4j;
+import net.educoder.constant.CommonConstants;
+import net.educoder.util.JCloudUtil;
+import net.educoder.util.ResponseResult;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -8,9 +17,97 @@ import org.springframework.web.bind.annotation.RestController;
* @Date: 2022/8/16
* @Description: 交大云交互接口
*/
+@Slf4j
@RestController
@RequestMapping("/jcloud")
public class CloudController {
+ @Value("${jcloud.password}")
+ private String username;
+
+ @Value("${jcloud.password}")
+ private String password;
+
+ @Autowired
+ private Cache cache;
+
+
+ /**
+ * 创建云主机
+ *
+ * @return
+ */
+ @PostMapping("/createCloudHost")
+ public ResponseResult createCloudHost() {
+
+ String apiToken = cache.getIfPresent(CommonConstants.API_TOKEN);
+ if (StringUtils.isBlank(apiToken)) {
+ apiToken = refreshApiToken();
+ }
+ JCloudUtil.createCloudHost(apiToken, JCloudUtil.defaultCreateCloudHostParam());
+ return ResponseResult.success();
+ }
+
+
+ /**
+ * 获取云主机列表
+ *
+ * @return
+ */
+ @PostMapping("/getCloudHostList")
+ public ResponseResult getCloudHostList() {
+
+ String apiToken = cache.getIfPresent(CommonConstants.API_TOKEN);
+ if (StringUtils.isBlank(apiToken)) {
+ apiToken = refreshApiToken();
+ }
+
+ String cloudHost = JCloudUtil.createCloudHost(apiToken, JCloudUtil.defaultCreateCloudHostParam());
+ return ResponseResult.success(cloudHost);
+ }
+
+
+ /**
+ * 重置云主机
+ *
+ * @return
+ */
+ @PostMapping("/resetCloudHost")
+ public ResponseResult resetCloudHost(String serverId) {
+
+ String apiToken = cache.getIfPresent(CommonConstants.API_TOKEN);
+ if (StringUtils.isBlank(apiToken)) {
+ apiToken = refreshApiToken();
+ }
+
+ boolean status = JCloudUtil.rebuildCloud(apiToken, serverId);
+ if (!status) {
+ return ResponseResult.failed(-1, "重置云主机失败");
+ }
+ // 等待30秒再重置密码
+ try {
+ Thread.sleep(30);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ // 重置之后需要再更新密码
+ boolean changePwdStatus = JCloudUtil.changePassword(apiToken, serverId, CommonConstants.DEFAULT_PASSWORD);
+
+ return changePwdStatus ? ResponseResult.success() : ResponseResult.failed(-1,"重置密码失败");
+ }
+
+
+ /**
+ * 刷新token
+ */
+ private String refreshApiToken() {
+ String apiToken = JCloudUtil.getApiToken(username, password);
+ log.info("token refresh--------{}", apiToken);
+ cache.put(CommonConstants.API_TOKEN, apiToken);
+
+ return apiToken;
+ }
+
+
}
diff --git a/src/main/java/net/educoder/exception/BridgeException.java b/src/main/java/net/educoder/exception/BusinessException.java
similarity index 80%
rename from src/main/java/net/educoder/exception/BridgeException.java
rename to src/main/java/net/educoder/exception/BusinessException.java
index 2271f2e..e699e0e 100644
--- a/src/main/java/net/educoder/exception/BridgeException.java
+++ b/src/main/java/net/educoder/exception/BusinessException.java
@@ -7,7 +7,7 @@ import com.alibaba.fastjson.JSONObject;
* @Date: 2022/8/16
* @Description:
*/
-public class BridgeException extends RuntimeException{
+public class BusinessException extends RuntimeException{
private int code;
private String msg;
@@ -20,13 +20,13 @@ public class BridgeException extends RuntimeException{
return msg;
}
- public BridgeException(int code, String msg) {
+ public BusinessException(int code, String msg) {
super(msg);
this.code = code;
this.msg = msg;
}
- public BridgeException(String msg) {
+ public BusinessException(String msg) {
super(msg);
this.code = -1;
this.msg = msg;
diff --git a/src/main/java/net/educoder/util/JCloudUtil.java b/src/main/java/net/educoder/util/JCloudUtil.java
index daba88a..362a5d6 100644
--- a/src/main/java/net/educoder/util/JCloudUtil.java
+++ b/src/main/java/net/educoder/util/JCloudUtil.java
@@ -5,7 +5,7 @@ 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.exception.BusinessException;
import net.educoder.model.param.CreateCloudHostParam;
import org.apache.commons.lang3.StringUtils;
@@ -55,7 +55,7 @@ public class JCloudUtil {
if (StringUtils.isNotBlank(result)) {
return JSONObject.parseObject(result);
}
- throw new BridgeException("获取云主机详情失败");
+ throw new BusinessException("获取云主机详情失败");
}
diff --git a/src/main/java/net/educoder/util/ResponseResult.java b/src/main/java/net/educoder/util/ResponseResult.java
new file mode 100644
index 0000000..8852d29
--- /dev/null
+++ b/src/main/java/net/educoder/util/ResponseResult.java
@@ -0,0 +1,54 @@
+package net.educoder.util;
+
+import lombok.Data;
+import net.educoder.exception.BusinessException;
+
+import java.io.Serializable;
+
+/**
+ * @Author: yys
+ * @Date: 2018/6/4
+ * @description:
+ */
+@Data
+public class ResponseResult implements Serializable {
+
+ private int code;
+ private String desc;
+ private T results;
+
+ public ResponseResult() {
+ this.code = 1;
+ this.desc = "成功";
+ }
+
+ public ResponseResult(int code, String desc) {
+ this.code = code;
+ this.desc = desc;
+ }
+
+ public ResponseResult(int code, String desc, T results) {
+ this.code = code;
+ this.desc = desc;
+ this.results = results;
+ }
+
+ public static ResponseResult success() {
+ return new ResponseResult();
+ }
+
+ public static ResponseResult success(T obj) {
+ return new ResponseResult(1, "成功", obj);
+ }
+
+ public static ResponseResult failed(int code, String desc) {
+ return new ResponseResult(code, desc);
+ }
+
+
+ public static ResponseResult failed(BusinessException be) {
+ return new ResponseResult(be.getCode(), be.getMsg());
+ }
+
+
+}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index b167e67..d60ef2d 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -2,3 +2,8 @@ server:
port: 8181
servlet:
context-path: /sjtu-bridge
+
+# 交大云平台账号密码
+jcloud:
+ username: educoder01
+ password: Educoder123