provision_docr.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package provision
  2. import (
  3. "encoding/json"
  4. "net/http"
  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/analytics"
  11. "github.com/porter-dev/porter/internal/kubernetes/provisioner"
  12. "github.com/porter-dev/porter/internal/kubernetes/provisioner/do/docr"
  13. "github.com/porter-dev/porter/internal/models"
  14. "github.com/porter-dev/porter/internal/repository"
  15. "gorm.io/gorm"
  16. )
  17. type ProvisionDOCRHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. }
  20. func NewProvisionDOCRHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *ProvisionDOCRHandler {
  25. return &ProvisionDOCRHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. }
  28. }
  29. func (c *ProvisionDOCRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  30. // read the user and project from context
  31. user, _ := r.Context().Value(types.UserScope).(*models.User)
  32. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  33. request := &types.CreateDOCRInfraRequest{
  34. ProjectID: proj.ID,
  35. }
  36. if ok := c.DecodeAndValidate(w, r, request); !ok {
  37. return
  38. }
  39. // get the DO integration, to check that integration exists and belongs to the project
  40. doInt, err := c.Repo().OAuthIntegration().ReadOAuthIntegration(proj.ID, request.DOIntegrationID)
  41. if err != nil {
  42. if err == gorm.ErrRecordNotFound {
  43. c.HandleAPIError(w, r, apierrors.NewErrForbidden(err))
  44. } else {
  45. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  46. }
  47. return
  48. }
  49. suffix, err := repository.GenerateRandomBytes(6)
  50. if err != nil {
  51. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  52. return
  53. }
  54. lastApplied, err := json.Marshal(request)
  55. // parse infra last applied into DOCR config
  56. if err != nil {
  57. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  58. return
  59. }
  60. infra := &models.Infra{
  61. Kind: types.InfraDOCR,
  62. ProjectID: proj.ID,
  63. Suffix: suffix,
  64. Status: types.StatusCreating,
  65. DOIntegrationID: request.DOIntegrationID,
  66. CreatedByUserID: user.ID,
  67. LastApplied: lastApplied,
  68. }
  69. // handle write to the database
  70. infra, err = c.Repo().Infra().CreateInfra(infra)
  71. if err != nil {
  72. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  73. return
  74. }
  75. opts, err := GetSharedProvisionerOpts(c.Config(), infra)
  76. if err != nil {
  77. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  78. return
  79. }
  80. vaultToken := ""
  81. if c.Config().CredentialBackend != nil {
  82. vaultToken, err = c.Config().CredentialBackend.CreateOAuthToken(doInt)
  83. if err != nil {
  84. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  85. return
  86. }
  87. }
  88. opts.CredentialExchange.VaultToken = vaultToken
  89. opts.DOCR = &docr.Conf{
  90. DOCRName: request.DOCRName,
  91. DOCRSubscriptionTier: request.DOCRSubscriptionTier,
  92. }
  93. opts.OperationKind = provisioner.Apply
  94. err = c.Config().ProvisionerAgent.Provision(opts)
  95. if err != nil {
  96. infra.Status = types.StatusError
  97. infra, _ = c.Repo().Infra().UpdateInfra(infra)
  98. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  99. return
  100. }
  101. c.Config().AnalyticsClient.Track(analytics.RegistryProvisioningStartTrack(
  102. &analytics.RegistryProvisioningStartTrackOpts{
  103. ProjectScopedTrackOpts: analytics.GetProjectScopedTrackOpts(user.ID, proj.ID),
  104. RegistryType: types.InfraDOCR,
  105. InfraID: infra.ID,
  106. },
  107. ))
  108. c.WriteResult(w, r, infra.ToInfraType())
  109. }