event_handler.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "github.com/go-chi/chi"
  8. "github.com/gorilla/schema"
  9. "github.com/porter-dev/porter/internal/forms"
  10. "github.com/porter-dev/porter/internal/models"
  11. "github.com/porter-dev/porter/internal/repository"
  12. "gorm.io/gorm"
  13. )
  14. // HandleCreateEvent creates a new event in a project
  15. func (app *App) HandleCreateEvent(w http.ResponseWriter, r *http.Request) {
  16. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  17. if err != nil || projID == 0 {
  18. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  19. return
  20. }
  21. vals, err := url.ParseQuery(r.URL.RawQuery)
  22. clusterID, err := strconv.ParseUint(vals["cluster_id"][0], 10, 64)
  23. if err != nil {
  24. app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
  25. Code: ErrReleaseReadData,
  26. Errors: []string{"cluster not found"},
  27. }, w)
  28. }
  29. form := &forms.CreateEventForm{}
  30. // decode from JSON to form value
  31. if err := json.NewDecoder(r.Body).Decode(form); err != nil {
  32. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  33. return
  34. }
  35. // validate the form
  36. if err := app.validator.Struct(form); err != nil {
  37. app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
  38. return
  39. }
  40. // convert the form to an invite
  41. event := form.ToEvent(uint(projID), uint(clusterID))
  42. if err != nil {
  43. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  44. return
  45. }
  46. // handle write to the database
  47. event, err = app.Repo.Event.CreateEvent(event)
  48. if err != nil {
  49. app.handleErrorDataWrite(err, w)
  50. return
  51. }
  52. w.WriteHeader(http.StatusCreated)
  53. }
  54. // HandleListEvents lists the events that match certain conditions in a project
  55. func (app *App) HandleListEvents(w http.ResponseWriter, r *http.Request) {
  56. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  57. if err != nil || projID == 0 {
  58. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  59. return
  60. }
  61. vals, err := url.ParseQuery(r.URL.RawQuery)
  62. opts := &repository.ListEventOpts{}
  63. decoder := schema.NewDecoder()
  64. if err := decoder.Decode(opts, vals); err != nil {
  65. app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
  66. Code: ErrReleaseReadData,
  67. Errors: []string{"bad request"},
  68. }, w)
  69. }
  70. // handle write to the database
  71. events, err := app.Repo.Event.ListEventsByProjectID(uint(projID), opts)
  72. if err != nil {
  73. app.handleErrorDataWrite(err, w)
  74. return
  75. }
  76. eventExts := make([]*models.EventExternalSimple, 0)
  77. for _, event := range events {
  78. eventExts = append(eventExts, event.ExternalizeSimple())
  79. }
  80. w.WriteHeader(http.StatusOK)
  81. if err := json.NewEncoder(w).Encode(eventExts); err != nil {
  82. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  83. return
  84. }
  85. }
  86. // HandleListEvents lists the events that match certain conditions in a project
  87. func (app *App) HandleGetEvent(w http.ResponseWriter, r *http.Request) {
  88. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  89. if err != nil || projID == 0 {
  90. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  91. return
  92. }
  93. vals, err := url.ParseQuery(r.URL.RawQuery)
  94. clusterID, err := strconv.ParseUint(vals["cluster_id"][0], 10, 64)
  95. if err != nil {
  96. app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
  97. Code: ErrReleaseReadData,
  98. Errors: []string{"cluster not found"},
  99. }, w)
  100. }
  101. eventID, err := strconv.ParseUint(chi.URLParam(r, "event_id"), 0, 64)
  102. if err != nil || projID == 0 {
  103. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  104. return
  105. }
  106. event, err := app.Repo.Event.ReadEvent(uint(eventID), uint(projID), uint(clusterID))
  107. if err != nil {
  108. if err == gorm.ErrRecordNotFound {
  109. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  110. return
  111. }
  112. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  113. return
  114. }
  115. eventExt := event.Externalize()
  116. w.WriteHeader(http.StatusOK)
  117. if err := json.NewEncoder(w).Encode(eventExt); err != nil {
  118. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  119. return
  120. }
  121. }