2016-05-20 10:56:12 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2016-05-20 11:12:23 +02:00
|
|
|
|
2016-05-20 10:56:12 +02:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/vmware/harbor/dao"
|
|
|
|
"github.com/vmware/harbor/models"
|
|
|
|
"github.com/vmware/harbor/utils/log"
|
|
|
|
)
|
|
|
|
|
2016-05-20 11:12:23 +02:00
|
|
|
//LogAPI handles request api/logs
|
2016-05-20 10:56:12 +02:00
|
|
|
type LogAPI struct {
|
|
|
|
BaseAPI
|
|
|
|
userID int
|
|
|
|
}
|
|
|
|
|
|
|
|
//Prepare validates the URL and the user
|
|
|
|
func (l *LogAPI) Prepare() {
|
|
|
|
l.userID = l.ValidateUser()
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get returns the recent logs according to parameters
|
|
|
|
func (l *LogAPI) Get() {
|
|
|
|
lines, err := l.GetInt("lines")
|
|
|
|
startTime := l.GetString("start_time")
|
|
|
|
endTime := l.GetString("end_time")
|
|
|
|
|
|
|
|
if err != nil {
|
2016-05-20 11:39:49 +02:00
|
|
|
log.Errorf("Get parameters error--lines, err: %v", err)
|
2016-05-20 10:56:12 +02:00
|
|
|
l.CustomAbort(http.StatusBadRequest, "bad request of lines")
|
|
|
|
}
|
|
|
|
if lines <= 0 {
|
|
|
|
lines = 10
|
|
|
|
}
|
|
|
|
if len(startTime) <= 0 {
|
2016-05-20 11:39:49 +02:00
|
|
|
log.Errorf("Get parameters error--startTime: %s", startTime)
|
2016-05-20 10:56:12 +02:00
|
|
|
l.CustomAbort(http.StatusBadRequest, "bad request of startTime")
|
|
|
|
}
|
|
|
|
if len(endTime) <= 0 {
|
2016-05-20 11:39:49 +02:00
|
|
|
log.Errorf("Get parameters error--endTime: %s", endTime)
|
2016-05-20 10:56:12 +02:00
|
|
|
l.CustomAbort(http.StatusBadRequest, "bad request of endTime")
|
|
|
|
}
|
|
|
|
var logList []models.AccessLog
|
|
|
|
logList, err = dao.GetRecentLogs(lines, startTime, endTime)
|
|
|
|
if err != nil {
|
|
|
|
l.CustomAbort(http.StatusInternalServerError, "Internal error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
l.Data["json"] = logList
|
|
|
|
l.ServeJSON()
|
|
|
|
}
|