Przeglądaj źródła

add create namespace endpoint

Anukul Sangwan 4 lat temu
rodzic
commit
5a0a1ed5a8

+ 57 - 0
api/server/handlers/cluster/create_namespace.go

@@ -0,0 +1,57 @@
+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 CreateNamespaceHandler struct {
+	handlers.PorterHandlerReadWriter
+	KubernetesAgentGetter
+}
+
+func NewCreateNamespaceHandler(
+	config *shared.Config,
+	decoderValidator shared.RequestDecoderValidator,
+	writer shared.ResultWriter,
+) *CreateNamespaceHandler {
+	return &CreateNamespaceHandler{
+		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
+		KubernetesAgentGetter:   NewDefaultKubernetesAgentGetter(config),
+	}
+}
+
+func (c *CreateNamespaceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	request := &types.CreateNamespaceRequest{}
+
+	if ok := c.DecodeAndValidate(w, r, request); !ok {
+		return
+	}
+
+	cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
+
+	agent, err := c.GetAgent(cluster)
+
+	if err != nil {
+		c.HandleAPIError(w, apierrors.NewErrInternal(err))
+		return
+	}
+
+	namespace, err := agent.CreateNamespace(request.Name)
+
+	if err != nil {
+		c.HandleAPIError(w, apierrors.NewErrInternal(err))
+		return
+	}
+
+	res := types.CreateNamespaceResponse{
+		Namespace: namespace,
+	}
+
+	c.WriteResult(w, res)
+}

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

@@ -107,5 +107,34 @@ func getClusterRoutes(
 		Router:   r,
 	})
 
+	// POST /api/projects/{project_id}/clusters/{cluster_id}/namespaces/create -> cluster.NewCreateNamespaceHandler
+	createNamespaceEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbCreate,
+			Method: types.HTTPVerbPost,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: relPath + "/namespaces/create",
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+				types.ClusterScope,
+			},
+		},
+	)
+
+	createNamespaceHandler := cluster.NewCreateNamespaceHandler(
+		config,
+		factory.GetDecoderValidator(),
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: createNamespaceEndpoint,
+		Handler:  createNamespaceHandler,
+		Router:   r,
+	})
+
 	return routes, newPath
 }

+ 8 - 0
api/types/cluster.go

@@ -46,3 +46,11 @@ const (
 type ClusterListNamespacesResponse struct {
 	*v1.NamespaceList
 }
+
+type CreateNamespaceRequest struct {
+	Name string `json:"name" form:"required"`
+}
+
+type CreateNamespaceResponse struct {
+	*v1.Namespace
+}

+ 1 - 1
dashboard/src/shared/api.tsx

@@ -947,7 +947,7 @@ const createNamespace = baseApi<
   { id: number; cluster_id: number }
 >("POST", (pathParams) => {
   let { id, cluster_id } = pathParams;
-  return `/api/projects/${id}/k8s/namespaces/create?cluster_id=${cluster_id}`;
+  return `/api/projects/${id}/clusters/${cluster_id}/namespaces/create`;
 });
 
 const deleteNamespace = baseApi<