event_handler.go 4.2 KB

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