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,
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
}
// 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) {
idStr := b.Ctx.Input.Param(":" + name)
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)

View File

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

View File

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

View File

@ -6,17 +6,20 @@ import (
"github.com/goharbor/harbor/src/pkg/retention/q"
)
// CreatePolicy Create Policy
func CreatePolicy(p *models.RetentionPolicy) (int64, error) {
o := dao.GetOrmer()
return o.Insert(p)
}
// UpdatePolicy Update Policy
func UpdatePolicy(p *models.RetentionPolicy) error {
o := dao.GetOrmer()
_, err := o.Update(p)
return err
}
// DeletePolicy Delete Policy
func DeletePolicy(id int64) error {
o := dao.GetOrmer()
_, err := o.Delete(&models.RetentionPolicy{
@ -25,6 +28,7 @@ func DeletePolicy(id int64) error {
return err
}
// GetPolicy Get Policy
func GetPolicy(id int64) (*models.RetentionPolicy, error) {
o := dao.GetOrmer()
p := &models.RetentionPolicy{
@ -32,22 +36,24 @@ func GetPolicy(id int64) (*models.RetentionPolicy, error) {
}
if err := o.Read(p); err != nil {
return nil, err
} else {
}
return p, nil
}
}
// CreateExecution Create Execution
func CreateExecution(e *models.RetentionExecution) (int64, error) {
o := dao.GetOrmer()
return o.Insert(e)
}
// UpdateExecution Update Execution
func UpdateExecution(e *models.RetentionExecution) error {
o := dao.GetOrmer()
_, err := o.Update(e)
return err
}
// DeleteExecution Delete Execution
func DeleteExecution(id int64) error {
o := dao.GetOrmer()
_, err := o.Delete(&models.RetentionExecution{
@ -56,6 +62,7 @@ func DeleteExecution(id int64) error {
return err
}
// GetExecution Get Execution
func GetExecution(id int64) (*models.RetentionExecution, error) {
o := dao.GetOrmer()
e := &models.RetentionExecution{
@ -63,11 +70,11 @@ func GetExecution(id int64) (*models.RetentionExecution, error) {
}
if err := o.Read(e); err != nil {
return nil, err
} else {
}
return e, nil
}
}
// ListExecutions List Executions
func ListExecutions(query *q.Query) ([]*models.RetentionExecution, error) {
o := dao.GetOrmer()
qs := o.QueryTable(new(models.RetentionExecution))
@ -76,11 +83,11 @@ func ListExecutions(query *q.Query) ([]*models.RetentionExecution, error) {
_, err := qs.All(&execs)
if err != nil {
return nil, err
} else {
}
return execs, nil
}
}
// ListExecHistories List Execution Histories
func ListExecHistories(executionID int64, query *q.Query) ([]*models.RetentionTask, error) {
o := dao.GetOrmer()
qs := o.QueryTable(new(models.RetentionTask))
@ -90,7 +97,12 @@ func ListExecHistories(executionID int64, query *q.Query) ([]*models.RetentionTa
_, err := qs.All(&tasks)
if err != nil {
return nil, err
} else {
}
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)
}
// DefaultManager ...
type DefaultManager struct {
}
// CreatePolicy Create Policy
func (d *DefaultManager) CreatePolicy(p *policy.Metadata) (int64, error) {
var p1 *models.RetentionPolicy
p1.ScopeLevel = p.Scope.Level
@ -63,6 +65,7 @@ func (d *DefaultManager) CreatePolicy(p *policy.Metadata) (int64, error) {
return dao.CreatePolicy(p1)
}
// UpdatePolicy Update Policy
func (d *DefaultManager) UpdatePolicy(p *policy.Metadata) error {
var p1 *models.RetentionPolicy
p1.ID = p.ID
@ -76,23 +79,25 @@ func (d *DefaultManager) UpdatePolicy(p *policy.Metadata) error {
return dao.UpdatePolicy(p1)
}
// DeletePolicy Delete Policy
func (d *DefaultManager) DeletePolicy(id int64) error {
return dao.DeletePolicy(id)
}
// GetPolicy Get Policy
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
}else{
}
var p *policy.Metadata
if err = json.Unmarshal([]byte(p1.Data), p); err != nil {
return nil, err
}else{
}
return p, nil
}
}
}
// CreateExecution Create Execution
func (d *DefaultManager) CreateExecution(execution *Execution) (int64, error) {
var exec *models.RetentionExecution
exec.PolicyID = execution.PolicyID
@ -101,6 +106,7 @@ func (d *DefaultManager) CreateExecution(execution *Execution) (int64, error) {
return dao.CreateExecution(exec)
}
// UpdateExecution Update Execution
func (d *DefaultManager) UpdateExecution(execution *Execution) error {
var exec *models.RetentionExecution
exec.ID = execution.ID
@ -110,50 +116,72 @@ func (d *DefaultManager) UpdateExecution(execution *Execution) error {
return dao.UpdateExecution(exec)
}
// ListExecutions List Executions
func (d *DefaultManager) ListExecutions(query *q.Query) ([]*Execution, error) {
return []*Execution{
{
ID: 1,
PolicyID: 1,
StartTime: time.Now().Add(-time.Minute),
EndTime: time.Now(),
Status: "Success",
},
{
ID: 2,
PolicyID: 1,
StartTime: time.Now().Add(-time.Minute),
EndTime: time.Now(),
Status: "Failed",
},
{
ID: 3,
PolicyID: 1,
StartTime: time.Now().Add(-time.Minute),
EndTime: time.Now(),
Status: "Running",
},
}, nil
execs, err := dao.ListExecutions(query)
if err != nil {
return nil, err
}
var execs1 []*Execution
for _, e := range execs {
var e1 *Execution
e1.ID = e.ID
e1.PolicyID = e.PolicyID
e1.Status = e.Status
e1.StartTime = e.StartTime
e1.EndTime = e.EndTime
execs1 = append(execs1, e1)
}
return execs1, nil
}
// GetExecution Get Execution
func (d *DefaultManager) GetExecution(eid int64) (*Execution, error) {
return &Execution{
ID: 1,
PolicyID: 1,
StartTime: time.Now().Add(-time.Minute),
EndTime: time.Now(),
Status: "Success",
}, nil
e, err := dao.GetExecution(eid)
if err != nil {
return nil, err
}
var e1 *Execution
e1.ID = e.ID
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) {
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 {
panic("implement me")
// AppendHistory Append History
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 {
return &DefaultManager{}
}

View File

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