update_notification_config.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package notifications
  2. import (
  3. "net/http"
  4. "connectrpc.com/connect"
  5. "github.com/porter-dev/porter/api/server/shared/requestutils"
  6. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  7. "github.com/porter-dev/porter/api/server/shared/apierrors"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/models"
  10. "github.com/porter-dev/porter/internal/telemetry"
  11. "github.com/porter-dev/porter/api/server/handlers"
  12. "github.com/porter-dev/porter/api/server/shared"
  13. "github.com/porter-dev/porter/api/server/shared/config"
  14. )
  15. // UpdateNotificationConfigHandler is the handler for the POST /notifications/{notification_config_id} endpoint
  16. type UpdateNotificationConfigHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. }
  19. // NewUpdateNotificationConfigHandler returns a new UpdateNotificationConfigHandler
  20. func NewUpdateNotificationConfigHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *UpdateNotificationConfigHandler {
  25. return &UpdateNotificationConfigHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. }
  28. }
  29. // UpdateNotificationConfigRequest is the request object for the /notifications/{notification_config_id} endpoint
  30. type UpdateNotificationConfigRequest struct {
  31. Config Config `json:"config"`
  32. SlackIntegrationID uint `json:"slack_integration_id"`
  33. }
  34. // Config is the config object for the /notifications endpoint
  35. type Config struct {
  36. Mention string `json:"mention"`
  37. Statuses []Status `json:"statuses"`
  38. }
  39. // Status is a wrapper object over a string for zod validation
  40. type Status struct {
  41. Status string `json:"status"`
  42. }
  43. // UpdateNotificationConfigResponse is the response object for the /notifications/{notification_config_id} endpoint
  44. type UpdateNotificationConfigResponse struct {
  45. ID uint `json:"id"`
  46. }
  47. // ServeHTTP updates a notification config
  48. func (n *UpdateNotificationConfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  49. ctx, span := telemetry.NewSpan(r.Context(), "serve-notification-config-update")
  50. defer span.End()
  51. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  52. telemetry.WithAttributes(span,
  53. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  54. )
  55. notificationConfigID, reqErr := requestutils.GetURLParamUint(r, types.URLParamNotificationConfigID)
  56. if reqErr != nil {
  57. e := telemetry.Error(ctx, span, nil, "error parsing event id from url")
  58. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest))
  59. return
  60. }
  61. telemetry.WithAttributes(span,
  62. telemetry.AttributeKV{Key: "notification-config-id", Value: notificationConfigID},
  63. )
  64. request := &UpdateNotificationConfigRequest{}
  65. if ok := n.DecodeAndValidate(w, r, request); !ok {
  66. err := telemetry.Error(ctx, span, nil, "error decoding request")
  67. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  68. return
  69. }
  70. updateReq := connect.NewRequest(&porterv1.UpdateNotificationConfigRequest{
  71. ProjectId: int64(project.ID),
  72. NotificationConfigId: int64(notificationConfigID),
  73. Config: configToProto(request.Config),
  74. SlackIntegrationId: int64(request.SlackIntegrationID),
  75. })
  76. updateResp, err := n.Config().ClusterControlPlaneClient.UpdateNotificationConfig(ctx, updateReq)
  77. if err != nil {
  78. err := telemetry.Error(ctx, span, err, "error calling ccp apply porter app")
  79. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  80. return
  81. }
  82. if updateResp == nil || updateResp.Msg == nil {
  83. err := telemetry.Error(ctx, span, nil, "ccp response or msg is nil")
  84. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  85. return
  86. }
  87. response := &UpdateNotificationConfigResponse{
  88. ID: uint(updateResp.Msg.NotificationConfigId),
  89. }
  90. n.WriteResult(w, r, response)
  91. }
  92. func configToProto(config Config) *porterv1.NotificationConfig {
  93. var statuses []porterv1.EnumNotificationStatus
  94. for _, status := range config.Statuses {
  95. statuses = append(statuses, transformStatusStringToProto[status.Status])
  96. }
  97. return &porterv1.NotificationConfig{
  98. Statuses: statuses,
  99. SlackConfig: &porterv1.SlackConfig{Mentions: []string{config.Mention}},
  100. }
  101. }
  102. var transformStatusStringToProto = map[string]porterv1.EnumNotificationStatus{
  103. "successful": porterv1.EnumNotificationStatus_ENUM_NOTIFICATION_STATUS_SUCCESSFUL,
  104. "failed": porterv1.EnumNotificationStatus_ENUM_NOTIFICATION_STATUS_FAILED,
  105. "progressing": porterv1.EnumNotificationStatus_ENUM_NOTIFICATION_STATUS_PROGRESSING,
  106. }