fix(p2p): delete all preheat policies before delete project

Signed-off-by: chlins <chlins.zhang@gmail.com>
This commit is contained in:
chlins 2020-08-31 17:02:15 +08:00
parent 5c6a5b12c5
commit e01de8b201
3 changed files with 42 additions and 1 deletions

View File

@ -117,6 +117,8 @@ type Controller interface {
ListPoliciesByProject(ctx context.Context, project int64, query *q.Query) ([]*policyModels.Schema, error)
// CheckHealth checks the instance health, for test connection
CheckHealth(ctx context.Context, instance *providerModels.Instance) error
// DeletePoliciesOfProject delete all policies under one project
DeletePoliciesOfProject(ctx context.Context, project int64) error
}
var _ Controller = (*controller)(nil)
@ -419,6 +421,21 @@ func (c *controller) DeletePolicy(ctx context.Context, id int64) error {
return c.pManager.Delete(ctx, id)
}
// DeletePoliciesOfProject deletes all the policy under project.
func (c *controller) DeletePoliciesOfProject(ctx context.Context, project int64) error {
policies, err := c.ListPoliciesByProject(ctx, project, nil)
if err != nil {
return err
}
for _, p := range policies {
if err = c.DeletePolicy(ctx, p.ID); err != nil {
return err
}
}
return nil
}
// deleteExecs delete executions
func (c *controller) deleteExecs(ctx context.Context, vendorID int64) error {
executions, err := c.executionMgr.List(ctx, &q.Query{

View File

@ -335,6 +335,22 @@ func (s *preheatSuite) TestListPoliciesByProject() {
s.NotNil(p)
}
func (s *preheatSuite) TestDeletePoliciesOfProject() {
fakePolicies := []*policy.Schema{
{ID: 1000, Name: "1-should-delete", ProjectID: 10},
{ID: 1001, Name: "2-should-delete", ProjectID: 10},
}
s.fakePolicyMgr.On("ListPoliciesByProject", s.ctx, int64(10), mock.Anything).Return(fakePolicies, nil)
for _, p := range fakePolicies {
s.fakePolicyMgr.On("Get", s.ctx, p.ID).Return(p, nil)
s.fakePolicyMgr.On("Delete", s.ctx, p.ID).Return(nil)
s.fakeExecutionMgr.On("List", s.ctx, &q.Query{Keywords: map[string]interface{}{"VendorID": p.ID, "VendorType": "P2P_PREHEAT"}}).Return([]*taskModel.Execution{}, nil)
}
err := s.controller.DeletePoliciesOfProject(s.ctx, 10)
s.NoError(err)
}
func (s *preheatSuite) TestCheckHealth() {
// if instance is nil
var instance *providerModel.Instance

View File

@ -3,7 +3,6 @@ package handler
import (
"context"
"fmt"
"github.com/goharbor/harbor/src/pkg/robot"
"strconv"
"strings"
"sync"
@ -15,6 +14,7 @@ import (
"github.com/goharbor/harbor/src/common/rbac"
"github.com/goharbor/harbor/src/common/security"
"github.com/goharbor/harbor/src/common/security/local"
"github.com/goharbor/harbor/src/controller/p2p/preheat"
"github.com/goharbor/harbor/src/controller/project"
"github.com/goharbor/harbor/src/controller/quota"
"github.com/goharbor/harbor/src/controller/repository"
@ -29,6 +29,7 @@ import (
"github.com/goharbor/harbor/src/pkg/project/metadata"
"github.com/goharbor/harbor/src/pkg/quota/types"
"github.com/goharbor/harbor/src/pkg/retention/policy"
"github.com/goharbor/harbor/src/pkg/robot"
"github.com/goharbor/harbor/src/pkg/user"
"github.com/goharbor/harbor/src/replication"
"github.com/goharbor/harbor/src/server/v2.0/handler/model"
@ -48,6 +49,7 @@ func newProjectAPI() *projectAPI {
projectCtl: project.Ctl,
quotaCtl: quota.Ctl,
robotMgr: robot.Mgr,
preheatCtl: preheat.Ctl,
}
}
@ -60,6 +62,7 @@ type projectAPI struct {
projectCtl project.Controller
quotaCtl quota.Controller
robotMgr robot.Manager
preheatCtl preheat.Controller
}
func (a *projectAPI) CreateProject(ctx context.Context, params operation.CreateProjectParams) middleware.Responder {
@ -212,6 +215,11 @@ func (a *projectAPI) DeleteProject(ctx context.Context, params operation.DeleteP
}
}
// preheat policies under the project should be deleted after deleting the project
if err = a.preheatCtl.DeletePoliciesOfProject(ctx, params.ProjectID); err != nil {
return a.SendError(ctx, err)
}
return operation.NewDeleteProjectOK()
}