event_handler.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. decoder.IgnoreUnknownKeys(true)
  65. if err := decoder.Decode(opts, vals); err != nil {
  66. app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
  67. Code: ErrReleaseReadData,
  68. Errors: []string{"bad request"},
  69. }, w)
  70. }
  71. // handle write to the database
  72. events, err := app.Repo.Event.ListEventsByProjectID(uint(projID), opts)
  73. if err != nil {
  74. app.handleErrorDataWrite(err, w)
  75. return
  76. }
  77. eventExts := make([]*models.EventExternalSimple, 0)
  78. for _, event := range events {
  79. eventExts = append(eventExts, event.ExternalizeSimple())
  80. }
  81. w.WriteHeader(http.StatusOK)
  82. if err := json.NewEncoder(w).Encode(eventExts); err != nil {
  83. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  84. return
  85. }
  86. }
  87. // HandleListEvents lists the events that match certain conditions in a project
  88. func (app *App) HandleGetEvent(w http.ResponseWriter, r *http.Request) {
  89. projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  90. if err != nil || projID == 0 {
  91. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  92. return
  93. }
  94. vals, err := url.ParseQuery(r.URL.RawQuery)
  95. clusterID, err := strconv.ParseUint(vals["cluster_id"][0], 10, 64)
  96. if err != nil {
  97. app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
  98. Code: ErrReleaseReadData,
  99. Errors: []string{"cluster not found"},
  100. }, w)
  101. }
  102. eventID, err := strconv.ParseUint(chi.URLParam(r, "event_id"), 0, 64)
  103. if err != nil || projID == 0 {
  104. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  105. return
  106. }
  107. event, err := app.Repo.Event.ReadEvent(uint(eventID), uint(projID), uint(clusterID))
  108. if err != nil {
  109. if err == gorm.ErrRecordNotFound {
  110. http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
  111. return
  112. }
  113. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  114. return
  115. }
  116. eventExt := event.Externalize()
  117. w.WriteHeader(http.StatusOK)
  118. if err := json.NewEncoder(w).Encode(eventExt); err != nil {
  119. app.handleErrorFormDecoding(err, ErrProjectDecode, w)
  120. return
  121. }
  122. }