Просмотр исходного кода

make cluster id url param for events

Alexander Belanger 4 лет назад
Родитель
Сommit
88c08b7a97
3 измененных файлов с 21 добавлено и 14 удалено
  1. 1 1
      internal/repository/event.go
  2. 14 7
      server/api/event_handler.go
  3. 6 6
      server/router/router.go

+ 1 - 1
internal/repository/event.go

@@ -4,7 +4,7 @@ import "github.com/porter-dev/porter/internal/models"
 
 // ListEventOpts are the options for listing events
 type ListEventOpts struct {
-	ClusterID uint `schema:"cluster_id"`
+	ClusterID uint
 
 	Limit int    `schema:"limit"`
 	Skip  int    `schema:"skip"`

+ 14 - 7
server/api/event_handler.go

@@ -23,9 +23,7 @@ func (app *App) HandleCreateEvent(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	vals, err := url.ParseQuery(r.URL.RawQuery)
-
-	clusterID, err := strconv.ParseUint(vals["cluster_id"][0], 10, 64)
+	clusterID, err := strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
 
 	if err != nil {
 		app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
@@ -76,9 +74,20 @@ func (app *App) HandleListEvents(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
+	clusterID, err := strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
+
+	if err != nil {
+		app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
+			Code:   ErrReleaseReadData,
+			Errors: []string{"cluster not found"},
+		}, w)
+	}
+
 	vals, err := url.ParseQuery(r.URL.RawQuery)
 
-	opts := &repository.ListEventOpts{}
+	opts := &repository.ListEventOpts{
+		ClusterID: uint(clusterID),
+	}
 
 	decoder := schema.NewDecoder()
 
@@ -122,9 +131,7 @@ func (app *App) HandleGetEvent(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	vals, err := url.ParseQuery(r.URL.RawQuery)
-
-	clusterID, err := strconv.ParseUint(vals["cluster_id"][0], 10, 64)
+	clusterID, err := strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
 
 	if err != nil {
 		app.sendExternalError(err, http.StatusInternalServerError, HTTPError{

+ 6 - 6
server/router/router.go

@@ -409,12 +409,12 @@ func New(a *api.App) *chi.Mux {
 			// /api/projects/{project_id}/events routes
 			r.Method(
 				"POST",
-				"/projects/{project_id}/events",
+				"/projects/{project_id}/clusters/{cluster_id}/events",
 				auth.DoesUserHaveProjectAccess(
 					auth.DoesUserHaveClusterAccess(
 						requestlog.NewHandler(a.HandleCreateEvent, l),
 						mw.URLParam,
-						mw.QueryParam,
+						mw.URLParam,
 					),
 					mw.URLParam,
 					mw.AdminAccess,
@@ -423,12 +423,12 @@ func New(a *api.App) *chi.Mux {
 
 			r.Method(
 				"GET",
-				"/projects/{project_id}/events",
+				"/projects/{project_id}/clusters/{cluster_id}/events",
 				auth.DoesUserHaveProjectAccess(
 					auth.DoesUserHaveClusterAccess(
 						requestlog.NewHandler(a.HandleListEvents, l),
 						mw.URLParam,
-						mw.QueryParam,
+						mw.URLParam,
 					),
 					mw.URLParam,
 					mw.AdminAccess,
@@ -437,12 +437,12 @@ func New(a *api.App) *chi.Mux {
 
 			r.Method(
 				"GET",
-				"/projects/{project_id}/events/{event_id}",
+				"/projects/{project_id}/clusters/{cluster_id}/events/{event_id}",
 				auth.DoesUserHaveProjectAccess(
 					auth.DoesUserHaveClusterAccess(
 						requestlog.NewHandler(a.HandleGetEvent, l),
 						mw.URLParam,
-						mw.QueryParam,
+						mw.URLParam,
 					),
 					mw.URLParam,
 					mw.AdminAccess,