integration_handler.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. // HandleListHelmRepoIntegrations lists the Helm repo integrations available to the
  31. // instance
  32. func (app *App) HandleListHelmRepoIntegrations(w http.ResponseWriter, r *http.Request) {
  33. hrs := ints.PorterHelmRepoIntegrations
  34. w.WriteHeader(http.StatusOK)
  35. if err := json.NewEncoder(w).Encode(&hrs); err != nil {
  36. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  37. return
  38. }
  39. }
  40. // HandleListRepoIntegrations lists the repo integrations available to the
  41. // instance
  42. func (app *App) HandleListRepoIntegrations(w http.ResponseWriter, r *http.Request) {
  43. repos := ints.PorterGitRepoIntegrations
  44. w.WriteHeader(http.StatusOK)
  45. if err := json.NewEncoder(w).Encode(&repos); err != nil {
  46. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  47. return
  48. }
  49. }
  50. // HandleCreateGCPIntegration creates a new GCP integration in the DB
  51. func (app *App) HandleCreateGCPIntegration(w http.ResponseWriter, r *http.Request) {
  52. session, err := app.store.Get(r, app.cookieName)
  53. if err != nil {
  54. http.Error(w, err.Error(), http.StatusInternalServerError)
  55. return
  56. }
  57. userID, _ := session.Values["user_id"].(uint)
  58. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  59. if err != nil || projID == 0 {
  60. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  61. return
  62. }
  63. form := &forms.CreateGCPIntegrationForm{
  64. UserID: userID,
  65. ProjectID: uint(projID),
  66. }
  67. // decode from JSON to form value
  68. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  69. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  70. return
  71. }
  72. // validate the form
  73. if err := app.validator.Struct(form); err != nil {
  74. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  75. return
  76. }
  77. // convert the form to a gcp integration
  78. gcp, err := form.ToGCPIntegration()
  79. if err != nil {
  80. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  81. return
  82. }
  83. // handle write to the database
  84. gcp, err = app.repo.GCPIntegration.CreateGCPIntegration(gcp)
  85. if err != nil {
  86. app.handleErrorDataWrite(err, w)
  87. return
  88. }
  89. app.logger.Info().Msgf("New gcp integration created: %d", gcp.ID)
  90. w.WriteHeader(http.StatusCreated)
  91. gcpExt := gcp.Externalize()
  92. if err := json.NewEncoder(w).Encode(gcpExt); err != nil {
  93. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  94. return
  95. }
  96. }
  97. // HandleCreateAWSIntegration creates a new AWS integration in the DB
  98. func (app *App) HandleCreateAWSIntegration(w http.ResponseWriter, r *http.Request) {
  99. session, err := app.store.Get(r, app.cookieName)
  100. if err != nil {
  101. http.Error(w, err.Error(), http.StatusInternalServerError)
  102. return
  103. }
  104. userID, _ := session.Values["user_id"].(uint)
  105. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  106. if err != nil || projID == 0 {
  107. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  108. return
  109. }
  110. form := &forms.CreateAWSIntegrationForm{
  111. UserID: userID,
  112. ProjectID: uint(projID),
  113. }
  114. // decode from JSON to form value
  115. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  116. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  117. return
  118. }
  119. // validate the form
  120. if err := app.validator.Struct(form); err != nil {
  121. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  122. return
  123. }
  124. // convert the form to a aws integration
  125. aws, err := form.ToAWSIntegration()
  126. if err != nil {
  127. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  128. return
  129. }
  130. // handle write to the database
  131. aws, err = app.repo.AWSIntegration.CreateAWSIntegration(aws)
  132. if err != nil {
  133. app.handleErrorDataWrite(err, w)
  134. return
  135. }
  136. app.logger.Info().Msgf("New aws integration created: %d", aws.ID)
  137. w.WriteHeader(http.StatusCreated)
  138. awsExt := aws.Externalize()
  139. if err := json.NewEncoder(w).Encode(awsExt); err != nil {
  140. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  141. return
  142. }
  143. }
  144. // HandleCreateBasicAuthIntegration creates a new basic auth integration in the DB
  145. func (app *App) HandleCreateBasicAuthIntegration(w http.ResponseWriter, r *http.Request) {
  146. session, err := app.store.Get(r, app.cookieName)
  147. if err != nil {
  148. http.Error(w, err.Error(), http.StatusInternalServerError)
  149. return
  150. }
  151. userID, _ := session.Values["user_id"].(uint)
  152. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  153. if err != nil || projID == 0 {
  154. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  155. return
  156. }
  157. form := &forms.CreateBasicAuthIntegrationForm{
  158. UserID: userID,
  159. ProjectID: uint(projID),
  160. }
  161. // decode from JSON to form value
  162. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  163. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  164. return
  165. }
  166. // validate the form
  167. if err := app.validator.Struct(form); err != nil {
  168. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  169. return
  170. }
  171. // convert the form to a gcp integration
  172. basic, err := form.ToBasicIntegration()
  173. if err != nil {
  174. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  175. return
  176. }
  177. // handle write to the database
  178. basic, err = app.repo.BasicIntegration.CreateBasicIntegration(basic)
  179. if err != nil {
  180. app.handleErrorDataWrite(err, w)
  181. return
  182. }
  183. app.logger.Info().Msgf("New basic integration created: %d", basic.ID)
  184. w.WriteHeader(http.StatusCreated)
  185. basicExt := basic.Externalize()
  186. if err := json.NewEncoder(w).Encode(basicExt); err != nil {
  187. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  188. return
  189. }
  190. }