provision_gcr.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/gcp/gcr"
  13. "github.com/porter-dev/porter/internal/models"
  14. "github.com/porter-dev/porter/internal/repository"
  15. "gorm.io/gorm"
  16. )
  17. type ProvisionGCRHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. }
  20. func NewProvisionGCRHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *ProvisionGCRHandler {
  25. return &ProvisionGCRHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. }
  28. }
  29. func (c *ProvisionGCRHandler) 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.CreateGCRInfraRequest{
  34. ProjectID: proj.ID,
  35. }
  36. if ok := c.DecodeAndValidate(w, r, request); !ok {
  37. return
  38. }
  39. // get the GCP integration, to check that integration exists and belongs to the project
  40. gcpInt, err := c.Repo().GCPIntegration().ReadGCPIntegration(proj.ID, request.GCPIntegrationID)
  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 GCR config
  56. if err != nil {
  57. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  58. return
  59. }
  60. infra := &models.Infra{
  61. Kind: types.InfraGCR,
  62. ProjectID: proj.ID,
  63. Suffix: suffix,
  64. Status: types.StatusCreating,
  65. GCPIntegrationID: request.GCPIntegrationID,
  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.CreateGCPToken(gcpInt)
  83. if err != nil {
  84. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  85. return
  86. }
  87. }
  88. opts.GCR = &gcr.Conf{
  89. GCPProjectID: gcpInt.GCPProjectID,
  90. }
  91. opts.CredentialExchange.VaultToken = vaultToken
  92. opts.OperationKind = provisioner.Apply
  93. err = c.Config().ProvisionerAgent.Provision(opts)
  94. if err != nil {
  95. infra.Status = types.StatusError
  96. infra, _ = c.Repo().Infra().UpdateInfra(infra)
  97. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  98. return
  99. }
  100. c.Config().AnalyticsClient.Track(analytics.RegistryProvisioningStartTrack(
  101. &analytics.RegistryProvisioningStartTrackOpts{
  102. ProjectScopedTrackOpts: analytics.GetProjectScopedTrackOpts(user.ID, proj.ID),
  103. RegistryType: types.InfraGCR,
  104. InfraID: infra.ID,
  105. },
  106. ))
  107. c.WriteResult(w, r, infra.ToInfraType())
  108. }