update.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package cluster
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/authz"
  5. "github.com/porter-dev/porter/api/server/handlers"
  6. "github.com/porter-dev/porter/api/server/shared"
  7. "github.com/porter-dev/porter/api/server/shared/apierrors"
  8. "github.com/porter-dev/porter/api/server/shared/config"
  9. "github.com/porter-dev/porter/api/types"
  10. "github.com/porter-dev/porter/internal/models"
  11. )
  12. type ClusterUpdateHandler struct {
  13. handlers.PorterHandlerReadWriter
  14. authz.KubernetesAgentGetter
  15. }
  16. func NewClusterUpdateHandler(
  17. config *config.Config,
  18. decoderValidator shared.RequestDecoderValidator,
  19. writer shared.ResultWriter,
  20. ) *ClusterUpdateHandler {
  21. return &ClusterUpdateHandler{
  22. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  23. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  24. }
  25. }
  26. func (c *ClusterUpdateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  27. cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
  28. request := &types.UpdateClusterRequest{}
  29. if ok := c.DecodeAndValidate(w, r, request); !ok {
  30. return
  31. }
  32. // if the cluster has an AWS integration, and the request does not have a cluster name attached, make
  33. // sure that the old cluster name is set
  34. if cluster.AWSIntegrationID != 0 && request.AWSClusterID == "" {
  35. awsInt, err := c.Repo().AWSIntegration().ReadAWSIntegration(cluster.ProjectID, cluster.AWSIntegrationID)
  36. if err != nil {
  37. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  38. return
  39. }
  40. if string(awsInt.AWSClusterID) == "" {
  41. awsInt.AWSClusterID = []byte(cluster.Name)
  42. awsInt, err = c.Repo().AWSIntegration().OverwriteAWSIntegration(awsInt)
  43. if err != nil {
  44. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  45. return
  46. }
  47. }
  48. } else if request.AWSClusterID != "" {
  49. cluster.AWSClusterID = request.AWSClusterID
  50. }
  51. if request.AgentIntegrationEnabled != nil {
  52. cluster.AgentIntegrationEnabled = *request.AgentIntegrationEnabled
  53. }
  54. if request.PreviewEnvsEnabled != nil {
  55. cluster.PreviewEnvsEnabled = *request.PreviewEnvsEnabled
  56. }
  57. if request.Name != "" && cluster.Name != request.Name {
  58. cluster.Name = request.Name
  59. }
  60. cluster, err := c.Repo().Cluster().UpdateCluster(cluster, c.Config().LaunchDarklyClient)
  61. if err != nil {
  62. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  63. return
  64. }
  65. c.WriteResult(w, r, cluster.ToClusterType())
  66. }