From 6c06a85f07f371bc619dec5db704edd214ab3d21 Mon Sep 17 00:00:00 2001 From: "1437892690@qq.com" <1437892690@qq.com> Date: Thu, 19 Jun 2025 18:07:59 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=8A=9F=E8=83=BD]=20=E5=8F=91=E5=B8=83?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E9=85=8D=E7=BD=AE-=E7=8E=AF=E5=A2=83db?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BE=BF=E6=8D=B7=E5=85=A5=E5=8F=A3=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 关联 #[1445757985456128]发布应用配置-环境db配置便捷入口优化 http://192.168.0.96:8090/demo/rdm.html#/story-detail/939050947543040/939050947543042/1445757985456128 --- .../account/AccountSaveApi.java | 161 ++------------ .../ResourceAccountAccessTestApi.java | 14 +- .../resource/ResourceAccountSaveApi.java | 83 ++------ .../DefaultResourceCenterDataSourceImpl.java | 15 +- .../account/ResourceCenterAccountService.java | 17 ++ .../ResourceCenterAccountServiceImpl.java | 198 +++++++++++++++++- 6 files changed, 254 insertions(+), 234 deletions(-) diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/account/AccountSaveApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/account/AccountSaveApi.java index abf799d8..1382fad0 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/account/AccountSaveApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/account/AccountSaveApi.java @@ -17,16 +17,11 @@ package neatlogic.module.cmdb.api.resourcecenter.account; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import neatlogic.framework.asynchronization.threadlocal.UserContext; import neatlogic.framework.auth.core.AuthAction; import neatlogic.framework.cmdb.auth.label.RESOURCECENTER_ACCOUNT_MODIFY; -import neatlogic.framework.cmdb.dto.resourcecenter.AccountProtocolVo; -import neatlogic.framework.cmdb.dto.resourcecenter.AccountTagVo; import neatlogic.framework.cmdb.dto.resourcecenter.AccountVo; -import neatlogic.framework.cmdb.dto.resourcecenter.ResourceAccountVo; -import neatlogic.framework.cmdb.dto.tag.TagVo; import neatlogic.framework.cmdb.enums.resourcecenter.AccountType; -import neatlogic.framework.cmdb.exception.resourcecenter.*; +import neatlogic.framework.cmdb.exception.resourcecenter.ResourceCenterAccountNameRepeatsException; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.dto.FieldValidResultVo; import neatlogic.framework.exception.type.ParamNotExistsException; @@ -35,15 +30,13 @@ import neatlogic.framework.restful.constvalue.OperationTypeEnum; import neatlogic.framework.restful.core.IValid; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceAccountMapper; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceTagMapper; -import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.lang3.StringUtils; +import neatlogic.module.cmdb.service.resourcecenter.account.ResourceCenterAccountService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; -import java.util.*; -import java.util.stream.Collectors; +import java.util.List; +import java.util.Objects; @Service @Transactional @@ -52,7 +45,7 @@ import java.util.stream.Collectors; public class AccountSaveApi extends PrivateApiComponentBase { @Resource - private ResourceTagMapper resourceTagMapper; + private ResourceCenterAccountService resourceCenterAccountService; @Resource private ResourceAccountMapper resourceAccountMapper; @@ -63,7 +56,7 @@ public class AccountSaveApi extends PrivateApiComponentBase { @Override public String getName() { - return "保存资源中心账号"; + return "nmcara.accountsaveapi.getname"; } @Override @@ -72,118 +65,26 @@ public class AccountSaveApi extends PrivateApiComponentBase { } @Input({ - @Param(name = "id", type = ApiParamType.LONG, desc = "账号ID"), - @Param(name = "name", type = ApiParamType.STRING, maxLength = 200, isRequired = true, desc = "名称"), - @Param(name = "account", type = ApiParamType.STRING, maxLength = 80, desc = "用户名"), - @Param(name = "passwordPlain", type = ApiParamType.STRING, isRequired = false, desc = "密码"), - @Param(name = "protocolId", type = ApiParamType.LONG, isRequired = true, desc = "协议id"), - @Param(name = "port", type = ApiParamType.INTEGER, isRequired = false, desc = "端口"), - @Param(name = "tagIdList", type = ApiParamType.JSONARRAY, isRequired = false, desc = "标签id列表"), - @Param(name = "type", type = ApiParamType.ENUM, member = AccountType.class, isRequired = true, desc = "标签id列表"), - @Param(name = "isDefault", type = ApiParamType.INTEGER, desc = "是否默认账号"), - @Param(name = "resourceId", type = ApiParamType.LONG, desc = "资产ID") + @Param(name = "id", type = ApiParamType.LONG, desc = "common.id"), + @Param(name = "name", type = ApiParamType.STRING, maxLength = 200, isRequired = true, desc = "common.name"), + @Param(name = "account", type = ApiParamType.STRING, maxLength = 80, desc = "common.username"), + @Param(name = "passwordPlain", type = ApiParamType.STRING, isRequired = false, desc = "common.password"), + @Param(name = "protocolId", type = ApiParamType.LONG, isRequired = true, desc = "term.cmdb.protocol"), + @Param(name = "port", type = ApiParamType.INTEGER, isRequired = false, desc = "term.cmdb.port"), + @Param(name = "tagIdList", type = ApiParamType.JSONARRAY, isRequired = false, desc = "common.tagidlist"), + @Param(name = "type", type = ApiParamType.ENUM, member = AccountType.class, isRequired = true, desc = "common.type"), + @Param(name = "isDefault", type = ApiParamType.INTEGER, desc = "common.isdefault"), + @Param(name = "resourceId", type = ApiParamType.LONG, desc = "term.cmdb.resourceid") }) @Output({ - @Param(name = "id", type = ApiParamType.LONG, desc = "账号ID") + @Param(name = "id", type = ApiParamType.LONG, desc = "common.id") }) - @Description(desc = "保存资源中心账号") + @Description(desc = "nmcara.accountsaveapi.getname") @Override public Object myDoService(JSONObject paramObj) throws Exception { AccountVo paramAccountVo = JSON.toJavaObject(paramObj, AccountVo.class); Long id = paramObj.getLong("id"); - - AccountProtocolVo protocolVo = resourceAccountMapper.getAccountProtocolVoByProtocolId(paramAccountVo.getProtocolId()); - if (protocolVo == null) { - throw new ResourceCenterAccountProtocolNotFoundException(paramAccountVo.getProtocolId()); - } - paramAccountVo.setProtocol(protocolVo.getName()); - if (!StringUtils.equals(protocolVo.getName(), "tagent") && StringUtils.isEmpty(paramObj.getString("account"))) { - throw new ResourceCenterAccountNameIsNotNullException(); - } - String type = paramAccountVo.getType(); - if (Objects.equals(type, AccountType.PUBLIC.getValue())) { - if (resourceAccountMapper.checkAccountNameIsRepeats(paramAccountVo) > 0) { - throw new ResourceCenterAccountNameRepeatsException(paramAccountVo.getName()); - } - } else { - Long resourceId = paramAccountVo.getResourceId(); - if (resourceId == null) { - throw new ParamNotExistsException("resourceId"); - } - List accountVoList = resourceAccountMapper.getResourceAccountListByResourceId(resourceId); - for (AccountVo accountVo : accountVoList) { - if (Objects.equals(paramAccountVo.getName(), accountVo.getName()) && !Objects.equals(paramAccountVo.getId(), accountVo.getId())) { - throw new ResourceCenterAccountNameRepeatsException(paramAccountVo.getName()); - } - } - } - // 如果是私有类型账号,需要校验该资产中所有公有和私有账号中是否存在账号及协议都相同的,如果存在则不能更新 - if (Objects.equals(type, AccountType.PRIVATE.getValue())) { - Long resourceId = paramObj.getLong("resourceId"); - if (resourceId == null) { - throw new ParamNotExistsException("资产ID(resourceId)"); - } - List failureReasonList = check(resourceId, paramAccountVo); - if (CollectionUtils.isNotEmpty(failureReasonList)) { - JSONObject resultObj = new JSONObject(); - resultObj.put("failureReasonList", failureReasonList); - return resultObj; - } - List resourceAccountVoList = new ArrayList<>(); - resourceAccountVoList.add(new ResourceAccountVo(resourceId, paramAccountVo.getId())); - resourceAccountMapper.insertIgnoreResourceAccount(resourceAccountVoList); - } - List tagIdList = paramAccountVo.getTagIdList(); - List accountTagVoList = new ArrayList<>(); - if (CollectionUtils.isNotEmpty(tagIdList)) { - List searchTagIdList = null; - List insertTagIdList = new ArrayList<>(tagIdList); - List tagVoList = resourceTagMapper.searchTagListByIdList(tagIdList); - searchTagIdList = tagVoList.stream().map(TagVo::getId).collect(Collectors.toList()); - insertTagIdList.removeAll(searchTagIdList); - if (CollectionUtils.isNotEmpty(insertTagIdList)) { - List notFoundTagIdList = new ArrayList<>(insertTagIdList); - if (CollectionUtils.isNotEmpty(notFoundTagIdList)) { - throw new ResourceCenterTagNotFoundException(notFoundTagIdList); - } - } - resourceAccountMapper.deleteAccountTagByAccountId(paramAccountVo.getId()); - for (Long tagId : tagIdList) { - accountTagVoList.add(new AccountTagVo(paramAccountVo.getId(), tagId)); - if (accountTagVoList.size() > 100) { - resourceAccountMapper.insertIgnoreAccountTag(accountTagVoList); - accountTagVoList.clear(); - } - } - if (CollectionUtils.isNotEmpty(accountTagVoList)) { - resourceAccountMapper.insertIgnoreAccountTag(accountTagVoList); - } - } - paramAccountVo.setLcu(UserContext.get().getUserUuid()); - - //一个协议只能存一个默认账号。例如,ssh协议当前默认账号为app,如果在编辑root账号时,把root设置为默认账号,需要替换掉原有的app默认账号表示标识。root代替app成为了新的ssh协议默认账号。 - if (Objects.equals(type, AccountType.PUBLIC.getValue()) && paramAccountVo.getIsDefault() == 1) { - resourceAccountMapper.resetAccountDefaultByProtocolIdAndAccount(paramAccountVo.getProtocolId(), paramAccountVo.getAccount()); - } - - if (id != null) { - AccountVo oldVo = resourceAccountMapper.getAccountById(id); - if (oldVo == null) { - throw new ResourceCenterAccountNotFoundException(id); - } - paramAccountVo.setProtocolId(protocolVo.getId()); - resourceAccountMapper.updateAccount(paramAccountVo); - } else { - if (Objects.equals(protocolVo.getName(), "tagent")) { - throw new ResourceCenterAccountNotCreateTagentAccountException(); - } - paramAccountVo.setFcu(UserContext.get().getUserUuid()); - resourceAccountMapper.insertAccount(paramAccountVo); - } - - JSONObject resultObj = new JSONObject(); - resultObj.put("id", paramAccountVo.getId()); - return resultObj; + return resourceCenterAccountService.saveAccount(id, paramAccountVo); } public IValid name() { @@ -208,28 +109,4 @@ public class AccountSaveApi extends PrivateApiComponentBase { return new FieldValidResultVo(); }; } - - /** - * 校验该资产中所有公有和私有账号中是否存在账号及协议都相同的 - * - * @param resourceId 资产ID - * @param newAccountVo 新账号信息 - */ - private List check(Long resourceId, AccountVo newAccountVo) { - List failureReasonList = new ArrayList<>(); - Map accountVoMap = new HashMap<>(); - List accountVoList = resourceAccountMapper.getResourceAccountListByResourceId(resourceId); - accountVoList.removeIf(accountVo -> Objects.equals(accountVo.getId(), newAccountVo.getId())); - accountVoList.add(newAccountVo); - for (AccountVo accountVo : accountVoList) { - String key = accountVo.getProtocol() + "#" + accountVo.getAccount(); - AccountVo account = accountVoMap.get(key); - if (account == null) { - accountVoMap.put(key, accountVo); - } else { - failureReasonList.add("选中项中\"" + accountVo.getName() + "(" + accountVo.getProtocol() + "/" + accountVo.getAccount() + ")\"与\"" + account.getName() + "(" + account.getProtocol() + "/" + account.getAccount() + ")\""); - } - } - return failureReasonList; - } } diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceAccountAccessTestApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceAccountAccessTestApi.java index 052daf8d..6b795075 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceAccountAccessTestApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceAccountAccessTestApi.java @@ -17,7 +17,7 @@ package neatlogic.module.cmdb.api.resourcecenter.resource; import com.alibaba.fastjson.JSONObject; import neatlogic.framework.auth.core.AuthAction; -import neatlogic.framework.cmdb.auth.label.CMDB_BASE; +import neatlogic.framework.cmdb.auth.label.CMDB; import neatlogic.framework.cmdb.dto.resourcecenter.AccountAccessTestVo; import neatlogic.framework.cmdb.dto.resourcecenter.AccountVo; import neatlogic.framework.cmdb.dto.resourcecenter.ResourceVo; @@ -49,7 +49,7 @@ import java.util.List; import java.util.Random; @Service -@AuthAction(action = CMDB_BASE.class) +@AuthAction(action = CMDB.class) @OperationType(type = OperationTypeEnum.SEARCH) public class ResourceAccountAccessTestApi extends PrivateApiComponentBase { @@ -69,7 +69,7 @@ public class ResourceAccountAccessTestApi extends PrivateApiComponentBase { @Override public String getName() { - return "测试账号可用性"; + return "nmcarr.resourceaccountaccesstestapi.getname"; } @Override @@ -78,14 +78,14 @@ public class ResourceAccountAccessTestApi extends PrivateApiComponentBase { } @Input({ - @Param(name = "resourceId", type = ApiParamType.LONG, isRequired = true, desc = "资源id"), - @Param(name = "runnerId", type = ApiParamType.LONG, desc = "执行器id"), - @Param(name = "accountIdList", type = ApiParamType.JSONARRAY, isRequired = true, desc = "账号ID列表"), + @Param(name = "resourceId", type = ApiParamType.LONG, isRequired = true, desc = "term.cmdb.resourceid"), + @Param(name = "runnerId", type = ApiParamType.LONG, desc = "term.deploy.runnerid"), + @Param(name = "accountIdList", type = ApiParamType.JSONARRAY, isRequired = true, desc = "term.cmdb.accountidlist"), }) @Output({ @Param(explode = AccountVo.class), }) - @Description(desc = "测试账号可用性") + @Description(desc = "nmcarr.resourceaccountaccesstestapi.getname") @Override public Object myDoService(JSONObject paramObj) throws Exception { Long resourceId = paramObj.getLong("resourceId"); diff --git a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceAccountSaveApi.java b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceAccountSaveApi.java index d9f73e7f..f1c9010e 100644 --- a/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceAccountSaveApi.java +++ b/src/main/java/neatlogic/module/cmdb/api/resourcecenter/resource/ResourceAccountSaveApi.java @@ -19,10 +19,6 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import neatlogic.framework.auth.core.AuthAction; import neatlogic.framework.cmdb.auth.label.RESOURCECENTER_MODIFY; -import neatlogic.framework.cmdb.dto.resourcecenter.AccountVo; -import neatlogic.framework.cmdb.dto.resourcecenter.ResourceAccountVo; -import neatlogic.framework.cmdb.enums.resourcecenter.AccountType; -import neatlogic.framework.cmdb.exception.resourcecenter.ResourceCenterAccountNotFoundException; import neatlogic.framework.cmdb.exception.resourcecenter.ResourceNotFoundException; import neatlogic.framework.common.constvalue.ApiParamType; import neatlogic.framework.restful.annotation.Description; @@ -31,16 +27,15 @@ import neatlogic.framework.restful.annotation.OperationType; import neatlogic.framework.restful.annotation.Param; import neatlogic.framework.restful.constvalue.OperationTypeEnum; import neatlogic.framework.restful.core.privateapi.PrivateApiComponentBase; -import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceAccountMapper; import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceMapper; +import neatlogic.module.cmdb.service.resourcecenter.account.ResourceCenterAccountService; import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.collections4.ListUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; -import java.util.*; -import java.util.stream.Collectors; +import java.util.ArrayList; +import java.util.List; /** * @author linbq @@ -55,7 +50,7 @@ public class ResourceAccountSaveApi extends PrivateApiComponentBase { @Resource private ResourceMapper resourceMapper; @Resource - private ResourceAccountMapper resourceAccountMapper; + private ResourceCenterAccountService resourceCenterAccountService; @Override public String getToken() { @@ -64,7 +59,7 @@ public class ResourceAccountSaveApi extends PrivateApiComponentBase { @Override public String getName() { - return "保存资源账号"; + return "nmcarr.resourceaccountsaveapi.getname"; } @Override @@ -73,75 +68,21 @@ public class ResourceAccountSaveApi extends PrivateApiComponentBase { } @Input({ - @Param(name = "resourceId", type = ApiParamType.LONG, isRequired = true, desc = "资源id"), - @Param(name = "accountIdList", type = ApiParamType.JSONARRAY, desc = "账号id列表") + @Param(name = "resourceId", type = ApiParamType.LONG, isRequired = true, desc = "term.cmdb.resourceid"), + @Param(name = "accountIdList", type = ApiParamType.JSONARRAY, desc = "term.cmdb.accountidlist") }) - @Description(desc = "保存资源账号") + @Description(desc = "nmcarr.resourceaccountsaveapi.getname") @Override public Object myDoService(JSONObject paramObj) throws Exception { - int successCount = 0; - List failureReasonList = new ArrayList<>(); Long resourceId = paramObj.getLong("resourceId"); if (resourceMapper.getResourceIdByResourceId(resourceId) == null) { throw new ResourceNotFoundException(resourceId); } - // 查询该资产绑定的公有账号列表,再根据账号ID解绑 - List accountList = resourceAccountMapper.getResourceAccountListByResourceIdAndType(resourceId, AccountType.PUBLIC.getValue()); - if (CollectionUtils.isNotEmpty(accountList)) { - List accountIdList = accountList.stream().map(AccountVo::getId).collect(Collectors.toList()); - resourceAccountMapper.deleteResourceAccountByResourceIdListAndAccountIdList(Arrays.asList(resourceId), accountIdList); - } + List accountIdList = new ArrayList<>(); JSONArray accountIdArray = paramObj.getJSONArray("accountIdList"); - if (CollectionUtils.isEmpty(accountIdArray)) { - return null; - } - List accountIdList = accountIdArray.toJavaList(Long.class); - Map accountVoMap = new HashMap<>(); - List existAccountIdList = new ArrayList<>(); - Set excludeAccountIdSet = new HashSet<>(); - List accountVoList = resourceAccountMapper.getAccountListByIdList(accountIdList); - for (AccountVo accountVo : accountVoList) { - existAccountIdList.add(accountVo.getId()); - String key = accountVo.getProtocol() + "#" + accountVo.getAccount(); - AccountVo account = accountVoMap.get(key); - if (account == null) { - accountVoMap.put(key, accountVo); - } else { - failureReasonList.add("选中项中\"" + accountVo.getName() + "(" + accountVo.getProtocol() + "/" + accountVo.getAccount() + ")\"与\"" + account.getName() + "(" + account.getProtocol() + "/" + account.getAccount() + ")\"的协议相同且用户名相同,同一资产不可绑定多个协议相同且用户名相同的账号"); - excludeAccountIdSet.add(accountVo.getId()); - excludeAccountIdSet.add(account.getId()); - } - } - if (accountIdList.size() > existAccountIdList.size()) { - List notFoundIdList = ListUtils.removeAll(accountIdList, existAccountIdList); - if (CollectionUtils.isNotEmpty(notFoundIdList)) { - StringBuilder stringBuilder = new StringBuilder(); - for (Long accountId : notFoundIdList) { - stringBuilder.append(accountId); - stringBuilder.append("、"); - } - stringBuilder.deleteCharAt(stringBuilder.length() - 1); - throw new ResourceCenterAccountNotFoundException(stringBuilder.toString()); - } + if (CollectionUtils.isNotEmpty(accountIdArray)) { + accountIdList = accountIdArray.toJavaList(Long.class); } - accountIdList.removeAll(excludeAccountIdSet); - List resourceAccountVoList = new ArrayList<>(); - for (Long accountId : accountIdList) { - resourceAccountVoList.add(new ResourceAccountVo(resourceId, accountId)); - successCount++; - if (resourceAccountVoList.size() > 100) { - resourceAccountMapper.insertIgnoreResourceAccount(resourceAccountVoList); - resourceAccountVoList.clear(); - } - } - if (CollectionUtils.isNotEmpty(resourceAccountVoList)) { - resourceAccountMapper.insertIgnoreResourceAccount(resourceAccountVoList); - } - - JSONObject resultObj = new JSONObject(); - resultObj.put("successCount", successCount); - resultObj.put("failureCount", failureReasonList.size()); - resultObj.put("failureReasonList", failureReasonList); - return resultObj; + return resourceCenterAccountService.saveResourceAccount(resourceId, accountIdList); } } diff --git a/src/main/java/neatlogic/module/cmdb/resourcecenter/datasource/handler/DefaultResourceCenterDataSourceImpl.java b/src/main/java/neatlogic/module/cmdb/resourcecenter/datasource/handler/DefaultResourceCenterDataSourceImpl.java index 4a39a869..33630936 100644 --- a/src/main/java/neatlogic/module/cmdb/resourcecenter/datasource/handler/DefaultResourceCenterDataSourceImpl.java +++ b/src/main/java/neatlogic/module/cmdb/resourcecenter/datasource/handler/DefaultResourceCenterDataSourceImpl.java @@ -391,16 +391,17 @@ public class DefaultResourceCenterDataSourceImpl implements IResourceCenterDataS ResourceEntityVo resourceEntityVo = resourceEntityMapper.getResourceEntityByName(name); if (resourceEntityVo != null) { searchVo.setViewName(name); + List fieldList = viewName2FieldListMap.get(name); List resourceList = getAppResourceList(searchVo, true); + JSONArray tbodyList = new JSONArray(); if (CollectionUtils.isNotEmpty(resourceList)) { - List fieldList = viewName2FieldListMap.get(name); - JSONArray theadList = getTheadList(fieldList); - JSONArray tbodyList = getTbodyList(fieldList, resourceList, resourceEntityVo); - JSONObject tableObj = TableResultUtil.getResult(theadList, tbodyList, searchVo); - tableObj.put("viewName", name); - tableObj.put("viewLabel", resourceEntityVo.getLabel()); - tableList.add(tableObj); + tbodyList = getTbodyList(fieldList, resourceList, resourceEntityVo); } + JSONArray theadList = getTheadList(fieldList); + JSONObject tableObj = TableResultUtil.getResult(theadList, tbodyList, searchVo); + tableObj.put("viewName", name); + tableObj.put("viewLabel", resourceEntityVo.getLabel()); + tableList.add(tableObj); } } } diff --git a/src/main/java/neatlogic/module/cmdb/service/resourcecenter/account/ResourceCenterAccountService.java b/src/main/java/neatlogic/module/cmdb/service/resourcecenter/account/ResourceCenterAccountService.java index f8f4ab52..f1dcca51 100644 --- a/src/main/java/neatlogic/module/cmdb/service/resourcecenter/account/ResourceCenterAccountService.java +++ b/src/main/java/neatlogic/module/cmdb/service/resourcecenter/account/ResourceCenterAccountService.java @@ -15,6 +15,7 @@ along with this program. If not, see .*/ package neatlogic.module.cmdb.service.resourcecenter.account; +import com.alibaba.fastjson.JSONObject; import neatlogic.framework.cmdb.dto.resourcecenter.AccountBaseVo; import neatlogic.framework.cmdb.dto.resourcecenter.AccountProtocolVo; import neatlogic.framework.cmdb.dto.resourcecenter.AccountVo; @@ -51,4 +52,20 @@ public interface ResourceCenterAccountService { * @param accountIdList 账号idList */ void deleteAccount(List accountIdList); + + /** + * 保存账号 + * @param id + * @param paramAccountVo + * @return + */ + JSONObject saveAccount(Long id, AccountVo paramAccountVo); + + /** + * 绑定账号与资源关系 + * @param resourceId + * @param accountIdList + * @return + */ + JSONObject saveResourceAccount(Long resourceId, List accountIdList); } diff --git a/src/main/java/neatlogic/module/cmdb/service/resourcecenter/account/ResourceCenterAccountServiceImpl.java b/src/main/java/neatlogic/module/cmdb/service/resourcecenter/account/ResourceCenterAccountServiceImpl.java index 29a0b771..b8b5222f 100644 --- a/src/main/java/neatlogic/module/cmdb/service/resourcecenter/account/ResourceCenterAccountServiceImpl.java +++ b/src/main/java/neatlogic/module/cmdb/service/resourcecenter/account/ResourceCenterAccountServiceImpl.java @@ -15,22 +15,27 @@ along with this program. If not, see .*/ package neatlogic.module.cmdb.service.resourcecenter.account; +import com.alibaba.fastjson.JSONObject; +import neatlogic.framework.asynchronization.threadlocal.UserContext; import neatlogic.framework.cmdb.crossover.IResourceCenterAccountCrossoverService; -import neatlogic.framework.cmdb.dto.resourcecenter.AccountBaseVo; -import neatlogic.framework.cmdb.dto.resourcecenter.AccountProtocolVo; -import neatlogic.framework.cmdb.dto.resourcecenter.AccountVo; +import neatlogic.framework.cmdb.dto.resourcecenter.*; +import neatlogic.framework.cmdb.dto.tag.TagVo; +import neatlogic.framework.cmdb.enums.resourcecenter.AccountType; import neatlogic.framework.cmdb.enums.resourcecenter.Protocol; +import neatlogic.framework.cmdb.exception.resourcecenter.*; +import neatlogic.framework.exception.type.ParamNotExistsException; import neatlogic.framework.tagent.dao.mapper.TagentMapper; import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceAccountMapper; import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceMapper; +import neatlogic.module.cmdb.dao.mapper.resourcecenter.ResourceTagMapper; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.ListUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; +import java.util.*; +import java.util.stream.Collectors; /** * @author lvzk @@ -38,6 +43,9 @@ import java.util.Optional; **/ @Service public class ResourceCenterAccountServiceImpl implements ResourceCenterAccountService, IResourceCenterAccountCrossoverService { + + @Resource + private ResourceTagMapper resourceTagMapper; @Resource ResourceMapper resourceMapper; @Resource @@ -105,4 +113,180 @@ public class ResourceCenterAccountServiceImpl implements ResourceCenterAccountSe // resourceAccountMapper.deleteAccountIpByAccountIdList(accountIdList); } } + + @Override + public JSONObject saveAccount(Long id, AccountVo paramAccountVo) { + AccountProtocolVo protocolVo = resourceAccountMapper.getAccountProtocolVoByProtocolId(paramAccountVo.getProtocolId()); + if (protocolVo == null) { + throw new ResourceCenterAccountProtocolNotFoundException(paramAccountVo.getProtocolId()); + } + paramAccountVo.setProtocol(protocolVo.getName()); + if (!StringUtils.equals(protocolVo.getName(), "tagent") && StringUtils.isEmpty(paramAccountVo.getAccount())) { + throw new ResourceCenterAccountNameIsNotNullException(); + } + String type = paramAccountVo.getType(); + if (Objects.equals(type, AccountType.PUBLIC.getValue())) { + if (resourceAccountMapper.checkAccountNameIsRepeats(paramAccountVo) > 0) { + throw new ResourceCenterAccountNameRepeatsException(paramAccountVo.getName()); + } + } else { + // 如果是私有类型账号,需要校验该资产中所有公有和私有账号中是否存在账号及协议都相同的,如果存在则不能更新 + Long resourceId = paramAccountVo.getResourceId(); + if (resourceId == null) { + throw new ParamNotExistsException("resourceId"); + } + List accountVoList = resourceAccountMapper.getResourceAccountListByResourceId(resourceId); + for (AccountVo accountVo : accountVoList) { + if (Objects.equals(paramAccountVo.getName(), accountVo.getName()) && !Objects.equals(paramAccountVo.getId(), accountVo.getId())) { + throw new ResourceCenterAccountNameRepeatsException(paramAccountVo.getName()); + } + } + List failureReasonList = check(resourceId, paramAccountVo); + if (CollectionUtils.isNotEmpty(failureReasonList)) { + JSONObject resultObj = new JSONObject(); + resultObj.put("failureReasonList", failureReasonList); + return resultObj; + } + List resourceAccountVoList = new ArrayList<>(); + resourceAccountVoList.add(new ResourceAccountVo(resourceId, paramAccountVo.getId())); + resourceAccountMapper.insertIgnoreResourceAccount(resourceAccountVoList); + } + List tagIdList = paramAccountVo.getTagIdList(); + List accountTagVoList = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(tagIdList)) { + List searchTagIdList = null; + List insertTagIdList = new ArrayList<>(tagIdList); + List tagVoList = resourceTagMapper.searchTagListByIdList(tagIdList); + searchTagIdList = tagVoList.stream().map(TagVo::getId).collect(Collectors.toList()); + insertTagIdList.removeAll(searchTagIdList); + if (CollectionUtils.isNotEmpty(insertTagIdList)) { + List notFoundTagIdList = new ArrayList<>(insertTagIdList); + if (CollectionUtils.isNotEmpty(notFoundTagIdList)) { + throw new ResourceCenterTagNotFoundException(notFoundTagIdList); + } + } + resourceAccountMapper.deleteAccountTagByAccountId(paramAccountVo.getId()); + for (Long tagId : tagIdList) { + accountTagVoList.add(new AccountTagVo(paramAccountVo.getId(), tagId)); + if (accountTagVoList.size() > 100) { + resourceAccountMapper.insertIgnoreAccountTag(accountTagVoList); + accountTagVoList.clear(); + } + } + if (CollectionUtils.isNotEmpty(accountTagVoList)) { + resourceAccountMapper.insertIgnoreAccountTag(accountTagVoList); + } + } + paramAccountVo.setLcu(UserContext.get().getUserUuid()); + + //一个协议只能存一个默认账号。例如,ssh协议当前默认账号为app,如果在编辑root账号时,把root设置为默认账号,需要替换掉原有的app默认账号表示标识。root代替app成为了新的ssh协议默认账号。 + if (Objects.equals(type, AccountType.PUBLIC.getValue()) && paramAccountVo.getIsDefault() == 1) { + resourceAccountMapper.resetAccountDefaultByProtocolIdAndAccount(paramAccountVo.getProtocolId(), paramAccountVo.getAccount()); + } + + if (id != null) { + AccountVo oldVo = resourceAccountMapper.getAccountById(id); + if (oldVo == null) { + throw new ResourceCenterAccountNotFoundException(id); + } + paramAccountVo.setProtocolId(protocolVo.getId()); + resourceAccountMapper.updateAccount(paramAccountVo); + } else { + if (Objects.equals(protocolVo.getName(), "tagent")) { + throw new ResourceCenterAccountNotCreateTagentAccountException(); + } + paramAccountVo.setFcu(UserContext.get().getUserUuid()); + resourceAccountMapper.insertAccount(paramAccountVo); + } + + JSONObject resultObj = new JSONObject(); + resultObj.put("id", paramAccountVo.getId()); + return resultObj; + } + + @Override + public JSONObject saveResourceAccount(Long resourceId, List accountIdList) { + int successCount = 0; + List failureReasonList = new ArrayList<>(); + // 查询该资产绑定的公有账号列表,再根据账号ID解绑 + List accountList = resourceAccountMapper.getResourceAccountListByResourceIdAndType(resourceId, AccountType.PUBLIC.getValue()); + if (CollectionUtils.isNotEmpty(accountList)) { + List accountIds = accountList.stream().map(AccountVo::getId).collect(Collectors.toList()); + resourceAccountMapper.deleteResourceAccountByResourceIdListAndAccountIdList(Collections.singletonList(resourceId), accountIds); + } + if (CollectionUtils.isEmpty(accountIdList)) { + return null; + } + Map accountVoMap = new HashMap<>(); + List existAccountIdList = new ArrayList<>(); + Set excludeAccountIdSet = new HashSet<>(); + List accountVoList = resourceAccountMapper.getAccountListByIdList(accountIdList); + for (AccountVo accountVo : accountVoList) { + existAccountIdList.add(accountVo.getId()); + String key = accountVo.getProtocol() + "#" + accountVo.getAccount(); + AccountVo account = accountVoMap.get(key); + if (account == null) { + accountVoMap.put(key, accountVo); + } else { + failureReasonList.add("选中项中\"" + accountVo.getName() + "(" + accountVo.getProtocol() + "/" + accountVo.getAccount() + ")\"与\"" + account.getName() + "(" + account.getProtocol() + "/" + account.getAccount() + ")\"的协议相同且用户名相同,同一资产不可绑定多个协议相同且用户名相同的账号"); + excludeAccountIdSet.add(accountVo.getId()); + excludeAccountIdSet.add(account.getId()); + } + } + if (accountIdList.size() > existAccountIdList.size()) { + List notFoundIdList = ListUtils.removeAll(accountIdList, existAccountIdList); + if (CollectionUtils.isNotEmpty(notFoundIdList)) { + StringBuilder stringBuilder = new StringBuilder(); + for (Long accountId : notFoundIdList) { + stringBuilder.append(accountId); + stringBuilder.append("、"); + } + stringBuilder.deleteCharAt(stringBuilder.length() - 1); + throw new ResourceCenterAccountNotFoundException(stringBuilder.toString()); + } + } + accountIdList.removeAll(excludeAccountIdSet); + List resourceAccountVoList = new ArrayList<>(); + for (Long accountId : accountIdList) { + resourceAccountVoList.add(new ResourceAccountVo(resourceId, accountId)); + successCount++; + if (resourceAccountVoList.size() > 100) { + resourceAccountMapper.insertIgnoreResourceAccount(resourceAccountVoList); + resourceAccountVoList.clear(); + } + } + if (CollectionUtils.isNotEmpty(resourceAccountVoList)) { + resourceAccountMapper.insertIgnoreResourceAccount(resourceAccountVoList); + } + + JSONObject resultObj = new JSONObject(); + resultObj.put("successCount", successCount); + resultObj.put("failureCount", failureReasonList.size()); + resultObj.put("failureReasonList", failureReasonList); + return resultObj; + } + + /** + * 校验该资产中所有公有和私有账号中是否存在账号及协议都相同的 + * + * @param resourceId 资产ID + * @param newAccountVo 新账号信息 + */ + private List check(Long resourceId, AccountVo newAccountVo) { + List failureReasonList = new ArrayList<>(); + Map accountVoMap = new HashMap<>(); + List accountVoList = resourceAccountMapper.getResourceAccountListByResourceId(resourceId); + accountVoList.removeIf(accountVo -> Objects.equals(accountVo.getId(), newAccountVo.getId())); + accountVoList.add(newAccountVo); + for (AccountVo accountVo : accountVoList) { + String key = accountVo.getProtocol() + "#" + accountVo.getAccount(); + AccountVo account = accountVoMap.get(key); + if (account == null) { + accountVoMap.put(key, accountVo); + } else { + failureReasonList.add("选中项中\"" + accountVo.getName() + "(" + accountVo.getProtocol() + "/" + accountVo.getAccount() + ")\"与\"" + account.getName() + "(" + account.getProtocol() + "/" + account.getAccount() + ")\""); + } + } + return failureReasonList; + } } -- Gitee