|
@@ -7,7 +7,11 @@ import (
|
|
|
"strconv"
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
"github.com/go-chi/chi"
|
|
|
|
|
+ "github.com/gorilla/schema"
|
|
|
"github.com/porter-dev/porter/internal/forms"
|
|
"github.com/porter-dev/porter/internal/forms"
|
|
|
|
|
+ "github.com/porter-dev/porter/internal/models"
|
|
|
|
|
+ "github.com/porter-dev/porter/internal/repository"
|
|
|
|
|
+ "gorm.io/gorm"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
// HandleCreateEvent creates a new event in a project
|
|
// HandleCreateEvent creates a new event in a project
|
|
@@ -62,3 +66,96 @@ func (app *App) HandleCreateEvent(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+// HandleListEvents lists the events that match certain conditions in a project
|
|
|
|
|
+func (app *App) HandleListEvents(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
+ projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
|
|
|
|
|
+
|
|
|
|
|
+ if err != nil || projID == 0 {
|
|
|
|
|
+ app.handleErrorFormDecoding(err, ErrProjectDecode, w)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ vals, err := url.ParseQuery(r.URL.RawQuery)
|
|
|
|
|
+
|
|
|
|
|
+ opts := &repository.ListEventOpts{}
|
|
|
|
|
+
|
|
|
|
|
+ decoder := schema.NewDecoder()
|
|
|
|
|
+
|
|
|
|
|
+ if err := decoder.Decode(opts, vals); err != nil {
|
|
|
|
|
+ app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
|
|
|
|
|
+ Code: ErrReleaseReadData,
|
|
|
|
|
+ Errors: []string{"bad request"},
|
|
|
|
|
+ }, w)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // handle write to the database
|
|
|
|
|
+ events, err := app.Repo.Event.ListEventsByProjectID(uint(projID), opts)
|
|
|
|
|
+
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ app.handleErrorDataWrite(err, w)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ eventExts := make([]*models.EventExternalSimple, 0)
|
|
|
|
|
+
|
|
|
|
|
+ for _, event := range events {
|
|
|
|
|
+ eventExts = append(eventExts, event.ExternalizeSimple())
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ w.WriteHeader(http.StatusOK)
|
|
|
|
|
+
|
|
|
|
|
+ if err := json.NewEncoder(w).Encode(eventExts); err != nil {
|
|
|
|
|
+ app.handleErrorFormDecoding(err, ErrProjectDecode, w)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// HandleListEvents lists the events that match certain conditions in a project
|
|
|
|
|
+func (app *App) HandleGetEvent(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
+ projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
|
|
|
|
|
+
|
|
|
|
|
+ if err != nil || projID == 0 {
|
|
|
|
|
+ app.handleErrorFormDecoding(err, ErrProjectDecode, w)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ vals, err := url.ParseQuery(r.URL.RawQuery)
|
|
|
|
|
+
|
|
|
|
|
+ clusterID, err := strconv.ParseUint(vals["cluster_id"][0], 10, 64)
|
|
|
|
|
+
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
|
|
|
|
|
+ Code: ErrReleaseReadData,
|
|
|
|
|
+ Errors: []string{"cluster not found"},
|
|
|
|
|
+ }, w)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ eventID, err := strconv.ParseUint(chi.URLParam(r, "event_id"), 0, 64)
|
|
|
|
|
+
|
|
|
|
|
+ if err != nil || projID == 0 {
|
|
|
|
|
+ app.handleErrorFormDecoding(err, ErrProjectDecode, w)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ event, err := app.Repo.Event.ReadEvent(uint(eventID), uint(projID), uint(clusterID))
|
|
|
|
|
+
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ if err == gorm.ErrRecordNotFound {
|
|
|
|
|
+ http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ app.handleErrorFormDecoding(err, ErrProjectDecode, w)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ eventExt := event.Externalize()
|
|
|
|
|
+
|
|
|
|
|
+ w.WriteHeader(http.StatusOK)
|
|
|
|
|
+
|
|
|
|
|
+ if err := json.NewEncoder(w).Encode(eventExt); err != nil {
|
|
|
|
|
+ app.handleErrorFormDecoding(err, ErrProjectDecode, w)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+}
|