提供接口

master
youys 2 years ago
parent 83de265d7e
commit 9f8d19c5c7

@ -46,6 +46,12 @@
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1-jre</version>
</dependency>
</dependencies>
<build>

@ -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<String, String> cache = guavaCache();
String apiToken = JCloudUtil.getApiToken(username, password);
log.info("token init--------{}", apiToken);
cache.put(CommonConstants.API_TOKEN, apiToken);
}
@Bean
public Cache<String, String> 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();
}
}

@ -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";
}

@ -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<String, String> cache;
/**
*
*
* @return
*/
@PostMapping("/createCloudHost")
public ResponseResult<String> 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<String> 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;
}
}

@ -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;

@ -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("获取云主机详情失败");
}

@ -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<T> 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 <T> 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());
}
}

@ -2,3 +2,8 @@ server:
port: 8181
servlet:
context-path: /sjtu-bridge
# 交大云平台账号密码
jcloud:
username: educoder01
password: Educoder123

Loading…
Cancel
Save