diff options
| author | tanghao <admin@LAPTOP-QCSKVLI9> | 2021-03-10 17:42:30 +0800 |
|---|---|---|
| committer | tanghao <admin@LAPTOP-QCSKVLI9> | 2021-03-10 17:42:30 +0800 |
| commit | 39cfa80568463be8a467c7157ea4f5cb7e095fb5 (patch) | |
| tree | 8d033eb4985d4e949a8e4808aaec78d6cdf1f4bc | |
| parent | 6e8e5c3145a5a8b19276c1f0da951b085e9de379 (diff) | |
| parent | 8936afc8fe8c87cf9e723a32cf601512324d9158 (diff) | |
Merge branch 'develop' of https://git.mesalab.cn/nezha/nz-web.git into develop
10 files changed, 380 insertions, 8 deletions
diff --git a/nz-admin/src/main/java/com/nis/modules/asset/controller/AssetController.java b/nz-admin/src/main/java/com/nis/modules/asset/controller/AssetController.java index 2ff7e916..ebd8d346 100644 --- a/nz-admin/src/main/java/com/nis/modules/asset/controller/AssetController.java +++ b/nz-admin/src/main/java/com/nis/modules/asset/controller/AssetController.java @@ -8,8 +8,10 @@ import com.nis.common.smartvalidate.ValidateUtils; import com.nis.common.utils.*; import com.nis.modules.asset.entity.Asset; import com.nis.modules.asset.entity.AssetStateConf; +import com.nis.modules.asset.entity.AssetTypeConf; import com.nis.modules.asset.service.AssetService; import com.nis.modules.asset.service.AssetStateConfService; +import com.nis.modules.asset.service.AssetTypeConfService; import com.nis.modules.terminal.entity.Account; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; @@ -39,7 +41,10 @@ public class AssetController { private AssetService assetService; @Autowired - private AssetStateConfService confService; + private AssetStateConfService stateConfService; + + @Autowired + private AssetTypeConfService typeConfService; /** * 资产请求列表 @@ -292,14 +297,14 @@ public class AssetController { @GetMapping("/stateConf") @SysLog(operation = OperationEnum.QUERY, type = TypeEnum.ASSETSTATECONF) public R queryStateConfList(@RequestParam Map<String, Object> params) { - PageUtils page = confService.queryPage(params); + PageUtils page = stateConfService.queryPage(params); return R.ok(page); } @GetMapping("/stateConf/{id}") @SysLog(operation = OperationEnum.QUERY, type = TypeEnum.ASSETSTATECONF) public R queryStateConfInfo(@PathVariable("id") Integer id) { - AssetStateConf assetStateConf = confService.queryConfInfo(id); + AssetStateConf assetStateConf = stateConfService.queryConfInfo(id); return R.ok(assetStateConf); } @@ -311,7 +316,7 @@ public class AssetController { .and(conf.getAlert()).notNull(RCode.STATECONF_ALERT_ISNULL) .and(conf.getMonitor()).notNull(RCode.STATECONF_MONITOR_ISNULL); - confService.saveConf(conf); + stateConfService.saveConf(conf); return R.ok(); } @@ -324,7 +329,7 @@ public class AssetController { .and(conf.getAlert()).notNull(RCode.STATECONF_ALERT_ISNULL) .and(conf.getMonitor()).notNull(RCode.STATECONF_MONITOR_ISNULL); - confService.updateConf(conf); + stateConfService.updateConf(conf); return R.ok(); } @@ -333,7 +338,58 @@ public class AssetController { public R deleteAssetStateConf(@RequestParam Integer... ids) { ValidateUtils.is(ids).notNull(RCode.STATECONF_ID_ISNULL); - confService.delete(ids); + stateConfService.delete(ids); + return R.ok(); + } + + + @GetMapping("/typeConf/{id}") + @SysLog(operation = OperationEnum.QUERY, type = TypeEnum.ASSETTYPECONF) + public R queryTypeConfInfo(@PathVariable("id") Integer id) { + AssetTypeConf assetTypeConf = typeConfService.queryConfInfo(id); + return R.ok(assetTypeConf); + } + + @GetMapping("/typeConf") + @SysLog(operation = OperationEnum.QUERY, type = TypeEnum.ASSETTYPECONF) + public R queryTypeConfList(@RequestParam Map<String, Object> params) { + PageUtils page = typeConfService.queryPage(params); + return R.ok(page); + } + + @PostMapping("/typeConf") + @SysLog(operation = OperationEnum.ADD, type = TypeEnum.ASSETTYPECONF) + public R saveAssetTypeConf(@RequestBody AssetTypeConf conf) { + ValidateUtils.is(conf.getName()).notNull(RCode.TYPECONF_NAME_ISNULL) + .and(conf.getVm()).notNull(RCode.TYPECONF_VM_ISNULL) + .and(conf.getVmh()).notNull(RCode.TYPECONF_VMH_ISNULL) + .and(conf.getSsh()).notNull(RCode.TYPECONF_SSH_ISNULL) + .and(conf.getTelnet()).notNull(RCode.TYPECONF_TELNET_ISNULL); + + typeConfService.saveConf(conf); + return R.ok(); + } + + @PutMapping("/typeConf") + @SysLog(operation = OperationEnum.UPDATE, type = TypeEnum.ASSETTYPECONF) + public R editAssetTypeConf(@RequestBody AssetTypeConf conf) { + ValidateUtils.is(conf.getId()).notNull(RCode.TYPECONF_ID_ISNULL) + .and(conf.getName()).notNull(RCode.TYPECONF_NAME_ISNULL) + .and(conf.getVm()).notNull(RCode.TYPECONF_VM_ISNULL) + .and(conf.getVmh()).notNull(RCode.TYPECONF_VMH_ISNULL) + .and(conf.getSsh()).notNull(RCode.TYPECONF_SSH_ISNULL) + .and(conf.getTelnet()).notNull(RCode.TYPECONF_TELNET_ISNULL); + + typeConfService.updateConf(conf); + return R.ok(); + } + + @DeleteMapping("/typeConf") + @SysLog(operation = OperationEnum.DELETE, type = TypeEnum.ASSETTYPECONF) + public R deleteAssetTypeConf(@RequestParam Integer... ids) { + ValidateUtils.is(ids).notNull(RCode.TYPECONF_ID_ISNULL); + + typeConfService.delete(ids); return R.ok(); } diff --git a/nz-admin/src/main/java/com/nis/modules/asset/dao/AssetStateConfDao.java b/nz-admin/src/main/java/com/nis/modules/asset/dao/AssetStateConfDao.java index 0f5626de..54967ca8 100644 --- a/nz-admin/src/main/java/com/nis/modules/asset/dao/AssetStateConfDao.java +++ b/nz-admin/src/main/java/com/nis/modules/asset/dao/AssetStateConfDao.java @@ -6,7 +6,7 @@ import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; /** - * AssetPing + * AssetStateConf * * @author shizhendong * @date 2021-03-08 diff --git a/nz-admin/src/main/java/com/nis/modules/asset/dao/AssetTypeConfDao.java b/nz-admin/src/main/java/com/nis/modules/asset/dao/AssetTypeConfDao.java new file mode 100644 index 00000000..18d9aeef --- /dev/null +++ b/nz-admin/src/main/java/com/nis/modules/asset/dao/AssetTypeConfDao.java @@ -0,0 +1,18 @@ +package com.nis.modules.asset.dao; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.nis.modules.asset.entity.AssetTypeConf; +import org.apache.ibatis.annotations.Mapper; +import org.springframework.stereotype.Repository; + +/** + * AssetTypeConfig + * + * @author shizhendong + * @date 2021-03-08 + */ +@Mapper +@Repository +public interface AssetTypeConfDao extends BaseMapper<AssetTypeConf> { + +} diff --git a/nz-admin/src/main/java/com/nis/modules/asset/entity/Asset.java b/nz-admin/src/main/java/com/nis/modules/asset/entity/Asset.java index 9c628dee..c7bbbe4a 100644 --- a/nz-admin/src/main/java/com/nis/modules/asset/entity/Asset.java +++ b/nz-admin/src/main/java/com/nis/modules/asset/entity/Asset.java @@ -207,4 +207,6 @@ public class Asset implements Serializable { private List<AssetTag> tags; private Integer stateId; + + private Integer typeId; } diff --git a/nz-admin/src/main/java/com/nis/modules/asset/entity/AssetTypeConf.java b/nz-admin/src/main/java/com/nis/modules/asset/entity/AssetTypeConf.java new file mode 100644 index 00000000..870d9ae9 --- /dev/null +++ b/nz-admin/src/main/java/com/nis/modules/asset/entity/AssetTypeConf.java @@ -0,0 +1,82 @@ +package com.nis.modules.asset.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; + +import java.io.Serializable; + + +/** + * 资产类型配置实体 + * + * @author shizhendong + * @date 2021-03-08 + */ +@Data +@TableName("asset_type_conf") +public class AssetTypeConf implements Serializable { + private static final long serialVersionUID = 1L; + + /** + * 主键 + */ + @TableId(type = IdType.AUTO) + private Integer id; + + /** + * 父id + */ + private Integer pid; + + /** + * 是否为虚拟机宿主 0:不是宿主机,不支持挂载 虚拟机 1:是宿主机,支持挂载虚拟机 + */ + private Integer vm; + + /** + * 是否为虚拟机宿主 0:不是宿主机,不支持挂载 虚拟机 1:是宿主机,支持挂载虚拟机 + */ + private Integer vmh; + + /** + * 名称 + */ + private String name; + + /** + * ssh服务 0:关闭 1:开启 + */ + private Integer ssh; + + /** + * telnet服务 0:关闭 1:开启 + */ + private Integer telnet; + + /** + * 描述 + */ + private String remark; + + /** + * 是否内置 1:内置,0:非内置,默认:0 内置数据不允许删除 + */ + private Integer buildIn; + + @Override + public String toString() { + return "AssetTypeConf{" + + "id=" + id + + ", pid=" + pid + + ", vm=" + vm + + ", vmh=" + vmh + + ", name='" + name + '\'' + + ", ssh=" + ssh + + ", telnet=" + telnet + + ", remark='" + remark + '\'' + + ", buildIn=" + buildIn + + '}'; + } +} diff --git a/nz-admin/src/main/java/com/nis/modules/asset/service/AssetTypeConfService.java b/nz-admin/src/main/java/com/nis/modules/asset/service/AssetTypeConfService.java new file mode 100644 index 00000000..eb5c5844 --- /dev/null +++ b/nz-admin/src/main/java/com/nis/modules/asset/service/AssetTypeConfService.java @@ -0,0 +1,26 @@ +package com.nis.modules.asset.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.nis.common.utils.PageUtils; +import com.nis.modules.asset.entity.AssetTypeConf; + +import java.util.Map; + +/** + * AssetTypeConf service + * + * @author shizhendong + * @date 2021-03-08 + */ +public interface AssetTypeConfService extends IService<AssetTypeConf> { + + AssetTypeConf queryConfInfo(Integer id); + + PageUtils queryPage(Map<String, Object> params); + + void saveConf(AssetTypeConf conf); + + void updateConf(AssetTypeConf conf); + + void delete(Integer... ids); +} diff --git a/nz-admin/src/main/java/com/nis/modules/asset/service/impl/AssetTypeConfServiceImpl.java b/nz-admin/src/main/java/com/nis/modules/asset/service/impl/AssetTypeConfServiceImpl.java new file mode 100644 index 00000000..f3f57729 --- /dev/null +++ b/nz-admin/src/main/java/com/nis/modules/asset/service/impl/AssetTypeConfServiceImpl.java @@ -0,0 +1,152 @@ +package com.nis.modules.asset.service.impl; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.nis.common.exception.NZException; +import com.nis.common.utils.PageUtils; +import com.nis.common.utils.Query; +import com.nis.common.utils.RCode; +import com.nis.common.utils.ToolUtil; +import com.nis.modules.asset.dao.AssetTypeConfDao; +import com.nis.modules.asset.entity.Asset; +import com.nis.modules.asset.entity.AssetTypeConf; +import com.nis.modules.asset.service.AssetService; +import com.nis.modules.asset.service.AssetTypeConfService; +import org.apache.commons.collections.CollectionUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * AssetTypeConf service + * + * @author shizhendong + * @date 2021-03-08 + */ +@Service +public class AssetTypeConfServiceImpl extends ServiceImpl<AssetTypeConfDao, AssetTypeConf> implements AssetTypeConfService { + + @Autowired + private AssetService assetService; + + @Override + public AssetTypeConf queryConfInfo(Integer id) { + AssetTypeConf conf = this.getById(id); + if (ToolUtil.isEmpty(conf)) { + throw new NZException(RCode.TYPECONF_NOT_FOUND); + } + return conf; + } + + @Override + public PageUtils queryPage(Map<String, Object> params) { + LambdaQueryWrapper<AssetTypeConf> queryWrapper = new LambdaQueryWrapper<AssetTypeConf>() + .eq(ToolUtil.isNotEmpty(params.get("id")), AssetTypeConf::getId, params.get("id")) + .like(ToolUtil.isNotEmpty(params.get("name")), AssetTypeConf::getName, params.get("name")) + .eq(ToolUtil.isNotEmpty(params.get("vm")), AssetTypeConf::getVm, params.get("vm")) + .eq(ToolUtil.isNotEmpty(params.get("vmh")), AssetTypeConf::getVmh, params.get("vmh")); + + IPage<AssetTypeConf> page = this.page(new Query<AssetTypeConf>().getPage(params), queryWrapper); + return new PageUtils(page); + } + + private void validateAssetTypeConfInfo(AssetTypeConf conf) { + // 1. 名称重复 + List<AssetTypeConf> list = this.list(new LambdaQueryWrapper<AssetTypeConf>().eq(AssetTypeConf::getPid, ToolUtil.isEmpty(conf.getPid()) ? 0 : conf.getPid()).eq(AssetTypeConf::getName, conf.getName())); + boolean flag = false; + // 当只有一个结果并且这个结果是自身时,或没有结果时,说明名称不重复 + if ((list == null || list.size() == 0) || (list != null && list.size() == 1 && list.get(0).getId().equals(conf.getId()))) { + flag = true; + } + + if (!flag) { + throw new NZException(RCode.TYPECONF_NAME_DUPLICATE); + } + + // 2. 校验 vm vmh ssh te;net build 值 + List<Integer> switchList = Arrays.asList(new Integer[]{1, 0}); + if (!switchList.contains(conf.getVm())) { + throw new NZException(RCode.TYPECONF_VM_INCORRECT); + } + if (!switchList.contains(conf.getVmh())) { + throw new NZException(RCode.TYPECONF_VMH_INCORRECT); + } + if (!switchList.contains(conf.getSsh())) { + throw new NZException(RCode.TYPECONF_SSH_INCORRECT); + } + if (!switchList.contains(conf.getTelnet())) { + throw new NZException(RCode.TYPECONF_TELNET_INCORRECT); + } + if (ToolUtil.isNotEmpty(conf.getBuildIn()) && !switchList.contains(conf.getBuildIn())) { + throw new NZException(RCode.TYPECONF_BUILDIN_INCORRECT); + } + + // 3. pid 是否存在 + if (ToolUtil.isNotEmpty(conf.getPid()) && !conf.getPid().equals(0)) { + AssetTypeConf parentPojo = this.getById(conf.getPid()); + if (ToolUtil.isEmpty(parentPojo)) { + throw new NZException(RCode.TYPECONF_PARENT_NOT_FOUND); + } + } + } + + @Override + public void saveConf(AssetTypeConf conf) { + // 1. 校验 + this.validateAssetTypeConfInfo(conf); + + // 2. 保存 + this.save(conf); + } + + @Override + public void updateConf(AssetTypeConf conf) { + // 1. 校验 + this.validateAssetTypeConfInfo(conf); + + // 2. 保存 + this.updateById(conf); + } + + @Override + public void delete(Integer... ids) { + List<AssetTypeConf> list = this.list(); + + // 1. 内置的 配置不允许删除 + List<Integer> buildInIdList = list.stream().filter(assetTypeConf -> assetTypeConf.getBuildIn().equals(1)).map(AssetTypeConf::getId).collect(Collectors.toList()); + buildInIdList.retainAll(Arrays.asList(ids)); + if (CollectionUtils.isNotEmpty(buildInIdList)) { + throw new NZException("These configurations are built-in and cannot be deleted. Details: " + buildInIdList.toString(), RCode.TYPECONF_BUILDIN_CAN_NOT_REMOVE.getCode()); + } + + // 2. 含有 子级 配置不允许删除 + List<Integer> containBabyConfIds = new ArrayList<>(); + for (Integer delId : Arrays.asList(ids)) { + List<AssetTypeConf> babyConfs = list.stream().filter(conf -> conf.getPid().equals(delId)).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(babyConfs)) { + containBabyConfIds.add(delId); + } + } + if (CollectionUtils.isNotEmpty(containBabyConfIds)) { + throw new NZException("These configurations contain sub-configuration and cannot be deleted. Details: " + containBabyConfIds.toString(), RCode.TYPECONF_CONTAIN_BABY_CAN_NOT_REMOVE.getCode()); + } + + // 3. 已被 asset 表引用的 类型不允许删除 + List<Asset> assetList = assetService.list(); + List<Integer> typeIdList = assetList.stream().map(Asset::getTypeId).distinct().collect(Collectors.toList()); + typeIdList.retainAll(Arrays.asList(ids)); + if (CollectionUtils.isNotEmpty(typeIdList)) { + throw new NZException("These configurations have been used by assets and cannot be deleted. Details: " + typeIdList.toString(), RCode.TYPECONF_BEUSED_CAN_NOT_REMOVE.getCode()); + } + + // 删除 + this.removeByIds(Arrays.asList(ids)); + } + +} diff --git a/nz-admin/src/main/resources/db/V2021.03.10__1.Insert asset_type_conf table.sql b/nz-admin/src/main/resources/db/V2021.03.10__1.Insert asset_type_conf table.sql new file mode 100644 index 00000000..2d58a3b7 --- /dev/null +++ b/nz-admin/src/main/resources/db/V2021.03.10__1.Insert asset_type_conf table.sql @@ -0,0 +1,18 @@ +-- ---------------------------- +-- Table structure for asset_type_conf +-- ---------------------------- +DROP TABLE IF EXISTS `asset_type_conf`; +CREATE TABLE `asset_type_conf` ( + `id` int(10) NOT NULL AUTO_INCREMENT, + `pid` int(10) NOT NULL DEFAULT 0 COMMENT '父id 默认为0', + `vm` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '是否为虚拟机 0:不是虚拟机\r\n\r\n1:是虚拟机', + `vmh` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '是否为虚拟机宿主 0:不是宿主机,不支持挂载 虚拟机\r\n\r\n1:是宿主机,支持挂载虚拟机', + `name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '名称', + `ssh` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'ssh服务 0:关闭\r\n\r\n1:开启', + `telnet` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'telnet服务 0:关闭\r\n\r\n1:开启 ', + `remark` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '', + `build_in` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '0' COMMENT '1:内置,0:非内置,默认:0\r\n\r\n内置数据不允许删除', + PRIMARY KEY (`id`) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; + +SET FOREIGN_KEY_CHECKS = 1; diff --git a/nz-common/src/main/java/com/nis/common/utils/RCode.java b/nz-common/src/main/java/com/nis/common/utils/RCode.java index 49624871..7950f782 100644 --- a/nz-common/src/main/java/com/nis/common/utils/RCode.java +++ b/nz-common/src/main/java/com/nis/common/utils/RCode.java @@ -270,6 +270,23 @@ public enum RCode { STATECONF_CAN_NOT_REMOVE(347011, "This config is already used by the asset and cannot be deleted"), STATECONF_NOT_FOUND(346012, "Asset state conf not found"), + TYPECONF_NOT_FOUND(356000, "Asset type config not found"), + TYPECONF_NAME_ISNULL(351001, "Asset type config name is null"), + TYPECONF_VM_ISNULL(351002, "Asset type config vm is null"), + TYPECONF_VMH_ISNULL(351003, "Asset type config vmh is null"), + TYPECONF_SSH_ISNULL(351004, "Asset type config ssh is null"), + TYPECONF_TELNET_ISNULL(351005, "Asset type config telnet is null"), + TYPECONF_NAME_DUPLICATE(352006, "Asset type config name is duplicate"), + TYPECONF_VM_INCORRECT(354007, "Asset type config vm must be 0 or 1"), + TYPECONF_VMH_INCORRECT(354008, "Asset type config vmh must be 0 or 1"), + TYPECONF_SSH_INCORRECT(354009, "Asset type config ssh must be 0 or 1"), + TYPECONF_TELNET_INCORRECT(354010, "Asset type config telnet must be 0 or 1"), + TYPECONF_BUILDIN_INCORRECT(354011, "Asset type config buildin must be 0 or 1"), + TYPECONF_ID_ISNULL(351012, "Asset type config id is null"), + TYPECONF_BUILDIN_CAN_NOT_REMOVE(357013, "This configuration is built-in and cannot be deleted"), + TYPECONF_BEUSED_CAN_NOT_REMOVE(357014, "This configurations is already used by the asset and cannot be deleted"), + TYPECONF_PARENT_NOT_FOUND(356015, "Asset type parent config not found"), + TYPECONF_CONTAIN_BABY_CAN_NOT_REMOVE(357016, "This configurations contain sub-configuration and cannot be deleted"), /** * alerts菜单栏相关 diff --git a/nz-common/src/main/java/com/nis/common/utils/TypeEnum.java b/nz-common/src/main/java/com/nis/common/utils/TypeEnum.java index c6713310..8ab1f691 100644 --- a/nz-common/src/main/java/com/nis/common/utils/TypeEnum.java +++ b/nz-common/src/main/java/com/nis/common/utils/TypeEnum.java @@ -24,7 +24,8 @@ public enum TypeEnum { LINK("link"), SCRIPT("notification script"), ALERTSILENCE("alert silence"), - ASSETSTATECONF("asset state conf"); + ASSETSTATECONF("asset state conf"), + ASSETTYPECONF("asset type conf"); private String value; public String getValue(){ |
