integration_handler.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. userID, err := app.getUserIDFromRequest(r)
  54. if err != nil {
  55. http.Error(w, err.Error(), http.StatusInternalServerError)
  56. return
  57. }
  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. userID, err := app.getUserIDFromRequest(r)
  100. if err != nil {
  101. http.Error(w, err.Error(), http.StatusInternalServerError)
  102. return
  103. }
  104. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  105. if err != nil || projID == 0 {
  106. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  107. return
  108. }
  109. form := &forms.CreateAWSIntegrationForm{
  110. UserID: userID,
  111. ProjectID: uint(projID),
  112. }
  113. // decode from JSON to form value
  114. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  115. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  116. return
  117. }
  118. // validate the form
  119. if err := app.validator.Struct(form); err != nil {
  120. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  121. return
  122. }
  123. // convert the form to a aws integration
  124. aws, err := form.ToAWSIntegration()
  125. if err != nil {
  126. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  127. return
  128. }
  129. // handle write to the database
  130. aws, err = app.Repo.AWSIntegration().CreateAWSIntegration(aws)
  131. if err != nil {
  132. app.handleErrorDataWrite(err, w)
  133. return
  134. }
  135. app.Logger.Info().Msgf("New aws integration created: %d", aws.ID)
  136. w.WriteHeader(http.StatusCreated)
  137. awsExt := aws.Externalize()
  138. if err := json.NewEncoder(w).Encode(awsExt); err != nil {
  139. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  140. return
  141. }
  142. }
  143. // HandleCreateBasicAuthIntegration creates a new basic auth integration in the DB
  144. func (app *App) HandleCreateBasicAuthIntegration(w http.ResponseWriter, r *http.Request) {
  145. userID, err := app.getUserIDFromRequest(r)
  146. if err != nil {
  147. http.Error(w, err.Error(), http.StatusInternalServerError)
  148. return
  149. }
  150. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  151. if err != nil || projID == 0 {
  152. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  153. return
  154. }
  155. form := &forms.CreateBasicAuthIntegrationForm{
  156. UserID: userID,
  157. ProjectID: uint(projID),
  158. }
  159. // decode from JSON to form value
  160. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  161. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  162. return
  163. }
  164. // validate the form
  165. if err := app.validator.Struct(form); err != nil {
  166. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  167. return
  168. }
  169. // convert the form to a gcp integration
  170. basic, err := form.ToBasicIntegration()
  171. if err != nil {
  172. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  173. return
  174. }
  175. // handle write to the database
  176. basic, err = app.Repo.BasicIntegration().CreateBasicIntegration(basic)
  177. if err != nil {
  178. app.handleErrorDataWrite(err, w)
  179. return
  180. }
  181. app.Logger.Info().Msgf("New basic integration created: %d", basic.ID)
  182. w.WriteHeader(http.StatusCreated)
  183. basicExt := basic.Externalize()
  184. if err := json.NewEncoder(w).Encode(basicExt); err != nil {
  185. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  186. return
  187. }
  188. }
  189. // HandleListProjectOAuthIntegrations lists the oauth integrations for the project
  190. func (app *App) HandleListProjectOAuthIntegrations(w http.ResponseWriter, r *http.Request) {
  191. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  192. if err != nil || projID == 0 {
  193. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  194. return
  195. }
  196. oauthInts, err := app.Repo.OAuthIntegration().ListOAuthIntegrationsByProjectID(uint(projID))
  197. if err != nil {
  198. app.handleErrorDataRead(err, w)
  199. return
  200. }
  201. res := make([]*integrations.OAuthIntegrationExternal, 0)
  202. for _, oauthInt := range oauthInts {
  203. res = append(res, oauthInt.Externalize())
  204. }
  205. w.WriteHeader(http.StatusOK)
  206. if err := json.NewEncoder(w).Encode(res); err != nil {
  207. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  208. return
  209. }
  210. }