Explorar el Código

add ProjectListInfraHandler

Anukul Sangwan hace 4 años
padre
commit
50d7101a07
Se han modificado 3 ficheros con 73 adiciones y 0 borrados
  1. 44 0
      api/server/handlers/project/list_infra.go
  2. 27 0
      api/server/router/project.go
  3. 2 0
      api/types/project.go

+ 44 - 0
api/server/handlers/project/list_infra.go

@@ -0,0 +1,44 @@
+package project
+
+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 ProjectListInfraHandler struct {
+	handlers.PorterHandlerWriter
+}
+
+func NewProjectListInfraHandler(
+	config *shared.Config,
+	writer shared.ResultWriter,
+) *ProjectListInfraHandler {
+	return &ProjectListInfraHandler{
+		PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
+	}
+}
+
+func (p *ProjectListInfraHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
+
+	infras, err := p.Repo().Infra().ListInfrasByProjectID(proj.ID)
+
+	if err != nil {
+		p.HandleAPIError(w, apierrors.NewErrInternal(err))
+	}
+
+	infraList := make([]*types.Infra, 0)
+
+	for _, infra := range infras {
+		infraList = append(infraList, infra.Externalize())
+	}
+
+	var res types.ListProjectInfraResponse = infraList
+
+	p.WriteResult(w, res)
+}

+ 27 - 0
api/server/router/project.go

@@ -79,6 +79,33 @@ func getProjectRoutes(
 		Router:   r,
 	})
 
+	// GET /api/projects/{project_id}/infra -> project.NewListProjectInfraHandler
+	listInfraEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbGet,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: relPath + "/infra",
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+			},
+		},
+	)
+
+	listInfraHandler := project.NewProjectListInfraHandler(
+		config,
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: listInfraEndpoint,
+		Handler:  listInfraHandler,
+		Router:   r,
+	})
+
 	// GET /api/projects/{project_id}/clusters -> cluster.NewClusterListHandler
 	listClusterEndpoint := factory.NewAPIEndpoint(
 		&types.APIRequestMetadata{

+ 2 - 0
api/types/project.go

@@ -28,3 +28,5 @@ type DeleteProjectRequest struct {
 }
 
 type DeleteProjectResponse Project
+
+type ListProjectInfraResponse []*Infra