provision_docr.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package provision
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/handlers"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/apierrors"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/kubernetes"
  10. "github.com/porter-dev/porter/internal/kubernetes/provisioner"
  11. "github.com/porter-dev/porter/internal/models"
  12. "github.com/porter-dev/porter/internal/repository"
  13. "gorm.io/gorm"
  14. )
  15. type ProvisionDOCRHandler struct {
  16. handlers.PorterHandlerReadWriter
  17. }
  18. func NewProvisionDOCRHandler(
  19. config *config.Config,
  20. decoderValidator shared.RequestDecoderValidator,
  21. writer shared.ResultWriter,
  22. ) *ProvisionDOCRHandler {
  23. return &ProvisionDOCRHandler{
  24. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  25. }
  26. }
  27. func (c *ProvisionDOCRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. // read the project from context
  29. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  30. request := &types.CreateDOCRInfraRequest{}
  31. if ok := c.DecodeAndValidate(w, r, request); !ok {
  32. return
  33. }
  34. // get the AWS integration
  35. doInt, err := c.Repo().OAuthIntegration().ReadOAuthIntegration(proj.ID, request.DOIntegrationID)
  36. if err != nil {
  37. if err == gorm.ErrRecordNotFound {
  38. c.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
  39. } else {
  40. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  41. }
  42. return
  43. }
  44. suffix, err := repository.GenerateRandomBytes(6)
  45. if err != nil {
  46. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  47. return
  48. }
  49. infra := &models.Infra{
  50. Kind: types.InfraDOCR,
  51. ProjectID: proj.ID,
  52. Suffix: suffix,
  53. Status: types.StatusCreating,
  54. DOIntegrationID: request.DOIntegrationID,
  55. }
  56. // handle write to the database
  57. infra, err = c.Repo().Infra().CreateInfra(infra)
  58. if err != nil {
  59. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  60. return
  61. }
  62. // launch provisioning pod
  63. _, err = c.Config().ProvisionerAgent.ProvisionDOCR(
  64. &kubernetes.SharedProvisionOpts{
  65. ProjectID: proj.ID,
  66. Repo: c.Repo(),
  67. Infra: infra,
  68. Operation: provisioner.Apply,
  69. PGConf: c.Config().DBConf,
  70. RedisConf: c.Config().RedisConf,
  71. ProvImageTag: c.Config().ServerConf.ProvisionerImageTag,
  72. ProvImagePullSecret: c.Config().ServerConf.ProvisionerImagePullSecret,
  73. },
  74. doInt,
  75. c.Config().DOConf,
  76. request.DOCRName,
  77. request.DOCRSubscriptionTier,
  78. )
  79. if err != nil {
  80. infra.Status = types.StatusError
  81. infra, _ = c.Repo().Infra().UpdateInfra(infra)
  82. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  83. return
  84. }
  85. c.WriteResult(w, r, infra.ToInfraType())
  86. }