create.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. secrets := make(map[string][]byte)
  64. for k, v := range request.SecretVariables {
  65. secrets[k] = []byte(v)
  66. }
  67. envGroup := environment_groups.EnvironmentGroup{
  68. Name: request.Name,
  69. Variables: request.Variables,
  70. SecretVariables: secrets,
  71. CreatedAtUTC: time.Now().UTC(),
  72. }
  73. err = environment_groups.CreateOrUpdateBaseEnvironmentGroup(ctx, agent, envGroup)
  74. if err != nil {
  75. err := telemetry.Error(ctx, span, err, "unable to create or update environment group")
  76. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  77. return
  78. }
  79. envGroupResponse := &UpdateEnvironmentGroupResponse{
  80. Name: envGroup.Name,
  81. CreatedAt: envGroup.CreatedAtUTC,
  82. }
  83. c.WriteResult(w, r, envGroupResponse)
  84. // TODO: Syncing applications that are linked is currently done by the frontend. This should be done entirely
  85. // applicationsToSync, err := environment_groups.LinkedApplications(ctx, agent, envGroup.Name)
  86. // if err != nil {
  87. // err := telemetry.Error(ctx, span, err, "unable to find linked applications for environment group")
  88. // c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  89. // return
  90. // }
  91. // for _, app := range applicationsToSync {
  92. // TODO: Call porter app update
  93. // }
  94. }