cluster_handler.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. // HandleDeleteProjectCluster handles the deletion of a Cluster via the cluster ID
  92. func (app *App) HandleDeleteProjectCluster(w http.ResponseWriter, r *http.Request) {
  93. id, err := strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
  94. if err != nil || id == 0 {
  95. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  96. return
  97. }
  98. cluster, err := app.repo.Cluster.ReadCluster(uint(id))
  99. if err != nil {
  100. app.handleErrorRead(err, ErrProjectDataRead, w)
  101. return
  102. }
  103. err = app.repo.Cluster.DeleteCluster(cluster)
  104. if err != nil {
  105. app.handleErrorRead(err, ErrProjectDataRead, w)
  106. return
  107. }
  108. w.WriteHeader(http.StatusOK)
  109. }
  110. // HandleCreateProjectClusterCandidates handles the creation of ClusterCandidates using
  111. // a kubeconfig and a project id
  112. func (app *App) HandleCreateProjectClusterCandidates(w http.ResponseWriter, r *http.Request) {
  113. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  114. if err != nil || projID == 0 {
  115. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  116. return
  117. }
  118. form := &forms.CreateClusterCandidatesForm{
  119. ProjectID: uint(projID),
  120. }
  121. // decode from JSON to form value
  122. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  123. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  124. return
  125. }
  126. // validate the form
  127. if err := app.validator.Struct(form); err != nil {
  128. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  129. return
  130. }
  131. // convert the form to a ClusterCandidate
  132. ccs, err := form.ToClusterCandidates(app.isLocal)
  133. if err != nil {
  134. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  135. return
  136. }
  137. extClusters := make([]*models.ClusterCandidateExternal, 0)
  138. session, err := app.store.Get(r, app.cookieName)
  139. if err != nil {
  140. http.Error(w, err.Error(), http.StatusInternalServerError)
  141. return
  142. }
  143. userID, _ := session.Values["user_id"].(uint)
  144. for _, cc := range ccs {
  145. // handle write to the database
  146. cc, err = app.repo.Cluster.CreateClusterCandidate(cc)
  147. if err != nil {
  148. app.handleErrorDataWrite(err, w)
  149. return
  150. }
  151. app.logger.Info().Msgf("New cluster candidate created: %d", cc.ID)
  152. // if the ClusterCandidate does not have any actions to perform, create the Cluster
  153. // automatically
  154. if len(cc.Resolvers) == 0 {
  155. // we query the repo again to get the decrypted version of the cluster candidate
  156. cc, err = app.repo.Cluster.ReadClusterCandidate(cc.ID)
  157. if err != nil {
  158. app.handleErrorDataRead(err, w)
  159. return
  160. }
  161. clusterForm := &forms.ResolveClusterForm{
  162. Resolver: &models.ClusterResolverAll{},
  163. ClusterCandidateID: cc.ID,
  164. ProjectID: uint(projID),
  165. UserID: userID,
  166. }
  167. err := clusterForm.ResolveIntegration(*app.repo)
  168. if err != nil {
  169. app.handleErrorDataWrite(err, w)
  170. return
  171. }
  172. cluster, err := clusterForm.ResolveCluster(*app.repo)
  173. if err != nil {
  174. app.handleErrorDataWrite(err, w)
  175. return
  176. }
  177. cc, err = app.repo.Cluster.UpdateClusterCandidateCreatedClusterID(cc.ID, cluster.ID)
  178. if err != nil {
  179. app.handleErrorDataWrite(err, w)
  180. return
  181. }
  182. app.logger.Info().Msgf("New cluster created: %d", cluster.ID)
  183. }
  184. extClusters = append(extClusters, cc.Externalize())
  185. }
  186. w.WriteHeader(http.StatusCreated)
  187. if err := json.NewEncoder(w).Encode(extClusters); err != nil {
  188. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  189. return
  190. }
  191. }
  192. // HandleListProjectClusterCandidates returns a list of externalized ClusterCandidates
  193. // ([]models.ClusterCandidateExternal) based on a project ID
  194. func (app *App) HandleListProjectClusterCandidates(w http.ResponseWriter, r *http.Request) {
  195. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  196. if err != nil || projID == 0 {
  197. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  198. return
  199. }
  200. ccs, err := app.repo.Cluster.ListClusterCandidatesByProjectID(uint(projID))
  201. if err != nil {
  202. app.handleErrorRead(err, ErrProjectDataRead, w)
  203. return
  204. }
  205. extCCs := make([]*models.ClusterCandidateExternal, 0)
  206. for _, cc := range ccs {
  207. extCCs = append(extCCs, cc.Externalize())
  208. }
  209. w.WriteHeader(http.StatusOK)
  210. if err := json.NewEncoder(w).Encode(extCCs); err != nil {
  211. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  212. return
  213. }
  214. }
  215. // HandleResolveClusterCandidate accepts a list of resolving objects (ClusterResolver)
  216. // for a given ClusterCandidate, which "resolves" that ClusterCandidate and creates a
  217. // Cluster for a specific project
  218. func (app *App) HandleResolveClusterCandidate(w http.ResponseWriter, r *http.Request) {
  219. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  220. if err != nil || projID == 0 {
  221. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  222. return
  223. }
  224. candID, err := strconv.ParseUint(chi.URLParam(r, "candidate_id"), 0, 64)
  225. if err != nil || projID == 0 {
  226. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  227. return
  228. }
  229. session, err := app.store.Get(r, app.cookieName)
  230. if err != nil {
  231. http.Error(w, err.Error(), http.StatusInternalServerError)
  232. return
  233. }
  234. userID, _ := session.Values["user_id"].(uint)
  235. // decode actions from request
  236. resolver := &models.ClusterResolverAll{}
  237. if err := json.NewDecoder(r.Body).Decode(resolver); err != nil {
  238. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  239. return
  240. }
  241. clusterResolver := &forms.ResolveClusterForm{
  242. Resolver: resolver,
  243. ClusterCandidateID: uint(candID),
  244. ProjectID: uint(projID),
  245. UserID: userID,
  246. }
  247. err = clusterResolver.ResolveIntegration(*app.repo)
  248. if err != nil {
  249. app.handleErrorDataWrite(err, w)
  250. return
  251. }
  252. cluster, err := clusterResolver.ResolveCluster(*app.repo)
  253. if err != nil {
  254. app.handleErrorDataWrite(err, w)
  255. return
  256. }
  257. _, err = app.repo.Cluster.UpdateClusterCandidateCreatedClusterID(uint(candID), cluster.ID)
  258. if err != nil {
  259. app.handleErrorDataWrite(err, w)
  260. return
  261. }
  262. app.logger.Info().Msgf("New cluster created: %d", cluster.ID)
  263. clusterExt := cluster.Externalize()
  264. w.WriteHeader(http.StatusCreated)
  265. if err := json.NewEncoder(w).Encode(clusterExt); err != nil {
  266. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  267. return
  268. }
  269. }