create.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/types"
  11. "github.com/porter-dev/porter/internal/models"
  12. "gorm.io/gorm"
  13. )
  14. type HelmRepoCreateHandler struct {
  15. handlers.PorterHandlerReadWriter
  16. }
  17. func NewHelmRepoCreateHandler(
  18. config *config.Config,
  19. decoderValidator shared.RequestDecoderValidator,
  20. writer shared.ResultWriter,
  21. ) *HelmRepoCreateHandler {
  22. return &HelmRepoCreateHandler{
  23. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  24. }
  25. }
  26. func (p *HelmRepoCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  27. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  28. request := &types.CreateUpdateHelmRepoRequest{}
  29. ok := p.DecodeAndValidate(w, r, request)
  30. if !ok {
  31. return
  32. }
  33. // if a basic integration is specified, verify that it exists in the project
  34. if request.BasicIntegrationID != 0 {
  35. _, err := p.Repo().BasicIntegration().ReadBasicIntegration(proj.ID, request.BasicIntegrationID)
  36. if err != nil {
  37. if errors.Is(err, gorm.ErrRecordNotFound) {
  38. p.HandleAPIError(w, r, apierrors.NewErrForbidden(
  39. fmt.Errorf("basic integration with id %d not found in project %d", request.BasicIntegrationID, proj.ID),
  40. ))
  41. return
  42. }
  43. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  44. return
  45. }
  46. }
  47. hr := &models.HelmRepo{
  48. Name: request.Name,
  49. ProjectID: proj.ID,
  50. RepoURL: request.URL,
  51. BasicAuthIntegrationID: request.BasicIntegrationID,
  52. }
  53. // handle write to the database
  54. hr, err := p.Repo().HelmRepo().CreateHelmRepo(hr)
  55. if err != nil {
  56. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  57. return
  58. }
  59. p.WriteResult(w, r, hr.ToHelmRepoType())
  60. }