chenyt 2025-08-28 20:19:46 +08:00
commit b584915bb7
24 changed files with 909 additions and 94 deletions

View File

@ -23,6 +23,7 @@
<properties> <properties>
<java.version>1.8</java.version> <java.version>1.8</java.version>
<protobuf.version>3.23.4</protobuf.version> <protobuf.version>3.23.4</protobuf.version>
<spring-cloud.version>2021.0.5</spring-cloud.version>
</properties> </properties>
<dependencies> <dependencies>
@ -32,6 +33,11 @@
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Spring Boot Configuration Processor (可选) --> <!-- Spring Boot Configuration Processor (可选) -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -124,7 +130,19 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build> <build>
<extensions> <extensions>

View File

@ -3,12 +3,14 @@ package com.unisinsight.project;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/** /**
* *
*/ */
@SpringBootApplication @SpringBootApplication
@MapperScan(basePackages = "com.unisinsight.project.mapper") @MapperScan(basePackages = "com.unisinsight.project.mapper")
@EnableFeignClients(basePackages = "com.unisinsight.project.feign")
public class Application { public class Application {
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -0,0 +1,35 @@
package com.unisinsight.project.config;
import feign.Logger;
import feign.Request;
import feign.Retryer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfig {
/**
*
*/
@Bean
public Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
/**
*
*/
@Bean
public Request.Options options() {
return new Request.Options(5000, 10000); // 连接超时5秒读取超时10秒
}
/**
*
*/
@Bean
public Retryer retryer() {
return new Retryer.Default(1000, 2000, 3); // 初始间隔1秒最大间隔2秒最多重试3次
}
}

View File

@ -52,6 +52,7 @@ public class ClientController {
deviceReq.setDeviceName("默认终端"); deviceReq.setDeviceName("默认终端");
deviceReq.setDeviceGroupId(3L); deviceReq.setDeviceGroupId(3L);
deviceReq.setDeviceGroupName("默认终端分组"); deviceReq.setDeviceGroupName("默认终端分组");
deviceReq.setMacAddr(loginReq.getMacAddr());
return deviceService.insert(deviceReq); return deviceService.insert(deviceReq);
} }
@ -108,6 +109,4 @@ public class ClientController {
hashMap.put("url", "https://intent-bathhouse.name"); hashMap.put("url", "https://intent-bathhouse.name");
return Result.successResult(hashMap); return Result.successResult(hashMap);
} }
} }

View File

@ -2,12 +2,9 @@ package com.unisinsight.project.controller;
import com.unisinsight.project.entity.dao.NetworkManage; import com.unisinsight.project.entity.dao.NetworkManage;
import com.unisinsight.project.entity.req.DeleteIdReq;
import com.unisinsight.project.entity.req.NetworkManagePageReq; import com.unisinsight.project.entity.req.NetworkManagePageReq;
import com.unisinsight.project.entity.req.NetworkManageReq; import com.unisinsight.project.entity.req.NetworkManageReq;
import com.unisinsight.project.entity.req.StoragePoolReq;
import com.unisinsight.project.entity.res.PageResult; import com.unisinsight.project.entity.res.PageResult;
import com.unisinsight.project.entity.res.StoragePoolRes;
import com.unisinsight.project.exception.Result; import com.unisinsight.project.exception.Result;
import com.unisinsight.project.service.NetworkManageService; import com.unisinsight.project.service.NetworkManageService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
@ -30,17 +27,19 @@ public class NetworkManageController {
@Resource @Resource
private NetworkManageService networkManageService; private NetworkManageService networkManageService;
@ApiOperation(value = "分页查询镜像") @ApiOperation(value = "分页查询")
@PostMapping("/select/page") @PostMapping("/select/page")
public Result<PageResult<NetworkManage>> selectPage(@RequestBody NetworkManagePageReq networkManagePageReq) { public Result<PageResult<NetworkManage>> selectPage(@RequestBody NetworkManagePageReq networkManagePageReq) {
return Result.successResult(networkManageService.pageNetworkManages(networkManagePageReq)); return Result.successResult(networkManageService.pageNetworkManages(networkManagePageReq));
} }
@ApiOperation(value = "新增") @ApiOperation(value = "新增")
@PostMapping("/add") @PostMapping("/add")
public Result<?> addNetworkManage(@Valid @RequestBody NetworkManageReq networkManageReq) { public Result<?> addNetworkManage(@Valid @RequestBody NetworkManageReq networkManageReq) {
networkManageService.addNetworkManage(networkManageReq); networkManageService.addNetworkManage(networkManageReq);
return Result.successResult(); return Result.successResult();
} }
@ApiOperation(value = "修改状态") @ApiOperation(value = "修改状态")
@PostMapping("/update") @PostMapping("/update")
public Result<?> updateNetworkManage(@RequestBody NetworkManageReq networkManageReq) { public Result<?> updateNetworkManage(@RequestBody NetworkManageReq networkManageReq) {
@ -57,11 +56,11 @@ public class NetworkManageController {
return Result.successResult(); return Result.successResult();
} }
@ApiOperation(value = "同步状态") @ApiOperation(value = "同步数据和状态")
@GetMapping("/synchStatus") @GetMapping("/synchData")
public Result<?> synchStatus() { public Result<?> synchData() {
networkManageService.synchData();
return null; return Result.successResult();
} }
} }

View File

@ -1,23 +1,19 @@
package com.unisinsight.project.controller; package com.unisinsight.project.controller;
import cn.hutool.json.JSONUtil; import com.unisinsight.project.entity.dao.StoragePool;
import com.unisinsight.project.entity.req.DeleteIdReq;
import com.unisinsight.project.entity.req.ImageVirtualMachinesReq;
import com.unisinsight.project.entity.req.StoragePoolReq; import com.unisinsight.project.entity.req.StoragePoolReq;
import com.unisinsight.project.entity.res.ImageVirtualMachinesRes;
import com.unisinsight.project.entity.res.PageResult; import com.unisinsight.project.entity.res.PageResult;
import com.unisinsight.project.entity.res.StoragePoolRes;
import com.unisinsight.project.exception.BaseErrorCode; import com.unisinsight.project.exception.BaseErrorCode;
import com.unisinsight.project.exception.Result; import com.unisinsight.project.exception.Result;
import com.unisinsight.project.service.ImageVirtualMachinesService; import com.unisinsight.project.service.StoragePoolService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Objects; import javax.validation.Valid;
/** /**
* *
@ -28,34 +24,48 @@ import java.util.Objects;
@Api(tags = "存储池类") @Api(tags = "存储池类")
public class StoragePoolController { public class StoragePoolController {
@ApiOperation(value = "分页查询镜像") @Resource
private StoragePoolService storagePoolService;
@ApiOperation(value = "分页查询")
@PostMapping("/select/page") @PostMapping("/select/page")
public Result<PageResult<StoragePoolRes>> selectPage(@RequestBody StoragePoolReq storagePoolReq) { public Result<PageResult<StoragePool>> selectPage(@RequestBody StoragePoolReq storagePoolReq) {
return null; return Result.successResult(storagePoolService.pageStoragePools(storagePoolReq));
} }
@ApiOperation(value = "新增") @ApiOperation(value = "新增")
@PostMapping("/add") @PostMapping("/add")
public Result<?> insertImageVirtualMachines(@RequestBody StoragePoolReq storagePoolReq) { public Result<?> insertStoragePool(@Valid @RequestBody StoragePoolReq storagePoolReq) {
return null; storagePoolService.addStoragePool(storagePoolReq);
return Result.successResult();
} }
@ApiOperation(value = "修改状态")
@ApiOperation(value = "修改")
@PostMapping("/update") @PostMapping("/update")
public Result<?> updateUser(@RequestBody StoragePoolReq storagePoolReq) { public Result<?> updateStoragePool(@RequestBody StoragePoolReq storagePoolReq) {
return null; if (storagePoolService.updateStoragePool(storagePoolReq)) {
return Result.successResult();
} else {
return Result.errorResult(BaseErrorCode.DATABASE_OPERATION_FAILED);
}
} }
@ApiOperation(value = "删除") @ApiOperation(value = "删除")
@PostMapping("/delete") @DeleteMapping("/{id}")
public Result<?> delete(@RequestBody DeleteIdReq deleteIdReq) { public Result<?> delete(@PathVariable Long id) {
if (storagePoolService.deleteStoragePool(id)) {
return null; return Result.successResult();
} else {
return Result.errorResult(BaseErrorCode.DATABASE_OPERATION_FAILED);
} }
@ApiOperation(value = "同步状态") }
@GetMapping("/synchStatus")
public Result<?> synchStatus() {
@ApiOperation(value = "同步数据")
@GetMapping("/synchData")
public Result<?> synchData() {
storagePoolService.synchData();
return null; return null;
} }

View File

@ -23,8 +23,8 @@ public class NetworkManage {
/** /**
* *
*/ */
@TableField("name") @TableField("network_name")
private String name; private String networkName;
/** /**
* *

View File

@ -31,9 +31,9 @@ public class StoragePool {
/** /**
* *
*/ */
@TableField("name") @TableField("pool_name")
@ApiModelProperty(value = "存储池名称") @ApiModelProperty(value = "存储池名称")
private String name; private String poolName;
/** /**
* *
@ -47,7 +47,7 @@ public class StoragePool {
*/ */
@TableField("status") @TableField("status")
@ApiModelProperty(value = "状态") @ApiModelProperty(value = "状态")
private Short status; private Integer status;
/** /**
* *

View File

@ -0,0 +1,91 @@
package com.unisinsight.project.entity.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/**
* API
*/
@Data
@ApiModel(description = "API响应结果")
public class ApiResponse<T> implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "响应码")
private String code;
@ApiModelProperty(value = "响应消息")
private String message;
@ApiModelProperty(value = "响应数据")
private T data;
public ApiResponse() {}
public ApiResponse(String code, String message) {
this.code = code;
this.message = message;
}
public ApiResponse(String code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
/**
*
*/
public static <T> ApiResponse<T> success() {
return new ApiResponse<>("200", "操作成功");
}
public static <T> ApiResponse<T> success(T data) {
return new ApiResponse<>("200", "操作成功", data);
}
public static <T> ApiResponse<T> success(String message, T data) {
return new ApiResponse<>("200", message, data);
}
/**
*
*/
public static <T> ApiResponse<T> error(String message) {
return new ApiResponse<>("500", message);
}
public static <T> ApiResponse<T> error(String code, String message) {
return new ApiResponse<>(code, message);
}
public static <T> ApiResponse<T> error(String code, String message, T data) {
return new ApiResponse<>(code, message, data);
}
/**
*
*/
public static <T> ApiResponse<T> badRequest(String message) {
return new ApiResponse<>("400", message);
}
/**
*
*/
public static <T> ApiResponse<T> forbidden(String message) {
return new ApiResponse<>("403", message);
}
/**
*
*/
public static <T> ApiResponse<T> notFound(String message) {
return new ApiResponse<>("404", message);
}
}

View File

@ -0,0 +1,150 @@
// Network.java - 网络信息对象
package com.unisinsight.project.entity.dto;
public class Network {
private String name;
private String uuid;
private String state;
private Integer autostart;
private Integer persistent;
private String description;
private String type;
private String bridge;
private String bridgeName;
private String ipAddress;
private String netmask;
private String gateway;
private String ipRange;
private Boolean dhcpEnabled;
private String dhcpStart;
private String dhcpEnd;
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Integer getAutostart() {
return autostart;
}
public void setAutostart(Integer autostart) {
this.autostart = autostart;
}
public Integer getPersistent() {
return persistent;
}
public void setPersistent(Integer persistent) {
this.persistent = persistent;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getBridge() {
return bridge;
}
public void setBridge(String bridge) {
this.bridge = bridge;
}
public String getBridgeName() {
return bridgeName;
}
public void setBridgeName(String bridgeName) {
this.bridgeName = bridgeName;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public String getNetmask() {
return netmask;
}
public void setNetmask(String netmask) {
this.netmask = netmask;
}
public String getGateway() {
return gateway;
}
public void setGateway(String gateway) {
this.gateway = gateway;
}
public String getIpRange() {
return ipRange;
}
public void setIpRange(String ipRange) {
this.ipRange = ipRange;
}
public Boolean getDhcpEnabled() {
return dhcpEnabled;
}
public void setDhcpEnabled(Boolean dhcpEnabled) {
this.dhcpEnabled = dhcpEnabled;
}
public String getDhcpStart() {
return dhcpStart;
}
public void setDhcpStart(String dhcpStart) {
this.dhcpStart = dhcpStart;
}
public String getDhcpEnd() {
return dhcpEnd;
}
public void setDhcpEnd(String dhcpEnd) {
this.dhcpEnd = dhcpEnd;
}
}

View File

@ -0,0 +1,24 @@
package com.unisinsight.project.entity.dto;
import java.util.List;
public class NetworkData {
private List<Network> networks;
private Pagination pagination;
// Getters and Setters
public List<Network> getNetworks() {
return networks;
}
public void setNetworks(List<Network> networks) {
this.networks = networks;
}
public Pagination getPagination() {
return pagination;
}
public void setPagination(Pagination pagination) {
this.pagination = pagination;
}
}

View File

@ -0,0 +1,42 @@
// Pagination.java - 分页信息对象
package com.unisinsight.project.entity.dto;
public class Pagination {
private Integer currentPage;
private Integer pageSize;
private Integer total;
private Integer totalPages;
// Getters and Setters
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public Integer getTotalPages() {
return totalPages;
}
public void setTotalPages(Integer totalPages) {
this.totalPages = totalPages;
}
}

View File

@ -0,0 +1,52 @@
package com.unisinsight.project.entity.dto;
import java.util.List;
public class StoragePoolData {
private List<VmStoragePool> items;
private Integer total;
private Integer page;
private Integer pageSize;
private Integer totalPages;
// Getters and Setters
public List<VmStoragePool> getItems() {
return items;
}
public void setItems(List<VmStoragePool> items) {
this.items = items;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalPages() {
return totalPages;
}
public void setTotalPages(Integer totalPages) {
this.totalPages = totalPages;
}
}

View File

@ -0,0 +1,105 @@
// StoragePool.java - 存储池信息对象
package com.unisinsight.project.entity.dto;
public class VmStoragePool {
private String name;
private String uuid;
private String state;
private Long capacity;
private Long allocation;
private Long available;
private Integer autostart;
private Integer persistent;
private Integer volumeCount;
private String type;
private String path;
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Long getCapacity() {
return capacity;
}
public void setCapacity(Long capacity) {
this.capacity = capacity;
}
public Long getAllocation() {
return allocation;
}
public void setAllocation(Long allocation) {
this.allocation = allocation;
}
public Long getAvailable() {
return available;
}
public void setAvailable(Long available) {
this.available = available;
}
public Integer getAutostart() {
return autostart;
}
public void setAutostart(Integer autostart) {
this.autostart = autostart;
}
public Integer getPersistent() {
return persistent;
}
public void setPersistent(Integer persistent) {
this.persistent = persistent;
}
public Integer getVolumeCount() {
return volumeCount;
}
public void setVolumeCount(Integer volumeCount) {
this.volumeCount = volumeCount;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}

View File

@ -30,5 +30,10 @@ public class LoginReq {
@JsonProperty("token") @JsonProperty("token")
private String token; private String token;
@ApiModelProperty("mac地址")
private String macAddr;
} }

View File

@ -17,7 +17,7 @@ public class NetworkManagePageReq {
*/ */
@Size(max = 64, message = "网络名称长度不能超过64个字符") @Size(max = 64, message = "网络名称长度不能超过64个字符")
@ApiModelProperty(value = "网络名称", required = true) @ApiModelProperty(value = "网络名称", required = true)
private String name; private String networkName;
/** /**
* *

View File

@ -22,7 +22,7 @@ public class NetworkManageReq {
@NotBlank(message = "网络名称不能为空") @NotBlank(message = "网络名称不能为空")
@Size(max = 64, message = "网络名称长度不能超过64个字符") @Size(max = 64, message = "网络名称长度不能超过64个字符")
@ApiModelProperty(value = "网络名称", required = true) @ApiModelProperty(value = "网络名称", required = true)
private String name; private String networkName;
/** /**
* *

View File

@ -1,14 +1,14 @@
package com.unisinsight.project.entity.req; package com.unisinsight.project.entity.req;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.unisinsight.project.entity.dao.StoragePool;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.*;
/** /**
* *
* *
* @author ch * @author ch
* @since 2025-08-25 11:42:52 * @since 2025-08-25 11:42:52
@ -17,34 +17,45 @@ import lombok.Data;
@ApiModel("存储池") @ApiModel("存储池")
public class StoragePoolReq { public class StoragePoolReq {
@ApiModelProperty(value = "id")
private Integer id;
@ApiModelProperty("自动启动,默认是")
private boolean autostart = true;
@ApiModelProperty("名称") @ApiModelProperty("名称")
private String name; @NotBlank(message = "名称不能为空")
@Size(max = 64, message = "名称长度不能超过64个字符")
private String poolName;
@ApiModelProperty("类型") @ApiModelProperty("类型")
@Size(max = 16, message = "类型长度不能超过16个字符")
private String type; private String type;
@ApiModelProperty("路劲") @ApiModelProperty("路径")
@Size(max = 128, message = "路径长度不能超过128个字符")
private String path; private String path;
/** /**
* : 1-2- * : 1-2-
**/ **/
@ApiModelProperty("状态") @ApiModelProperty("状态")
private String status; private Integer status;
/** /**
* *
*/ */
@ApiModelProperty(value = "查询页", notes = "分页查询时再传") @ApiModelProperty(value = "查询页", notes = "分页查询时再传")
@JsonProperty("page_num") @JsonProperty("page_num")
private Integer pageNum; @Min(value = 1, message = "页码必须大于0")
private Integer pageNum = 1;
/** /**
* *
*/ */
@ApiModelProperty(value = "每页数量", notes = "分页查询时再传") @ApiModelProperty(value = "每页数量", notes = "分页查询时再传")
@JsonProperty("page_size") @JsonProperty("page_size")
private Integer pageSize; @Min(value = 1, message = "每页数量必须大于0")
@Max(value = 100, message = "每页数量不能超过100")
private Integer pageSize = 10;
} }

View File

@ -0,0 +1,57 @@
package com.unisinsight.project.feign;
import com.unisinsight.project.config.FeignConfig;
import com.unisinsight.project.entity.dao.NetworkManage;
import com.unisinsight.project.entity.dto.ApiResponse;
import com.unisinsight.project.entity.dto.NetworkData;
import com.unisinsight.project.entity.dto.StoragePoolData;
import com.unisinsight.project.entity.req.NetworkManageReq;
import com.unisinsight.project.entity.req.StoragePoolReq;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* API
*/
@FeignClient(
name = "external-api-client",
url = "${external.api.url:http://10.100.51.118:8000}",
configuration = FeignConfig.class
)
public interface ExternalApiClient {
@PostMapping("/api/v1/network/create")
ApiResponse createNetwork(@RequestBody NetworkManageReq networkManageReq);
@DeleteMapping("/api/v1/network/{network_name}")
ApiResponse deleteNetwork(@PathVariable("network_name") String networkName);
@PostMapping("/api/v1/network/update")
ApiResponse updateNetwork(@RequestBody NetworkManageReq networkManageReq);
@PostMapping("/api/v1/network/start")
ApiResponse startNetwork(@RequestBody NetworkManageReq networkManageReq);
@PostMapping("/api/v1/network/stop")
ApiResponse stopNetwork(@RequestBody NetworkManageReq networkManageReq);
@PostMapping("/api/v1/storage/pools")
ApiResponse createStorage(@RequestBody StoragePoolReq storagePoolReq);
@PostMapping("/api/v1/storage/pools/start")
ApiResponse startStorage(@RequestBody StoragePoolReq storagePoolReq);
@PostMapping("/api/v1/storage/pools/stop")
ApiResponse stopStorage(@RequestBody StoragePoolReq storagePoolReq);
@DeleteMapping("/api/v1/storage/pools/{pool_name}")
ApiResponse deleteStorage(@PathVariable("pool_name") String poolName);
@GetMapping("/api/v1/network/list")
ApiResponse<NetworkData> list(@RequestParam("page")int page, @RequestParam("page_size")int pageSize);
@GetMapping("/api/v1/storage/pools")
ApiResponse<StoragePoolData> listStorage(@RequestParam("page")int page, @RequestParam("page_size")int pageSize);
}

View File

@ -61,4 +61,6 @@ public interface NetworkManageService {
* @return Page<NetworkManage> * @return Page<NetworkManage>
*/ */
PageResult<NetworkManage> pageNetworkManages(NetworkManagePageReq networkManagePageReq); PageResult<NetworkManage> pageNetworkManages(NetworkManagePageReq networkManagePageReq);
void synchData();
} }

View File

@ -3,6 +3,8 @@ package com.unisinsight.project.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unisinsight.project.entity.dao.StoragePool; import com.unisinsight.project.entity.dao.StoragePool;
import com.unisinsight.project.entity.req.StoragePoolReq; import com.unisinsight.project.entity.req.StoragePoolReq;
import com.unisinsight.project.entity.res.PageResult;
import com.unisinsight.project.entity.res.StoragePoolRes;
/** /**
* *
@ -12,18 +14,18 @@ public interface StoragePoolService {
/** /**
* *
* *
* @param storagePool * @param storagePoolReq
* @return StoragePool * @return StoragePool
*/ */
StoragePool addStoragePool(StoragePool storagePool); StoragePool addStoragePool(StoragePoolReq storagePoolReq);
/** /**
* ID * ID
* *
* @param storagePool * @param storagePoolReq
* @return boolean * @return boolean
*/ */
boolean updateStoragePool(StoragePool storagePool); boolean updateStoragePool(StoragePoolReq storagePoolReq);
/** /**
* ID * ID
@ -44,12 +46,10 @@ public interface StoragePoolService {
/** /**
* *
* *
* @param currentPage * @param storagePoolReq
* @param pageSize
* @param name
* @param type
* @param status
* @return Page<StoragePool> * @return Page<StoragePool>
*/ */
Page<StoragePool> pageStoragePools(StoragePoolReq storagePoolReq); PageResult<StoragePool> pageStoragePools(StoragePoolReq storagePoolReq);
void synchData();
} }

View File

@ -1,20 +1,25 @@
package com.unisinsight.project.service.impl; package com.unisinsight.project.service.impl;
import cn.hutool.core.exceptions.CheckedUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unisinsight.project.entity.dao.NetworkManage; import com.unisinsight.project.entity.dao.NetworkManage;
import com.unisinsight.project.entity.dto.ApiResponse;
import com.unisinsight.project.entity.dto.Network;
import com.unisinsight.project.entity.dto.NetworkData;
import com.unisinsight.project.entity.req.NetworkManagePageReq; import com.unisinsight.project.entity.req.NetworkManagePageReq;
import com.unisinsight.project.entity.req.NetworkManageReq; import com.unisinsight.project.entity.req.NetworkManageReq;
import com.unisinsight.project.entity.res.PageResult; import com.unisinsight.project.entity.res.PageResult;
import com.unisinsight.project.exception.BusinessException; import com.unisinsight.project.exception.BusinessException;
import com.unisinsight.project.feign.ExternalApiClient;
import com.unisinsight.project.mapper.NetworkManageMapper; import com.unisinsight.project.mapper.NetworkManageMapper;
import com.unisinsight.project.service.NetworkManageService; import com.unisinsight.project.service.NetworkManageService;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -24,6 +29,9 @@ import java.util.List;
@Service @Service
public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, NetworkManage> implements NetworkManageService { public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, NetworkManage> implements NetworkManageService {
@Resource
private ExternalApiClient client;
/** /**
* *
* *
@ -34,9 +42,19 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
public NetworkManage addNetworkManage(NetworkManageReq networkManageReq) { public NetworkManage addNetworkManage(NetworkManageReq networkManageReq) {
// 检查名称是否重复 // 检查名称是否重复
QueryWrapper<NetworkManage> queryWrapper = new QueryWrapper<>(); QueryWrapper<NetworkManage> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", networkManageReq.getName()); queryWrapper.eq("name", networkManageReq.getNetworkName());
if (this.count(queryWrapper) > 0) { if (this.count(queryWrapper) > 0) {
throw new BusinessException("网络名称'" + networkManageReq.getName() + "'已存在,请使用其他名称"); throw new BusinessException("网络名称'" + networkManageReq.getNetworkName() + "'已存在,请使用其他名称");
}
// 调用虚拟机创建存储池
try {
ApiResponse response = client.createNetwork(networkManageReq);
if (!"200".equals(response.getCode())) {
throw new BusinessException("调用外部接口失败: " + response.getMessage());
}
} catch (Exception e) {
throw new BusinessException("调用外部接口失败: " + e.getMessage());
} }
// 设置创建时间和更新时间 // 设置创建时间和更新时间
NetworkManage networkManage = new NetworkManage(); NetworkManage networkManage = new NetworkManage();
@ -66,18 +84,43 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
// 修改名称 // 修改名称
existing.setUpdateTime(new Date()); existing.setUpdateTime(new Date());
// 如果名称不为空且与原名称不同,则进行重名校验 // 如果名称不为空且与原名称不同,则进行重名校验
if (networkManageReq.getName() != null && !networkManageReq.getName().isEmpty() if (networkManageReq.getNetworkName() != null && !networkManageReq.getNetworkName().isEmpty()
&& !networkManageReq.getName().equals(existing.getName())) { && !networkManageReq.getNetworkName().equals(existing.getNetworkName())) {
// 检查新名称是否重复 // 检查新名称是否重复
QueryWrapper<NetworkManage> queryWrapper = new QueryWrapper<>(); QueryWrapper<NetworkManage> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", networkManageReq.getName()); queryWrapper.eq("name", networkManageReq.getNetworkName());
if (this.count(queryWrapper) > 0) { if (this.count(queryWrapper) > 0) {
throw new BusinessException("网络名称'" + networkManageReq.getName() + "'已存在,请使用其他名称"); throw new BusinessException("网络名称'" + networkManageReq.getNetworkName() + "'已存在,请使用其他名称");
} }
existing.setName(networkManageReq.getName()); existing.setNetworkName(networkManageReq.getNetworkName());
} }
// 调用虚拟机创建存储池
try {
ApiResponse response = client.updateNetwork(networkManageReq);
if (!"200".equals(response.getCode())) {
throw new BusinessException("调用外部接口失败: " + response.getMessage());
}
} catch (Exception e) {
throw new BusinessException("调用外部接口失败: " + e.getMessage());
}
// 修改状态 // 修改状态
if(networkManageReq.getStatus() != null ){ if (networkManageReq.getStatus() != null) {
try {
networkManageReq.setNetworkName(existing.getNetworkName());
ApiResponse response;
if (1 == networkManageReq.getStatus()) {
response = client.startNetwork(networkManageReq);
} else {
response = client.stopNetwork(networkManageReq);
}
if (!"200".equals(response.getCode())) {
throw new BusinessException("调用外部接口失败: " + response.getMessage());
}
} catch (Exception e) {
throw new BusinessException("调用外部接口失败: " + e.getMessage());
}
existing.setStatus(networkManageReq.getStatus()); existing.setStatus(networkManageReq.getStatus());
} }
return this.updateById(existing); return this.updateById(existing);
@ -90,8 +133,19 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
* @return boolean * @return boolean
*/ */
@Override @Override
@Transactional(rollbackFor = Exception.class)
public boolean deleteNetworkManage(Integer id) { public boolean deleteNetworkManage(Integer id) {
NetworkManage existing = this.getById(id);
if (existing == null) {
throw new BusinessException("网络管理信息不存在");
}
try {
ApiResponse response = client.deleteNetwork(existing.getNetworkName());
if (!"200".equals(response.getCode())) {
throw new BusinessException("调用外部接口失败: " + response.getMessage());
}
} catch (Exception e) {
throw new BusinessException("调用外部接口失败: " + e.getMessage());
}
return this.removeById(id); return this.removeById(id);
} }
@ -145,8 +199,8 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
QueryWrapper<NetworkManage> queryWrapper = new QueryWrapper<>(); QueryWrapper<NetworkManage> queryWrapper = new QueryWrapper<>();
// 添加查询条件 // 添加查询条件
if (pageReq.getName() != null && !pageReq.getName().isEmpty()) { if (pageReq.getNetworkName() != null && !pageReq.getNetworkName().isEmpty()) {
queryWrapper.like("name", pageReq.getName()); queryWrapper.like("network_name", pageReq.getNetworkName());
} }
if (pageReq.getType() != null && !pageReq.getType().isEmpty()) { if (pageReq.getType() != null && !pageReq.getType().isEmpty()) {
queryWrapper.eq("type", pageReq.getType()); queryWrapper.eq("type", pageReq.getType());
@ -160,5 +214,41 @@ public class NetworkManageServiceImpl extends ServiceImpl<NetworkManageMapper, N
// 转换为 PageResult // 转换为 PageResult
return PageResult.convertPage(resultPage, NetworkManage.class); return PageResult.convertPage(resultPage, NetworkManage.class);
}
/**
*
*/
@Override
public void synchData() {
ApiResponse<NetworkData> response;
try {
response = client.list(1, 100);
if (!"200".equals(response.getCode())) {
throw new BusinessException("调用外部接口失败: " + response.getMessage());
}
} catch (Exception e) {
throw new BusinessException("调用外部接口失败: " + e.getMessage());
}
List<Network> networkList = response.getData().getNetworks();
if (CollectionUtils.isEmpty(networkList)) {
return;
}
List<NetworkManage> networkManageList = new ArrayList<>();
for (Network network : networkList) {
NetworkManage networkManage = new NetworkManage();
BeanUtils.copyProperties(network, networkManage);
networkManage.setNetworkName(network.getName());
networkManage.setStatus("active".equals(network.getState()) ? 1 : 2);
networkManageList.add(networkManage);
}
// 清空数据并重新插入
this.remove(new QueryWrapper<>());
this.saveOrUpdateBatch(networkManageList);
} }
} }

View File

@ -3,14 +3,24 @@ package com.unisinsight.project.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unisinsight.project.entity.dao.NetworkManage;
import com.unisinsight.project.entity.dao.StoragePool; import com.unisinsight.project.entity.dao.StoragePool;
import com.unisinsight.project.entity.dto.*;
import com.unisinsight.project.entity.req.StoragePoolReq; import com.unisinsight.project.entity.req.StoragePoolReq;
import com.unisinsight.project.entity.res.PageResult;
import com.unisinsight.project.exception.BusinessException;
import com.unisinsight.project.feign.ExternalApiClient;
import com.unisinsight.project.mapper.StoragePoolMapper; import com.unisinsight.project.mapper.StoragePoolMapper;
import com.unisinsight.project.service.StoragePoolService; import com.unisinsight.project.service.StoragePoolService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* *
@ -18,17 +28,40 @@ import java.util.Date;
@Service @Service
public class StoragePoolServiceImpl extends ServiceImpl<StoragePoolMapper, StoragePool> implements StoragePoolService { public class StoragePoolServiceImpl extends ServiceImpl<StoragePoolMapper, StoragePool> implements StoragePoolService {
@Resource
private ExternalApiClient client;
/** /**
* *
* *
* @param storagePool * @param storagePoolReq
* @return StoragePool * @return StoragePool
*/ */
@Override @Override
@Transactional(rollbackFor = Exception.class) public StoragePool addStoragePool(StoragePoolReq storagePoolReq) {
public StoragePool addStoragePool(StoragePool storagePool) { // 检查名称是否重复
QueryWrapper<StoragePool> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("name", storagePoolReq.getPoolName());
if (this.count(queryWrapper) > 0) {
throw new BusinessException("存储池名称'" + storagePoolReq.getPoolName() + "'已存在,请使用其他名称");
}
// 调用虚拟机创建存储池
try {
ApiResponse response = client.createStorage(storagePoolReq);
if (!"200".equals(response.getCode())) {
throw new BusinessException("调用外部接口失败: " + response.getMessage());
}
} catch (Exception e) {
throw new BusinessException("调用外部接口失败: " + e.getMessage());
}
// 本地保存
StoragePool storagePool = new StoragePool();
BeanUtils.copyProperties(storagePoolReq, storagePool);
storagePool.setCreateTime(new Date()); storagePool.setCreateTime(new Date());
storagePool.setUpdateTime(new Date()); storagePool.setUpdateTime(new Date());
storagePool.setStatus(1);
this.save(storagePool); this.save(storagePool);
return storagePool; return storagePool;
} }
@ -36,18 +69,51 @@ public class StoragePoolServiceImpl extends ServiceImpl<StoragePoolMapper, Stora
/** /**
* ID * ID
* *
* @param storagePool * @param storagePoolReq
* @return boolean * @return boolean
*/ */
@Override @Override
@Transactional(rollbackFor = Exception.class) public boolean updateStoragePool(StoragePoolReq storagePoolReq) {
public boolean updateStoragePool(StoragePool storagePool) { StoragePool existing = this.getById(storagePoolReq.getId());
StoragePool existing = this.getById(storagePool.getId());
if (existing == null) { if (existing == null) {
return false; return false;
} }
storagePool.setUpdateTime(new Date());
return this.updateById(storagePool); // 不能修改名称
// 如果名称不为空且与原名称不同,则进行重名校验
// if (storagePoolReq.getName() != null && !storagePoolReq.getName().isEmpty()
// && !storagePoolReq.getName().equals(existing.getName())) {
// // 检查新名称是否重复
// QueryWrapper<StoragePool> queryWrapper = new QueryWrapper<>();
// queryWrapper.eq("name", storagePoolReq.getName());
// if (this.count(queryWrapper) > 0) {
// throw new BusinessException("存储池名称'" + storagePoolReq.getName() + "'已存在,请使用其他名称");
// }
// existing.setName(storagePoolReq.getName());
// }
// 调用虚拟机创建存储池
// 修改状态
if (storagePoolReq.getStatus() != null) {
try {
storagePoolReq.setPoolName(existing.getPoolName());
ApiResponse response;
if (1 == storagePoolReq.getStatus()) {
response = client.startStorage(storagePoolReq);
} else {
response = client.stopStorage(storagePoolReq);
}
if (!"200".equals(response.getCode())) {
throw new BusinessException("调用外部接口失败: " + response.getMessage());
}
} catch (Exception e) {
throw new BusinessException("调用外部接口失败: " + e.getMessage());
}
existing.setStatus(storagePoolReq.getStatus());
}
existing.setUpdateTime(new Date());
return this.updateById(existing);
} }
/** /**
@ -59,6 +125,19 @@ public class StoragePoolServiceImpl extends ServiceImpl<StoragePoolMapper, Stora
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public boolean deleteStoragePool(Long id) { public boolean deleteStoragePool(Long id) {
StoragePool existing = this.getById(id);
if (existing == null) {
return false;
}
try {
ApiResponse response = client.deleteStorage(existing.getPoolName());
if (!"200".equals(response.getCode())) {
throw new BusinessException("调用外部接口失败: " + response.getMessage());
}
} catch (Exception e) {
throw new BusinessException("调用外部接口失败: " + e.getMessage());
}
return this.removeById(id); return this.removeById(id);
} }
@ -76,21 +155,17 @@ public class StoragePoolServiceImpl extends ServiceImpl<StoragePoolMapper, Stora
/** /**
* *
* *
* @param currentPage * @param storagePoolReq
* @param pageSize
* @param name
* @param type
* @param status
* @return Page<StoragePool> * @return Page<StoragePool>
*/ */
@Override @Override
public Page<StoragePool> pageStoragePools(StoragePoolReq storagePoolReq) { public PageResult<StoragePool> pageStoragePools(StoragePoolReq storagePoolReq) {
Page<StoragePool> page = new Page<>(storagePoolReq.getPageNum(), storagePoolReq.getPageSize()); Page<StoragePool> page = new Page<>(storagePoolReq.getPageNum(), storagePoolReq.getPageSize());
QueryWrapper<StoragePool> queryWrapper = new QueryWrapper<>(); QueryWrapper<StoragePool> queryWrapper = new QueryWrapper<>();
// 添加查询条件 // 添加查询条件
if (storagePoolReq.getName() != null && !storagePoolReq.getName().isEmpty()) { if (storagePoolReq.getPoolName() != null && !storagePoolReq.getPoolName().isEmpty()) {
queryWrapper.like("name", storagePoolReq.getName()); queryWrapper.like("pool_name", storagePoolReq.getPoolName());
} }
if (storagePoolReq.getType() != null && !storagePoolReq.getType().isEmpty()) { if (storagePoolReq.getType() != null && !storagePoolReq.getType().isEmpty()) {
queryWrapper.eq("type", storagePoolReq.getType()); queryWrapper.eq("type", storagePoolReq.getType());
@ -100,6 +175,39 @@ public class StoragePoolServiceImpl extends ServiceImpl<StoragePoolMapper, Stora
} }
queryWrapper.orderByDesc("create_time"); queryWrapper.orderByDesc("create_time");
return this.page(page, queryWrapper);
Page<StoragePool> resultPage = this.page(page, queryWrapper);
return PageResult.convertPage(resultPage, StoragePool.class);
}
@Override
public void synchData() {
ApiResponse<StoragePoolData> response;
try {
response = client.listStorage(1, 100);
if (!"200".equals(response.getCode())) {
throw new BusinessException("调用外部接口失败: " + response.getMessage());
}
} catch (Exception e) {
throw new BusinessException("调用外部接口失败: " + e.getMessage());
}
List<VmStoragePool> vmStoragePoolList = response.getData().getItems();
if (CollectionUtils.isEmpty(vmStoragePoolList)) {
return;
}
List<StoragePool> networkManageList = new ArrayList<>();
for (VmStoragePool vmStoragePool : vmStoragePoolList) {
StoragePool storagePool = new StoragePool();
BeanUtils.copyProperties(vmStoragePool, storagePool);
storagePool.setPoolName(vmStoragePool.getName());
storagePool.setStatus("active".equals(vmStoragePool.getState()) ? 1 : 2);
networkManageList.add(storagePool);
}
// 清空数据并重新插入
this.remove(new QueryWrapper<>());
this.saveOrUpdateBatch(networkManageList);
} }
} }

View File

@ -56,3 +56,18 @@ grpc:
server: server:
# 指定Grpc暴露的端口后续客户端通过这个端口访问 # 指定Grpc暴露的端口后续客户端通过这个端口访问
port: 51051 port: 51051
# Feign 配置
feign:
client:
config:
external-api-client: # 对应 FeignClient 的 name
logger-level: full # 完整日志级别
default: # 全局默认配置
logger-level: basic
# 日志配置
logging:
level:
com.unisinsight.project.feign: debug # Feign 客户端包路径
com.unisinsight.project.feign.ExternalApiClient: debug