mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-22 23:51:27 +01:00
refactor retention dao
Signed-off-by: Ziming Zhang <zziming@vmware.com>
This commit is contained in:
parent
e4678dc7db
commit
c775e989df
@ -96,12 +96,12 @@ type TriggerParam struct {
|
||||
|
||||
// GetRetention Get Retention
|
||||
func (r *defaultController) GetRetention(ctx context.Context, id int64) (*policy.Metadata, error) {
|
||||
return r.manager.GetPolicy(id)
|
||||
return r.manager.GetPolicy(ctx, id)
|
||||
}
|
||||
|
||||
// CreateRetention Create Retention
|
||||
func (r *defaultController) CreateRetention(ctx context.Context, p *policy.Metadata) (int64, error) {
|
||||
id, err := r.manager.CreatePolicy(p)
|
||||
id, err := r.manager.CreatePolicy(ctx, p)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@ -124,7 +124,7 @@ func (r *defaultController) CreateRetention(ctx context.Context, p *policy.Metad
|
||||
|
||||
// UpdateRetention Update Retention
|
||||
func (r *defaultController) UpdateRetention(ctx context.Context, p *policy.Metadata) error {
|
||||
p0, err := r.manager.GetPolicy(p.ID)
|
||||
p0, err := r.manager.GetPolicy(ctx, p.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -159,7 +159,7 @@ func (r *defaultController) UpdateRetention(ctx context.Context, p *policy.Metad
|
||||
return fmt.Errorf("not support Trigger %s", p.Trigger.Kind)
|
||||
}
|
||||
}
|
||||
if err = r.manager.UpdatePolicy(p); err != nil {
|
||||
if err = r.manager.UpdatePolicy(ctx, p); err != nil {
|
||||
return err
|
||||
}
|
||||
if needUn {
|
||||
@ -184,7 +184,7 @@ func (r *defaultController) UpdateRetention(ctx context.Context, p *policy.Metad
|
||||
|
||||
// DeleteRetention Delete Retention
|
||||
func (r *defaultController) DeleteRetention(ctx context.Context, id int64) error {
|
||||
p, err := r.manager.GetPolicy(id)
|
||||
p, err := r.manager.GetPolicy(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -199,7 +199,7 @@ func (r *defaultController) DeleteRetention(ctx context.Context, id int64) error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.manager.DeletePolicy(id)
|
||||
return r.manager.DeletePolicy(ctx, id)
|
||||
}
|
||||
|
||||
// deleteExecs delete executions
|
||||
@ -226,7 +226,7 @@ func (r *defaultController) deleteExecs(ctx context.Context, vendorID int64) err
|
||||
|
||||
// TriggerRetentionExec Trigger Retention Execution
|
||||
func (r *defaultController) TriggerRetentionExec(ctx context.Context, policyID int64, trigger string, dryRun bool) (int64, error) {
|
||||
p, err := r.manager.GetPolicy(policyID)
|
||||
p, err := r.manager.GetPolicy(ctx, policyID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
@ -1,36 +1,49 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"github.com/goharbor/harbor/src/common/dao"
|
||||
"context"
|
||||
"github.com/goharbor/harbor/src/lib/orm"
|
||||
"github.com/goharbor/harbor/src/pkg/retention/dao/models"
|
||||
)
|
||||
|
||||
// CreatePolicy Create Policy
|
||||
func CreatePolicy(p *models.RetentionPolicy) (int64, error) {
|
||||
o := dao.GetOrmer()
|
||||
func CreatePolicy(ctx context.Context, p *models.RetentionPolicy) (int64, error) {
|
||||
o, err := orm.FromContext(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return o.Insert(p)
|
||||
}
|
||||
|
||||
// UpdatePolicy Update Policy
|
||||
func UpdatePolicy(p *models.RetentionPolicy, cols ...string) error {
|
||||
o := dao.GetOrmer()
|
||||
_, err := o.Update(p, cols...)
|
||||
func UpdatePolicy(ctx context.Context, p *models.RetentionPolicy, cols ...string) error {
|
||||
o, err := orm.FromContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = o.Update(p, cols...)
|
||||
return err
|
||||
}
|
||||
|
||||
// DeletePolicy Update Policy
|
||||
func DeletePolicy(id int64) error {
|
||||
o := dao.GetOrmer()
|
||||
func DeletePolicy(ctx context.Context, id int64) error {
|
||||
o, err := orm.FromContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p := &models.RetentionPolicy{
|
||||
ID: id,
|
||||
}
|
||||
_, err := o.Delete(p)
|
||||
_, err = o.Delete(p)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetPolicy Get Policy
|
||||
func GetPolicy(id int64) (*models.RetentionPolicy, error) {
|
||||
o := dao.GetOrmer()
|
||||
func GetPolicy(ctx context.Context, id int64) (*models.RetentionPolicy, error) {
|
||||
o, err := orm.FromContext(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p := &models.RetentionPolicy{
|
||||
ID: id,
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package dao
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/goharbor/harbor/src/lib/orm"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
@ -69,26 +70,27 @@ func TestPolicy(t *testing.T) {
|
||||
data, _ := json.Marshal(p)
|
||||
p1.Data = string(data)
|
||||
|
||||
id, err := CreatePolicy(p1)
|
||||
ctx := orm.Context()
|
||||
id, err := CreatePolicy(ctx, p1)
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, id > 0)
|
||||
|
||||
p1, err = GetPolicy(id)
|
||||
p1, err = GetPolicy(ctx, id)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualValues(t, "project", p1.ScopeLevel)
|
||||
assert.True(t, p1.ID > 0)
|
||||
|
||||
p1.ScopeLevel = "test"
|
||||
err = UpdatePolicy(p1)
|
||||
err = UpdatePolicy(ctx, p1)
|
||||
assert.Nil(t, err)
|
||||
p1, err = GetPolicy(id)
|
||||
p1, err = GetPolicy(ctx, id)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualValues(t, "test", p1.ScopeLevel)
|
||||
|
||||
err = DeletePolicy(id)
|
||||
err = DeletePolicy(ctx, id)
|
||||
assert.Nil(t, err)
|
||||
|
||||
p1, err = GetPolicy(id)
|
||||
p1, err = GetPolicy(ctx, id)
|
||||
assert.NotNil(t, err)
|
||||
assert.True(t, strings.Contains(err.Error(), "no row found"))
|
||||
}
|
||||
|
@ -15,6 +15,7 @@
|
||||
package retention
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/goharbor/harbor/src/lib/orm"
|
||||
"testing"
|
||||
|
||||
@ -45,16 +46,16 @@ func (f *fakeRetentionManager) GetTotalOfTasks(executionID int64) (int64, error)
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (f *fakeRetentionManager) CreatePolicy(p *policy.Metadata) (int64, error) {
|
||||
func (f *fakeRetentionManager) CreatePolicy(ctx context.Context, p *policy.Metadata) (int64, error) {
|
||||
return 0, nil
|
||||
}
|
||||
func (f *fakeRetentionManager) UpdatePolicy(p *policy.Metadata) error {
|
||||
func (f *fakeRetentionManager) UpdatePolicy(ctx context.Context, p *policy.Metadata) error {
|
||||
return nil
|
||||
}
|
||||
func (f *fakeRetentionManager) DeletePolicy(ID int64) error {
|
||||
func (f *fakeRetentionManager) DeletePolicy(ctx context.Context, ID int64) error {
|
||||
return nil
|
||||
}
|
||||
func (f *fakeRetentionManager) GetPolicy(ID int64) (*policy.Metadata, error) {
|
||||
func (f *fakeRetentionManager) GetPolicy(ctx context.Context, ID int64) (*policy.Metadata, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (f *fakeRetentionManager) CreateExecution(execution *Execution) (int64, error) {
|
||||
|
@ -15,6 +15,7 @@
|
||||
package retention
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/goharbor/harbor/src/pkg/retention/policy"
|
||||
@ -28,15 +29,15 @@ import (
|
||||
// Manager defines operations of managing policy
|
||||
type Manager interface {
|
||||
// Create new policy and return ID
|
||||
CreatePolicy(p *policy.Metadata) (int64, error)
|
||||
CreatePolicy(ctx context.Context, p *policy.Metadata) (int64, error)
|
||||
// Update the existing policy
|
||||
// Full update
|
||||
UpdatePolicy(p *policy.Metadata) error
|
||||
UpdatePolicy(ctx context.Context, p *policy.Metadata) error
|
||||
// Delete the specified policy
|
||||
// No actual use so far
|
||||
DeletePolicy(ID int64) error
|
||||
DeletePolicy(ctx context.Context, id int64) error
|
||||
// Get the specified policy
|
||||
GetPolicy(ID int64) (*policy.Metadata, error)
|
||||
GetPolicy(ctx context.Context, id int64) (*policy.Metadata, error)
|
||||
}
|
||||
|
||||
// DefaultManager ...
|
||||
@ -44,7 +45,7 @@ type DefaultManager struct {
|
||||
}
|
||||
|
||||
// CreatePolicy Create Policy
|
||||
func (d *DefaultManager) CreatePolicy(p *policy.Metadata) (int64, error) {
|
||||
func (d *DefaultManager) CreatePolicy(ctx context.Context, p *policy.Metadata) (int64, error) {
|
||||
p1 := &models.RetentionPolicy{}
|
||||
p1.ScopeLevel = p.Scope.Level
|
||||
p1.ScopeReference = p.Scope.Reference
|
||||
@ -53,11 +54,11 @@ func (d *DefaultManager) CreatePolicy(p *policy.Metadata) (int64, error) {
|
||||
p1.Data = string(data)
|
||||
p1.CreateTime = time.Now()
|
||||
p1.UpdateTime = p1.CreateTime
|
||||
return dao.CreatePolicy(p1)
|
||||
return dao.CreatePolicy(ctx, p1)
|
||||
}
|
||||
|
||||
// UpdatePolicy Update Policy
|
||||
func (d *DefaultManager) UpdatePolicy(p *policy.Metadata) error {
|
||||
func (d *DefaultManager) UpdatePolicy(ctx context.Context, p *policy.Metadata) error {
|
||||
p1 := &models.RetentionPolicy{}
|
||||
p1.ID = p.ID
|
||||
p1.ScopeLevel = p.Scope.Level
|
||||
@ -68,17 +69,17 @@ func (d *DefaultManager) UpdatePolicy(p *policy.Metadata) error {
|
||||
p.ID = p1.ID
|
||||
p1.Data = string(data)
|
||||
p1.UpdateTime = time.Now()
|
||||
return dao.UpdatePolicy(p1, "scope_level", "trigger_kind", "data", "update_time")
|
||||
return dao.UpdatePolicy(ctx, p1, "scope_level", "trigger_kind", "data", "update_time")
|
||||
}
|
||||
|
||||
// DeletePolicy Delete Policy
|
||||
func (d *DefaultManager) DeletePolicy(id int64) error {
|
||||
return dao.DeletePolicy(id)
|
||||
func (d *DefaultManager) DeletePolicy(ctx context.Context, id int64) error {
|
||||
return dao.DeletePolicy(ctx, id)
|
||||
}
|
||||
|
||||
// GetPolicy Get Policy
|
||||
func (d *DefaultManager) GetPolicy(id int64) (*policy.Metadata, error) {
|
||||
p1, err := dao.GetPolicy(id)
|
||||
func (d *DefaultManager) GetPolicy(ctx context.Context, id int64) (*policy.Metadata, error) {
|
||||
p1, err := dao.GetPolicy(ctx, id)
|
||||
if err != nil {
|
||||
if err == orm.ErrNoRows {
|
||||
return nil, fmt.Errorf("no such Retention policy with id %v", id)
|
||||
|
@ -2,6 +2,7 @@ package retention
|
||||
|
||||
import (
|
||||
"github.com/goharbor/harbor/src/common/dao"
|
||||
"github.com/goharbor/harbor/src/lib/orm"
|
||||
"github.com/goharbor/harbor/src/pkg/retention/policy"
|
||||
"github.com/goharbor/harbor/src/pkg/retention/policy/rule"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -57,26 +58,27 @@ func TestPolicy(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
id, err := m.CreatePolicy(p1)
|
||||
ctx := orm.Context()
|
||||
id, err := m.CreatePolicy(ctx, p1)
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, id > 0)
|
||||
|
||||
p1, err = m.GetPolicy(id)
|
||||
p1, err = m.GetPolicy(ctx, id)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualValues(t, "project", p1.Scope.Level)
|
||||
assert.True(t, p1.ID > 0)
|
||||
|
||||
p1.Scope.Level = "test"
|
||||
err = m.UpdatePolicy(p1)
|
||||
err = m.UpdatePolicy(ctx, p1)
|
||||
assert.Nil(t, err)
|
||||
p1, err = m.GetPolicy(id)
|
||||
p1, err = m.GetPolicy(ctx, id)
|
||||
assert.Nil(t, err)
|
||||
assert.EqualValues(t, "test", p1.Scope.Level)
|
||||
|
||||
err = m.DeletePolicy(id)
|
||||
err = m.DeletePolicy(ctx, id)
|
||||
assert.Nil(t, err)
|
||||
|
||||
p1, err = m.GetPolicy(id)
|
||||
p1, err = m.GetPolicy(ctx, id)
|
||||
assert.NotNil(t, err)
|
||||
assert.True(t, strings.Contains(err.Error(), "no such Retention policy"))
|
||||
assert.Nil(t, p1)
|
||||
@ -124,7 +126,8 @@ func TestExecution(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
policyID, err := m.CreatePolicy(p1)
|
||||
ctx := orm.Context()
|
||||
policyID, err := m.CreatePolicy(ctx, p1)
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, policyID > 0)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user