provision_docr.go 3.2 KB

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