Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
feat(alert): general alert apis for firehose and optimus
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekv24 committed Sep 25, 2023
1 parent d27bef1 commit 385be84
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
1 change: 1 addition & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func Serve(ctx context.Context, addr string,
router.Route("/dex", func(r chi.Router) {
r.Get("/alertTemplates", alertSvc.HandleListTemplates())
r.Route("/subscriptions", alertsv1.SubscriptionRoutes(sirenClient, shieldClient))
r.Route("/alerts", alertsv1.AlertRoutes(sirenClient, shieldClient))
r.Route("/optimus", optimusv1.Routes(optimusClient))
r.Route("/projects", projectsv1.Routes(shieldClient))
r.Route("/dlq", dlqv1.Routes(entropyClient, gcsClient))
Expand Down
21 changes: 21 additions & 0 deletions internal/server/v1/alert/alert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package alert

Check failure on line 1 in internal/server/v1/alert/alert.go

View workflow job for this annotation

GitHub Actions / golangci-lint

: # github.com/goto/dex/internal/server/v1/alert

import (
shieldv1beta1rpc "buf.build/gen/go/gotocompany/proton/grpc/go/gotocompany/shield/v1beta1/shieldv1beta1grpc"
sirenv1beta1grpc "buf.build/gen/go/gotocompany/proton/grpc/go/gotocompany/siren/v1beta1/sirenv1beta1grpc"
)

type AlertService struct {
sirenClient sirenv1beta1grpc.SirenServiceClient
shieldClient shieldv1beta1rpc.ShieldServiceClient
}

func NewAlertService(
sirenClient sirenv1beta1grpc.SirenServiceClient,
shieldClient shieldv1beta1rpc.ShieldServiceClient,
) *AlertService {
return &AlertService{
sirenClient: sirenClient,
shieldClient: shieldClient,
}
}
74 changes: 73 additions & 1 deletion internal/server/v1/alert/http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import (

type Handler struct {
subscriptionService *SubscriptionService
alertService *Service
}

func NewHandler(subscriptionService *SubscriptionService) *Handler {
func NewHandler(subscriptionService *SubscriptionService, alertService *Service) *Handler {
return &Handler{
subscriptionService: subscriptionService,
alertService: alertService,
}
}

Expand Down Expand Up @@ -287,3 +289,73 @@ func (h *Handler) setAlertChannels(w http.ResponseWriter, r *http.Request) {
"alert_channels": alertChannels,
})
}

func (h *Handler) getAlerts(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
fmt.Println("here")
projectSlug := chi.URLParam(r, "project_slug")
resourceUrn := chi.URLParam(r, "resource_urn")

alerts, err := h.alertService.ListAlerts(ctx, projectSlug, resourceUrn)
if err != nil {
utils.WriteErr(w, err)
return
}

utils.WriteJSON(w, http.StatusOK,
utils.ListResponse[Alert]{Items: alerts})
}

func (h *Handler) getAlertPolicy(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
resourceType := chi.URLParam(r, "resource_type")
projectSlug := chi.URLParam(r, "project_slug")
resourceUrn := chi.URLParam(r, "resource_urn")

policy, err := h.alertService.GetAlertPolicy(ctx, projectSlug, resourceUrn, resourceType)
if err != nil {
utils.WriteErr(w, err)
return
}

var suppliedAlertVariableNames = []string{"name", "team", "entity"}

policy.Rules = RemoveSuppliedVariablesFromRules(policy.Rules, suppliedAlertVariableNames)

utils.WriteJSON(w, http.StatusOK, policy)
}

// func (h *Handler) setAlertPolicy(w http.ResponseWriter, r *http.Request) {
// ctx := r.Context()

// resourceType := chi.URLParam(r, "resource_type")
// resourceUrn := chi.URLParam(r, "resource_urn")
// projectSlug := chi.URLParam(r, "project_slug")

// var policyDef Policy
// if err := utils.ReadJSON(r, &policyDef); err != nil {
// utils.WriteErr(w, err)
// return
// }

// entity, err := h.sirenService.GetProjectDataSource(ctx, projectSlug)
// if err != nil {
// utils.WriteErr(w, err)
// return
// }

// policyDef.Rules = AddSuppliedVariablesFromRules(policyDef.Rules, map[string]string{
// "team": group.slug,
// "name": resourceUrn,
// "entity": entity,
// })

// alertPolicy, err := h.sirenService.UpsertAlertPolicy(ctx, projectSlug, policyDef)
// if err != nil {
// utils.WriteErr(w, err)
// return
// }

// utils.WriteJSON(w, http.StatusOK, alertPolicy)

// }
16 changes: 16 additions & 0 deletions internal/server/v1/alert/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,19 @@ func SubscriptionRoutes(
r.Put("/groups/{group_id}/alert_channels", handler.setAlertChannels)
}
}

func AlertRoutes(
siren sirenv1beta1rpc.SirenServiceClient,
shield shieldv1beta1rpc.ShieldServiceClient,
) func(chi.Router) {
subSrv := NewSubscriptionService(siren, shield)
handler := NewHandler(subSrv, Service)

Check failure on line 35 in internal/server/v1/alert/routes.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Service (type) is not an expression (typecheck)

Check failure on line 35 in internal/server/v1/alert/routes.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Service (type) is not an expression) (typecheck)

Check failure on line 35 in internal/server/v1/alert/routes.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Service (type) is not an expression) (typecheck)

Check failure on line 35 in internal/server/v1/alert/routes.go

View workflow job for this annotation

GitHub Actions / test

Service (type) is not an expression

return func(r chi.Router) {
// CRUD operations

r.Get("/{project_slug}/{resource_urn}", handler.getAlerts)
r.Get("/{resource_type}/{project_slug}/{resource_urn}/policies", handler.getAlertPolicy)
// r.Put("/{resource_type}/{project_slug}/{resource_urn}/policies", handler.setAlertPolicy)
}
}

0 comments on commit 385be84

Please sign in to comment.