Signed-off-by: Ziming Zhang <zziming@vmware.com>
Change-Id: I3f2d3c7f1e32b4983c31c23d9753f04239e3c82f
Signed-off-by: Ziming Zhang <zziming@vmware.com>
This commit is contained in:
Ziming Zhang 2019-07-16 15:56:53 +08:00
parent aa661a14ca
commit 815901ea33
7 changed files with 181 additions and 81 deletions

View File

@ -8,3 +8,51 @@ CREATE TABLE cve_whitelist (
items text NOT NULL, items text NOT NULL,
UNIQUE (project_id) UNIQUE (project_id)
); );
create table retention_policy
(
id serial PRIMARY KEY NOT NULL,
scope_level varchar(20),
scope_reference integer,
trigger_kind varchar(20),
data text,
create_time time,
update_time time
);
create table retention_execution
(
id integer PRIMARY KEY NOT NULL,
policy_id integer,
status varchar(20),
status_text text,
dry boolean,
trigger varchar(20),
total integer,
succeed integer,
failed integer,
in_progress integer,
stopped integer,
start_time time,
end_time time
);
create table retention_task
(
id integer PRIMARY KEY NOT NULL,
execution_id integer,
rule_id integer,
rule_display_text varchar(255),
artifact varchar(255),
timestamp time
);
create table retention_schedule_job
(
id integer PRIMARY KEY NOT NULL,
status varchar(20),
policy_id integer,
job_id integer,
create_time time,
update_time time
);

View File

@ -131,11 +131,11 @@ func (b *BaseAPI) GetIDFromURL() (int64, error) {
return id, nil return id, nil
} }
// GetIDFromURL checks the ID in request URL // GetSpecialIDFromURL checks the ID with special name in request URL
func (b *BaseAPI) GetSpecialIDFromURL(name string) (int64, error) { func (b *BaseAPI) GetSpecialIDFromURL(name string) (int64, error) {
idStr := b.Ctx.Input.Param(":" + name) idStr := b.Ctx.Input.Param(":" + name)
if len(idStr) == 0 { if len(idStr) == 0 {
return 0, errors.New(fmt.Sprintf("invalid %s in URL", name)) return 0, fmt.Errorf("invalid %s in URL", name)
} }
id, err := strconv.ParseInt(idStr, 10, 64) id, err := strconv.ParseInt(idStr, 10, 64)

View File

@ -8,17 +8,19 @@ import (
"time" "time"
) )
// RetentionAPI ...
type RetentionAPI struct { type RetentionAPI struct {
api.BaseController api.BaseController
manager retention.Manager manager retention.Manager
} }
// Prepare validates the user // Prepare validates the user
func (t *RetentionAPI) Prepare() { func (r *RetentionAPI) Prepare() {
t.BaseController.Prepare() r.BaseController.Prepare()
t.manager = retention.NewManager() r.manager = retention.NewManager()
} }
// GetRetention Get Retention
func (r *RetentionAPI) GetRetention() { func (r *RetentionAPI) GetRetention() {
id, err := r.GetIDFromURL() id, err := r.GetIDFromURL()
if err != nil { if err != nil {
@ -34,6 +36,7 @@ func (r *RetentionAPI) GetRetention() {
r.ServeJSON() r.ServeJSON()
} }
// CreateRetention Create Retention
func (r *RetentionAPI) CreateRetention() { func (r *RetentionAPI) CreateRetention() {
p := &policy.Metadata{} p := &policy.Metadata{}
isValid, err := r.DecodeJSONReqAndValidate(p) isValid, err := r.DecodeJSONReqAndValidate(p)
@ -45,6 +48,7 @@ func (r *RetentionAPI) CreateRetention() {
r.manager.CreatePolicy(p) r.manager.CreatePolicy(p)
} }
// UpdateRetention Update Retention
func (r *RetentionAPI) UpdateRetention() { func (r *RetentionAPI) UpdateRetention() {
id, err := r.GetIDFromURL() id, err := r.GetIDFromURL()
if err != nil { if err != nil {
@ -61,6 +65,7 @@ func (r *RetentionAPI) UpdateRetention() {
r.manager.UpdatePolicy(p) r.manager.UpdatePolicy(p)
} }
// DeleteRetention Delete Retention
func (r *RetentionAPI) DeleteRetention() { func (r *RetentionAPI) DeleteRetention() {
id, err := r.GetIDFromURL() id, err := r.GetIDFromURL()
if err != nil { if err != nil {
@ -70,6 +75,7 @@ func (r *RetentionAPI) DeleteRetention() {
r.manager.DeletePolicy(id) r.manager.DeletePolicy(id)
} }
// TriggerRetentionExec Trigger Retention Execution
func (r *RetentionAPI) TriggerRetentionExec() { func (r *RetentionAPI) TriggerRetentionExec() {
id, err := r.GetIDFromURL() id, err := r.GetIDFromURL()
if err != nil { if err != nil {
@ -84,6 +90,7 @@ func (r *RetentionAPI) TriggerRetentionExec() {
r.manager.CreateExecution(exec) r.manager.CreateExecution(exec)
} }
// OperateRetentionExec Operate Retention Execution
func (r *RetentionAPI) OperateRetentionExec() { func (r *RetentionAPI) OperateRetentionExec() {
eid, err := r.GetSpecialIDFromURL("eid") eid, err := r.GetSpecialIDFromURL("eid")
if err != nil { if err != nil {
@ -100,6 +107,7 @@ func (r *RetentionAPI) OperateRetentionExec() {
r.manager.UpdateExecution(nil) r.manager.UpdateExecution(nil)
} }
// GetRetentionExec Get Retention Execution
func (r *RetentionAPI) GetRetentionExec() { func (r *RetentionAPI) GetRetentionExec() {
eid, err := r.GetSpecialIDFromURL("eid") eid, err := r.GetSpecialIDFromURL("eid")
if err != nil { if err != nil {
@ -115,6 +123,7 @@ func (r *RetentionAPI) GetRetentionExec() {
r.ServeJSON() r.ServeJSON()
} }
// ListRetentionExec List Retention Execution
func (r *RetentionAPI) ListRetentionExec() { func (r *RetentionAPI) ListRetentionExec() {
page, size, err := r.GetPaginationParams() page, size, err := r.GetPaginationParams()
if err != nil { if err != nil {
@ -134,6 +143,7 @@ func (r *RetentionAPI) ListRetentionExec() {
r.ServeJSON() r.ServeJSON()
} }
// ListRetentionExecHistory List Retention Execution Histories
func (r *RetentionAPI) ListRetentionExecHistory() { func (r *RetentionAPI) ListRetentionExecHistory() {
eid, err := r.GetSpecialIDFromURL("eid") eid, err := r.GetSpecialIDFromURL("eid")
if err != nil { if err != nil {

View File

@ -14,6 +14,7 @@ func init() {
) )
} }
// RetentionPolicy Retention Policy
type RetentionPolicy struct { type RetentionPolicy struct {
ID int64 `orm:"pk;auto;column(id)" json:"id"` ID int64 `orm:"pk;auto;column(id)" json:"id"`
// 'system', 'project' and 'repository' // 'system', 'project' and 'repository'
@ -26,6 +27,7 @@ type RetentionPolicy struct {
UpdateTime time.Time UpdateTime time.Time
} }
// RetentionExecution Retention Execution
type RetentionExecution struct { type RetentionExecution struct {
ID int64 `orm:"pk;auto;column(id)" json:"id"` ID int64 `orm:"pk;auto;column(id)" json:"id"`
PolicyID int64 PolicyID int64
@ -43,17 +45,17 @@ type RetentionExecution struct {
EndTime time.Time EndTime time.Time
} }
// RetentionTask Retention Task
type RetentionTask struct { type RetentionTask struct {
ID int64 ID int64
ExecutionID int64 ExecutionID int64
// image, chart RuleID int
ResourceType string RuleDisplayText string
Resource string Artifact string
Status string Timestamp time.Time
StartTime time.Time
EndTime time.Time
} }
// RetentionScheduleJob Retention Schedule Job
type RetentionScheduleJob struct { type RetentionScheduleJob struct {
ID int64 ID int64
Status string Status string

View File

@ -6,17 +6,20 @@ import (
"github.com/goharbor/harbor/src/pkg/retention/q" "github.com/goharbor/harbor/src/pkg/retention/q"
) )
// CreatePolicy Create Policy
func CreatePolicy(p *models.RetentionPolicy) (int64, error) { func CreatePolicy(p *models.RetentionPolicy) (int64, error) {
o := dao.GetOrmer() o := dao.GetOrmer()
return o.Insert(p) return o.Insert(p)
} }
// UpdatePolicy Update Policy
func UpdatePolicy(p *models.RetentionPolicy) error { func UpdatePolicy(p *models.RetentionPolicy) error {
o := dao.GetOrmer() o := dao.GetOrmer()
_, err := o.Update(p) _, err := o.Update(p)
return err return err
} }
// DeletePolicy Delete Policy
func DeletePolicy(id int64) error { func DeletePolicy(id int64) error {
o := dao.GetOrmer() o := dao.GetOrmer()
_, err := o.Delete(&models.RetentionPolicy{ _, err := o.Delete(&models.RetentionPolicy{
@ -25,6 +28,7 @@ func DeletePolicy(id int64) error {
return err return err
} }
// GetPolicy Get Policy
func GetPolicy(id int64) (*models.RetentionPolicy, error) { func GetPolicy(id int64) (*models.RetentionPolicy, error) {
o := dao.GetOrmer() o := dao.GetOrmer()
p := &models.RetentionPolicy{ p := &models.RetentionPolicy{
@ -32,22 +36,24 @@ func GetPolicy(id int64) (*models.RetentionPolicy, error) {
} }
if err := o.Read(p); err != nil { if err := o.Read(p); err != nil {
return nil, err return nil, err
} else { }
return p, nil return p, nil
} }
}
// CreateExecution Create Execution
func CreateExecution(e *models.RetentionExecution) (int64, error) { func CreateExecution(e *models.RetentionExecution) (int64, error) {
o := dao.GetOrmer() o := dao.GetOrmer()
return o.Insert(e) return o.Insert(e)
} }
// UpdateExecution Update Execution
func UpdateExecution(e *models.RetentionExecution) error { func UpdateExecution(e *models.RetentionExecution) error {
o := dao.GetOrmer() o := dao.GetOrmer()
_, err := o.Update(e) _, err := o.Update(e)
return err return err
} }
// DeleteExecution Delete Execution
func DeleteExecution(id int64) error { func DeleteExecution(id int64) error {
o := dao.GetOrmer() o := dao.GetOrmer()
_, err := o.Delete(&models.RetentionExecution{ _, err := o.Delete(&models.RetentionExecution{
@ -56,6 +62,7 @@ func DeleteExecution(id int64) error {
return err return err
} }
// GetExecution Get Execution
func GetExecution(id int64) (*models.RetentionExecution, error) { func GetExecution(id int64) (*models.RetentionExecution, error) {
o := dao.GetOrmer() o := dao.GetOrmer()
e := &models.RetentionExecution{ e := &models.RetentionExecution{
@ -63,11 +70,11 @@ func GetExecution(id int64) (*models.RetentionExecution, error) {
} }
if err := o.Read(e); err != nil { if err := o.Read(e); err != nil {
return nil, err return nil, err
} else { }
return e, nil return e, nil
} }
}
// ListExecutions List Executions
func ListExecutions(query *q.Query) ([]*models.RetentionExecution, error) { func ListExecutions(query *q.Query) ([]*models.RetentionExecution, error) {
o := dao.GetOrmer() o := dao.GetOrmer()
qs := o.QueryTable(new(models.RetentionExecution)) qs := o.QueryTable(new(models.RetentionExecution))
@ -76,11 +83,11 @@ func ListExecutions(query *q.Query) ([]*models.RetentionExecution, error) {
_, err := qs.All(&execs) _, err := qs.All(&execs)
if err != nil { if err != nil {
return nil, err return nil, err
} else { }
return execs, nil return execs, nil
} }
}
// ListExecHistories List Execution Histories
func ListExecHistories(executionID int64, query *q.Query) ([]*models.RetentionTask, error) { func ListExecHistories(executionID int64, query *q.Query) ([]*models.RetentionTask, error) {
o := dao.GetOrmer() o := dao.GetOrmer()
qs := o.QueryTable(new(models.RetentionTask)) qs := o.QueryTable(new(models.RetentionTask))
@ -90,7 +97,12 @@ func ListExecHistories(executionID int64, query *q.Query) ([]*models.RetentionTa
_, err := qs.All(&tasks) _, err := qs.All(&tasks)
if err != nil { if err != nil {
return nil, err return nil, err
} else { }
return tasks, nil return tasks, nil
} }
// AppendExecHistory Append Execution History
func AppendExecHistory(t *models.RetentionTask) (int64, error) {
o := dao.GetOrmer()
return o.Insert(t)
} }

View File

@ -49,9 +49,11 @@ type Manager interface {
ListHistories(executionID int64, query *q.Query) ([]*History, error) ListHistories(executionID int64, query *q.Query) ([]*History, error)
} }
// DefaultManager ...
type DefaultManager struct { type DefaultManager struct {
} }
// CreatePolicy Create Policy
func (d *DefaultManager) CreatePolicy(p *policy.Metadata) (int64, error) { func (d *DefaultManager) CreatePolicy(p *policy.Metadata) (int64, error) {
var p1 *models.RetentionPolicy var p1 *models.RetentionPolicy
p1.ScopeLevel = p.Scope.Level p1.ScopeLevel = p.Scope.Level
@ -63,6 +65,7 @@ func (d *DefaultManager) CreatePolicy(p *policy.Metadata) (int64, error) {
return dao.CreatePolicy(p1) return dao.CreatePolicy(p1)
} }
// UpdatePolicy Update Policy
func (d *DefaultManager) UpdatePolicy(p *policy.Metadata) error { func (d *DefaultManager) UpdatePolicy(p *policy.Metadata) error {
var p1 *models.RetentionPolicy var p1 *models.RetentionPolicy
p1.ID = p.ID p1.ID = p.ID
@ -76,23 +79,25 @@ func (d *DefaultManager) UpdatePolicy(p *policy.Metadata) error {
return dao.UpdatePolicy(p1) return dao.UpdatePolicy(p1)
} }
// DeletePolicy Delete Policy
func (d *DefaultManager) DeletePolicy(id int64) error { func (d *DefaultManager) DeletePolicy(id int64) error {
return dao.DeletePolicy(id) return dao.DeletePolicy(id)
} }
// GetPolicy Get Policy
func (d *DefaultManager) GetPolicy(id int64) (*policy.Metadata, error) { func (d *DefaultManager) GetPolicy(id int64) (*policy.Metadata, error) {
if p1,err:=dao.GetPolicy(id);err!=nil{ p1, err := dao.GetPolicy(id)
if err != nil {
return nil, err return nil, err
}else{ }
var p *policy.Metadata var p *policy.Metadata
if err = json.Unmarshal([]byte(p1.Data), p); err != nil { if err = json.Unmarshal([]byte(p1.Data), p); err != nil {
return nil, err return nil, err
}else{ }
return p, nil return p, nil
} }
}
}
// CreateExecution Create Execution
func (d *DefaultManager) CreateExecution(execution *Execution) (int64, error) { func (d *DefaultManager) CreateExecution(execution *Execution) (int64, error) {
var exec *models.RetentionExecution var exec *models.RetentionExecution
exec.PolicyID = execution.PolicyID exec.PolicyID = execution.PolicyID
@ -101,6 +106,7 @@ func (d *DefaultManager) CreateExecution(execution *Execution) (int64, error) {
return dao.CreateExecution(exec) return dao.CreateExecution(exec)
} }
// UpdateExecution Update Execution
func (d *DefaultManager) UpdateExecution(execution *Execution) error { func (d *DefaultManager) UpdateExecution(execution *Execution) error {
var exec *models.RetentionExecution var exec *models.RetentionExecution
exec.ID = execution.ID exec.ID = execution.ID
@ -110,50 +116,72 @@ func (d *DefaultManager) UpdateExecution(execution *Execution) error {
return dao.UpdateExecution(exec) return dao.UpdateExecution(exec)
} }
// ListExecutions List Executions
func (d *DefaultManager) ListExecutions(query *q.Query) ([]*Execution, error) { func (d *DefaultManager) ListExecutions(query *q.Query) ([]*Execution, error) {
return []*Execution{ execs, err := dao.ListExecutions(query)
{ if err != nil {
ID: 1, return nil, err
PolicyID: 1, }
StartTime: time.Now().Add(-time.Minute), var execs1 []*Execution
EndTime: time.Now(), for _, e := range execs {
Status: "Success", var e1 *Execution
}, e1.ID = e.ID
{ e1.PolicyID = e.PolicyID
ID: 2, e1.Status = e.Status
PolicyID: 1, e1.StartTime = e.StartTime
StartTime: time.Now().Add(-time.Minute), e1.EndTime = e.EndTime
EndTime: time.Now(), execs1 = append(execs1, e1)
Status: "Failed", }
}, return execs1, nil
{
ID: 3,
PolicyID: 1,
StartTime: time.Now().Add(-time.Minute),
EndTime: time.Now(),
Status: "Running",
},
}, nil
} }
// GetExecution Get Execution
func (d *DefaultManager) GetExecution(eid int64) (*Execution, error) { func (d *DefaultManager) GetExecution(eid int64) (*Execution, error) {
return &Execution{ e, err := dao.GetExecution(eid)
ID: 1, if err != nil {
PolicyID: 1, return nil, err
StartTime: time.Now().Add(-time.Minute), }
EndTime: time.Now(), var e1 *Execution
Status: "Success", e1.ID = e.ID
}, nil e1.PolicyID = e.PolicyID
e1.Status = e.Status
e1.StartTime = e.StartTime
e1.EndTime = e.EndTime
return e1, nil
} }
// ListHistories List Histories
func (d *DefaultManager) ListHistories(executionID int64, query *q.Query) ([]*History, error) { func (d *DefaultManager) ListHistories(executionID int64, query *q.Query) ([]*History, error) {
panic("implement me") his, err := dao.ListExecHistories(executionID, query)
if err != nil {
return nil, err
}
var his1 []*History
for _, h := range his {
var h1 *History
h1.ExecutionID = h.ExecutionID
h1.Artifact = h.Artifact
h1.Rule.ID = h.RuleID
h1.Rule.DisplayText = h.RuleDisplayText
h1.Timestamp = h.Timestamp
his1 = append(his1, h1)
}
return his1, nil
} }
func (d *DefaultManager) AppendHistory(history *History) error { // AppendHistory Append History
panic("implement me") func (d *DefaultManager) AppendHistory(h *History) error {
var h1 *models.RetentionTask
h1.ExecutionID = h.ExecutionID
h1.Artifact = h.Artifact
h1.RuleID = h.Rule.ID
h1.RuleDisplayText = h.Rule.DisplayText
h1.Timestamp = h.Timestamp
_, err := dao.AppendExecHistory(h1)
return err
} }
// NewManager ...
func NewManager() Manager { func NewManager() Manager {
return &DefaultManager{} return &DefaultManager{}
} }

View File

@ -35,7 +35,7 @@ type TaskSubmitResult struct {
// History of retention // History of retention
type History struct { type History struct {
ExecutionID string `json:"execution_id"` ExecutionID int64 `json:"execution_id"`
Rule struct { Rule struct {
ID int `json:"id"` ID int `json:"id"`
DisplayText string `json:"display_text"` DisplayText string `json:"display_text"`