2
0

create.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package registry
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "github.com/porter-dev/porter/api/server/handlers"
  7. "github.com/porter-dev/porter/api/server/shared"
  8. "github.com/porter-dev/porter/api/server/shared/apierrors"
  9. "github.com/porter-dev/porter/api/server/shared/config"
  10. "github.com/porter-dev/porter/api/types"
  11. "github.com/porter-dev/porter/internal/analytics"
  12. "github.com/porter-dev/porter/internal/models"
  13. "github.com/porter-dev/porter/internal/oauth"
  14. "github.com/porter-dev/porter/internal/registry"
  15. "gorm.io/gorm"
  16. )
  17. type RegistryCreateHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. }
  20. func NewRegistryCreateHandler(
  21. config *config.Config,
  22. decoderValidator shared.RequestDecoderValidator,
  23. writer shared.ResultWriter,
  24. ) *RegistryCreateHandler {
  25. return &RegistryCreateHandler{
  26. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  27. }
  28. }
  29. func (p *RegistryCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  30. user, _ := r.Context().Value(types.UserScope).(*models.User)
  31. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  32. operationID := oauth.CreateRandomState()
  33. p.Config().AnalyticsClient.Track(analytics.RegistryConnectionStartTrack(
  34. &analytics.RegistryConnectionStartTrackOpts{
  35. ProjectScopedTrackOpts: analytics.GetProjectScopedTrackOpts(user.ID, proj.ID),
  36. FlowID: operationID,
  37. },
  38. ))
  39. request := &types.CreateRegistryRequest{}
  40. ok := p.DecodeAndValidate(w, r, request)
  41. if !ok {
  42. return
  43. }
  44. // validate request before saving it to the DB
  45. integrationIDs := []uint{
  46. request.GCPIntegrationID,
  47. request.AWSIntegrationID,
  48. request.DOIntegrationID,
  49. request.BasicIntegrationID,
  50. request.AzureIntegrationID,
  51. }
  52. idCount := 0
  53. for _, id := range integrationIDs {
  54. if id != 0 {
  55. idCount += 1
  56. }
  57. }
  58. if idCount > 1 {
  59. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  60. fmt.Errorf("only one integration ID should be set"), http.StatusBadRequest,
  61. ))
  62. return
  63. } else if idCount == 0 {
  64. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  65. fmt.Errorf("at least one integration ID should be set"), http.StatusBadRequest,
  66. ))
  67. return
  68. }
  69. var err error
  70. if request.GCPIntegrationID != 0 {
  71. _, err := p.Repo().GCPIntegration().ReadGCPIntegration(proj.ID, request.GCPIntegrationID)
  72. if err != nil {
  73. if errors.Is(err, gorm.ErrRecordNotFound) {
  74. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  75. fmt.Errorf("no such GCP integration ID: %d for project ID: %d", request.GCPIntegrationID, proj.ID),
  76. http.StatusNotFound,
  77. ))
  78. return
  79. }
  80. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  81. return
  82. }
  83. } else if request.AWSIntegrationID != 0 {
  84. _, err = p.Repo().AWSIntegration().ReadAWSIntegration(proj.ID, request.AWSIntegrationID)
  85. if err != nil {
  86. if errors.Is(err, gorm.ErrRecordNotFound) {
  87. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  88. fmt.Errorf("no such AWS integration ID: %d for project ID: %d", request.AWSIntegrationID, proj.ID),
  89. http.StatusNotFound,
  90. ))
  91. return
  92. }
  93. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  94. return
  95. }
  96. } else if request.DOIntegrationID != 0 {
  97. _, err = p.Repo().OAuthIntegration().ReadOAuthIntegration(proj.ID, request.DOIntegrationID)
  98. if err != nil {
  99. if errors.Is(err, gorm.ErrRecordNotFound) {
  100. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  101. fmt.Errorf("no such DO integration ID: %d for project ID: %d", request.DOIntegrationID, proj.ID),
  102. http.StatusNotFound,
  103. ))
  104. return
  105. }
  106. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  107. return
  108. }
  109. } else if request.BasicIntegrationID != 0 {
  110. _, err = p.Repo().BasicIntegration().ReadBasicIntegration(proj.ID, request.BasicIntegrationID)
  111. if err != nil {
  112. if errors.Is(err, gorm.ErrRecordNotFound) {
  113. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  114. fmt.Errorf("no such basic integration ID: %d for project ID: %d", request.BasicIntegrationID, proj.ID),
  115. http.StatusNotFound,
  116. ))
  117. return
  118. }
  119. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  120. return
  121. }
  122. } else if request.AzureIntegrationID != 0 {
  123. _, err = p.Repo().AzureIntegration().ReadAzureIntegration(proj.ID, request.AzureIntegrationID)
  124. if err != nil {
  125. if errors.Is(err, gorm.ErrRecordNotFound) {
  126. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  127. fmt.Errorf("no such Azure integration ID: %d for project ID: %d", request.AzureIntegrationID, proj.ID),
  128. http.StatusNotFound,
  129. ))
  130. return
  131. }
  132. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  133. return
  134. }
  135. }
  136. // create a registry model
  137. regModel := &models.Registry{
  138. Name: request.Name,
  139. ProjectID: proj.ID,
  140. URL: request.URL,
  141. GCPIntegrationID: request.GCPIntegrationID,
  142. AWSIntegrationID: request.AWSIntegrationID,
  143. DOIntegrationID: request.DOIntegrationID,
  144. BasicIntegrationID: request.BasicIntegrationID,
  145. AzureIntegrationID: request.AzureIntegrationID,
  146. }
  147. if regModel.URL == "" && regModel.AWSIntegrationID != 0 {
  148. url, err := registry.GetECRRegistryURL(p.Repo().AWSIntegration(), regModel.ProjectID, regModel.AWSIntegrationID)
  149. if err != nil {
  150. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  151. return
  152. }
  153. regModel.URL = url
  154. } else if request.AzureIntegrationID != 0 {
  155. // if azure integration id is non-zero check that resource group name and repo name are set
  156. if request.ACRName == "" {
  157. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  158. fmt.Errorf("acr_name must be set if azure_integration_id is not 0"),
  159. http.StatusBadRequest,
  160. ))
  161. return
  162. } else if request.ACRResourceGroupName == "" {
  163. p.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
  164. fmt.Errorf("acr_resource_group_name must be set if azure_integration_id is not 0"),
  165. http.StatusBadRequest,
  166. ))
  167. return
  168. }
  169. // get the azure integration and overwrite the names
  170. az, err := p.Repo().AzureIntegration().ReadAzureIntegration(proj.ID, request.AzureIntegrationID)
  171. if err != nil {
  172. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  173. return
  174. }
  175. az.ACRName = request.ACRName
  176. az.ACRResourceGroupName = request.ACRResourceGroupName
  177. _, err = p.Repo().AzureIntegration().OverwriteAzureIntegration(az)
  178. if err != nil {
  179. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  180. return
  181. }
  182. }
  183. // handle write to the database
  184. regModel, err = p.Repo().Registry().CreateRegistry(regModel)
  185. if err != nil {
  186. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  187. return
  188. }
  189. p.Config().AnalyticsClient.Track(analytics.RegistryConnectionSuccessTrack(
  190. &analytics.RegistryConnectionSuccessTrackOpts{
  191. RegistryScopedTrackOpts: analytics.GetRegistryScopedTrackOpts(user.ID, proj.ID, regModel.ID),
  192. FlowID: operationID,
  193. },
  194. ))
  195. w.WriteHeader(http.StatusCreated)
  196. p.WriteResult(w, r, regModel.ToRegistryType())
  197. }