Skip to content

Commit

Permalink
Merge pull request #4948 from huangjiao-heart/issue_4934_shared_env_name
Browse files Browse the repository at this point in the history
feat: 共享的环境名称使用project@poolName格式,以区分跨项目还是本项目的环境 #4934
  • Loading branch information
irwinsun authored Aug 17, 2021
2 parents 8958ac5 + 4582c5d commit 1a72bf5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,5 @@ object EnvironmentMessageCode {
const val ERROR_NODE_INFLUX_QUERY_NET_INFO_FAIL = "2105035" // 环境管理:查询构建机网络信息失败: {0}
const val ERROR_NODE_SHARE_PROJECT_EMPTY = "2105036" // 环境管理:共享的项目列表为空
const val ERROR_NODE_SHARE_PROJECT_TYPE_ERROR = "2105037" // 环境管理:仅构建环境支持共享
const val ERROR_NODE_NAME_INVALID_CHARACTER = "2105038" // 环境管理:环境名称包含非法字符@
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@ class EnvShareProjectDao {
}
}

fun get(dslContext: DSLContext, envName: String, sharedProjectId: String): List<TEnvShareProjectRecord> {
fun get(
dslContext: DSLContext,
envName: String,
sharedProjectId: String,
mainProjectId: String
): List<TEnvShareProjectRecord> {
with(TEnvShareProject.T_ENV_SHARE_PROJECT) {
return dslContext.selectFrom(this)
.where(ENV_NAME.eq(envName))
.and(MAIN_PROJECT_ID.eq(mainProjectId))
.and(SHARED_PROJECT_ID.eq(sharedProjectId))
.fetch()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import com.tencent.devops.environment.constant.EnvironmentMessageCode.ERROR_ENV_
import com.tencent.devops.environment.constant.EnvironmentMessageCode.ERROR_ENV_NO_VIEW_PERMISSSION
import com.tencent.devops.environment.constant.EnvironmentMessageCode.ERROR_NODE_INSUFFICIENT_PERMISSIONS
import com.tencent.devops.environment.constant.EnvironmentMessageCode.ERROR_NODE_NAME_DUPLICATE
import com.tencent.devops.environment.constant.EnvironmentMessageCode.ERROR_NODE_NAME_INVALID_CHARACTER
import com.tencent.devops.environment.constant.EnvironmentMessageCode.ERROR_NODE_NOT_EXISTS
import com.tencent.devops.environment.constant.EnvironmentMessageCode.ERROR_NODE_SHARE_PROJECT_TYPE_ERROR
import com.tencent.devops.environment.dao.EnvDao
Expand Down Expand Up @@ -92,6 +93,9 @@ class EnvService @Autowired constructor(
) : IEnvService {

override fun checkName(projectId: String, envId: Long?, envName: String) {
if (envName.contains("@")) {
throw ErrorCodeException(errorCode = ERROR_NODE_NAME_INVALID_CHARACTER, params = arrayOf(envName))
}
if (envDao.isNameExist(dslContext, projectId, envId, envName)) {
throw ErrorCodeException(errorCode = ERROR_NODE_NAME_DUPLICATE, params = arrayOf(envName))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,15 +659,23 @@ class ThirdPartyAgentMgrService @Autowired(required = false) constructor(
}).plus(sharedThridPartyAgentList)
}

private fun getSharedThirdPartyAgentList(projectId: String, envName: String): List<ThirdPartyAgent> {
val sharedEnvRecord = envShareProjectDao.get(dslContext, envName, projectId)
private fun getSharedThirdPartyAgentList(projectId: String, projectEnvName: String): List<ThirdPartyAgent> {
val sharedProjEnv = projectEnvName.split("@") // sharedProjId@poolName
if (sharedProjEnv.size != 2 || sharedProjEnv[0].isNullOrBlank() || sharedProjEnv[1].isNullOrBlank()) {
return emptyList()
}
val sharedProjectId = sharedProjEnv[0]
val sharedEnvName = sharedProjEnv[1]
val sharedEnvRecord = envShareProjectDao.get(dslContext, sharedEnvName, projectId, sharedProjectId)
if (sharedEnvRecord.isEmpty()) {
logger.info("env name not exists, envName: $sharedEnvName, projectId:$projectId, " +
"mainProjectId: $sharedProjectId")
return emptyList()
}
logger.info("sharedEnvRecord size: ${sharedEnvRecord.size}")
val sharedThirdPartyAgents = mutableListOf<ThirdPartyAgent>()
sharedEnvRecord.forEach {
val envRecord = envDao.getByEnvName(dslContext, it.mainProjectId, envName) ?: return@forEach
val envRecord = envDao.getByEnvName(dslContext, it.mainProjectId, sharedEnvName) ?: return@forEach
sharedThirdPartyAgents.addAll(getAgentByEnvId(it.mainProjectId, HashUtil.encodeLongId(envRecord.envId)))
}
logger.info("sharedThirdPartyAgents size: ${sharedThirdPartyAgents.size}")
Expand Down
1 change: 1 addition & 0 deletions support-files/sql/5001_ci_project-init_dml_mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ INSERT IGNORE INTO `T_MESSAGE_CODE_DETAIL` (`ID`, `MESSAGE_CODE`, `MODULE_CODE`
INSERT IGNORE INTO `T_MESSAGE_CODE_DETAIL` (`ID`, `MESSAGE_CODE`, `MODULE_CODE`, `MESSAGE_DETAIL_ZH_CN`, `MESSAGE_DETAIL_EN`) VALUES ('b73e717a920d4b9a9ea257096b6279ba', '2105035', '05', '环境管理:查询构建机网络信息失败: {0}', 'Environment: Querying builder network information failed: {0}');
INSERT IGNORE INTO `T_MESSAGE_CODE_DETAIL` (`ID`, `MESSAGE_CODE`, `MODULE_CODE`, `MESSAGE_DETAIL_ZH_CN`, `MESSAGE_DETAIL_EN`) VALUES ('50355e6b4e164bb08155bd8ba509ebf0', '2105036', '05', '环境管理:共享的项目列表为空', 'Environmental Management: The shared items list is empty');
INSERT IGNORE INTO `T_MESSAGE_CODE_DETAIL` (`ID`, `MESSAGE_CODE`, `MODULE_CODE`, `MESSAGE_DETAIL_ZH_CN`, `MESSAGE_DETAIL_EN`) VALUES ('fb98e8d23a734f7aa6c25064bd7e3923', '2105037', '05', '环境管理:仅构建环境支持共享', 'Environmental Management: Build only support shared environment');
INSERT IGNORE INTO `T_MESSAGE_CODE_DETAIL` (`ID`, `MESSAGE_CODE`, `MODULE_CODE`, `MESSAGE_DETAIL_ZH_CN`, `MESSAGE_DETAIL_EN`) VALUES ('dbd80b0a51774bd288bf5590fbe2216e', '2105038', '05', '环境管理:环境名称包含非法字符@', 'Environmental Management: Environmental name contains illegal characters @ ');
INSERT IGNORE INTO `T_MESSAGE_CODE_DETAIL` (`ID`, `MESSAGE_CODE`, `MODULE_CODE`, `MESSAGE_DETAIL_ZH_CN`, `MESSAGE_DETAIL_EN`) VALUES ('9203931c09b343ba9413df4d353cddaa', '2110001', '10', '监控服务:通知发送失败', 'Monitoring: Notification failed');
INSERT IGNORE INTO `T_MESSAGE_CODE_DETAIL` (`ID`, `MESSAGE_CODE`, `MODULE_CODE`, `MESSAGE_DETAIL_ZH_CN`, `MESSAGE_DETAIL_EN`) VALUES ('9203931c09b343ba9413df4d353cdda2', '2110002', '10', '监控服务:写入influxdb失败', 'Monitoring: Failed to write to influxdb');
INSERT IGNORE INTO `T_MESSAGE_CODE_DETAIL` (`ID`, `MESSAGE_CODE`, `MODULE_CODE`, `MESSAGE_DETAIL_ZH_CN`, `MESSAGE_DETAIL_EN`) VALUES ('3c1e8db76f444a08bec4aa9bdf54ea0e', '2110003', '10', '监控服务:influxdb异常', 'monitoring: bad influxdb');
Expand Down

0 comments on commit 1a72bf5

Please sign in to comment.