update.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package helmrepo
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  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. "gorm.io/gorm"
  14. )
  15. type HelmRepoUpdateHandler struct {
  16. handlers.PorterHandlerReadWriter
  17. }
  18. func NewHelmRepoUpdateHandler(
  19. config *config.Config,
  20. decoderValidator shared.RequestDecoderValidator,
  21. writer shared.ResultWriter,
  22. ) *HelmRepoUpdateHandler {
  23. return &HelmRepoUpdateHandler{
  24. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  25. }
  26. }
  27. func (p *HelmRepoUpdateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  29. helmRepoID, reqErr := requestutils.GetURLParamUint(r, "helm_repo_id")
  30. if reqErr != nil {
  31. p.HandleAPIError(w, r, reqErr)
  32. return
  33. }
  34. helmRepo, err := p.Repo().HelmRepo().ReadHelmRepo(proj.ID, helmRepoID)
  35. if err != nil {
  36. if errors.Is(err, gorm.ErrRecordNotFound) {
  37. p.HandleAPIError(w, r, apierrors.NewErrNotFound(fmt.Errorf("no such helm repo")))
  38. return
  39. }
  40. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  41. return
  42. }
  43. request := &types.CreateUpdateHelmRepoRequest{}
  44. ok := p.DecodeAndValidate(w, r, request)
  45. if !ok {
  46. return
  47. }
  48. if request.BasicIntegrationID != 0 &&
  49. helmRepo.BasicAuthIntegrationID != 0 &&
  50. request.BasicIntegrationID != helmRepo.BasicAuthIntegrationID {
  51. bi, err := p.Repo().BasicIntegration().ReadBasicIntegration(proj.ID, helmRepo.BasicAuthIntegrationID)
  52. if err != nil {
  53. if !errors.Is(err, gorm.ErrRecordNotFound) {
  54. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  55. return
  56. }
  57. } else {
  58. _, err = p.Repo().BasicIntegration().DeleteBasicIntegration(bi)
  59. if err != nil {
  60. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  61. return
  62. }
  63. }
  64. }
  65. // if a basic integration is specified, verify that it exists in the project
  66. if request.BasicIntegrationID != 0 {
  67. _, err := p.Repo().BasicIntegration().ReadBasicIntegration(proj.ID, request.BasicIntegrationID)
  68. if err != nil {
  69. if errors.Is(err, gorm.ErrRecordNotFound) {
  70. p.HandleAPIError(w, r, apierrors.NewErrForbidden(
  71. fmt.Errorf("basic integration with id %d not found in project %d", request.BasicIntegrationID, proj.ID),
  72. ))
  73. return
  74. }
  75. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  76. return
  77. }
  78. }
  79. helmRepo.Name = request.Name
  80. helmRepo.RepoURL = request.URL
  81. helmRepo.BasicAuthIntegrationID = request.BasicIntegrationID
  82. helmRepo, err = p.Repo().HelmRepo().UpdateHelmRepo(helmRepo)
  83. if err != nil {
  84. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  85. return
  86. }
  87. p.WriteResult(w, r, helmRepo.ToHelmRepoType())
  88. }