Skip to content

Commit

Permalink
feat:增加可以根据PROJECT_ID获取数据库分片信息的接口 issue:TencentBlueKing#6186
Browse files Browse the repository at this point in the history
  • Loading branch information
yjieliang committed Feb 28, 2022
1 parent 66ea30f commit e5ff5d3
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ package com.tencent.devops.project.api.op
import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_USER_ID
import com.tencent.devops.common.api.auth.AUTH_HEADER_DEVOPS_USER_ID_DEFAULT_VALUE
import com.tencent.devops.common.api.pojo.Result
import com.tencent.devops.project.pojo.DataBasePiecewiseInfo
import com.tencent.devops.common.web.annotation.BkField
import com.tencent.devops.common.web.constant.BkStyleEnum
import com.tencent.devops.project.pojo.DataSource
Expand All @@ -41,6 +42,7 @@ import javax.ws.rs.Consumes
import javax.ws.rs.DELETE
import javax.ws.rs.GET
import javax.ws.rs.HeaderParam
import javax.ws.rs.QueryParam
import javax.ws.rs.POST
import javax.ws.rs.PUT
import javax.ws.rs.Path
Expand Down Expand Up @@ -107,4 +109,19 @@ interface OPDataSourceResource {
@BkField(patternStyle = BkStyleEnum.ID_STYLE)
id: String
): Result<Boolean>

@ApiOperation("根据PROJECT_ID获取数据库分片信息")
@GET
@Path("/piecewise/get")
fun getDataBasePiecewiseById(
@ApiParam("项目ID", required = true)
@QueryParam("projectId")
projectId: String,
@ApiParam("微服务code", required = true)
@QueryParam("moduleCode")
moduleCode: String,
@ApiParam("集群名称", required = true)
@QueryParam("clusterName")
clusterName: String
): Result<DataBasePiecewiseInfo>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.tencent.devops.project.pojo
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
@ApiModel("数据库分片信息")
data class DataBasePiecewiseInfo(
@ApiModelProperty("项目ID/项目CODE")
val projectId: String,
@ApiModelProperty("集群名称")
val clusterName: String,
@ApiModelProperty("微服务模块名称")
val moduleCode: String,
@ApiModelProperty("数据源名称")
val dataSourceName: String,
@ApiModelProperty("数据源URL")
val dsUrl: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package com.tencent.devops.project.dao

import com.tencent.devops.common.api.util.UUIDUtil
import com.tencent.devops.model.project.tables.TDataSource
import com.tencent.devops.model.project.tables.TShardingRoutingRule
import com.tencent.devops.model.project.tables.records.TDataSourceRecord
import com.tencent.devops.project.pojo.DataSource
import org.jooq.Condition
Expand Down Expand Up @@ -123,4 +124,15 @@ class DataSourceDao {
.execute()
}
}

fun getDataBasePiecewiseById(dslContext: DSLContext, projectId: String, moduleCode: String, clusterName: String): TDataSourceRecord{
val tr = TShardingRoutingRule.T_SHARDING_ROUTING_RULE
val routingRule = dslContext.select(tr.ROUTING_RULE).from(tr).where(tr.ROUTING_NAME.eq(projectId)).fetchOne()!!.get(0) as String
with(TDataSource.T_DATA_SOURCE) {
return dslContext.selectFrom(this)
.where(MODULE_CODE.eq(moduleCode))
.and(DATA_SOURCE_NAME.eq(routingRule))
.and(CLUSTER_NAME.eq(clusterName)).fetchOne()!!
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.tencent.devops.project.api.op.OPDataSourceResource
import com.tencent.devops.project.pojo.DataSource
import com.tencent.devops.project.service.DataSourceService
import org.springframework.beans.factory.annotation.Autowired
import com.tencent.devops.project.pojo.DataBasePiecewiseInfo

@RestResource
class OPDataSourceResourceImpl @Autowired constructor(
Expand All @@ -53,4 +54,12 @@ class OPDataSourceResourceImpl @Autowired constructor(
override fun deleteDataSourceById(userId: String, id: String): Result<Boolean> {
return Result(dataSourceService.deleteDataSource(userId, id))
}

override fun getDataBasePiecewiseById(
projectId: String,
moduleCode: String,
clusterName: String
): Result<DataBasePiecewiseInfo> {
return Result(dataSourceService.getDataBasePiecewiseById(projectId, moduleCode, clusterName))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ interface DataSourceService {
fun updateDataSource(userId: String, id: String, dataSource: DataSource): Boolean

fun getDataSourceById(id: String): DataSource?

fun getDataBasePiecewiseById(
projectId: String,
moduleCode: String,
clusterName: String
): DataBasePiecewiseInfo
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,19 @@ class DataSourceServiceImpl @Autowired constructor(
null
}
}

override fun getDataBasePiecewiseById(
projectId: String,
moduleCode: String,
clusterName: String
): DataBasePiecewiseInfo {
val dataSource = dataSourceDao.getDataBasePiecewiseById(dslContext, projectId, moduleCode, clusterName)
return DataBasePiecewiseInfo(
projectId = projectId,
moduleCode = moduleCode,
clusterName = dataSource.clusterName,
dataSourceName = dataSource.dataSourceName,
dsUrl = dataSource.dsUrl
)
}
}

0 comments on commit e5ff5d3

Please sign in to comment.