latest_variables.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package environment_groups
  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/server/shared/requestutils"
  11. "github.com/porter-dev/porter/api/types"
  12. "github.com/porter-dev/porter/internal/models"
  13. "github.com/porter-dev/porter/internal/telemetry"
  14. )
  15. // LatestEnvGroupVariablesHandler is the handler for the /projects/{project_id}/clusters/{cluster_id}/environment-groups/{env_group_name}/latest endpoint
  16. type LatestEnvGroupVariablesHandler struct {
  17. handlers.PorterHandlerReadWriter
  18. }
  19. // NewLatestEnvGroupVariablesHandler handles GET requests to /projects/{project_id}/clusters/{cluster_id}/environment-groups/{env_group_name}/latest
  20. func NewLatestEnvGroupVariablesHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *LatestEnvGroupVariablesHandler {
  25. return &LatestEnvGroupVariablesHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. }
  28. }
  29. // LatestEnvGroupVariablesRequest is the request object for the /projects/{project_id}/clusters/{cluster_id}/environment-groups/{env_group_name}/latest endpoint
  30. type LatestEnvGroupVariablesRequest struct{}
  31. // LatestEnvGroupVariablesResponse is the response object for the /projects/{project_id}/clusters/{cluster_id}/environment-groups/{env_group_name}/latest endpoint
  32. type LatestEnvGroupVariablesResponse struct {
  33. Variables map[string]string `json:"variables"`
  34. Secrets map[string]string `json:"secrets"`
  35. }
  36. // ServeHTTP retrieves the latest env group variables from CCP and writes them to the response
  37. func (c *LatestEnvGroupVariablesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  38. ctx, span := telemetry.NewSpan(r.Context(), "serve-get-latest-env-group-variables")
  39. defer span.End()
  40. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  41. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  42. envGroupName, reqErr := requestutils.GetURLParamString(r, types.URLParamEnvGroupName)
  43. if reqErr != nil {
  44. err := telemetry.Error(ctx, span, reqErr, "error parsing env group name from url")
  45. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  46. return
  47. }
  48. latestVariablesReq := connect.NewRequest(&porterv1.LatestEnvGroupWithVariablesRequest{
  49. ProjectId: int64(project.ID),
  50. ClusterId: int64(cluster.ID),
  51. EnvGroupName: envGroupName,
  52. })
  53. ccpResp, err := c.Config().ClusterControlPlaneClient.LatestEnvGroupWithVariables(ctx, latestVariablesReq)
  54. if err != nil {
  55. err := telemetry.Error(ctx, span, err, "error getting env group variables from ccp")
  56. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  57. return
  58. }
  59. if ccpResp == nil || ccpResp.Msg == nil {
  60. err := telemetry.Error(ctx, span, nil, "ccp returned nil response")
  61. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  62. return
  63. }
  64. if ccpResp.Msg.EnvGroupVariables == nil {
  65. err := telemetry.Error(ctx, span, nil, "no env variables returned")
  66. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  67. return
  68. }
  69. res := &LatestEnvGroupVariablesResponse{
  70. Variables: ccpResp.Msg.EnvGroupVariables.Normal,
  71. Secrets: ccpResp.Msg.EnvGroupVariables.Secret,
  72. }
  73. c.WriteResult(w, r, res)
  74. }