create.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package registry
  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/models"
  11. "github.com/porter-dev/porter/internal/oauth"
  12. "github.com/porter-dev/porter/internal/registry"
  13. )
  14. type RegistryCreateHandler struct {
  15. handlers.PorterHandlerReadWriter
  16. }
  17. func NewRegistryCreateHandler(
  18. config *config.Config,
  19. decoderValidator shared.RequestDecoderValidator,
  20. writer shared.ResultWriter,
  21. ) *RegistryCreateHandler {
  22. return &RegistryCreateHandler{
  23. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  24. }
  25. }
  26. func (p *RegistryCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  27. user, _ := r.Context().Value(types.UserScope).(*models.User)
  28. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  29. operationID := oauth.CreateRandomState()
  30. p.Config().AnalyticsClient.Track(analytics.RegistryConnectionStartTrack(
  31. &analytics.RegistryConnectionStartTrackOpts{
  32. ProjectScopedTrackOpts: analytics.GetProjectScopedTrackOpts(user.ID, proj.ID),
  33. FlowID: operationID,
  34. },
  35. ))
  36. request := &types.CreateRegistryRequest{}
  37. ok := p.DecodeAndValidate(w, r, request)
  38. if !ok {
  39. return
  40. }
  41. // create a registry model
  42. regModel := &models.Registry{
  43. Name: request.Name,
  44. ProjectID: proj.ID,
  45. URL: request.URL,
  46. GCPIntegrationID: request.GCPIntegrationID,
  47. AWSIntegrationID: request.AWSIntegrationID,
  48. DOIntegrationID: request.DOIntegrationID,
  49. BasicIntegrationID: request.BasicIntegrationID,
  50. AzureIntegrationID: request.AzureIntegrationID,
  51. }
  52. if regModel.URL == "" && regModel.AWSIntegrationID != 0 {
  53. url, err := registry.GetECRRegistryURL(p.Repo().AWSIntegration(), regModel.ProjectID, regModel.AWSIntegrationID)
  54. if err != nil {
  55. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  56. return
  57. }
  58. regModel.URL = url
  59. }
  60. // handle write to the database
  61. regModel, err := p.Repo().Registry().CreateRegistry(regModel)
  62. if err != nil {
  63. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  64. return
  65. }
  66. p.Config().AnalyticsClient.Track(analytics.RegistryConnectionSuccessTrack(
  67. &analytics.RegistryConnectionSuccessTrackOpts{
  68. RegistryScopedTrackOpts: analytics.GetRegistryScopedTrackOpts(user.ID, proj.ID, regModel.ID),
  69. FlowID: operationID,
  70. },
  71. ))
  72. p.WriteResult(w, r, regModel.ToRegistryType())
  73. }