cluster_handler.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. // HandleCreateProjectCluster creates a new cluster
  11. func (app *App) HandleCreateProjectCluster(w http.ResponseWriter, r *http.Request) {
  12. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  13. if err != nil || projID == 0 {
  14. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  15. return
  16. }
  17. form := &forms.CreateClusterForm{
  18. ProjectID: uint(projID),
  19. }
  20. // decode from JSON to form value
  21. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  22. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  23. return
  24. }
  25. // validate the form
  26. if err := app.validator.Struct(form); err != nil {
  27. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  28. return
  29. }
  30. // convert the form to a registry
  31. cluster, err := form.ToCluster()
  32. if err != nil {
  33. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  34. return
  35. }
  36. // handle write to the database
  37. cluster, err = app.repo.Cluster.CreateCluster(cluster)
  38. if err != nil {
  39. app.handleErrorDataWrite(err, w)
  40. return
  41. }
  42. app.logger.Info().Msgf("New cluster created: %d", cluster.ID)
  43. w.WriteHeader(http.StatusCreated)
  44. clusterExt := cluster.Externalize()
  45. if err := json.NewEncoder(w).Encode(clusterExt); err != nil {
  46. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  47. return
  48. }
  49. }
  50. // HandleReadProjectCluster reads a cluster by id
  51. func (app *App) HandleReadProjectCluster(w http.ResponseWriter, r *http.Request) {
  52. id, err := strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
  53. if err != nil || id == 0 {
  54. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  55. return
  56. }
  57. cluster, err := app.repo.Cluster.ReadCluster(uint(id))
  58. if err != nil {
  59. app.handleErrorRead(err, ErrProjectDataRead, w)
  60. return
  61. }
  62. clusterExt := cluster.Externalize()
  63. w.WriteHeader(http.StatusOK)
  64. if err := json.NewEncoder(w).Encode(clusterExt); err != nil {
  65. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  66. return
  67. }
  68. }
  69. // HandleListProjectClusters returns a list of clusters that have linked Integrations.
  70. func (app *App) HandleListProjectClusters(w http.ResponseWriter, r *http.Request) {
  71. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  72. if err != nil || projID == 0 {
  73. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  74. return
  75. }
  76. clusters, err := app.repo.Cluster.ListClustersByProjectID(uint(projID))
  77. if err != nil {
  78. app.handleErrorRead(err, ErrProjectDataRead, w)
  79. return
  80. }
  81. extClusters := make([]*models.ClusterExternal, 0)
  82. for _, cluster := range clusters {
  83. extClusters = append(extClusters, cluster.Externalize())
  84. }
  85. w.WriteHeader(http.StatusOK)
  86. if err := json.NewEncoder(w).Encode(extClusters); err != nil {
  87. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  88. return
  89. }
  90. }
  91. // HandleCreateProjectClusterCandidates handles the creation of ClusterCandidates using
  92. // a kubeconfig and a project id
  93. func (app *App) HandleCreateProjectClusterCandidates(w http.ResponseWriter, r *http.Request) {
  94. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  95. if err != nil || projID == 0 {
  96. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  97. return
  98. }
  99. form := &forms.CreateClusterCandidatesForm{
  100. ProjectID: uint(projID),
  101. }
  102. // decode from JSON to form value
  103. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  104. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  105. return
  106. }
  107. // validate the form
  108. if err := app.validator.Struct(form); err != nil {
  109. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  110. return
  111. }
  112. // convert the form to a ClusterCandidate
  113. ccs, err := form.ToClusterCandidates(app.isLocal)
  114. if err != nil {
  115. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  116. return
  117. }
  118. extClusters := make([]*models.ClusterCandidateExternal, 0)
  119. session, err := app.store.Get(r, app.cookieName)
  120. if err != nil {
  121. http.Error(w, err.Error(), http.StatusInternalServerError)
  122. return
  123. }
  124. userID, _ := session.Values["user_id"].(uint)
  125. for _, cc := range ccs {
  126. // handle write to the database
  127. cc, err = app.repo.Cluster.CreateClusterCandidate(cc)
  128. if err != nil {
  129. app.handleErrorDataWrite(err, w)
  130. return
  131. }
  132. app.logger.Info().Msgf("New cluster candidate created: %d", cc.ID)
  133. // if the ClusterCandidate does not have any actions to perform, create the Cluster
  134. // automatically
  135. if len(cc.Resolvers) == 0 {
  136. // we query the repo again to get the decrypted version of the cluster candidate
  137. cc, err = app.repo.Cluster.ReadClusterCandidate(cc.ID)
  138. if err != nil {
  139. app.handleErrorDataRead(err, w)
  140. return
  141. }
  142. clusterForm := &forms.ResolveClusterForm{
  143. Resolver: &models.ClusterResolverAll{},
  144. ClusterCandidateID: cc.ID,
  145. ProjectID: uint(projID),
  146. UserID: userID,
  147. }
  148. err := clusterForm.ResolveIntegration(*app.repo)
  149. if err != nil {
  150. app.handleErrorDataWrite(err, w)
  151. return
  152. }
  153. cluster, err := clusterForm.ResolveCluster(*app.repo)
  154. if err != nil {
  155. app.handleErrorDataWrite(err, w)
  156. return
  157. }
  158. cc, err = app.repo.Cluster.UpdateClusterCandidateCreatedClusterID(cc.ID, cluster.ID)
  159. if err != nil {
  160. app.handleErrorDataWrite(err, w)
  161. return
  162. }
  163. app.logger.Info().Msgf("New cluster created: %d", cluster.ID)
  164. }
  165. extClusters = append(extClusters, cc.Externalize())
  166. }
  167. w.WriteHeader(http.StatusCreated)
  168. if err := json.NewEncoder(w).Encode(extClusters); err != nil {
  169. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  170. return
  171. }
  172. }
  173. // HandleListProjectClusterCandidates returns a list of externalized ClusterCandidates
  174. // ([]models.ClusterCandidateExternal) based on a project ID
  175. func (app *App) HandleListProjectClusterCandidates(w http.ResponseWriter, r *http.Request) {
  176. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  177. if err != nil || projID == 0 {
  178. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  179. return
  180. }
  181. ccs, err := app.repo.Cluster.ListClusterCandidatesByProjectID(uint(projID))
  182. if err != nil {
  183. app.handleErrorRead(err, ErrProjectDataRead, w)
  184. return
  185. }
  186. extCCs := make([]*models.ClusterCandidateExternal, 0)
  187. for _, cc := range ccs {
  188. extCCs = append(extCCs, cc.Externalize())
  189. }
  190. w.WriteHeader(http.StatusOK)
  191. if err := json.NewEncoder(w).Encode(extCCs); err != nil {
  192. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  193. return
  194. }
  195. }
  196. // HandleResolveClusterCandidate accepts a list of resolving objects (ClusterResolver)
  197. // for a given ClusterCandidate, which "resolves" that ClusterCandidate and creates a
  198. // Cluster for a specific project
  199. func (app *App) HandleResolveClusterCandidate(w http.ResponseWriter, r *http.Request) {
  200. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  201. if err != nil || projID == 0 {
  202. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  203. return
  204. }
  205. candID, err := strconv.ParseUint(chi.URLParam(r, "candidate_id"), 0, 64)
  206. if err != nil || projID == 0 {
  207. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  208. return
  209. }
  210. session, err := app.store.Get(r, app.cookieName)
  211. if err != nil {
  212. http.Error(w, err.Error(), http.StatusInternalServerError)
  213. return
  214. }
  215. userID, _ := session.Values["user_id"].(uint)
  216. // decode actions from request
  217. resolver := &models.ClusterResolverAll{}
  218. if err := json.NewDecoder(r.Body).Decode(resolver); err != nil {
  219. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  220. return
  221. }
  222. clusterResolver := &forms.ResolveClusterForm{
  223. Resolver: resolver,
  224. ClusterCandidateID: uint(candID),
  225. ProjectID: uint(projID),
  226. UserID: userID,
  227. }
  228. err = clusterResolver.ResolveIntegration(*app.repo)
  229. if err != nil {
  230. app.handleErrorDataWrite(err, w)
  231. return
  232. }
  233. cluster, err := clusterResolver.ResolveCluster(*app.repo)
  234. if err != nil {
  235. app.handleErrorDataWrite(err, w)
  236. return
  237. }
  238. _, err = app.repo.Cluster.UpdateClusterCandidateCreatedClusterID(uint(candID), cluster.ID)
  239. if err != nil {
  240. app.handleErrorDataWrite(err, w)
  241. return
  242. }
  243. app.logger.Info().Msgf("New cluster created: %d", cluster.ID)
  244. clusterExt := cluster.Externalize()
  245. w.WriteHeader(http.StatusCreated)
  246. if err := json.NewEncoder(w).Encode(clusterExt); err != nil {
  247. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  248. return
  249. }
  250. }