Skip to content

Commit

Permalink
wrap webhook client in one interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bthari committed Oct 11, 2024
1 parent a69c706 commit e097e56
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 193 deletions.
13 changes: 10 additions & 3 deletions api/api/model_endpoints_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"net/http"

"github.com/caraml-dev/merlin/log"
"github.com/caraml-dev/merlin/models"
"github.com/caraml-dev/merlin/webhook"
"gorm.io/gorm"
Expand Down Expand Up @@ -126,7 +127,9 @@ func (c *ModelEndpointsController) CreateModelEndpoint(r *http.Request, vars map
}

// trigger webhook call
_ = c.Webhook.TriggerModelEndpointEvent(ctx, webhook.OnModelEndpointCreated, endpoint)
if err = c.Webhook.TriggerWebhooks(ctx, webhook.OnModelEndpointCreated, webhook.SetBody(endpoint)); err != nil {
log.Warnf("unable to invoke webhook for event type: %s, model: %s, endpoint: %d, error: %v", webhook.OnModelEndpointCreated, model.Name, endpoint.ID, err)
}

// Success. Return endpoint as response.
return Created(endpoint)
Expand Down Expand Up @@ -197,7 +200,9 @@ func (c *ModelEndpointsController) UpdateModelEndpoint(r *http.Request, vars map
}

// trigger webhook call
_ = c.Webhook.TriggerModelEndpointEvent(ctx, webhook.OnModelEndpointUpdated, newEndpoint)
if err = c.Webhook.TriggerWebhooks(ctx, webhook.OnModelEndpointUpdated, webhook.SetBody(newEndpoint)); err != nil {
log.Warnf("unable to invoke webhook for event type: %s, model: %s, error: %v", webhook.OnModelEndpointUpdated, model.Name, err)
}

return Ok(newEndpoint)
}
Expand Down Expand Up @@ -235,7 +240,9 @@ func (c *ModelEndpointsController) DeleteModelEndpoint(r *http.Request, vars map
}

// trigger webhook call
_ = c.Webhook.TriggerModelEndpointEvent(ctx, webhook.OnModelEndpointDeleted, modelEndpoint)
if err = c.Webhook.TriggerWebhooks(ctx, webhook.OnModelEndpointDeleted, webhook.SetBody(modelEndpoint)); err != nil {
log.Warnf("unable to invoke webhook for event type: %s, model: %s, error: %v", webhook.OnModelEndpointDeleted, model.Name, err)
}

return Ok(nil)
}
5 changes: 4 additions & 1 deletion api/api/models_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/caraml-dev/merlin/webhook"
"gorm.io/gorm"

"github.com/caraml-dev/merlin/log"
"github.com/caraml-dev/merlin/mlflow"
"github.com/caraml-dev/merlin/models"
"github.com/caraml-dev/merlin/service"
Expand Down Expand Up @@ -80,7 +81,9 @@ func (c *ModelsController) CreateModel(r *http.Request, vars map[string]string,
}

// trigger webhook call
_ = c.Webhook.TriggerModelEvent(ctx, webhook.OnModelCreated, model)
if err = c.Webhook.TriggerWebhooks(ctx, webhook.OnModelCreated, webhook.SetBody(model)); err != nil {
log.Warnf("unable to invoke webhook for event type: %s, project: %d, model: %s, error: %v", webhook.OnModelCreated, model.ProjectID, model.Name, err)
}

return Created(model)
}
Expand Down
13 changes: 10 additions & 3 deletions api/api/versions_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"net/http"

"github.com/caraml-dev/merlin/log"
"github.com/caraml-dev/merlin/models"
"github.com/caraml-dev/merlin/service"
"github.com/caraml-dev/merlin/utils"
Expand Down Expand Up @@ -80,7 +81,9 @@ func (c *VersionsController) PatchVersion(r *http.Request, vars map[string]strin
}

// trigger webhook call
_ = c.Webhook.TriggerModelVersionEvent(ctx, webhook.OnModelVersionUpdated, v)
if err = c.Webhook.TriggerWebhooks(ctx, webhook.OnModelVersionUpdated, webhook.SetBody(v)); err != nil {
log.Warnf("unable to invoke webhook for event type: %s, model: %s, version: %d, error: %v", webhook.OnModelVersionUpdated, v.ModelID, v.ID, err)
}

return Ok(patchedVersion)
}
Expand Down Expand Up @@ -153,7 +156,9 @@ func (c *VersionsController) CreateVersion(r *http.Request, vars map[string]stri
}

// trigger webhook call
_ = c.Webhook.TriggerModelVersionEvent(ctx, webhook.OnModelVersionCreated, version)
if err = c.Webhook.TriggerWebhooks(ctx, webhook.OnModelVersionCreated, webhook.SetBody(version)); err != nil {
log.Warnf("unable to invoke webhook for event type: %s, model: %s, version: %d, error: %v", webhook.OnModelVersionCreated, version.ModelID, version.ID, err)
}

return Created(version)
}
Expand Down Expand Up @@ -219,7 +224,9 @@ func (c *VersionsController) DeleteVersion(r *http.Request, vars map[string]stri
}

// trigger webhook call
_ = c.Webhook.TriggerModelVersionEvent(ctx, webhook.OnModelVersionDeleted, version)
if err = c.Webhook.TriggerWebhooks(ctx, webhook.OnModelVersionDeleted, webhook.SetBody(version)); err != nil {
log.Warnf("unable to invoke webhook for event type: %s, model: %s, version: %d, error: %v", webhook.OnModelVersionDeleted, version.ModelID, version.ID, err)
}

return Ok(versionID)
}
Expand Down
18 changes: 9 additions & 9 deletions api/api/versions_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func TestPatchVersion(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerModelVersionEvent", mock.Anything, webhook.OnModelVersionUpdated, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnModelVersionUpdated, mock.Anything).Return(nil)
return w
},
expected: &Response{
Expand Down Expand Up @@ -453,7 +453,7 @@ func TestPatchVersion(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerModelVersionEvent", mock.Anything, webhook.OnModelVersionUpdated, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnModelVersionUpdated, mock.Anything).Return(nil)
return w
},
expected: &Response{
Expand Down Expand Up @@ -541,7 +541,7 @@ func TestPatchVersion(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerModelVersionEvent", mock.Anything, webhook.OnModelVersionUpdated, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnModelVersionUpdated, mock.Anything).Return(nil)
return w
},
expected: &Response{
Expand Down Expand Up @@ -866,7 +866,7 @@ func TestPatchVersion(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerModelVersionEvent", mock.Anything, webhook.OnModelVersionUpdated, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnModelVersionUpdated, mock.Anything).Return(nil)
return w
},
expected: &Response{
Expand Down Expand Up @@ -1083,7 +1083,7 @@ func TestCreateVersion(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerModelVersionEvent", mock.Anything, webhook.OnModelVersionCreated, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnModelVersionCreated, mock.Anything).Return(nil)
return w
},
expected: &Response{
Expand Down Expand Up @@ -1496,7 +1496,7 @@ func TestCreateVersion(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerModelVersionEvent", mock.Anything, webhook.OnModelVersionCreated, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnModelVersionCreated, mock.Anything).Return(nil)
return w
},
expected: &Response{
Expand Down Expand Up @@ -1634,7 +1634,7 @@ func TestCreateVersion(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerModelVersionEvent", mock.Anything, webhook.OnModelVersionCreated, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnModelVersionCreated, mock.Anything).Return(nil)
return w
},
expected: &Response{
Expand Down Expand Up @@ -1846,7 +1846,7 @@ func TestDeleteVersion(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerModelVersionEvent", mock.Anything, webhook.OnModelVersionDeleted, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnModelVersionDeleted, mock.Anything).Return(nil)
return w
},
expected: &Response{
Expand Down Expand Up @@ -1912,7 +1912,7 @@ func TestDeleteVersion(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerModelVersionEvent", mock.Anything, webhook.OnModelVersionDeleted, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnModelVersionDeleted, mock.Anything).Return(nil)
return w
},
expected: &Response{
Expand Down
4 changes: 3 additions & 1 deletion api/queue/work/model_service_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ func (depl *ModelServiceDeployment) Deploy(job *queue.Job) error {
}

// trigger webhook call
_ = depl.Webhook.TriggerVersionEndpointEvent(ctx, webhook.OnVersionEndpointDeployed, endpoint)
if err = depl.Webhook.TriggerWebhooks(ctx, webhook.OnVersionEndpointDeployed, webhook.SetBody(endpoint)); err != nil {
log.Warnf("unable to invoke webhook for event type: %s, model: %s, endpoint: %d, error: %v", webhook.OnVersionEndpointDeployed, endpoint.VersionModelID, endpoint.ID, err)
}

return nil
}
Expand Down
30 changes: 13 additions & 17 deletions api/queue/work/model_service_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestExecuteDeployment(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerVersionEndpointEvent", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
return w
},
},
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestExecuteDeployment(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerVersionEndpointEvent", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
return w
},

Expand Down Expand Up @@ -237,7 +237,7 @@ func TestExecuteDeployment(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerVersionEndpointEvent", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
return w
},
},
Expand Down Expand Up @@ -287,7 +287,7 @@ func TestExecuteDeployment(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerVersionEndpointEvent", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
return w
},

Expand Down Expand Up @@ -360,7 +360,7 @@ func TestExecuteDeployment(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerVersionEndpointEvent", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
return w
},
},
Expand Down Expand Up @@ -417,7 +417,7 @@ func TestExecuteDeployment(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerVersionEndpointEvent", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
return w
},
},
Expand Down Expand Up @@ -466,7 +466,7 @@ func TestExecuteDeployment(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerVersionEndpointEvent", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
return w
},
},
Expand Down Expand Up @@ -517,7 +517,7 @@ func TestExecuteDeployment(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerVersionEndpointEvent", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
return w
},
},
Expand Down Expand Up @@ -568,7 +568,7 @@ func TestExecuteDeployment(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerVersionEndpointEvent", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
return w
},
},
Expand Down Expand Up @@ -631,7 +631,7 @@ func TestExecuteDeployment(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerVersionEndpointEvent", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
return w
},
},
Expand Down Expand Up @@ -673,8 +673,6 @@ func TestExecuteDeployment(t *testing.T) {
return mockImgBuilder
},
webhook: func() *webhookMock.Client {
//w := webhookMock.NewClient(t)
//w.On("TriggerVersionEndpointEvent", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
return webhookMock.NewClient(t)
},
},
Expand Down Expand Up @@ -715,8 +713,6 @@ func TestExecuteDeployment(t *testing.T) {
return mockImgBuilder
},
webhook: func() *webhookMock.Client {
//w := webhookMock.NewClient(t)
//w.On("TriggerVersionEndpointEvent", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
return webhookMock.NewClient(t)
},
},
Expand Down Expand Up @@ -921,7 +917,7 @@ func TestExecuteRedeployment(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerVersionEndpointEvent", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
return w
},
},
Expand Down Expand Up @@ -998,7 +994,7 @@ func TestExecuteRedeployment(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerVersionEndpointEvent", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
return w
},
},
Expand Down Expand Up @@ -1075,7 +1071,7 @@ func TestExecuteRedeployment(t *testing.T) {
},
webhook: func() *webhookMock.Client {
w := webhookMock.NewClient(t)
w.On("TriggerVersionEndpointEvent", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
w.On("TriggerWebhooks", mock.Anything, webhook.OnVersionEndpointDeployed, mock.Anything).Return(nil)
return w
},
},
Expand Down
9 changes: 6 additions & 3 deletions api/service/version_endpoint_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/caraml-dev/merlin/cluster"
"github.com/caraml-dev/merlin/config"
"github.com/caraml-dev/merlin/log"
"github.com/caraml-dev/merlin/models"
"github.com/caraml-dev/merlin/pkg/autoscaling"
"github.com/caraml-dev/merlin/pkg/deployment"
Expand Down Expand Up @@ -137,8 +138,8 @@ func (k *endpointService) DeployEndpoint(ctx context.Context, environment *model
}

// calling webhook if there's any webhook configured
err = k.webhook.TriggerVersionEndpointEvent(ctx, webhook.OnVersionEndpointPredeployment, endpoint)
if err != nil {
if err = k.webhook.TriggerWebhooks(ctx, webhook.OnVersionEndpointPredeployment, webhook.SetBody(endpoint)); err != nil {
log.Errorf("unable to invoke webhook for event type: %s, model: %s, endpoint: %d, error: %v", webhook.OnVersionEndpointPredeployment, endpoint.VersionModelID, endpoint.ID, err)
return nil, err
}

Expand Down Expand Up @@ -292,7 +293,9 @@ func (k *endpointService) UndeployEndpoint(ctx context.Context, environment *mod
}

// calling webhook if there's any webhook configured
_ = k.webhook.TriggerVersionEndpointEvent(ctx, webhook.OnVersionEndpointUndeployed, endpoint)
if err = k.webhook.TriggerWebhooks(ctx, webhook.OnVersionEndpointUndeployed, webhook.SetBody(endpoint)); err != nil {
log.Warnf("unable to invoke webhook for event type: %s, model: %s, endpoint: %d, error: %v", webhook.OnVersionEndpointUndeployed, endpoint.VersionModelID, endpoint.ID, err)
}

return endpoint, nil
}
Expand Down
Loading

0 comments on commit e097e56

Please sign in to comment.