create.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package environment_groups
  2. import (
  3. "net/http"
  4. "time"
  5. "connectrpc.com/connect"
  6. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  7. "github.com/porter-dev/porter/api/server/authz"
  8. "github.com/porter-dev/porter/api/server/handlers"
  9. "github.com/porter-dev/porter/api/server/shared"
  10. "github.com/porter-dev/porter/api/server/shared/apierrors"
  11. "github.com/porter-dev/porter/api/server/shared/config"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/internal/kubernetes/environment_groups"
  14. "github.com/porter-dev/porter/internal/models"
  15. "github.com/porter-dev/porter/internal/telemetry"
  16. )
  17. type UpdateEnvironmentGroupHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. authz.KubernetesAgentGetter
  20. }
  21. func NewUpdateEnvironmentGroupHandler(
  22. config *config.Config,
  23. decoderValidator shared.RequestDecoderValidator,
  24. writer shared.ResultWriter,
  25. ) *UpdateEnvironmentGroupHandler {
  26. return &UpdateEnvironmentGroupHandler{
  27. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  28. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  29. }
  30. }
  31. type UpdateEnvironmentGroupRequest struct {
  32. // Name of the env group to create or update
  33. Name string `json:"name"`
  34. // Type of the env group to create or update
  35. Type string `json:"type"`
  36. // AuthToken for the env group
  37. AuthToken string `json:"auth_token"`
  38. // Variables are values which are not sensitive. All values must be a string due to a kubernetes limitation.
  39. Variables map[string]string `json:"variables"`
  40. // SecretVariables are sensitive values. All values must be a string due to a kubernetes limitation.
  41. SecretVariables map[string]string `json:"secret_variables"`
  42. }
  43. type UpdateEnvironmentGroupResponse struct {
  44. // Name of the env group to create or update
  45. Name string `json:"name"`
  46. // Variables are variables which should are not sensitive. All values must be a string due to a kubernetes limitation.
  47. Variables map[string]string `json:"variables,omitempty"`
  48. // SecretVariables are sensitive variables. All values must be a string due to a kubernetes limitation.
  49. SecretVariables map[string]string `json:"secret_variables,omitempty"`
  50. CreatedAt time.Time `json:"created_at"`
  51. }
  52. func (c *UpdateEnvironmentGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  53. ctx, span := telemetry.NewSpan(r.Context(), "serve-update-env-group")
  54. defer span.End()
  55. request := &UpdateEnvironmentGroupRequest{}
  56. if ok := c.DecodeAndValidate(w, r, request); !ok {
  57. return
  58. }
  59. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  60. telemetry.WithAttributes(span,
  61. telemetry.AttributeKV{Key: "environment-group-name", Value: request.Name},
  62. telemetry.AttributeKV{Key: "environment-group-type", Value: request.Type},
  63. )
  64. agent, err := c.GetAgent(r, cluster, "")
  65. if err != nil {
  66. err := telemetry.Error(ctx, span, err, "unable to connect to kubernetes cluster")
  67. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  68. return
  69. }
  70. var envGroup environment_groups.EnvironmentGroup
  71. switch request.Type {
  72. case "doppler":
  73. _, err := c.Config().ClusterControlPlaneClient.CreateOrUpdateEnvGroup(ctx, connect.NewRequest(&porterv1.CreateOrUpdateEnvGroupRequest{
  74. ProjectId: int64(cluster.ProjectID),
  75. ClusterId: int64(cluster.ID),
  76. EnvGroupProviderType: porterv1.EnumEnvGroupProviderType_ENUM_ENV_GROUP_PROVIDER_TYPE_DOPPLER,
  77. EnvGroupName: request.Name,
  78. EnvGroupAuthToken: request.AuthToken,
  79. }))
  80. if err != nil {
  81. err := telemetry.Error(ctx, span, err, "unable to create environment group")
  82. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  83. return
  84. }
  85. envGroup = environment_groups.EnvironmentGroup{
  86. Name: request.Name,
  87. CreatedAtUTC: time.Now().UTC(),
  88. }
  89. default:
  90. envGroup := environment_groups.EnvironmentGroup{
  91. Name: request.Name,
  92. Variables: request.Variables,
  93. SecretVariables: request.SecretVariables,
  94. CreatedAtUTC: time.Now().UTC(),
  95. }
  96. err = environment_groups.CreateOrUpdateBaseEnvironmentGroup(ctx, agent, envGroup, nil)
  97. if err != nil {
  98. err := telemetry.Error(ctx, span, err, "unable to create or update environment group")
  99. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  100. return
  101. }
  102. }
  103. envGroupResponse := &UpdateEnvironmentGroupResponse{
  104. Name: envGroup.Name,
  105. CreatedAt: envGroup.CreatedAtUTC,
  106. }
  107. c.WriteResult(w, r, envGroupResponse)
  108. }