app_instances.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package porter_app
  2. import (
  3. "net/http"
  4. "connectrpc.com/connect"
  5. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  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/porter_app"
  13. "github.com/porter-dev/porter/internal/telemetry"
  14. )
  15. // AppInstancesHandler is the handler for the /apps/instances endpoint
  16. type AppInstancesHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. }
  19. // NewAppInstancesHandler handles GET requests to the /apps/instances endpoint
  20. func NewAppInstancesHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *AppInstancesHandler {
  25. return &AppInstancesHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. }
  28. }
  29. // AppInstancesRequest is the request object for the /apps/instances endpoint
  30. type AppInstancesRequest struct {
  31. DeploymentTargetID string `schema:"deployment_target_id"`
  32. }
  33. // AppInstancesResponse is the response object for the /apps/instances endpoint
  34. type AppInstancesResponse struct {
  35. AppInstances []porter_app.AppInstance `json:"app_instances"`
  36. }
  37. // ServeHTTP translates the request into a ListAppInstancesRequest to the cluster control plane
  38. func (c *AppInstancesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  39. ctx, span := telemetry.NewSpan(r.Context(), "serve-list-app-instances")
  40. defer span.End()
  41. project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  42. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  43. request := &AppInstancesRequest{}
  44. if ok := c.DecodeAndValidate(w, r, request); !ok {
  45. err := telemetry.Error(ctx, span, nil, "error decoding request")
  46. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  47. return
  48. }
  49. telemetry.WithAttributes(span,
  50. telemetry.AttributeKV{Key: "project-id", Value: project.ID},
  51. telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID},
  52. telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID},
  53. )
  54. var deploymentTargetIdentifier *porterv1.DeploymentTargetIdentifier
  55. if request.DeploymentTargetID != "" {
  56. deploymentTargetIdentifier = &porterv1.DeploymentTargetIdentifier{
  57. Id: request.DeploymentTargetID,
  58. }
  59. }
  60. listAppInstancesReq := connect.NewRequest(&porterv1.ListAppInstancesRequest{
  61. ProjectId: int64(project.ID),
  62. DeploymentTargetIdentifier: deploymentTargetIdentifier,
  63. })
  64. latestAppInstancesResp, err := c.Config().ClusterControlPlaneClient.ListAppInstances(ctx, listAppInstancesReq)
  65. if err != nil {
  66. err = telemetry.Error(ctx, span, err, "error getting latest app revisions")
  67. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  68. return
  69. }
  70. if latestAppInstancesResp == nil || latestAppInstancesResp.Msg == nil {
  71. err = telemetry.Error(ctx, span, nil, "latest app revisions response is nil")
  72. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  73. return
  74. }
  75. var appInstances []porter_app.AppInstance
  76. for _, instance := range latestAppInstancesResp.Msg.AppInstances {
  77. appInstances = append(appInstances, porter_app.AppInstance{
  78. Id: instance.Id,
  79. DeploymentTarget: porter_app.DeploymentTarget{
  80. ID: instance.DeploymentTargetId,
  81. Name: "",
  82. },
  83. Name: instance.Name,
  84. })
  85. }
  86. c.WriteResult(w, r, AppInstancesResponse{AppInstances: appInstances})
  87. }