Skip to content

Commit

Permalink
feat: add test notification endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Apr 3, 2024
1 parent 1b5e967 commit eb22ae2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions echo/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/flanksource/incident-commander/connection"
"github.com/flanksource/incident-commander/db"
"github.com/flanksource/incident-commander/logs"
"github.com/flanksource/incident-commander/notification"
"github.com/flanksource/incident-commander/playbook"
"github.com/flanksource/incident-commander/rbac"
"github.com/flanksource/incident-commander/snapshot"
Expand Down Expand Up @@ -94,6 +95,8 @@ func New(ctx context.Context) *echov4.Echo {

playbook.RegisterRoutes(e)
connection.RegisterRoutes(e)
notification.RegisterRoutes(e)

e.POST("/agent/generate", agent.GenerateAgent, rbac.Authorization(rbac.ObjectAgentCreate, rbac.ActionWrite))
e.POST("/logs", logs.LogsHandler)
return e
Expand Down
41 changes: 41 additions & 0 deletions notification/controllers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package notification

import (
"net/http"
"time"

dutyAPI "github.com/flanksource/duty/api"
"github.com/flanksource/duty/context"
"github.com/flanksource/postq"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
)

func RegisterRoutes(e *echo.Echo) {
apiGroup := e.Group("/notifications")
apiGroup.POST("/test", TestNotification)
}

func TestNotification(c echo.Context) error {
ctx := c.Request().Context().(context.Context)

var reqData struct {
ID uuid.UUID `json:"id"`
EventName string `json:"eventName"`
}
if err := c.Bind(&reqData); err != nil {
return dutyAPI.WriteError(c, dutyAPI.Errorf(dutyAPI.EINVALID, "invalid request: %v", err))
}

e := postq.Event{
Name: reqData.EventName,
Properties: map[string]string{"id": reqData.ID.String()},
CreatedAt: time.Now(),
}

if err := addNotificationEvent(ctx, e); err != nil {
return dutyAPI.WriteError(c, dutyAPI.Errorf(dutyAPI.EINTERNAL, "unable to create notification event: %v", err))
}

return c.JSON(http.StatusOK, dutyAPI.HTTPSuccess{Message: "success"})
}

0 comments on commit eb22ae2

Please sign in to comment.