2
0

app_notifications.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package porter_app
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/authz"
  5. "github.com/porter-dev/porter/api/server/shared/requestutils"
  6. "connectrpc.com/connect"
  7. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  8. "github.com/google/uuid"
  9. "github.com/porter-dev/porter/internal/porter_app"
  10. "github.com/porter-dev/porter/internal/porter_app/notifications"
  11. "github.com/porter-dev/porter/internal/telemetry"
  12. "github.com/porter-dev/porter/api/server/handlers"
  13. "github.com/porter-dev/porter/api/server/shared"
  14. "github.com/porter-dev/porter/api/server/shared/apierrors"
  15. "github.com/porter-dev/porter/api/server/shared/config"
  16. "github.com/porter-dev/porter/api/types"
  17. "github.com/porter-dev/porter/internal/models"
  18. )
  19. // AppNotificationsHandler handles requests to the /apps/{porter_app_name}/notifications endpoint
  20. type AppNotificationsHandler struct {
  21. handlers.PorterHandlerReadWriter
  22. authz.KubernetesAgentGetter
  23. }
  24. // NewAppNotificationsHandler returns a new AppNotificationsHandler
  25. func NewAppNotificationsHandler(
  26. config *config.Config,
  27. decoderValidator shared.RequestDecoderValidator,
  28. writer shared.ResultWriter,
  29. ) *AppNotificationsHandler {
  30. return &AppNotificationsHandler{
  31. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  32. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  33. }
  34. }
  35. // AppNotificationsRequest is the request object for the /apps/{porter_app_name}/notifications endpoint
  36. type AppNotificationsRequest struct {
  37. DeploymentTargetID string `schema:"deployment_target_id"`
  38. }
  39. // AppNotificationsResponse is the response object for the /apps/{porter_app_name}/notifications endpoint
  40. type AppNotificationsResponse struct {
  41. // Notifications are the notifications associated with the app revision
  42. Notifications []notifications.Notification `json:"notifications"`
  43. }
  44. func (c *AppNotificationsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  45. ctx, span := telemetry.NewSpan(r.Context(), "serve-app-notifications")
  46. defer span.End()
  47. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  48. appName, reqErr := requestutils.GetURLParamString(r, types.URLParamPorterAppName)
  49. if reqErr != nil {
  50. e := telemetry.Error(ctx, span, reqErr, "error parsing stack name from url")
  51. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest))
  52. return
  53. }
  54. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: appName})
  55. request := &AppNotificationsRequest{}
  56. if ok := c.DecodeAndValidate(w, r, request); !ok {
  57. err := telemetry.Error(ctx, span, nil, "error decoding request")
  58. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  59. return
  60. }
  61. _, err := uuid.Parse(request.DeploymentTargetID)
  62. if err != nil {
  63. err := telemetry.Error(ctx, span, err, "error parsing deployment target id")
  64. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  65. return
  66. }
  67. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID})
  68. porterApps, err := c.Repo().PorterApp().ReadPorterAppsByProjectIDAndName(project.ID, appName)
  69. if err != nil {
  70. err := telemetry.Error(ctx, span, err, "error getting porter apps")
  71. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  72. return
  73. }
  74. if len(porterApps) == 0 {
  75. err := telemetry.Error(ctx, span, err, "no porter apps returned")
  76. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  77. return
  78. }
  79. if len(porterApps) > 1 {
  80. err := telemetry.Error(ctx, span, err, "multiple porter apps returned; unable to determine which one to use")
  81. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  82. return
  83. }
  84. appId := porterApps[0].ID
  85. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-id", Value: appId})
  86. if appId == 0 {
  87. err := telemetry.Error(ctx, span, err, "porter app id is missing")
  88. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  89. return
  90. }
  91. currentAppRevisionReq := connect.NewRequest(&porterv1.CurrentAppRevisionRequest{
  92. ProjectId: int64(project.ID),
  93. AppId: int64(appId),
  94. DeploymentTargetIdentifier: &porterv1.DeploymentTargetIdentifier{
  95. Id: request.DeploymentTargetID,
  96. },
  97. })
  98. currentAppRevisionResp, err := c.Config().ClusterControlPlaneClient.CurrentAppRevision(ctx, currentAppRevisionReq)
  99. if err != nil {
  100. err := telemetry.Error(ctx, span, err, "error getting current app revision from cluster control plane client")
  101. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  102. return
  103. }
  104. if currentAppRevisionResp == nil || currentAppRevisionResp.Msg == nil {
  105. err := telemetry.Error(ctx, span, err, "current app revision resp is nil")
  106. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  107. return
  108. }
  109. appRevision := currentAppRevisionResp.Msg.AppRevision
  110. encodedRevision, err := porter_app.EncodedRevisionFromProto(ctx, appRevision)
  111. if err != nil {
  112. err := telemetry.Error(ctx, span, err, "error encoding revision from proto")
  113. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  114. return
  115. }
  116. appRevisionId := encodedRevision.ID
  117. appInstanceId := encodedRevision.AppInstanceID
  118. telemetry.WithAttributes(span,
  119. telemetry.AttributeKV{Key: "app-revision-id", Value: appRevisionId},
  120. telemetry.AttributeKV{Key: "app-instance-id", Value: appInstanceId},
  121. )
  122. notificationEvents, err := c.Repo().PorterAppEvent().ReadNotificationsByAppRevisionID(ctx, appInstanceId, appRevisionId)
  123. if err != nil {
  124. err := telemetry.Error(ctx, span, err, "error getting notifications from repo")
  125. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  126. return
  127. }
  128. latestNotifications := make([]notifications.Notification, 0)
  129. for _, event := range notificationEvents {
  130. notification, err := notifications.NotificationFromPorterAppEvent(event)
  131. if err != nil {
  132. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "notification-conversion-error", Value: err.Error()})
  133. continue
  134. }
  135. if notification == nil {
  136. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "notification-conversion-error", Value: "notification is nil"})
  137. continue
  138. }
  139. // TODO: remove this check once this attribute is not found in the span for >30 days
  140. if notification.Scope == "" {
  141. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "notification-conversion-error", Value: "old-notification-format"})
  142. continue
  143. }
  144. latestNotifications = append(latestNotifications, *notification)
  145. }
  146. response := AppNotificationsResponse{
  147. Notifications: latestNotifications,
  148. }
  149. c.WriteResult(w, r, response)
  150. }