Selaa lähdekoodia

updated list to allow query by cluster

Stefan McShane 3 vuotta sitten
vanhempi
sitoutus
a57fb34580
1 muutettua tiedostoa jossa 16 lisäystä ja 3 poistoa
  1. 16 3
      api/server/handlers/api_contract/list.go

+ 16 - 3
api/server/handlers/api_contract/list.go

@@ -3,7 +3,9 @@ package api_contract
 import (
 	"fmt"
 	"net/http"
+	"strconv"
 
+	"github.com/go-chi/chi"
 	"github.com/porter-dev/porter/api/server/handlers"
 	"github.com/porter-dev/porter/api/server/shared"
 	"github.com/porter-dev/porter/api/server/shared/apierrors"
@@ -30,13 +32,24 @@ func NewAPIContractRevisionListHandler(
 // If clusterID is also given, it will list by project_id, cluster_id
 func (c *APIContractRevisionListHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 	proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
-	cluster, _ := r.Context().Value(types.ProjectScope).(*models.Cluster)
+
+	clusterID := 0
+	clusterIDParam := chi.URLParam(r, "cluster_id")
+	if clusterIDParam != "" {
+		i, err := strconv.Atoi(clusterIDParam)
+		if err != nil {
+			e := fmt.Errorf("invalid cluster_id query param given: %w", err)
+			c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
+			return
+		}
+		clusterID = i
+	}
 
 	ctx := r.Context()
 
-	revisions, err := c.Config().Repo.APIContractRevisioner().List(ctx, proj.ID, cluster.ID)
+	revisions, err := c.Config().Repo.APIContractRevisioner().List(ctx, proj.ID, uint(clusterID))
 	if err != nil {
-		e := fmt.Errorf("error creating new capi config: %w", err)
+		e := fmt.Errorf("error creating new api contract revision: %w", err)
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
 		return
 	}