get_notification_config.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package notifications
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "connectrpc.com/connect"
  7. "github.com/porter-dev/porter/api/server/shared/requestutils"
  8. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  9. "github.com/porter-dev/porter/api/server/shared/apierrors"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/internal/models"
  12. "github.com/porter-dev/porter/internal/telemetry"
  13. "github.com/porter-dev/porter/api/server/handlers"
  14. "github.com/porter-dev/porter/api/server/shared"
  15. "github.com/porter-dev/porter/api/server/shared/config"
  16. )
  17. // GetNotificationConfigHandler is the handler for the POST /notifications/config/{notification_config_id} endpoint
  18. type GetNotificationConfigHandler struct {
  19. handlers.PorterHandlerReadWriter
  20. }
  21. // NewNotificationConfigHandler returns a new GetNotificationConfigHandler
  22. func NewNotificationConfigHandler(
  23. config *config.Config,
  24. decoderValidator shared.RequestDecoderValidator,
  25. writer shared.ResultWriter,
  26. ) *GetNotificationConfigHandler {
  27. return &GetNotificationConfigHandler{
  28. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  29. }
  30. }
  31. // GetNotificationConfigRequest is the request object for the /notifications/config/{notification_config_id} endpoint
  32. type GetNotificationConfigRequest struct{}
  33. // GetNotificationConfigResponse is the response object for the /notifications/config/{notification_config_id} endpoint
  34. type GetNotificationConfigResponse struct {
  35. Config Config `json:"config"`
  36. }
  37. // ServeHTTP updates a notification config
  38. func (n *GetNotificationConfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  39. ctx, span := telemetry.NewSpan(r.Context(), "serve-notification-config")
  40. defer span.End()
  41. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  42. notificationConfigID, reqErr := requestutils.GetURLParamUint(r, types.URLParamNotificationConfigID)
  43. if reqErr != nil {
  44. e := telemetry.Error(ctx, span, nil, "error parsing event id from url")
  45. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest))
  46. return
  47. }
  48. telemetry.WithAttributes(span,
  49. telemetry.AttributeKV{Key: "notification-config-id", Value: notificationConfigID},
  50. )
  51. request := &GetNotificationConfigRequest{}
  52. if ok := n.DecodeAndValidate(w, r, request); !ok {
  53. err := telemetry.Error(ctx, span, nil, "error decoding request")
  54. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  55. return
  56. }
  57. configReq := connect.NewRequest(&porterv1.NotificationConfigRequest{
  58. ProjectId: int64(project.ID),
  59. NotificationConfigId: int64(notificationConfigID),
  60. })
  61. ccpResp, err := n.Config().ClusterControlPlaneClient.NotificationConfig(ctx, configReq)
  62. if err != nil {
  63. err := telemetry.Error(ctx, span, err, "error calling ccp apply porter app")
  64. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  65. return
  66. }
  67. if ccpResp == nil || ccpResp.Msg == nil {
  68. err := telemetry.Error(ctx, span, nil, "ccp response or msg is nil")
  69. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  70. return
  71. }
  72. config, err := configFromProto(ccpResp.Msg.Config)
  73. if err != nil {
  74. err := telemetry.Error(ctx, span, err, "error getting config from proto")
  75. n.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  76. return
  77. }
  78. response := &GetNotificationConfigResponse{
  79. Config: config,
  80. }
  81. n.WriteResult(w, r, response)
  82. }
  83. func configFromProto(proto *porterv1.NotificationConfig) (Config, error) {
  84. // initializing the map to true for all statuses and types
  85. // ensures that the default behavior is to notify for missing statuses and types
  86. statuses := trueMap(allStatuses)
  87. types := trueMap(allTypes)
  88. for _, protoStatus := range proto.EnabledStatuses {
  89. if status, ok := transformProtoToStatusString[protoStatus.Status]; ok {
  90. statuses[status] = protoStatus.Enabled
  91. }
  92. }
  93. for _, protoType := range proto.EnabledTypes {
  94. if t, ok := transformProtoToTypeString[protoType.Type]; ok {
  95. types[t] = protoType.Enabled
  96. }
  97. }
  98. statusesStruct := StatusesEnabled{}
  99. by, err := json.Marshal(statuses)
  100. if err != nil {
  101. return Config{}, fmt.Errorf("error marshalling statuses: %s", err)
  102. }
  103. err = json.Unmarshal(by, &statusesStruct)
  104. if err != nil {
  105. return Config{}, fmt.Errorf("error unmarshalling statuses: %s", err)
  106. }
  107. typesStruct := TypesEnabled{}
  108. by, err = json.Marshal(types)
  109. if err != nil {
  110. return Config{}, fmt.Errorf("error marshalling types: %s", err)
  111. }
  112. err = json.Unmarshal(by, &typesStruct)
  113. if err != nil {
  114. return Config{}, fmt.Errorf("error unmarshalling types: %s", err)
  115. }
  116. var mention string
  117. if proto.SlackConfig != nil && len(proto.SlackConfig.Mentions) > 0 {
  118. mention = proto.SlackConfig.Mentions[0]
  119. }
  120. config := Config{
  121. Statuses: statusesStruct,
  122. Mention: mention,
  123. Types: typesStruct,
  124. }
  125. return config, nil
  126. }