create.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. // TODO!!!: validate the credentials here!!!
  43. // create a registry model
  44. regModel := &models.Registry{
  45. Name: request.Name,
  46. ProjectID: proj.ID,
  47. URL: request.URL,
  48. GCPIntegrationID: request.GCPIntegrationID,
  49. AWSIntegrationID: request.AWSIntegrationID,
  50. DOIntegrationID: request.DOIntegrationID,
  51. BasicIntegrationID: request.BasicIntegrationID,
  52. AzureIntegrationID: request.AzureIntegrationID,
  53. }
  54. if regModel.URL == "" && regModel.AWSIntegrationID != 0 {
  55. url, err := registry.GetECRRegistryURL(p.Repo().AWSIntegration(), regModel.ProjectID, regModel.AWSIntegrationID)
  56. if err != nil {
  57. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  58. return
  59. }
  60. regModel.URL = url
  61. } else if request.AzureIntegrationID != 0 {
  62. // if azure integration id is non-zero check that resource group name and repo name are set
  63. if request.ACRName == "" {
  64. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  65. fmt.Errorf("acr_name must be set if azure_integration_id is not 0"),
  66. http.StatusBadRequest,
  67. ))
  68. return
  69. } else if request.ACRResourceGroupName == "" {
  70. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  71. fmt.Errorf("acr_resource_group_name must be set if azure_integration_id is not 0"),
  72. http.StatusBadRequest,
  73. ))
  74. return
  75. }
  76. // get the azure integration and overwrite the names
  77. az, err := p.Repo().AzureIntegration().ReadAzureIntegration(proj.ID, request.AzureIntegrationID)
  78. if err != nil {
  79. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  80. return
  81. }
  82. az.ACRName = request.ACRName
  83. az.ACRResourceGroupName = request.ACRResourceGroupName
  84. az, err = p.Repo().AzureIntegration().OverwriteAzureIntegration(az)
  85. if err != nil {
  86. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  87. return
  88. }
  89. }
  90. // handle write to the database
  91. regModel, err := p.Repo().Registry().CreateRegistry(regModel)
  92. if err != nil {
  93. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  94. return
  95. }
  96. p.Config().AnalyticsClient.Track(analytics.RegistryConnectionSuccessTrack(
  97. &analytics.RegistryConnectionSuccessTrackOpts{
  98. RegistryScopedTrackOpts: analytics.GetRegistryScopedTrackOpts(user.ID, proj.ID, regModel.ID),
  99. FlowID: operationID,
  100. },
  101. ))
  102. p.WriteResult(w, r, regModel.ToRegistryType())
  103. }