Bläddra i källkod

update cluster/registry names from api

Alexander Belanger 5 år sedan
förälder
incheckning
8fe103285d

+ 21 - 0
internal/forms/cluster.go

@@ -69,6 +69,27 @@ func (ccf *CreateClusterForm) ToCluster() (*models.Cluster, error) {
 	}, nil
 }
 
+// UpdateClusterForm represents the accepted values for updating a
+// cluster (only name for now)
+type UpdateClusterForm struct {
+	ID uint
+
+	Name string `json:"name" form:"required"`
+}
+
+// ToCluster converts the form to a cluster
+func (ucf *UpdateClusterForm) ToCluster(repo repository.ClusterRepository) (*models.Cluster, error) {
+	cluster, err := repo.ReadCluster(ucf.ID)
+
+	if err != nil {
+		return nil, err
+	}
+
+	cluster.Name = ucf.Name
+
+	return cluster, nil
+}
+
 // ResolveClusterForm will resolve a cluster candidate and create a new cluster
 type ResolveClusterForm struct {
 	Resolver *models.ClusterResolverAll `form:"required"`

+ 22 - 0
internal/forms/registry.go

@@ -2,6 +2,7 @@ package forms
 
 import (
 	"github.com/porter-dev/porter/internal/models"
+	"github.com/porter-dev/porter/internal/repository"
 )
 
 // CreateRegistry represents the accepted values for creating a
@@ -22,3 +23,24 @@ func (cr *CreateRegistry) ToRegistry() (*models.Registry, error) {
 		AWSIntegrationID: cr.AWSIntegrationID,
 	}, nil
 }
+
+// UpdateRegistryForm represents the accepted values for updating a
+// registry (only name for now)
+type UpdateRegistryForm struct {
+	ID uint
+
+	Name string `json:"name" form:"required"`
+}
+
+// ToRegistry converts the form to a cluster
+func (urf *UpdateRegistryForm) ToRegistry(repo repository.RegistryRepository) (*models.Registry, error) {
+	registry, err := repo.ReadRegistry(urf.ID)
+
+	if err != nil {
+		return nil, err
+	}
+
+	registry.Name = urf.Name
+
+	return registry, nil
+}

+ 58 - 0
server/api/cluster_handler.go

@@ -119,6 +119,64 @@ func (app *App) HandleListProjectClusters(w http.ResponseWriter, r *http.Request
 	}
 }
 
+// HandleUpdateProjectCluster updates a project's cluster
+func (app *App) HandleUpdateProjectCluster(w http.ResponseWriter, r *http.Request) {
+	projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
+
+	if err != nil || projID == 0 {
+		app.handleErrorFormDecoding(err, ErrProjectDecode, w)
+		return
+	}
+
+	clusterID, err := strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)
+
+	if err != nil || clusterID == 0 {
+		app.handleErrorFormDecoding(err, ErrProjectDecode, w)
+		return
+	}
+
+	form := &forms.UpdateClusterForm{
+		ID: uint(clusterID),
+	}
+
+	// decode from JSON to form value
+	if err := json.NewDecoder(r.Body).Decode(form); err != nil {
+		app.handleErrorFormDecoding(err, ErrProjectDecode, w)
+		return
+	}
+
+	// validate the form
+	if err := app.validator.Struct(form); err != nil {
+		app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
+		return
+	}
+
+	// convert the form to a registry
+	cluster, err := form.ToCluster(app.repo.Cluster)
+
+	if err != nil {
+		app.handleErrorFormDecoding(err, ErrProjectDecode, w)
+		return
+	}
+
+	// handle write to the database
+	cluster, err = app.repo.Cluster.UpdateCluster(cluster)
+
+	if err != nil {
+		app.handleErrorDataWrite(err, w)
+		return
+	}
+
+	w.WriteHeader(http.StatusOK)
+
+	clusterExt := cluster.Externalize()
+
+	if err := json.NewEncoder(w).Encode(clusterExt); err != nil {
+		app.handleErrorFormDecoding(err, ErrProjectDecode, w)
+		return
+	}
+}
+
 // HandleDeleteProjectCluster handles the deletion of a Cluster via the cluster ID
 func (app *App) HandleDeleteProjectCluster(w http.ResponseWriter, r *http.Request) {
 	id, err := strconv.ParseUint(chi.URLParam(r, "cluster_id"), 0, 64)

+ 24 - 0
server/api/cluster_handler_test.go

@@ -145,6 +145,30 @@ func TestHandleListProjectClusters(t *testing.T) {
 	testClusterRequests(t, listProjectClustersTest, true)
 }
 
+var updateClusterTests = []*clusterTest{
+	&clusterTest{
+		initializers: []func(t *tester){
+			initUserDefault,
+			initProject,
+			initProjectClusterDefault,
+		},
+		msg:       "Update cluster name",
+		method:    "POST",
+		endpoint:  "/api/projects/1/clusters/1",
+		body:      `{"name":"cluster-new-name"}`,
+		expStatus: http.StatusOK,
+		expBody:   `{"id":1,"project_id":1,"name":"cluster-new-name","server":"https://10.10.10.10","service":"kube"}`,
+		useCookie: true,
+		validators: []func(c *clusterTest, tester *tester, t *testing.T){
+			projectClusterBodyValidator,
+		},
+	},
+}
+
+func TestHandleUpdateCluster(t *testing.T) {
+	testClusterRequests(t, updateClusterTests, true)
+}
+
 var deleteClusterTests = []*clusterTest{
 	&clusterTest{
 		initializers: []func(t *tester){

+ 58 - 0
server/api/registry_handler.go

@@ -95,6 +95,64 @@ func (app *App) HandleListProjectRegistries(w http.ResponseWriter, r *http.Reque
 	}
 }
 
+// HandleUpdateProjectRegistry updates a registry
+func (app *App) HandleUpdateProjectRegistry(w http.ResponseWriter, r *http.Request) {
+	projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
+
+	if err != nil || projID == 0 {
+		app.handleErrorFormDecoding(err, ErrProjectDecode, w)
+		return
+	}
+
+	registryID, err := strconv.ParseUint(chi.URLParam(r, "registry_id"), 0, 64)
+
+	if err != nil || registryID == 0 {
+		app.handleErrorFormDecoding(err, ErrProjectDecode, w)
+		return
+	}
+
+	form := &forms.UpdateRegistryForm{
+		ID: uint(registryID),
+	}
+
+	// decode from JSON to form value
+	if err := json.NewDecoder(r.Body).Decode(form); err != nil {
+		app.handleErrorFormDecoding(err, ErrProjectDecode, w)
+		return
+	}
+
+	// validate the form
+	if err := app.validator.Struct(form); err != nil {
+		app.handleErrorFormValidation(err, ErrProjectValidateFields, w)
+		return
+	}
+
+	// convert the form to a registry
+	registry, err := form.ToRegistry(app.repo.Registry)
+
+	if err != nil {
+		app.handleErrorFormDecoding(err, ErrProjectDecode, w)
+		return
+	}
+
+	// handle write to the database
+	registry, err = app.repo.Registry.UpdateRegistry(registry)
+
+	if err != nil {
+		app.handleErrorDataWrite(err, w)
+		return
+	}
+
+	w.WriteHeader(http.StatusOK)
+
+	regExt := registry.Externalize()
+
+	if err := json.NewEncoder(w).Encode(regExt); err != nil {
+		app.handleErrorFormDecoding(err, ErrProjectDecode, w)
+		return
+	}
+}
+
 // HandleDeleteProjectRegistry handles the deletion of a Registry via the registry ID
 func (app *App) HandleDeleteProjectRegistry(w http.ResponseWriter, r *http.Request) {
 	id, err := strconv.ParseUint(chi.URLParam(r, "registry_id"), 0, 64)

+ 24 - 0
server/api/registry_handler_test.go

@@ -171,6 +171,30 @@ func TestHandleListRegistries(t *testing.T) {
 	testRegistryRequests(t, listRegistryTests, true)
 }
 
+var updateRegistryTests = []*regTest{
+	&regTest{
+		initializers: []func(t *tester){
+			initUserDefault,
+			initProject,
+			initRegistry,
+		},
+		msg:       "Update registry name",
+		method:    "POST",
+		endpoint:  "/api/projects/1/registries/1",
+		body:      `{"name":"registry-new-name"}`,
+		expStatus: http.StatusOK,
+		expBody:   `{"id":1,"name":"registry-new-name","project_id":1,"service":"ecr"}`,
+		useCookie: true,
+		validators: []func(c *regTest, tester *tester, t *testing.T){
+			regBodyValidator,
+		},
+	},
+}
+
+func TestHandleUpdateRegistry(t *testing.T) {
+	testRegistryRequests(t, updateRegistryTests, true)
+}
+
 var deleteRegTests = []*regTest{
 	&regTest{
 		initializers: []func(t *tester){

+ 28 - 0
server/router/router.go

@@ -202,6 +202,20 @@ func New(
 			),
 		)
 
+		r.Method(
+			"POST",
+			"/projects/{project_id}/clusters/{cluster_id}",
+			auth.DoesUserHaveProjectAccess(
+				auth.DoesUserHaveClusterAccess(
+					requestlog.NewHandler(a.HandleUpdateProjectCluster, l),
+					mw.URLParam,
+					mw.URLParam,
+				),
+				mw.URLParam,
+				mw.WriteAccess,
+			),
+		)
+
 		r.Method(
 			"DELETE",
 			"/projects/{project_id}/clusters/{cluster_id}",
@@ -289,6 +303,20 @@ func New(
 			),
 		)
 
+		r.Method(
+			"POST",
+			"/projects/{project_id}/registries/{registry_id}",
+			auth.DoesUserHaveProjectAccess(
+				auth.DoesUserHaveRegistryAccess(
+					requestlog.NewHandler(a.HandleUpdateProjectRegistry, l),
+					mw.URLParam,
+					mw.URLParam,
+				),
+				mw.URLParam,
+				mw.WriteAccess,
+			),
+		)
+
 		r.Method(
 			"DELETE",
 			"/projects/{project_id}/registries/{registry_id}",