project_handler.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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"
  9. )
  10. // Enumeration of user API error codes, represented as int64
  11. const (
  12. ErrProjectDecode ErrorCode = iota + 600
  13. ErrProjectValidateFields
  14. ErrProjectDataRead
  15. )
  16. // HandleCreateProject validates a project form entry, converts the project to a gorm
  17. // model, and saves the user to the database
  18. func (app *App) HandleCreateProject(w http.ResponseWriter, r *http.Request) {
  19. session, err := app.store.Get(r, app.cookieName)
  20. if err != nil {
  21. http.Error(w, err.Error(), http.StatusInternalServerError)
  22. return
  23. }
  24. userID, _ := session.Values["user_id"].(uint)
  25. form := &forms.CreateProjectForm{}
  26. // decode from JSON to form value
  27. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  28. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  29. return
  30. }
  31. // validate the form
  32. if err := app.validator.Struct(form); err != nil {
  33. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  34. return
  35. }
  36. // convert the form to a project model
  37. projModel, err := form.ToProject(app.repo.Project)
  38. if err != nil {
  39. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  40. return
  41. }
  42. // handle write to the database
  43. projModel, err = app.repo.Project.CreateProject(projModel)
  44. if err != nil {
  45. app.handleErrorDataWrite(err, w)
  46. return
  47. }
  48. // create a new Role with the user as the admin
  49. _, err = app.repo.Project.CreateProjectRole(projModel, &models.Role{
  50. UserID: userID,
  51. ProjectID: projModel.ID,
  52. Kind: models.RoleAdmin,
  53. })
  54. if err != nil {
  55. app.handleErrorDataWrite(err, w)
  56. return
  57. }
  58. app.logger.Info().Msgf("New project created: %d", projModel.ID)
  59. w.WriteHeader(http.StatusCreated)
  60. projExt := projModel.Externalize()
  61. if err := json.NewEncoder(w).Encode(projExt); err != nil {
  62. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  63. return
  64. }
  65. }
  66. // HandleReadProject returns an externalized Project (models.ProjectExternal)
  67. // based on an ID
  68. func (app *App) HandleReadProject(w http.ResponseWriter, r *http.Request) {
  69. id, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  70. if err != nil || id == 0 {
  71. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  72. return
  73. }
  74. proj, err := app.repo.Project.ReadProject(uint(id))
  75. if err != nil {
  76. app.handleErrorRead(err, ErrProjectDataRead, w)
  77. return
  78. }
  79. projExt := proj.Externalize()
  80. w.WriteHeader(http.StatusOK)
  81. if err := json.NewEncoder(w).Encode(projExt); err != nil {
  82. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  83. return
  84. }
  85. }
  86. // HandleReadProjectCluster reads a cluster by id
  87. func (app *App) HandleReadProjectCluster(w http.ResponseWriter, r *http.Request) {
  88. id, err := strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
  89. if err != nil || id == 0 {
  90. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  91. return
  92. }
  93. cluster, err := app.repo.Cluster.ReadCluster(uint(id))
  94. if err != nil {
  95. app.handleErrorRead(err, ErrProjectDataRead, w)
  96. return
  97. }
  98. clusterExt := cluster.Externalize()
  99. w.WriteHeader(http.StatusOK)
  100. if err := json.NewEncoder(w).Encode(clusterExt); err != nil {
  101. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  102. return
  103. }
  104. }
  105. // HandleListProjectClusters returns a list of clusters that have linked Integrations.
  106. func (app *App) HandleListProjectClusters(w http.ResponseWriter, r *http.Request) {
  107. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  108. if err != nil || projID == 0 {
  109. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  110. return
  111. }
  112. clusters, err := app.repo.Cluster.ListClustersByProjectID(uint(projID))
  113. if err != nil {
  114. app.handleErrorRead(err, ErrProjectDataRead, w)
  115. return
  116. }
  117. extClusters := make([]*models.ClusterExternal, 0)
  118. for _, cluster := range clusters {
  119. extClusters = append(extClusters, cluster.Externalize())
  120. }
  121. w.WriteHeader(http.StatusOK)
  122. if err := json.NewEncoder(w).Encode(extClusters); err != nil {
  123. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  124. return
  125. }
  126. }
  127. // HandleCreateProjectClusterCandidates handles the creation of ClusterCandidates using
  128. // a kubeconfig and a project id
  129. func (app *App) HandleCreateProjectClusterCandidates(w http.ResponseWriter, r *http.Request) {
  130. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  131. if err != nil || projID == 0 {
  132. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  133. return
  134. }
  135. form := &forms.CreateClusterCandidatesForm{
  136. ProjectID: uint(projID),
  137. }
  138. // decode from JSON to form value
  139. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  140. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  141. return
  142. }
  143. // validate the form
  144. if err := app.validator.Struct(form); err != nil {
  145. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  146. return
  147. }
  148. // convert the form to a ClusterCandidate
  149. ccs, err := form.ToClusterCandidates(app.isLocal)
  150. if err != nil {
  151. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  152. return
  153. }
  154. extClusters := make([]*models.ClusterCandidateExternal, 0)
  155. session, err := app.store.Get(r, app.cookieName)
  156. if err != nil {
  157. http.Error(w, err.Error(), http.StatusInternalServerError)
  158. return
  159. }
  160. userID, _ := session.Values["user_id"].(uint)
  161. for _, cc := range ccs {
  162. // handle write to the database
  163. cc, err = app.repo.Cluster.CreateClusterCandidate(cc)
  164. if err != nil {
  165. app.handleErrorDataWrite(err, w)
  166. return
  167. }
  168. app.logger.Info().Msgf("New cluster candidate created: %d", cc.ID)
  169. // if the ClusterCandidate does not have any actions to perform, create the Cluster
  170. // automatically
  171. if len(cc.Resolvers) == 0 {
  172. // we query the repo again to get the decrypted version of the cluster candidate
  173. cc, err = app.repo.Cluster.ReadClusterCandidate(cc.ID)
  174. if err != nil {
  175. app.handleErrorDataRead(err, w)
  176. return
  177. }
  178. clusterForm := &forms.ResolveClusterForm{
  179. Resolver: &models.ClusterResolverAll{},
  180. ClusterCandidateID: cc.ID,
  181. ProjectID: uint(projID),
  182. UserID: userID,
  183. }
  184. err := clusterForm.ResolveIntegration(*app.repo)
  185. if err != nil {
  186. app.handleErrorDataWrite(err, w)
  187. return
  188. }
  189. cluster, err := clusterForm.ResolveCluster(*app.repo)
  190. if err != nil {
  191. app.handleErrorDataWrite(err, w)
  192. return
  193. }
  194. cc, err = app.repo.Cluster.UpdateClusterCandidateCreatedClusterID(cc.ID, cluster.ID)
  195. if err != nil {
  196. app.handleErrorDataWrite(err, w)
  197. return
  198. }
  199. app.logger.Info().Msgf("New cluster created: %d", cluster.ID)
  200. }
  201. extClusters = append(extClusters, cc.Externalize())
  202. }
  203. w.WriteHeader(http.StatusCreated)
  204. if err := json.NewEncoder(w).Encode(extClusters); err != nil {
  205. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  206. return
  207. }
  208. }
  209. // HandleListProjectClusterCandidates returns a list of externalized ClusterCandidates
  210. // ([]models.ClusterCandidateExternal) based on a project ID
  211. func (app *App) HandleListProjectClusterCandidates(w http.ResponseWriter, r *http.Request) {
  212. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  213. if err != nil || projID == 0 {
  214. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  215. return
  216. }
  217. ccs, err := app.repo.Cluster.ListClusterCandidatesByProjectID(uint(projID))
  218. if err != nil {
  219. app.handleErrorRead(err, ErrProjectDataRead, w)
  220. return
  221. }
  222. extCCs := make([]*models.ClusterCandidateExternal, 0)
  223. for _, cc := range ccs {
  224. extCCs = append(extCCs, cc.Externalize())
  225. }
  226. w.WriteHeader(http.StatusOK)
  227. if err := json.NewEncoder(w).Encode(extCCs); err != nil {
  228. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  229. return
  230. }
  231. }
  232. // HandleResolveClusterCandidate accepts a list of resolving objects (ClusterResolver)
  233. // for a given ClusterCandidate, which "resolves" that ClusterCandidate and creates a
  234. // Cluster for a specific project
  235. func (app *App) HandleResolveClusterCandidate(w http.ResponseWriter, r *http.Request) {
  236. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  237. if err != nil || projID == 0 {
  238. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  239. return
  240. }
  241. candID, err := strconv.ParseUint(chi.URLParam(r, "candidate_id"), 0, 64)
  242. if err != nil || projID == 0 {
  243. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  244. return
  245. }
  246. session, err := app.store.Get(r, app.cookieName)
  247. if err != nil {
  248. http.Error(w, err.Error(), http.StatusInternalServerError)
  249. return
  250. }
  251. userID, _ := session.Values["user_id"].(uint)
  252. // decode actions from request
  253. resolver := &models.ClusterResolverAll{}
  254. if err := json.NewDecoder(r.Body).Decode(resolver); err != nil {
  255. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  256. return
  257. }
  258. clusterResolver := &forms.ResolveClusterForm{
  259. Resolver: resolver,
  260. ClusterCandidateID: uint(candID),
  261. ProjectID: uint(projID),
  262. UserID: userID,
  263. }
  264. err = clusterResolver.ResolveIntegration(*app.repo)
  265. if err != nil {
  266. app.handleErrorDataWrite(err, w)
  267. return
  268. }
  269. cluster, err := clusterResolver.ResolveCluster(*app.repo)
  270. if err != nil {
  271. app.handleErrorDataWrite(err, w)
  272. return
  273. }
  274. _, err = app.repo.Cluster.UpdateClusterCandidateCreatedClusterID(uint(candID), cluster.ID)
  275. if err != nil {
  276. app.handleErrorDataWrite(err, w)
  277. return
  278. }
  279. app.logger.Info().Msgf("New cluster created: %d", cluster.ID)
  280. clusterExt := cluster.Externalize()
  281. w.WriteHeader(http.StatusCreated)
  282. if err := json.NewEncoder(w).Encode(clusterExt); err != nil {
  283. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  284. return
  285. }
  286. }
  287. // HandleDeleteProject deletes a project from the db, reading from the project_id
  288. // in the URL param
  289. func (app *App) HandleDeleteProject(w http.ResponseWriter, r *http.Request) {
  290. id, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  291. if err != nil || id == 0 {
  292. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  293. return
  294. }
  295. proj, err := app.repo.Project.ReadProject(uint(id))
  296. if err != nil {
  297. app.handleErrorRead(err, ErrProjectDataRead, w)
  298. return
  299. }
  300. proj, err = app.repo.Project.DeleteProject(proj)
  301. if err != nil {
  302. app.handleErrorRead(err, ErrProjectDataRead, w)
  303. return
  304. }
  305. projExternal := proj.Externalize()
  306. w.WriteHeader(http.StatusOK)
  307. if err := json.NewEncoder(w).Encode(projExternal); err != nil {
  308. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  309. return
  310. }
  311. }