update project logs api to v2.0 (#11097)

use audit log api to get project logs

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
Wang Yan 2020-03-17 11:58:43 +08:00 committed by GitHub
parent 2b6fb4abcf
commit ce2257dc22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 0 deletions

View File

@ -615,6 +615,39 @@ paths:
$ref: '#/responses/401'
'403':
$ref: '#/responses/403'
/projects/{project_name}/logs:
get:
summary: Get recent logs of the projects
description: Get recent logs of the projects
tags:
- project
operationId: getLogs
parameters:
- $ref: '#/parameters/projectName'
- $ref: '#/parameters/requestId'
- $ref: '#/parameters/query'
- $ref: '#/parameters/page'
- $ref: '#/parameters/pageSize'
responses:
'200':
description: Success
headers:
X-Total-Count:
description: The total count of auditlogs
type: integer
Link:
description: Link refers to the previous page and next page
type: string
schema:
type: array
items:
$ref: '#/definitions/AuditLog'
'400':
$ref: '#/responses/400'
'401':
$ref: '#/responses/401'
'500':
$ref: '#/responses/500'
parameters:
query:
name: q

View File

@ -33,6 +33,7 @@ func New() http.Handler {
RepositoryAPI: newRepositoryAPI(),
AuditlogAPI: newAuditLogAPI(),
ScanAPI: newScanAPI(),
ProjectAPI: newProjectAPI(),
})
if err != nil {
log.Fatal(err)

View File

@ -0,0 +1,64 @@
package handler
import (
"context"
"github.com/go-openapi/runtime/middleware"
"github.com/goharbor/harbor/src/api/project"
"github.com/goharbor/harbor/src/common/rbac"
"github.com/goharbor/harbor/src/pkg/audit"
"github.com/goharbor/harbor/src/server/v2.0/models"
operation "github.com/goharbor/harbor/src/server/v2.0/restapi/operations/project"
)
func newProjectAPI() *projectAPI {
return &projectAPI{
auditMgr: audit.Mgr,
proCtl: project.Ctl,
}
}
type projectAPI struct {
BaseAPI
auditMgr audit.Manager
proCtl project.Controller
}
func (a *projectAPI) GetLogs(ctx context.Context, params operation.GetLogsParams) middleware.Responder {
if err := a.RequireProjectAccess(ctx, params.ProjectName, rbac.ActionRead, rbac.ResourceLog); err != nil {
return a.SendError(ctx, err)
}
pro, err := a.proCtl.GetByName(ctx, params.ProjectName)
if err != nil {
return a.SendError(ctx, err)
}
query, err := a.BuildQuery(ctx, params.Q, params.Page, params.PageSize)
if err != nil {
return a.SendError(ctx, err)
}
query.Keywords["ProjectID"] = pro.ProjectID
total, err := a.auditMgr.Count(ctx, query)
if err != nil {
return a.SendError(ctx, err)
}
logs, err := a.auditMgr.List(ctx, query)
if err != nil {
return a.SendError(ctx, err)
}
var auditLogs []*models.AuditLog
for _, log := range logs {
auditLogs = append(auditLogs, &models.AuditLog{
ID: log.ID,
Resource: log.Resource,
ResourceType: log.ResourceType,
Username: log.Username,
Operation: log.Operation,
OpTime: log.OpTime.String(),
})
}
return operation.NewGetLogsOK().
WithXTotalCount(total).
WithLink(a.Links(ctx, params.HTTPRequest.URL, total, query.PageNumber, query.PageSize).String()).
WithPayload(auditLogs)
}