mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-22 16:48:30 +01:00
Update gc api to fix issues found by UI implemention (#5920)
This commit is to update gc api to fix issues found by UI implemention: 1, Return json format of gc schedule 2, Unify capital and small letter 3,Return gc records by desc Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
parent
30e71a25de
commit
29ca31cf6c
@ -90,7 +90,7 @@ func SetAdminJobUUID(id int64, uuid string) error {
|
||||
// GetTop10AdminJobs ...
|
||||
func GetTop10AdminJobs() ([]*models.AdminJob, error) {
|
||||
sql := `select * from admin_job
|
||||
where deleted = false order by update_time limit 10`
|
||||
where deleted = false order by update_time desc limit 10`
|
||||
jobs := []*models.AdminJob{}
|
||||
_, err := GetOrmer().Raw(sql).QueryRows(&jobs)
|
||||
return jobs, err
|
||||
|
@ -24,6 +24,7 @@ import (
|
||||
"github.com/goharbor/harbor/src/common/utils"
|
||||
"github.com/goharbor/harbor/src/common/utils/log"
|
||||
"github.com/goharbor/harbor/src/ui/config"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -50,9 +51,22 @@ type ScheduleParam struct {
|
||||
// Daily, Weekly, Manual, None
|
||||
Type string `json:"type"`
|
||||
// Optional, only used when type is 'weekly'
|
||||
Weekday int8 `json:"Weekday"`
|
||||
Weekday int8 `json:"weekday"`
|
||||
// The time offset with the UTC 00:00 in seconds
|
||||
Offtime int64 `json:"Offtime"`
|
||||
Offtime int64 `json:"offtime"`
|
||||
}
|
||||
|
||||
// GCRep holds the response of query gc
|
||||
type GCRep struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"job_name"`
|
||||
Kind string `json:"job_kind"`
|
||||
Schedule *ScheduleParam `json:"schedule"`
|
||||
Status string `json:"job_status"`
|
||||
UUID string `json:"-"`
|
||||
Deleted bool `json:"deleted"`
|
||||
CreationTime time.Time `json:"creation_time"`
|
||||
UpdateTime time.Time `json:"update_time"`
|
||||
}
|
||||
|
||||
// Valid validates the gc request
|
||||
|
@ -20,6 +20,7 @@ import (
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"encoding/json"
|
||||
"github.com/goharbor/harbor/src/common/dao"
|
||||
common_http "github.com/goharbor/harbor/src/common/http"
|
||||
common_job "github.com/goharbor/harbor/src/common/job"
|
||||
@ -109,11 +110,22 @@ func (gc *GCAPI) GetGC() {
|
||||
jobs, err := dao.GetAdminJobs(&common_models.AdminJobQuery{
|
||||
ID: id,
|
||||
})
|
||||
|
||||
gcreps := []*models.GCRep{}
|
||||
for _, job := range jobs {
|
||||
gcrep, err := convertToGCRep(job)
|
||||
if err != nil {
|
||||
gc.HandleInternalServerError(fmt.Sprintf("failed to convert gc response: %v", err))
|
||||
return
|
||||
}
|
||||
gcreps = append(gcreps, &gcrep)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
gc.HandleInternalServerError(fmt.Sprintf("failed to get admin jobs: %v", err))
|
||||
return
|
||||
}
|
||||
gc.Data["json"] = jobs
|
||||
gc.Data["json"] = gcreps
|
||||
gc.ServeJSON()
|
||||
}
|
||||
|
||||
@ -124,7 +136,16 @@ func (gc *GCAPI) List() {
|
||||
gc.HandleInternalServerError(fmt.Sprintf("failed to get admin jobs: %v", err))
|
||||
return
|
||||
}
|
||||
gc.Data["json"] = jobs
|
||||
gcreps := []*models.GCRep{}
|
||||
for _, job := range jobs {
|
||||
gcrep, err := convertToGCRep(job)
|
||||
if err != nil {
|
||||
gc.HandleInternalServerError(fmt.Sprintf("failed to convert gc response: %v", err))
|
||||
return
|
||||
}
|
||||
gcreps = append(gcreps, &gcrep)
|
||||
}
|
||||
gc.Data["json"] = gcreps
|
||||
gc.ServeJSON()
|
||||
}
|
||||
|
||||
@ -142,7 +163,16 @@ func (gc *GCAPI) Get() {
|
||||
gc.HandleInternalServerError("Get more than one GC scheduled job, make sure there has only one.")
|
||||
return
|
||||
}
|
||||
gc.Data["json"] = jobs
|
||||
gcreps := []*models.GCRep{}
|
||||
for _, job := range jobs {
|
||||
gcrep, err := convertToGCRep(job)
|
||||
if err != nil {
|
||||
gc.HandleInternalServerError(fmt.Sprintf("failed to convert gc response: %v", err))
|
||||
return
|
||||
}
|
||||
gcreps = append(gcreps, &gcrep)
|
||||
}
|
||||
gc.Data["json"] = gcreps
|
||||
gc.ServeJSON()
|
||||
}
|
||||
|
||||
@ -230,3 +260,27 @@ func (gc *GCAPI) submitJob(gr *models.GCReq) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func convertToGCRep(job *common_models.AdminJob) (models.GCRep, error) {
|
||||
if job == nil {
|
||||
return models.GCRep{}, nil
|
||||
}
|
||||
|
||||
gcrep := models.GCRep{
|
||||
ID: job.ID,
|
||||
Name: job.Name,
|
||||
Kind: job.Kind,
|
||||
Status: job.Status,
|
||||
Deleted: job.Deleted,
|
||||
CreationTime: job.CreationTime,
|
||||
UpdateTime: job.UpdateTime,
|
||||
}
|
||||
if len(job.Cron) > 0 {
|
||||
schedule := &models.ScheduleParam{}
|
||||
if err := json.Unmarshal([]byte(job.Cron), &schedule); err != nil {
|
||||
return models.GCRep{}, err
|
||||
}
|
||||
gcrep.Schedule = schedule
|
||||
}
|
||||
return gcrep, nil
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package api
|
||||
import (
|
||||
"testing"
|
||||
|
||||
common_models "github.com/goharbor/harbor/src/common/models"
|
||||
api_modes "github.com/goharbor/harbor/src/ui/api/models"
|
||||
"github.com/goharbor/harbor/tests/apitests/apilib"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@ -37,3 +39,42 @@ func TestAdminJobGet(t *testing.T) {
|
||||
assert.Equal(200, code, "Get adminjob status should be 200")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertToGCRep(t *testing.T) {
|
||||
cases := []struct {
|
||||
input *common_models.AdminJob
|
||||
expected api_modes.GCRep
|
||||
}{
|
||||
{
|
||||
input: nil,
|
||||
expected: api_modes.GCRep{},
|
||||
},
|
||||
{
|
||||
input: &common_models.AdminJob{
|
||||
ID: 1,
|
||||
Name: "IMAGE_GC",
|
||||
Kind: "Generic",
|
||||
Cron: "{\"Type\":\"Manual\",\"Weekday\":0,\"Offtime\":0}",
|
||||
Status: "pending",
|
||||
Deleted: false,
|
||||
},
|
||||
expected: api_modes.GCRep{
|
||||
ID: 1,
|
||||
Name: "IMAGE_GC",
|
||||
Kind: "Generic",
|
||||
Schedule: &api_modes.ScheduleParam{
|
||||
Type: "Manual",
|
||||
Weekday: 0,
|
||||
Offtime: 0,
|
||||
},
|
||||
Status: "pending",
|
||||
Deleted: false,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
actual, _ := convertToGCRep(c.input)
|
||||
assert.EqualValues(t, c.expected, actual)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user