integration_handler.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. // HandleOverwriteAWSIntegration overwrites the ID of an AWS integration in the DB
  144. func (app *App) HandleOverwriteAWSIntegration(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. awsIntegrationID, err := strconv.ParseUint(chi.URLParam(r, "aws_integration_id"), 0, 64)
  156. if err != nil || awsIntegrationID == 0 {
  157. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  158. return
  159. }
  160. form := &forms.OverwriteAWSIntegrationForm{
  161. UserID: userID,
  162. ProjectID: uint(projID),
  163. }
  164. // decode from JSON to form value
  165. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  166. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  167. return
  168. }
  169. // validate the form
  170. if err := app.validator.Struct(form); err != nil {
  171. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  172. return
  173. }
  174. // read the aws integration by ID and overwrite the access id/secret
  175. awsIntegration, err := app.Repo.AWSIntegration.ReadAWSIntegration(uint(awsIntegrationID))
  176. if err != nil {
  177. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  178. return
  179. }
  180. awsIntegration.AWSAccessKeyID = []byte(form.AWSAccessKeyID)
  181. awsIntegration.AWSSecretAccessKey = []byte(form.AWSSecretAccessKey)
  182. // handle write to the database
  183. awsIntegration, err = app.Repo.AWSIntegration.OverwriteAWSIntegration(awsIntegration)
  184. if err != nil {
  185. app.handleErrorDataWrite(err, w)
  186. return
  187. }
  188. app.Logger.Info().Msgf("AWS integration overwritten: %d", awsIntegration.ID)
  189. w.WriteHeader(http.StatusCreated)
  190. awsExt := awsIntegration.Externalize()
  191. if err := json.NewEncoder(w).Encode(awsExt); err != nil {
  192. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  193. return
  194. }
  195. }
  196. // HandleCreateBasicAuthIntegration creates a new basic auth integration in the DB
  197. func (app *App) HandleCreateBasicAuthIntegration(w http.ResponseWriter, r *http.Request) {
  198. userID, err := app.getUserIDFromRequest(r)
  199. if err != nil {
  200. http.Error(w, err.Error(), http.StatusInternalServerError)
  201. return
  202. }
  203. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  204. if err != nil || projID == 0 {
  205. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  206. return
  207. }
  208. form := &forms.CreateBasicAuthIntegrationForm{
  209. UserID: userID,
  210. ProjectID: uint(projID),
  211. }
  212. // decode from JSON to form value
  213. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  214. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  215. return
  216. }
  217. // validate the form
  218. if err := app.validator.Struct(form); err != nil {
  219. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  220. return
  221. }
  222. // convert the form to a gcp integration
  223. basic, err := form.ToBasicIntegration()
  224. if err != nil {
  225. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  226. return
  227. }
  228. // handle write to the database
  229. basic, err = app.Repo.BasicIntegration.CreateBasicIntegration(basic)
  230. if err != nil {
  231. app.handleErrorDataWrite(err, w)
  232. return
  233. }
  234. app.Logger.Info().Msgf("New basic integration created: %d", basic.ID)
  235. w.WriteHeader(http.StatusCreated)
  236. basicExt := basic.Externalize()
  237. if err := json.NewEncoder(w).Encode(basicExt); err != nil {
  238. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  239. return
  240. }
  241. }
  242. // HandleListProjectOAuthIntegrations lists the oauth integrations for the project
  243. func (app *App) HandleListProjectOAuthIntegrations(w http.ResponseWriter, r *http.Request) {
  244. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  245. if err != nil || projID == 0 {
  246. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  247. return
  248. }
  249. oauthInts, err := app.Repo.OAuthIntegration.ListOAuthIntegrationsByProjectID(uint(projID))
  250. if err != nil {
  251. app.handleErrorDataRead(err, w)
  252. return
  253. }
  254. res := make([]*integrations.OAuthIntegrationExternal, 0)
  255. for _, oauthInt := range oauthInts {
  256. res = append(res, oauthInt.Externalize())
  257. }
  258. w.WriteHeader(http.StatusOK)
  259. if err := json.NewEncoder(w).Encode(res); err != nil {
  260. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  261. return
  262. }
  263. }