integration_handler.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. "github.com/porter-dev/porter/internal/models/integrations"
  9. ints "github.com/porter-dev/porter/internal/models/integrations"
  10. )
  11. // HandleListClusterIntegrations lists the cluster integrations available to the
  12. // instance
  13. func (app *App) HandleListClusterIntegrations(w http.ResponseWriter, r *http.Request) {
  14. clusters := ints.PorterClusterIntegrations
  15. w.WriteHeader(http.StatusOK)
  16. if err := json.NewEncoder(w).Encode(&clusters); err != nil {
  17. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  18. return
  19. }
  20. }
  21. // HandleListRegistryIntegrations lists the image registry integrations available to the
  22. // instance
  23. func (app *App) HandleListRegistryIntegrations(w http.ResponseWriter, r *http.Request) {
  24. registries := ints.PorterRegistryIntegrations
  25. w.WriteHeader(http.StatusOK)
  26. if err := json.NewEncoder(w).Encode(&registries); err != nil {
  27. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  28. return
  29. }
  30. }
  31. // HandleListHelmRepoIntegrations lists the Helm repo integrations available to the
  32. // instance
  33. func (app *App) HandleListHelmRepoIntegrations(w http.ResponseWriter, r *http.Request) {
  34. hrs := ints.PorterHelmRepoIntegrations
  35. w.WriteHeader(http.StatusOK)
  36. if err := json.NewEncoder(w).Encode(&hrs); err != nil {
  37. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  38. return
  39. }
  40. }
  41. // HandleListRepoIntegrations lists the repo integrations available to the
  42. // instance
  43. func (app *App) HandleListRepoIntegrations(w http.ResponseWriter, r *http.Request) {
  44. repos := ints.PorterGitRepoIntegrations
  45. w.WriteHeader(http.StatusOK)
  46. if err := json.NewEncoder(w).Encode(&repos); err != nil {
  47. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  48. return
  49. }
  50. }
  51. // HandleCreateGCPIntegration creates a new GCP integration in the DB
  52. func (app *App) HandleCreateGCPIntegration(w http.ResponseWriter, r *http.Request) {
  53. session, err := app.Store.Get(r, app.ServerConf.CookieName)
  54. if err != nil {
  55. http.Error(w, err.Error(), http.StatusInternalServerError)
  56. return
  57. }
  58. userID, _ := session.Values["user_id"].(uint)
  59. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  60. if err != nil || projID == 0 {
  61. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  62. return
  63. }
  64. form := &forms.CreateGCPIntegrationForm{
  65. UserID: userID,
  66. ProjectID: uint(projID),
  67. }
  68. // decode from JSON to form value
  69. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  70. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  71. return
  72. }
  73. // validate the form
  74. if err := app.validator.Struct(form); err != nil {
  75. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  76. return
  77. }
  78. // convert the form to a gcp integration
  79. gcp, err := form.ToGCPIntegration()
  80. if err != nil {
  81. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  82. return
  83. }
  84. // handle write to the database
  85. gcp, err = app.Repo.GCPIntegration.CreateGCPIntegration(gcp)
  86. if err != nil {
  87. app.handleErrorDataWrite(err, w)
  88. return
  89. }
  90. app.Logger.Info().Msgf("New gcp integration created: %d", gcp.ID)
  91. w.WriteHeader(http.StatusCreated)
  92. gcpExt := gcp.Externalize()
  93. if err := json.NewEncoder(w).Encode(gcpExt); err != nil {
  94. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  95. return
  96. }
  97. }
  98. // HandleCreateAWSIntegration creates a new AWS integration in the DB
  99. func (app *App) HandleCreateAWSIntegration(w http.ResponseWriter, r *http.Request) {
  100. session, err := app.Store.Get(r, app.ServerConf.CookieName)
  101. if err != nil {
  102. http.Error(w, err.Error(), http.StatusInternalServerError)
  103. return
  104. }
  105. userID, _ := session.Values["user_id"].(uint)
  106. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  107. if err != nil || projID == 0 {
  108. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  109. return
  110. }
  111. form := &forms.CreateAWSIntegrationForm{
  112. UserID: userID,
  113. ProjectID: uint(projID),
  114. }
  115. // decode from JSON to form value
  116. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  117. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  118. return
  119. }
  120. // validate the form
  121. if err := app.validator.Struct(form); err != nil {
  122. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  123. return
  124. }
  125. // convert the form to a aws integration
  126. aws, err := form.ToAWSIntegration()
  127. if err != nil {
  128. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  129. return
  130. }
  131. // handle write to the database
  132. aws, err = app.Repo.AWSIntegration.CreateAWSIntegration(aws)
  133. if err != nil {
  134. app.handleErrorDataWrite(err, w)
  135. return
  136. }
  137. app.Logger.Info().Msgf("New aws integration created: %d", aws.ID)
  138. w.WriteHeader(http.StatusCreated)
  139. awsExt := aws.Externalize()
  140. if err := json.NewEncoder(w).Encode(awsExt); err != nil {
  141. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  142. return
  143. }
  144. }
  145. // HandleCreateBasicAuthIntegration creates a new basic auth integration in the DB
  146. func (app *App) HandleCreateBasicAuthIntegration(w http.ResponseWriter, r *http.Request) {
  147. session, err := app.Store.Get(r, app.ServerConf.CookieName)
  148. if err != nil {
  149. http.Error(w, err.Error(), http.StatusInternalServerError)
  150. return
  151. }
  152. userID, _ := session.Values["user_id"].(uint)
  153. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  154. if err != nil || projID == 0 {
  155. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  156. return
  157. }
  158. form := &forms.CreateBasicAuthIntegrationForm{
  159. UserID: userID,
  160. ProjectID: uint(projID),
  161. }
  162. // decode from JSON to form value
  163. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  164. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  165. return
  166. }
  167. // validate the form
  168. if err := app.validator.Struct(form); err != nil {
  169. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  170. return
  171. }
  172. // convert the form to a gcp integration
  173. basic, err := form.ToBasicIntegration()
  174. if err != nil {
  175. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  176. return
  177. }
  178. // handle write to the database
  179. basic, err = app.Repo.BasicIntegration.CreateBasicIntegration(basic)
  180. if err != nil {
  181. app.handleErrorDataWrite(err, w)
  182. return
  183. }
  184. app.Logger.Info().Msgf("New basic integration created: %d", basic.ID)
  185. w.WriteHeader(http.StatusCreated)
  186. basicExt := basic.Externalize()
  187. if err := json.NewEncoder(w).Encode(basicExt); err != nil {
  188. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  189. return
  190. }
  191. }
  192. // HandleListProjectOAuthIntegrations lists the oauth integrations for the project
  193. func (app *App) HandleListProjectOAuthIntegrations(w http.ResponseWriter, r *http.Request) {
  194. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  195. if err != nil || projID == 0 {
  196. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  197. return
  198. }
  199. oauthInts, err := app.Repo.OAuthIntegration.ListOAuthIntegrationsByProjectID(uint(projID))
  200. if err != nil {
  201. app.handleErrorDataRead(err, w)
  202. return
  203. }
  204. res := make([]*integrations.OAuthIntegrationExternal, 0)
  205. for _, oauthInt := range oauthInts {
  206. res = append(res, oauthInt.Externalize())
  207. }
  208. w.WriteHeader(http.StatusOK)
  209. if err := json.NewEncoder(w).Encode(res); err != nil {
  210. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  211. return
  212. }
  213. }