Przeglądaj źródła

add resolve candidate endpoint

Alexander Belanger 4 lat temu
rodzic
commit
255e8caed2

+ 44 - 0
api/server/handlers/cluster/list_candidates.go

@@ -0,0 +1,44 @@
+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/server/shared/config"
+	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/internal/models"
+)
+
+type ListClusterCandidatesHandler struct {
+	handlers.PorterHandlerReadWriter
+}
+
+func NewListClusterCandidatesHandler(
+	config *config.Config,
+	writer shared.ResultWriter,
+) *ListClusterCandidatesHandler {
+	return &ListClusterCandidatesHandler{
+		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
+	}
+}
+
+func (c *ListClusterCandidatesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
+
+	ccs, err := c.Repo().Cluster().ListClusterCandidatesByProjectID(proj.ID)
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	res := make([]*types.ClusterCandidate, 0)
+
+	for _, cc := range ccs {
+		res = append(res, cc.ToClusterCandidateType())
+	}
+
+	c.WriteResult(w, r, res)
+}

+ 56 - 0
api/server/handlers/cluster/resolve_candidate.go

@@ -0,0 +1,56 @@
+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/server/shared/config"
+	"github.com/porter-dev/porter/api/server/shared/requestutils"
+	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/internal/models"
+)
+
+type ResolveClusterCandidateHandler struct {
+	handlers.PorterHandlerReadWriter
+}
+
+func NewResolveClusterCandidateHandler(
+	config *config.Config,
+	decoderValidator shared.RequestDecoderValidator,
+	writer shared.ResultWriter,
+) *ResolveClusterCandidateHandler {
+	return &ResolveClusterCandidateHandler{
+		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
+	}
+}
+
+func (c *ResolveClusterCandidateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+	// read the project from context
+	proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
+	user, _ := r.Context().Value(types.UserScope).(*models.User)
+	ccID, _ := requestutils.GetURLParamUint(r, types.URLParamCandidateID)
+
+	request := &types.ClusterResolverAll{}
+
+	if ok := c.DecodeAndValidate(w, r, request); !ok {
+		return
+	}
+
+	cc, err := c.Repo().Cluster().ReadClusterCandidate(proj.ID, ccID)
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	cluster, cc, err := createClusterFromCandidate(c.Repo(), proj, user, cc)
+
+	if err != nil {
+		c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
+		return
+	}
+
+	c.WriteResult(w, r, cluster.ToClusterType())
+}

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

@@ -110,6 +110,64 @@ func getClusterRoutes(
 		Router:   r,
 	})
 
+	// GET /api/projects/{project_id}/clusters/candidates -> project.NewListClusterCandidatesHandler
+	listCandidatesEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbList,
+			Method: types.HTTPVerbGet,
+			Path: &types.Path{
+				Parent:       basePath,
+				RelativePath: "/clusters/candidates",
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+			},
+		},
+	)
+
+	listCandidatesHandler := cluster.NewListClusterCandidatesHandler(
+		config,
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: listCandidatesEndpoint,
+		Handler:  listCandidatesHandler,
+		Router:   r,
+	})
+
+	// POST /api/projects/{project_id}/clusters/candidates/{candidate_id}/resolve -> project.NewResolveClusterCandidateHandler
+	resolveCandidateEndpoint := factory.NewAPIEndpoint(
+		&types.APIRequestMetadata{
+			Verb:   types.APIVerbCreate,
+			Method: types.HTTPVerbPost,
+			Path: &types.Path{
+				Parent: basePath,
+				RelativePath: fmt.Sprintf(
+					"/clusters/candidates/{%s}/resolve",
+					types.URLParamCandidateID,
+				),
+			},
+			Scopes: []types.PermissionScope{
+				types.UserScope,
+				types.ProjectScope,
+			},
+		},
+	)
+
+	resolveCandidateHandler := cluster.NewResolveClusterCandidateHandler(
+		config,
+		factory.GetDecoderValidator(),
+		factory.GetResultWriter(),
+	)
+
+	routes = append(routes, &Route{
+		Endpoint: resolveCandidateEndpoint,
+		Handler:  resolveCandidateHandler,
+		Router:   r,
+	})
+
 	// GET /api/projects/{project_id}/clusters/{cluster_id} -> project.NewClusterGetHandler
 	getEndpoint := factory.NewAPIEndpoint(
 		&types.APIRequestMetadata{

+ 4 - 0
api/types/cluster.go

@@ -5,6 +5,10 @@ import (
 	v1 "k8s.io/api/core/v1"
 )
 
+const (
+	URLParamCandidateID URLParam = "candidate_id"
+)
+
 type Cluster struct {
 	ID uint `json:"id"`
 

+ 1 - 1
docs/developing/backend-refactor-status.md

@@ -39,7 +39,7 @@
 | <li>- [x] `GET /api/projects/{project_id}/clusters`                                                                         | AB          |                 |             | yes              |
 | <li>- [X] `POST /api/projects/{project_id}/clusters`                                                                        | AB          |                 |             |                  |
 | <li>- [X] `POST /api/projects/{project_id}/clusters/candidates`                                                             | AB          |                 |             |                  |
-| <li>- [ ] `GET /api/projects/{project_id}/clusters/candidates`                                                              |             |                 |             |                  |
+| <li>- [X] `GET /api/projects/{project_id}/clusters/candidates`                                                              | AB          |                 |             |                  |
 | <li>- [ ] `POST /api/projects/{project_id}/clusters/candidates/{candidate_id}/resolve`                                      |             |                 |             |                  |
 | <li>- [x] `GET /api/projects/{project_id}/clusters/{cluster_id}`                                                            | AB          |                 |             | yes              |
 | <li>- [ ] `POST /api/projects/{project_id}/clusters/{cluster_id}`                                                           |             |                 |             |                  |