create.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package environment_groups
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/porter-dev/porter/api/server/authz"
  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/kubernetes/environment_groups"
  12. "github.com/porter-dev/porter/internal/models"
  13. "github.com/porter-dev/porter/internal/telemetry"
  14. )
  15. type UpdateEnvironmentGroupHandler struct {
  16. handlers.PorterHandlerReadWriter
  17. authz.KubernetesAgentGetter
  18. }
  19. func NewUpdateEnvironmentGroupHandler(
  20. config *config.Config,
  21. decoderValidator shared.RequestDecoderValidator,
  22. writer shared.ResultWriter,
  23. ) *UpdateEnvironmentGroupHandler {
  24. return &UpdateEnvironmentGroupHandler{
  25. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  26. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  27. }
  28. }
  29. type UpdateEnvironmentGroupRequest struct {
  30. // Name of the env group to create or update
  31. Name string `json:"name"`
  32. // Variables are values which are not sensitive. All values must be a string due to a kubernetes limitation.
  33. Variables map[string]string `json:"variables"`
  34. // SecretVariables are sensitive values. All values must be a string due to a kubernetes limitation.
  35. SecretVariables map[string]string `json:"secret_variables"`
  36. }
  37. type UpdateEnvironmentGroupResponse struct {
  38. // Name of the env group to create or update
  39. Name string `json:"name"`
  40. // Variables are variables which should are not sensitive. All values must be a string due to a kubernetes limitation.
  41. Variables map[string]string `json:"variables,omitempty"`
  42. // SecretVariables are sensitive variables. All values must be a string due to a kubernetes limitation.
  43. SecretVariables map[string]string `json:"secret_variables,omitempty"`
  44. CreatedAt time.Time `json:"created_at"`
  45. }
  46. func (c *UpdateEnvironmentGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  47. ctx, span := telemetry.NewSpan(r.Context(), "serve-update-env-group")
  48. defer span.End()
  49. request := &UpdateEnvironmentGroupRequest{}
  50. if ok := c.DecodeAndValidate(w, r, request); !ok {
  51. return
  52. }
  53. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  54. telemetry.WithAttributes(span,
  55. telemetry.AttributeKV{Key: "environment-group-name", Value: request.Name},
  56. )
  57. agent, err := c.GetAgent(r, cluster, "")
  58. if err != nil {
  59. err := telemetry.Error(ctx, span, err, "unable to connect to kubernetes cluster")
  60. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  61. return
  62. }
  63. envGroup := environment_groups.EnvironmentGroup{
  64. Name: request.Name,
  65. Variables: request.Variables,
  66. SecretVariables: request.SecretVariables,
  67. CreatedAtUTC: time.Now().UTC(),
  68. }
  69. err = environment_groups.CreateOrUpdateBaseEnvironmentGroup(ctx, agent, envGroup, nil)
  70. if err != nil {
  71. err := telemetry.Error(ctx, span, err, "unable to create or update environment group")
  72. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  73. return
  74. }
  75. envGroupResponse := &UpdateEnvironmentGroupResponse{
  76. Name: envGroup.Name,
  77. CreatedAt: envGroup.CreatedAtUTC,
  78. }
  79. c.WriteResult(w, r, envGroupResponse)
  80. // TODO: Syncing applications that are linked is currently done by the frontend. This should be done entirely
  81. // applicationsToSync, err := environment_groups.LinkedApplications(ctx, agent, envGroup.Name)
  82. // if err != nil {
  83. // err := telemetry.Error(ctx, span, err, "unable to find linked applications for environment group")
  84. // c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  85. // return
  86. // }
  87. // for _, app := range applicationsToSync {
  88. // TODO: Call porter app update
  89. // }
  90. }