mirror of
https://github.com/goharbor/harbor.git
synced 2025-02-22 14:52:17 +01:00
fix robot deletion event (#21234) * fix robot deletion event * resolve comments --------- Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
parent
7201b3bd5a
commit
b72b5cec13
@ -51,8 +51,9 @@ func (c *CreateRobotEventMetadata) Resolve(event *event.Event) error {
|
||||
|
||||
// DeleteRobotEventMetadata is the metadata from which the delete robot event can be resolved
|
||||
type DeleteRobotEventMetadata struct {
|
||||
Ctx context.Context
|
||||
Robot *model.Robot
|
||||
Ctx context.Context
|
||||
Robot *model.Robot
|
||||
Operator string
|
||||
}
|
||||
|
||||
// Resolve to the event from the metadata
|
||||
@ -62,9 +63,13 @@ func (d *DeleteRobotEventMetadata) Resolve(event *event.Event) error {
|
||||
Robot: d.Robot,
|
||||
OccurAt: time.Now(),
|
||||
}
|
||||
cx, exist := security.FromContext(d.Ctx)
|
||||
if exist {
|
||||
data.Operator = cx.GetUsername()
|
||||
if d.Operator != "" {
|
||||
data.Operator = d.Operator
|
||||
} else {
|
||||
cx, exist := security.FromContext(d.Ctx)
|
||||
if exist {
|
||||
data.Operator = cx.GetUsername()
|
||||
}
|
||||
}
|
||||
data.Robot.Name = fmt.Sprintf("%s%s", config.RobotPrefix(d.Ctx), data.Robot.Name)
|
||||
event.Topic = event2.TopicDeleteRobot
|
||||
|
@ -56,7 +56,7 @@ type Controller interface {
|
||||
Create(ctx context.Context, r *Robot) (int64, string, error)
|
||||
|
||||
// Delete ...
|
||||
Delete(ctx context.Context, id int64) error
|
||||
Delete(ctx context.Context, id int64, option ...*Option) error
|
||||
|
||||
// Update ...
|
||||
Update(ctx context.Context, r *Robot, option *Option) error
|
||||
@ -149,7 +149,7 @@ func (d *controller) Create(ctx context.Context, r *Robot) (int64, string, error
|
||||
}
|
||||
|
||||
// Delete ...
|
||||
func (d *controller) Delete(ctx context.Context, id int64) error {
|
||||
func (d *controller) Delete(ctx context.Context, id int64, option ...*Option) error {
|
||||
rDelete, err := d.robotMgr.Get(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -161,10 +161,14 @@ func (d *controller) Delete(ctx context.Context, id int64) error {
|
||||
return err
|
||||
}
|
||||
// fire event
|
||||
notification.AddEvent(ctx, &metadata.DeleteRobotEventMetadata{
|
||||
deleteMetadata := &metadata.DeleteRobotEventMetadata{
|
||||
Ctx: ctx,
|
||||
Robot: rDelete,
|
||||
})
|
||||
}
|
||||
if len(option) != 0 && option[0].Operator != "" {
|
||||
deleteMetadata.Operator = option[0].Operator
|
||||
}
|
||||
notification.AddEvent(ctx, deleteMetadata)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -84,4 +84,5 @@ func (p *Permission) IsCoverAll() bool {
|
||||
// Option ...
|
||||
type Option struct {
|
||||
WithPermission bool
|
||||
Operator string
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/goharbor/harbor/src/common/secret"
|
||||
"github.com/goharbor/harbor/src/controller/artifact"
|
||||
"github.com/goharbor/harbor/src/controller/event/metadata"
|
||||
"github.com/goharbor/harbor/src/controller/event/operator"
|
||||
@ -92,7 +93,7 @@ func scanTaskStatusChange(ctx context.Context, taskID int64, status string) (err
|
||||
|
||||
robotID := getRobotID(t.ExtraAttrs)
|
||||
if robotID > 0 {
|
||||
if err := robotCtl.Delete(ctx, robotID); err != nil {
|
||||
if err := robotCtl.Delete(ctx, robotID, &robot.Option{Operator: secret.JobserviceUser}); err != nil {
|
||||
// Should not block the main flow, just logged
|
||||
logger.WithFields(log.Fields{"robot_id": robotID, "error": err}).Error("delete robot account failed")
|
||||
} else {
|
||||
|
@ -96,7 +96,7 @@ func (suite *CallbackTestSuite) TestScanTaskStatusChange() {
|
||||
},
|
||||
nil,
|
||||
).Once()
|
||||
suite.robotCtl.On("Delete", mock.Anything, int64(1)).Return(nil).Once()
|
||||
suite.robotCtl.On("Delete", mock.Anything, int64(1), mock.Anything).Return(nil).Once()
|
||||
suite.NoError(scanTaskStatusChange(suite.ctx, 1, job.SuccessStatus.String()))
|
||||
}
|
||||
|
||||
@ -108,7 +108,7 @@ func (suite *CallbackTestSuite) TestScanTaskStatusChange() {
|
||||
},
|
||||
nil,
|
||||
).Once()
|
||||
suite.robotCtl.On("Delete", mock.Anything, int64(1)).Return(fmt.Errorf("failed")).Once()
|
||||
suite.robotCtl.On("Delete", mock.Anything, int64(1), mock.Anything).Return(fmt.Errorf("failed")).Once()
|
||||
suite.NoError(scanTaskStatusChange(suite.ctx, 1, job.SuccessStatus.String()))
|
||||
}
|
||||
|
||||
|
@ -79,17 +79,24 @@ func (_m *Controller) Create(ctx context.Context, r *robot.Robot) (int64, string
|
||||
return r0, r1, r2
|
||||
}
|
||||
|
||||
// Delete provides a mock function with given fields: ctx, id
|
||||
func (_m *Controller) Delete(ctx context.Context, id int64) error {
|
||||
ret := _m.Called(ctx, id)
|
||||
// Delete provides a mock function with given fields: ctx, id, option
|
||||
func (_m *Controller) Delete(ctx context.Context, id int64, option ...*robot.Option) error {
|
||||
_va := make([]interface{}, len(option))
|
||||
for _i := range option {
|
||||
_va[_i] = option[_i]
|
||||
}
|
||||
var _ca []interface{}
|
||||
_ca = append(_ca, ctx, id)
|
||||
_ca = append(_ca, _va...)
|
||||
ret := _m.Called(_ca...)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for Delete")
|
||||
}
|
||||
|
||||
var r0 error
|
||||
if rf, ok := ret.Get(0).(func(context.Context, int64) error); ok {
|
||||
r0 = rf(ctx, id)
|
||||
if rf, ok := ret.Get(0).(func(context.Context, int64, ...*robot.Option) error); ok {
|
||||
r0 = rf(ctx, id, option...)
|
||||
} else {
|
||||
r0 = ret.Error(0)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user