project_handler.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 owner
  49. _, err = app.repo.Project.CreateProjectRole(projModel, &models.Role{
  50. UserID: userID,
  51. ProjectID: projModel.ID,
  52. Kind: "Owner",
  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. }
  61. // HandleReadProject returns an externalized Project (models.ProjectExternal)
  62. // based on an ID
  63. func (app *App) HandleReadProject(w http.ResponseWriter, r *http.Request) {
  64. id, err := strconv.ParseUint(chi.URLParam(r, "id"), 0, 64)
  65. if err != nil || id == 0 {
  66. app.handleErrorFormDecoding(err, ErrUserDecode, w)
  67. return
  68. }
  69. proj, err := app.repo.Project.ReadProject(uint(id))
  70. if err != nil {
  71. app.handleErrorRead(err, ErrProjectDataRead, w)
  72. return
  73. }
  74. projExt := proj.Externalize()
  75. w.WriteHeader(http.StatusOK)
  76. if err := json.NewEncoder(w).Encode(projExt); err != nil {
  77. app.handleErrorFormDecoding(err, ErrUserDecode, w)
  78. return
  79. }
  80. }
  81. // HandleCreateProjectSACandidates handles the creation of ServiceAccountCandidates
  82. // using a kubeconfig and a project id
  83. func (app *App) HandleCreateProjectSACandidates(w http.ResponseWriter, r *http.Request) {
  84. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  85. if err != nil || projID == 0 {
  86. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  87. return
  88. }
  89. form := &forms.CreateServiceAccountCandidatesForm{
  90. ProjectID: uint(projID),
  91. }
  92. // decode from JSON to form value
  93. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  94. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  95. return
  96. }
  97. // validate the form
  98. if err := app.validator.Struct(form); err != nil {
  99. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  100. return
  101. }
  102. // convert the form to a ServiceAccountCandidate
  103. saCandidates, err := form.ToServiceAccountCandidates()
  104. if err != nil {
  105. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  106. return
  107. }
  108. for _, saCandidate := range saCandidates {
  109. // handle write to the database
  110. saCandidate, err = app.repo.ServiceAccount.CreateServiceAccountCandidate(saCandidate)
  111. if err != nil {
  112. app.handleErrorDataWrite(err, w)
  113. return
  114. }
  115. app.logger.Info().Msgf("New service account candidate created: %d", saCandidate.ID)
  116. }
  117. w.WriteHeader(http.StatusCreated)
  118. }