Skip to content

Commit

Permalink
feat: 新增日志熔断后下载全量日志的OpenAPI TencentBlueKing#5145
Browse files Browse the repository at this point in the history
  • Loading branch information
royalhuang committed Sep 15, 2021
1 parent b50dfb6 commit 71d61ce
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.tencent.devops.artifactory.api.user
package com.tencent.devops.artifactory.api.service

import com.tencent.devops.artifactory.pojo.Url
import com.tencent.devops.common.api.auth.AUTH_HEADER_USER_ID
Expand All @@ -45,11 +45,11 @@ import javax.ws.rs.core.MediaType
@Path("/user/artifactories/log")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
interface UserLogFileResource {
interface ServiceLogFileResource {

@ApiOperation("下载日志")
@GET
@Path("plugin/{projectId}/{pipelineId}/{buildId}/{elementId}/{executeCount}")
@Path("/{projectId}/{pipelineId}/{buildId}/{elementId}/{executeCount}")
fun getPluginLogUrl(
@ApiParam("userId", required = true)
@HeaderParam(AUTH_HEADER_USER_ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ interface ServiceLogResource {
@GET
@Path("/{projectId}/{pipelineId}/{buildId}/")
fun getInitLogs(
@ApiParam(value = "用户ID", required = true, defaultValue = AUTH_HEADER_USER_ID_DEFAULT_VALUE)
@HeaderParam(AUTH_HEADER_USER_ID)
userId: String,
@ApiParam("项目ID", required = true)
@PathParam("projectId")
projectId: String,
Expand Down Expand Up @@ -87,6 +90,9 @@ interface ServiceLogResource {
@GET
@Path("/{projectId}/{pipelineId}/{buildId}/more")
fun getMoreLogs(
@ApiParam(value = "用户ID", required = true, defaultValue = AUTH_HEADER_USER_ID_DEFAULT_VALUE)
@HeaderParam(AUTH_HEADER_USER_ID)
userId: String,
@ApiParam("项目ID", required = true)
@PathParam("projectId")
projectId: String,
Expand Down Expand Up @@ -126,6 +132,9 @@ interface ServiceLogResource {
@GET
@Path("/{projectId}/{pipelineId}/{buildId}/after")
fun getAfterLogs(
@ApiParam(value = "用户ID", required = true, defaultValue = AUTH_HEADER_USER_ID_DEFAULT_VALUE)
@HeaderParam(AUTH_HEADER_USER_ID)
userId: String,
@ApiParam("项目ID", required = true)
@PathParam("projectId")
projectId: String,
Expand Down Expand Up @@ -157,6 +166,9 @@ interface ServiceLogResource {
@Path("/{projectId}/{pipelineId}/{buildId}/download")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
fun downloadLogs(
@ApiParam(value = "用户ID", required = true, defaultValue = AUTH_HEADER_USER_ID_DEFAULT_VALUE)
@HeaderParam(AUTH_HEADER_USER_ID)
userId: String,
@ApiParam("项目ID", required = true)
@PathParam("projectId")
projectId: String,
Expand All @@ -181,6 +193,9 @@ interface ServiceLogResource {
@GET
@Path("/{projectId}/{pipelineId}/{buildId}/mode")
fun getLogMode(
@ApiParam(value = "用户ID", required = true, defaultValue = AUTH_HEADER_USER_ID_DEFAULT_VALUE)
@HeaderParam(AUTH_HEADER_USER_ID)
userId: String,
@ApiParam("项目ID", required = true)
@PathParam("projectId")
projectId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,13 @@

package com.tencent.devops.log.resources

import com.tencent.devops.common.api.exception.ParamBlankException
import com.tencent.devops.common.api.exception.PermissionForbiddenException
import com.tencent.devops.common.api.pojo.Result
import com.tencent.devops.common.auth.api.AuthPermission
import com.tencent.devops.common.auth.code.PipelineAuthServiceCode
import com.tencent.devops.common.log.pojo.EndPageQueryLogs
import com.tencent.devops.common.log.pojo.PageQueryLogs
import com.tencent.devops.common.log.pojo.QueryLogs
import com.tencent.devops.common.web.RestResource
import com.tencent.devops.log.api.AppLogResource
import com.tencent.devops.log.service.BuildLogQueryService
import com.tencent.devops.log.service.LogPermissionService
import org.springframework.beans.factory.annotation.Autowired
import javax.ws.rs.core.Response

Expand All @@ -48,9 +43,7 @@ import javax.ws.rs.core.Response
*/
@RestResource
class AppLogResourceImpl @Autowired constructor(
private val buildLogQuery: BuildLogQueryService,
private val authPermissionApi: LogPermissionService,
private val pipelineAuthServiceCode: PipelineAuthServiceCode
private val buildLogQueryService: BuildLogQueryService
) : AppLogResource {

companion object {
Expand All @@ -70,8 +63,7 @@ class AppLogResourceImpl @Autowired constructor(
page: Int?,
pageSize: Int?
): Result<PageQueryLogs> {
validateAuth(userId, projectId, pipelineId, buildId)
return buildLogQuery.getInitLogsPage(
return buildLogQueryService.getInitLogsPage(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
Expand Down Expand Up @@ -101,8 +93,8 @@ class AppLogResourceImpl @Autowired constructor(
jobId: String?,
executeCount: Int?
): Result<QueryLogs> {
validateAuth(userId, projectId, pipelineId, buildId)
return buildLogQuery.getMoreLogs(
return buildLogQueryService.getMoreLogs(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
Expand Down Expand Up @@ -130,8 +122,8 @@ class AppLogResourceImpl @Autowired constructor(
jobId: String?,
executeCount: Int?
): Result<QueryLogs> {
validateAuth(userId, projectId, pipelineId, buildId)
return buildLogQuery.getAfterLogs(
return buildLogQueryService.getAfterLogs(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
Expand All @@ -157,8 +149,8 @@ class AppLogResourceImpl @Autowired constructor(
jobId: String?,
executeCount: Int?
): Result<QueryLogs> {
validateAuth(userId, projectId, pipelineId, buildId)
return buildLogQuery.getBeforeLogs(
return buildLogQueryService.getBeforeLogs(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
Expand All @@ -182,8 +174,8 @@ class AppLogResourceImpl @Autowired constructor(
jobId: String?,
executeCount: Int?
): Response {
validateAuth(userId, projectId, pipelineId, buildId)
return buildLogQuery.downloadLogs(
return buildLogQueryService.downloadLogs(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
Expand All @@ -207,8 +199,7 @@ class AppLogResourceImpl @Autowired constructor(
jobId: String?,
executeCount: Int?
): Result<EndPageQueryLogs> {
validateAuth(userId, projectId, pipelineId, buildId)
return buildLogQuery.getEndLogsPage(
return buildLogQueryService.getEndLogsPage(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
Expand All @@ -234,8 +225,7 @@ class AppLogResourceImpl @Autowired constructor(
jobId: String?,
executeCount: Int?
): Result<QueryLogs> {
validateAuth(userId, projectId, pipelineId, buildId)
return buildLogQuery.getBottomLogs(
return buildLogQueryService.getBottomLogs(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
Expand All @@ -248,29 +238,4 @@ class AppLogResourceImpl @Autowired constructor(
executeCount = executeCount
)
}

private fun validateAuth(userId: String, projectId: String, pipelineId: String, buildId: String) {
if (userId.isBlank()) {
throw ParamBlankException("Invalid userId")
}
if (projectId.isBlank()) {
throw ParamBlankException("Invalid projectId")
}
if (pipelineId.isBlank()) {
throw ParamBlankException("Invalid pipelineId")
}
if (buildId.isBlank()) {
throw ParamBlankException("Invalid buildId")
}

if (!authPermissionApi.verifyUserLogPermission(
userId = userId,
pipelineId = pipelineId,
projectCode = projectId,
permission = AuthPermission.VIEW
)
) {
throw PermissionForbiddenException("用户($userId)无权限在工程($projectId)下查看流水线")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ServiceLogResourceImpl @Autowired constructor(
) : ServiceLogResource {

override fun getInitLogs(
userId: String,
projectId: String,
pipelineId: String,
buildId: String,
Expand All @@ -55,6 +56,7 @@ class ServiceLogResourceImpl @Autowired constructor(
executeCount: Int?
): Result<QueryLogs> {
return buildLogQueryService.getInitLogs(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
Expand All @@ -66,6 +68,7 @@ class ServiceLogResourceImpl @Autowired constructor(
}

override fun getMoreLogs(
userId: String,
projectId: String,
pipelineId: String,
buildId: String,
Expand All @@ -79,6 +82,7 @@ class ServiceLogResourceImpl @Autowired constructor(
executeCount: Int?
): Result<QueryLogs> {
return buildLogQueryService.getMoreLogs(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
Expand All @@ -94,6 +98,7 @@ class ServiceLogResourceImpl @Autowired constructor(
}

override fun getAfterLogs(
userId: String,
projectId: String,
pipelineId: String,
buildId: String,
Expand All @@ -104,6 +109,7 @@ class ServiceLogResourceImpl @Autowired constructor(
executeCount: Int?
): Result<QueryLogs> {
return buildLogQueryService.getAfterLogs(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
Expand All @@ -116,6 +122,7 @@ class ServiceLogResourceImpl @Autowired constructor(
}

override fun downloadLogs(
userId: String,
projectId: String,
pipelineId: String,
buildId: String,
Expand All @@ -124,6 +131,7 @@ class ServiceLogResourceImpl @Autowired constructor(
executeCount: Int?
): Response {
return buildLogQueryService.downloadLogs(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
Expand All @@ -135,13 +143,15 @@ class ServiceLogResourceImpl @Autowired constructor(
}

override fun getLogMode(
userId: String,
projectId: String,
pipelineId: String,
buildId: String,
tag: String,
executeCount: Int?,
): Result<QueryLogStatus> {
return buildLogQueryService.getLogMode(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ import javax.ws.rs.core.Response
*/
@RestResource
class UserLogResourceImpl @Autowired constructor(
private val buildLogQuery: BuildLogQueryService,
private val logPermissionService: LogPermissionService
private val buildLogQueryService: BuildLogQueryService
) : UserLogResource {

override fun getInitLogs(
Expand All @@ -61,8 +60,8 @@ class UserLogResourceImpl @Autowired constructor(
jobId: String?,
executeCount: Int?
): Result<QueryLogs> {
validateAuth(userId, projectId, pipelineId, buildId, AuthPermission.VIEW)
return buildLogQuery.getInitLogs(
return buildLogQueryService.getInitLogs(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
Expand All @@ -89,8 +88,8 @@ class UserLogResourceImpl @Autowired constructor(
jobId: String?,
executeCount: Int?
): Result<QueryLogs> {
validateAuth(userId, projectId, pipelineId, buildId, AuthPermission.VIEW)
return buildLogQuery.getMoreLogs(
return buildLogQueryService.getMoreLogs(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
Expand Down Expand Up @@ -118,8 +117,8 @@ class UserLogResourceImpl @Autowired constructor(
jobId: String?,
executeCount: Int?
): Result<QueryLogs> {
validateAuth(userId, projectId, pipelineId, buildId, AuthPermission.VIEW)
return buildLogQuery.getAfterLogs(
return buildLogQueryService.getAfterLogs(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
Expand All @@ -143,8 +142,8 @@ class UserLogResourceImpl @Autowired constructor(
executeCount: Int?,
fileName: String?
): Response {
validateAuth(userId, projectId, pipelineId, buildId, AuthPermission.DOWNLOAD)
return buildLogQuery.downloadLogs(
return buildLogQueryService.downloadLogs(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
Expand All @@ -164,43 +163,13 @@ class UserLogResourceImpl @Autowired constructor(
tag: String,
executeCount: Int?
): Result<QueryLogStatus> {
validateAuth(userId, projectId, pipelineId, buildId, AuthPermission.VIEW)
return buildLogQuery.getLogMode(
return buildLogQueryService.getLogMode(
userId = userId,
projectId = projectId,
pipelineId = pipelineId,
buildId = buildId,
tag = tag,
executeCount = executeCount
)
}

private fun validateAuth(
userId: String,
projectId: String,
pipelineId: String,
buildId: String,
permission: AuthPermission
) {
if (userId.isBlank()) {
throw ParamBlankException("Invalid userId")
}
if (projectId.isBlank()) {
throw ParamBlankException("Invalid projectId")
}
if (pipelineId.isBlank()) {
throw ParamBlankException("Invalid pipelineId")
}
if (buildId.isBlank()) {
throw ParamBlankException("Invalid buildId")
}
if (!logPermissionService.verifyUserLogPermission(
userId = userId,
pipelineId = pipelineId,
projectCode = projectId,
permission = permission
)
) {
throw PermissionForbiddenException("用户($userId)无权限在工程($projectId)下${permission.alias}流水线")
}
}
}
Loading

0 comments on commit 71d61ce

Please sign in to comment.