Преглед изворни кода

add list namespaces endpoint

Anukul Sangwan пре 4 година
родитељ
комит
71a2d86120
3 измењених фајлова са 84 додато и 0 уклоњено
  1. 50 0
      api/server/handlers/cluster/list_namespaces.go
  2. 28 0
      api/server/router/cluster.go
  3. 6 0
      api/types/cluster.go

+ 50 - 0
api/server/handlers/cluster/list_namespaces.go

@@ -0,0 +1,50 @@
+package cluster
+
+import (
+	"net/http"
+
+	"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"
+	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/internal/models"
+)
+
+type ClusterListNamespacesHandler struct {
+	handlers.PorterHandlerWriter
+	KubernetesAgentGetter
+}
+
+func NewClusterListNamespacesHandler(
+	config *shared.Config,
+	writer shared.ResultWriter,
+) *ClusterListNamespacesHandler {
+	return &ClusterListNamespacesHandler{
+		PorterHandlerWriter:   handlers.NewDefaultPorterHandler(config, nil, writer),
+		KubernetesAgentGetter: NewDefaultKubernetesAgentGetter(config),
+	}
+}
+
+func (c *ClusterListNamespacesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
+
+	agent, err := c.GetAgent(cluster)
+
+	if err != nil {
+		c.HandleAPIError(w, apierrors.NewErrInternal(err))
+		return
+	}
+
+	namespaceList, err := agent.ListNamespaces()
+
+	if err != nil {
+		c.HandleAPIError(w, apierrors.NewErrInternal(err))
+		return
+	}
+
+	res := types.ClusterListNamespacesResponse{
+		NamespaceList: namespaceList,
+	}
+
+	c.WriteResult(w, res)
+}

+ 28 - 0
api/server/router/cluster.go

@@ -79,5 +79,33 @@ func getClusterRoutes(
 		Router:   r,
 	})
 
+	// GET /api/projects/{project_id}/clusters/{cluster_id}/namespaces -> cluster.NewClusterListNamespacesHandler
+	listNamespacesEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbGet,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: relPath + "/namespaces",
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+				types.ClusterScope,
+			},
+		},
+	)
+
+	listNamespacesHandler := cluster.NewClusterListNamespacesHandler(
+		config,
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: listNamespacesEndpoint,
+		Handler:  listNamespacesHandler,
+		Router:   r,
+	})
+
 	return routes, newPath
 }

+ 6 - 0
api/types/cluster.go

@@ -1,5 +1,7 @@
 package types
 
+import v1 "k8s.io/api/core/v1"
+
 type Cluster struct {
 	ID uint `json:"id"`
 
@@ -40,3 +42,7 @@ const (
 	GKE  ClusterService = "gke"
 	Kube ClusterService = "kube"
 )
+
+type ClusterListNamespacesResponse struct {
+	*v1.NamespaceList
+}