Skip to content

Commit

Permalink
Merge pull request #195 from xuzhu-591/feat-members-webhook
Browse files Browse the repository at this point in the history
feat: add webhook events of members
  • Loading branch information
xuzhu-591 authored Oct 17, 2023
2 parents d09b910 + 8c9a394 commit f265604
Show file tree
Hide file tree
Showing 25 changed files with 326 additions and 195 deletions.
3 changes: 3 additions & 0 deletions core/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ import (
"github.com/horizoncd/horizon/pkg/cd"
clustermetrcis "github.com/horizoncd/horizon/pkg/cluster/metrics"
"github.com/horizoncd/horizon/pkg/environment/service"
eventservice "github.com/horizoncd/horizon/pkg/event/service"
"github.com/horizoncd/horizon/pkg/grafana"
"github.com/horizoncd/horizon/pkg/jobs"
"github.com/horizoncd/horizon/pkg/jobs/autofree"
Expand Down Expand Up @@ -431,6 +432,7 @@ func Init(ctx context.Context, flags *Flags, coreConfig *config.Config) {
}

groupSvc := groupservice.NewService(manager)
eventSvc := eventservice.New(manager)
applicationSvc := applicationservice.NewService(groupSvc, manager)
clusterSvc := clusterservice.NewService(applicationSvc, manager)
userSvc := userservice.NewService(manager)
Expand All @@ -454,6 +456,7 @@ func Init(ctx context.Context, flags *Flags, coreConfig *config.Config) {
ApplicationSvc: applicationSvc,
ClusterSvc: clusterSvc,
GroupSvc: groupSvc,
EventSvc: eventSvc,
UserSvc: userSvc,
TokenSvc: tokenSvc,
RoleService: roleService,
Expand Down
2 changes: 2 additions & 0 deletions core/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ const (

ResourceWebhook = "webhooks"
ResourceWebhookLog = "webhooklogs"

ResourceMember = "members"
)

const (
Expand Down
67 changes: 15 additions & 52 deletions core/controller/application/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
codemodels "github.com/horizoncd/horizon/pkg/cluster/code"
clustermanager "github.com/horizoncd/horizon/pkg/cluster/manager"
perror "github.com/horizoncd/horizon/pkg/errors"
eventmanager "github.com/horizoncd/horizon/pkg/event/manager"
eventmodels "github.com/horizoncd/horizon/pkg/event/models"
eventservice "github.com/horizoncd/horizon/pkg/event/service"
groupmanager "github.com/horizoncd/horizon/pkg/group/manager"
groupsvc "github.com/horizoncd/horizon/pkg/group/service"
"github.com/horizoncd/horizon/pkg/member"
Expand All @@ -49,7 +49,6 @@ import (
usersvc "github.com/horizoncd/horizon/pkg/user/service"
"github.com/horizoncd/horizon/pkg/util/errors"
"github.com/horizoncd/horizon/pkg/util/jsonschema"
"github.com/horizoncd/horizon/pkg/util/log"
"github.com/horizoncd/horizon/pkg/util/permission"
"github.com/horizoncd/horizon/pkg/util/validate"
"github.com/horizoncd/horizon/pkg/util/wlog"
Expand Down Expand Up @@ -95,7 +94,7 @@ type controller struct {
clusterMgr clustermanager.Manager
userSvc usersvc.Service
memberManager member.Manager
eventMgr eventmanager.Manager
eventSvc eventservice.Service
tagMgr tagmanager.Manager
applicationRegionMgr applicationregionmanager.Manager
pipelinemanager pipelinemanager.Manager
Expand All @@ -116,7 +115,7 @@ func NewController(param *param.Param) Controller {
clusterMgr: param.ClusterMgr,
userSvc: param.UserSvc,
memberManager: param.MemberMgr,
eventMgr: param.EventMgr,
eventSvc: param.EventSvc,
tagMgr: param.TagMgr,
applicationRegionMgr: param.ApplicationRegionMgr,
pipelinemanager: param.PipelineMgr,
Expand Down Expand Up @@ -309,16 +308,9 @@ func (c *controller) CreateApplication(ctx context.Context, groupID uint,
request.TemplateInput.Pipeline, request.TemplateInput.Application)

// 7. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceApplication,
EventType: eventmodels.ApplicationCreated,
ResourceID: ret.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}

c.eventSvc.CreateEventIgnoreError(ctx, common.ResourceApplication, ret.ID,
eventmodels.ApplicationCreated, nil)
c.eventSvc.RecordMemberCreatedEvent(ctx, common.ResourceApplication, ret.ID)
return ret, nil
}

Expand Down Expand Up @@ -436,16 +428,9 @@ func (c *controller) CreateApplicationV2(ctx context.Context, groupID uint,
ret.Priority = *request.Priority
}

if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceApplication,
EventType: eventmodels.ApplicationCreated,
ResourceID: applicationDBModel.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}

c.eventSvc.CreateEventIgnoreError(ctx, common.ResourceApplication, ret.ID,
eventmodels.ApplicationCreated, nil)
c.eventSvc.RecordMemberCreatedEvent(ctx, common.ResourceApplication, ret.ID)
return ret, nil
}

Expand Down Expand Up @@ -498,15 +483,8 @@ func (c *controller) UpdateApplication(ctx context.Context, id uint,
}

// 5. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceApplication,
EventType: eventmodels.ApplicationUpdated,
ResourceID: applicationModel.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
c.eventSvc.CreateEventIgnoreError(ctx, common.ResourceApplication, applicationModel.ID,
eventmodels.ApplicationUpdated, nil)

// 6. get fullPath
group, err := c.groupSvc.GetChildByID(ctx, appExistsInDB.GroupID)
Expand Down Expand Up @@ -591,15 +569,8 @@ func (c *controller) UpdateApplicationV2(ctx context.Context, id uint,
}

// 6. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceApplication,
EventType: eventmodels.ApplicationUpdated,
ResourceID: appExistsInDB.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
c.eventSvc.CreateEventIgnoreError(ctx, common.ResourceApplication, appExistsInDB.ID,
eventmodels.ApplicationUpdated, nil)
return err
}

Expand Down Expand Up @@ -652,16 +623,8 @@ func (c *controller) DeleteApplication(ctx context.Context, id uint, hard bool)
}

// 4. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceApplication,
EventType: eventmodels.ApplicationDeleted,
ResourceID: id,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}

c.eventSvc.CreateEventIgnoreError(ctx, common.ResourceApplication, id,
eventmodels.ApplicationDeleted, nil)
return nil
}

Expand Down
11 changes: 9 additions & 2 deletions core/controller/application/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
codemodels "github.com/horizoncd/horizon/pkg/cluster/code"
clustermodels "github.com/horizoncd/horizon/pkg/cluster/models"
eventmodels "github.com/horizoncd/horizon/pkg/event/models"
eventservice "github.com/horizoncd/horizon/pkg/event/service"
groupmodels "github.com/horizoncd/horizon/pkg/group/models"
groupservice "github.com/horizoncd/horizon/pkg/group/service"
membermodels "github.com/horizoncd/horizon/pkg/member/models"
Expand All @@ -41,6 +42,7 @@ import (
tmodels "github.com/horizoncd/horizon/pkg/template/models"
trmodels "github.com/horizoncd/horizon/pkg/templaterelease/models"
trschema "github.com/horizoncd/horizon/pkg/templaterelease/schema"
usermodel "github.com/horizoncd/horizon/pkg/user/models"
userservice "github.com/horizoncd/horizon/pkg/user/service"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -293,6 +295,9 @@ func TestMain(m *testing.M) {
if err := db.AutoMigrate(&eventmodels.Event{}); err != nil {
panic(err)
}
if err := db.AutoMigrate(&usermodel.User{}); err != nil {
panic(err)
}
ctx = context.TODO()
ctx = context.WithValue(ctx, common.UserContextKey(), &userauth.DefaultInfo{
Name: "Tony",
Expand Down Expand Up @@ -362,7 +367,8 @@ func Test(t *testing.T) {
templateReleaseMgr: manager.TemplateReleaseMgr,
clusterMgr: manager.ClusterMgr,
userSvc: userservice.NewService(manager),
eventMgr: manager.EventMgr,
eventSvc: eventservice.New(manager),
memberManager: manager.MemberMgr,
}

group, err := manager.GroupMgr.Create(ctx, &groupmodels.Group{
Expand Down Expand Up @@ -517,7 +523,8 @@ func TestV2(t *testing.T) {
templateReleaseMgr: manager.TemplateReleaseMgr,
clusterMgr: manager.ClusterMgr,
userSvc: userservice.NewService(manager),
eventMgr: manager.EventMgr,
eventSvc: eventservice.New(manager),
memberManager: manager.MemberMgr,
}

group, err := manager.GroupMgr.Create(ctx, &groupmodels.Group{
Expand Down
6 changes: 3 additions & 3 deletions core/controller/cluster/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
envmanager "github.com/horizoncd/horizon/pkg/environment/manager"
"github.com/horizoncd/horizon/pkg/environment/service"
environmentregionmapper "github.com/horizoncd/horizon/pkg/environmentregion/manager"
eventmanager "github.com/horizoncd/horizon/pkg/event/manager"
eventservice "github.com/horizoncd/horizon/pkg/event/service"
grafanaservice "github.com/horizoncd/horizon/pkg/grafana"
groupmanager "github.com/horizoncd/horizon/pkg/group/manager"
groupsvc "github.com/horizoncd/horizon/pkg/group/service"
Expand Down Expand Up @@ -159,7 +159,7 @@ type controller struct {
grafanaService grafanaservice.Service
grafanaConfig grafana.Config
buildSchema *build.Schema
eventMgr eventmanager.Manager
eventSvc eventservice.Service
tokenSvc tokenservice.Service
tokenConfig token.Config
templateUpgradeMapper template.UpgradeMapper
Expand Down Expand Up @@ -201,7 +201,7 @@ func NewController(config *config.Config, param *param.Param) Controller {
grafanaService: param.GrafanaService,
grafanaConfig: config.GrafanaConfig,
buildSchema: param.BuildSchema,
eventMgr: param.EventMgr,
eventSvc: param.EventSvc,
tokenSvc: param.TokenSvc,
tokenConfig: config.TokenConfig,
templateUpgradeMapper: config.TemplateUpgradeMapper,
Expand Down
47 changes: 9 additions & 38 deletions core/controller/cluster/controller_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,16 +553,9 @@ func (c *controller) CreateCluster(ctx context.Context, applicationID uint, envi
r.TemplateInput.Pipeline, r.TemplateInput.Application)

// 11. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceCluster,
EventType: eventmodels.ClusterCreated,
ResourceID: ret.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}

c.eventSvc.CreateEventIgnoreError(ctx, common.ResourceCluster, ret.ID,
eventmodels.ClusterCreated, nil)
c.eventSvc.RecordMemberCreatedEvent(ctx, common.ResourceCluster, ret.ID)
return ret, nil
}

Expand Down Expand Up @@ -725,15 +718,8 @@ func (c *controller) UpdateCluster(ctx context.Context, clusterID uint,
}

// 6. record event
if _, err := c.eventMgr.CreateEvent(ctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceCluster,
EventType: eventmodels.ClusterUpdated,
ResourceID: cluster.ID,
},
}); err != nil {
log.Warningf(ctx, "failed to create event, err: %s", err.Error())
}
c.eventSvc.CreateEventIgnoreError(ctx, common.ResourceCluster, cluster.ID,
eventmodels.ClusterUpdated, nil)

// 7. get full path
group, err := c.groupSvc.GetChildByID(ctx, application.GroupID)
Expand Down Expand Up @@ -917,16 +903,8 @@ func (c *controller) DeleteCluster(ctx context.Context, clusterID uint, hard boo
}

// 5. record event
if _, err := c.eventMgr.CreateEvent(newctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceCluster,
EventType: eventmodels.ClusterDeleted,
ResourceID: clusterID,
},
ReqID: rid,
}); err != nil {
log.Warningf(newctx, "failed to create event, err: %s", err.Error())
}
c.eventSvc.CreateEventIgnoreError(newctx, common.ResourceCluster, clusterID,
eventmodels.ClusterDeleted, nil)
}()

return nil
Expand Down Expand Up @@ -997,15 +975,8 @@ func (c *controller) FreeCluster(ctx context.Context, clusterID uint) (err error
}

// 4. create event
if _, err := c.eventMgr.CreateEvent(newctx, &eventmodels.Event{
EventSummary: eventmodels.EventSummary{
ResourceType: common.ResourceCluster,
EventType: eventmodels.ClusterFreed,
ResourceID: clusterID,
},
}); err != nil {
log.Warningf(newctx, "failed to create event, err: %s", err.Error())
}
c.eventSvc.CreateEventIgnoreError(newctx, common.ResourceCluster, clusterID,
eventmodels.ClusterFreed, nil)
}()

return nil
Expand Down
7 changes: 4 additions & 3 deletions core/controller/cluster/controller_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"time"

"github.com/golang/mock/gomock"
eventservice "github.com/horizoncd/horizon/pkg/event/service"
"github.com/stretchr/testify/assert"

"github.com/horizoncd/horizon/core/common"
Expand Down Expand Up @@ -101,7 +102,7 @@ func testListClusterByNameFuzzily(t *testing.T) {
applicationSvc: applicationservice.NewService(groupservice.NewService(manager), manager),
groupManager: manager.GroupMgr,
memberManager: manager.MemberMgr,
eventMgr: manager.EventMgr,
eventSvc: eventservice.New(manager),
commitGetter: commitGetter,
}

Expand Down Expand Up @@ -218,7 +219,7 @@ func testListUserClustersByNameFuzzily(t *testing.T) {
applicationSvc: applicationservice.NewService(groupservice.NewService(manager), manager),
groupManager: manager.GroupMgr,
memberManager: manager.MemberMgr,
eventMgr: manager.EventMgr,
eventSvc: eventservice.New(manager),
commitGetter: commitGetter,
}

Expand Down Expand Up @@ -306,7 +307,7 @@ func testControllerFreeOrDeleteClusterFailed(t *testing.T) {
groupManager: manager.GroupMgr,
envMgr: manager.EnvMgr,
regionMgr: manager.RegionMgr,
eventMgr: manager.EventMgr,
eventSvc: eventservice.New(manager),
}

id, err := registrydao.NewDAO(db).Create(ctx, &registrymodels.Registry{
Expand Down
Loading

0 comments on commit f265604

Please sign in to comment.