Quellcode durchsuchen

add list templates endpoint

Alexander Belanger vor 4 Jahren
Ursprung
Commit
1d8df9d3f3

+ 0 - 0
api/server/handlers/template/get.go


+ 47 - 0
api/server/handlers/template/list.go

@@ -0,0 +1,47 @@
+package template
+
+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/server/shared/config"
+	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/internal/helm/loader"
+)
+
+type TemplateListHandler struct {
+	handlers.PorterHandlerReadWriter
+}
+
+func NewTemplateListHandler(
+	config *config.Config,
+	decoderValidator shared.RequestDecoderValidator,
+	writer shared.ResultWriter,
+) *TemplateListHandler {
+	return &TemplateListHandler{
+		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
+	}
+}
+
+func (t *TemplateListHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	request := &types.ListTemplatesRequest{}
+
+	ok := t.DecodeAndValidate(w, r, request)
+
+	if !ok {
+		return
+	}
+
+	repoIndex, err := loader.LoadRepoIndexPublic(request.RepoURL)
+
+	if err != nil {
+		t.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	porterCharts := loader.RepoIndexToPorterChartList(repoIndex)
+
+	t.WriteResult(w, r, porterCharts)
+}

+ 26 - 0
api/server/router/user.go

@@ -3,6 +3,7 @@ package router
 import (
 	"github.com/go-chi/chi"
 	"github.com/porter-dev/porter/api/server/handlers/project"
+	"github.com/porter-dev/porter/api/server/handlers/template"
 	"github.com/porter-dev/porter/api/server/handlers/user"
 	"github.com/porter-dev/porter/api/server/shared"
 	"github.com/porter-dev/porter/api/server/shared/config"
@@ -231,5 +232,30 @@ func getUserRoutes(
 		Router:   r,
 	})
 
+	// GET /api/templates -> template.NewTemplateListHandler
+	listTemplatesEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbList,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: "/templates",
+			},
+			Scopes: []types.PermissionScope{types.UserScope},
+		},
+	)
+
+	listTemplatesRequest := template.NewTemplateListHandler(
+		config,
+		factory.GetDecoderValidator(),
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: listTemplatesEndpoint,
+		Handler:  listTemplatesRequest,
+		Router:   r,
+	})
+
 	return routes
 }

+ 5 - 0
api/types/template.go

@@ -0,0 +1,5 @@
+package types
+
+type ListTemplatesRequest struct {
+	RepoURL string `schema:"repo_url"`
+}