integration_handler.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "github.com/go-chi/chi"
  7. "github.com/porter-dev/porter/internal/forms"
  8. ints "github.com/porter-dev/porter/internal/models/integrations"
  9. )
  10. // HandleListClusterIntegrations lists the cluster integrations available to the
  11. // instance
  12. func (app *App) HandleListClusterIntegrations(w http.ResponseWriter, r *http.Request) {
  13. clusters := ints.PorterClusterIntegrations
  14. w.WriteHeader(http.StatusOK)
  15. if err := json.NewEncoder(w).Encode(&clusters); err != nil {
  16. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  17. return
  18. }
  19. }
  20. // HandleListRegistryIntegrations lists the image registry integrations available to the
  21. // instance
  22. func (app *App) HandleListRegistryIntegrations(w http.ResponseWriter, r *http.Request) {
  23. registries := ints.PorterRegistryIntegrations
  24. w.WriteHeader(http.StatusOK)
  25. if err := json.NewEncoder(w).Encode(&registries); err != nil {
  26. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  27. return
  28. }
  29. }
  30. // HandleListRepoIntegrations lists the repo integrations available to the
  31. // instance
  32. func (app *App) HandleListRepoIntegrations(w http.ResponseWriter, r *http.Request) {
  33. repos := ints.PorterRepoIntegrations
  34. w.WriteHeader(http.StatusOK)
  35. if err := json.NewEncoder(w).Encode(&repos); err != nil {
  36. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  37. return
  38. }
  39. }
  40. // HandleCreateGCPIntegration creates a new GCP integration in the DB
  41. func (app *App) HandleCreateGCPIntegration(w http.ResponseWriter, r *http.Request) {
  42. session, err := app.store.Get(r, app.cookieName)
  43. if err != nil {
  44. http.Error(w, err.Error(), http.StatusInternalServerError)
  45. return
  46. }
  47. userID, _ := session.Values["user_id"].(uint)
  48. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  49. if err != nil || projID == 0 {
  50. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  51. return
  52. }
  53. form := &forms.CreateGCPIntegrationForm{
  54. UserID: userID,
  55. ProjectID: uint(projID),
  56. }
  57. // decode from JSON to form value
  58. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  59. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  60. return
  61. }
  62. // validate the form
  63. if err := app.validator.Struct(form); err != nil {
  64. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  65. return
  66. }
  67. // convert the form to a gcp integration
  68. gcp, err := form.ToGCPIntegration()
  69. if err != nil {
  70. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  71. return
  72. }
  73. // handle write to the database
  74. gcp, err = app.repo.GCPIntegration.CreateGCPIntegration(gcp)
  75. if err != nil {
  76. app.handleErrorDataWrite(err, w)
  77. return
  78. }
  79. app.logger.Info().Msgf("New gcp integration created: %d", gcp.ID)
  80. w.WriteHeader(http.StatusCreated)
  81. gcpExt := gcp.Externalize()
  82. if err := json.NewEncoder(w).Encode(gcpExt); err != nil {
  83. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  84. return
  85. }
  86. }
  87. // HandleCreateAWSIntegration creates a new AWS integration in the DB
  88. func (app *App) HandleCreateAWSIntegration(w http.ResponseWriter, r *http.Request) {
  89. session, err := app.store.Get(r, app.cookieName)
  90. if err != nil {
  91. http.Error(w, err.Error(), http.StatusInternalServerError)
  92. return
  93. }
  94. userID, _ := session.Values["user_id"].(uint)
  95. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  96. if err != nil || projID == 0 {
  97. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  98. return
  99. }
  100. form := &forms.CreateAWSIntegrationForm{
  101. UserID: userID,
  102. ProjectID: uint(projID),
  103. }
  104. // decode from JSON to form value
  105. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  106. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  107. return
  108. }
  109. // validate the form
  110. if err := app.validator.Struct(form); err != nil {
  111. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  112. return
  113. }
  114. // convert the form to a aws integration
  115. aws, err := form.ToAWSIntegration()
  116. if err != nil {
  117. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  118. return
  119. }
  120. // handle write to the database
  121. aws, err = app.repo.AWSIntegration.CreateAWSIntegration(aws)
  122. if err != nil {
  123. app.handleErrorDataWrite(err, w)
  124. return
  125. }
  126. app.logger.Info().Msgf("New aws integration created: %d", aws.ID)
  127. w.WriteHeader(http.StatusCreated)
  128. awsExt := aws.Externalize()
  129. if err := json.NewEncoder(w).Encode(awsExt); err != nil {
  130. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  131. return
  132. }
  133. }