notify_resolved_incident.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package cluster
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/porter-dev/porter/api/server/authz"
  6. "github.com/porter-dev/porter/api/server/handlers"
  7. "github.com/porter-dev/porter/api/server/shared"
  8. "github.com/porter-dev/porter/api/server/shared/apierrors"
  9. "github.com/porter-dev/porter/api/server/shared/config"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/internal/models"
  12. "github.com/porter-dev/porter/internal/notifier"
  13. "github.com/porter-dev/porter/internal/notifier/sendgrid"
  14. "github.com/porter-dev/porter/internal/notifier/slack"
  15. )
  16. type NotifyResolvedIncidentHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. authz.KubernetesAgentGetter
  19. }
  20. func NewNotifyResolvedIncidentHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *NotifyResolvedIncidentHandler {
  25. return &NotifyResolvedIncidentHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  28. }
  29. }
  30. func (c *NotifyResolvedIncidentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  31. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  32. request := &types.Incident{}
  33. if ok := c.DecodeAndValidate(w, r, request); !ok {
  34. return
  35. }
  36. slackInts, _ := c.Repo().SlackIntegration().ListSlackIntegrationsByProjectID(cluster.ProjectID)
  37. rel, err := c.Repo().Release().ReadRelease(cluster.ID, request.ReleaseName, request.ReleaseNamespace)
  38. if err != nil {
  39. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  40. return
  41. }
  42. var notifConf *types.NotificationConfig
  43. if rel != nil && rel.NotificationConfig != 0 {
  44. conf, err := c.Repo().NotificationConfig().ReadNotificationConfig(rel.NotificationConfig)
  45. if err != nil {
  46. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  47. return
  48. }
  49. notifConf = conf.ToNotificationConfigType()
  50. }
  51. notifiers := make([]notifier.IncidentNotifier, 0)
  52. if c.Config().SlackConf != nil {
  53. notifiers = append(notifiers, slack.NewIncidentNotifier(slackInts...))
  54. }
  55. if sc := c.Config().ServerConf; sc.SendgridAPIKey != "" && sc.SendgridSenderEmail != "" && sc.SendgridIncidentAlertTemplateID != "" {
  56. users, err := getUsersByProjectID(c.Repo(), cluster.ProjectID)
  57. if err != nil {
  58. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  59. return
  60. }
  61. notifiers = append(notifiers, sendgrid.NewIncidentNotifier(&sendgrid.IncidentNotifierOpts{
  62. SharedOpts: &sendgrid.SharedOpts{
  63. APIKey: c.Config().ServerConf.SendgridAPIKey,
  64. SenderEmail: c.Config().ServerConf.SendgridSenderEmail,
  65. },
  66. IncidentResolvedTemplateID: sc.SendgridIncidentResolvedTemplateID,
  67. Users: users,
  68. }))
  69. }
  70. multi := notifier.NewMultiIncidentNotifier(
  71. notifConf,
  72. notifiers...,
  73. )
  74. if !cluster.NotificationsDisabled {
  75. err := multi.NotifyResolved(
  76. request, fmt.Sprintf(
  77. "%s/applications/%s/%s/%s?project_id=%d",
  78. c.Config().ServerConf.ServerURL,
  79. cluster.Name,
  80. request.ReleaseNamespace,
  81. request.ReleaseName,
  82. cluster.ProjectID,
  83. ),
  84. )
  85. if err != nil {
  86. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  87. return
  88. }
  89. }
  90. }