create.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package registry
  2. import (
  3. "fmt"
  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/models"
  12. "github.com/porter-dev/porter/internal/oauth"
  13. "github.com/porter-dev/porter/internal/registry"
  14. )
  15. type RegistryCreateHandler struct {
  16. handlers.PorterHandlerReadWriter
  17. }
  18. func NewRegistryCreateHandler(
  19. config *config.Config,
  20. decoderValidator shared.RequestDecoderValidator,
  21. writer shared.ResultWriter,
  22. ) *RegistryCreateHandler {
  23. return &RegistryCreateHandler{
  24. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  25. }
  26. }
  27. func (p *RegistryCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  28. user, _ := r.Context().Value(types.UserScope).(*models.User)
  29. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  30. operationID := oauth.CreateRandomState()
  31. p.Config().AnalyticsClient.Track(analytics.RegistryConnectionStartTrack(
  32. &analytics.RegistryConnectionStartTrackOpts{
  33. ProjectScopedTrackOpts: analytics.GetProjectScopedTrackOpts(user.ID, proj.ID),
  34. FlowID: operationID,
  35. },
  36. ))
  37. request := &types.CreateRegistryRequest{}
  38. ok := p.DecodeAndValidate(w, r, request)
  39. if !ok {
  40. return
  41. }
  42. // create a registry model
  43. regModel := &models.Registry{
  44. Name: request.Name,
  45. ProjectID: proj.ID,
  46. URL: request.URL,
  47. GCPIntegrationID: request.GCPIntegrationID,
  48. AWSIntegrationID: request.AWSIntegrationID,
  49. DOIntegrationID: request.DOIntegrationID,
  50. BasicIntegrationID: request.BasicIntegrationID,
  51. AzureIntegrationID: request.AzureIntegrationID,
  52. }
  53. if regModel.URL == "" && regModel.AWSIntegrationID != 0 {
  54. url, err := registry.GetECRRegistryURL(p.Repo().AWSIntegration(), regModel.ProjectID, regModel.AWSIntegrationID)
  55. if err != nil {
  56. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  57. return
  58. }
  59. regModel.URL = url
  60. } else if request.AzureIntegrationID != 0 {
  61. // if azure integration id is non-zero check that resource group name and repo name are set
  62. if request.ACRName == "" {
  63. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  64. fmt.Errorf("acr_name must be set if azure_integration_id is not 0"),
  65. http.StatusBadRequest,
  66. ))
  67. return
  68. } else if request.ACRResourceGroupName == "" {
  69. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  70. fmt.Errorf("acr_resource_group_name must be set if azure_integration_id is not 0"),
  71. http.StatusBadRequest,
  72. ))
  73. return
  74. }
  75. // get the azure integration and overwrite the names
  76. az, err := p.Repo().AzureIntegration().ReadAzureIntegration(proj.ID, request.AzureIntegrationID)
  77. if err != nil {
  78. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  79. return
  80. }
  81. az.ACRName = request.ACRName
  82. az.ACRResourceGroupName = request.ACRResourceGroupName
  83. az, err = p.Repo().AzureIntegration().OverwriteAzureIntegration(az)
  84. if err != nil {
  85. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  86. return
  87. }
  88. }
  89. // handle write to the database
  90. regModel, err := p.Repo().Registry().CreateRegistry(regModel)
  91. if err != nil {
  92. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  93. return
  94. }
  95. p.Config().AnalyticsClient.Track(analytics.RegistryConnectionSuccessTrack(
  96. &analytics.RegistryConnectionSuccessTrackOpts{
  97. RegistryScopedTrackOpts: analytics.GetRegistryScopedTrackOpts(user.ID, proj.ID, regModel.ID),
  98. FlowID: operationID,
  99. },
  100. ))
  101. p.WriteResult(w, r, regModel.ToRegistryType())
  102. }