Ver Fonte

Replaced method for update stack source config from put to patch

jnfrati há 3 anos atrás
pai
commit
7f31f9826d

+ 19 - 7
api/server/handlers/stack/update_source_put.go → api/server/handlers/stack/update_source_patch.go

@@ -16,23 +16,23 @@ import (
 	"gorm.io/gorm"
 )
 
-type StackPutSourceConfigHandler struct {
+type StackPatchSourceConfigHandler struct {
 	handlers.PorterHandlerReadWriter
 	authz.KubernetesAgentGetter
 }
 
-func NewStackPutSourceConfigHandler(
+func NewStackPatchSourceConfigHandler(
 	config *config.Config,
 	reader shared.RequestDecoderValidator,
 	writer shared.ResultWriter,
-) *StackPutSourceConfigHandler {
-	return &StackPutSourceConfigHandler{
+) *StackPatchSourceConfigHandler {
+	return &StackPatchSourceConfigHandler{
 		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, reader, writer),
 		KubernetesAgentGetter:   authz.NewOutOfClusterAgentGetter(config),
 	}
 }
 
-func (p *StackPutSourceConfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+func (p *StackPatchSourceConfigHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 	proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
 	cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
 	namespace, _ := r.Context().Value(types.NamespaceScope).(string)
@@ -45,7 +45,7 @@ func (p *StackPutSourceConfigHandler) ServeHTTP(w http.ResponseWriter, r *http.R
 		return
 	}
 
-	req := &types.PutStackSourceConfigRequest{}
+	req := &types.PatchStackSourceConfigRequest{}
 
 	if ok := p.DecodeAndValidate(w, r, req); !ok {
 		return
@@ -59,7 +59,19 @@ func (p *StackPutSourceConfigHandler) ServeHTTP(w http.ResponseWriter, r *http.R
 		return
 	}
 
-	sourceConfigs, err := getSourceConfigModels(req.SourceConfigs)
+	parsedSourceConfigs := []*types.CreateStackSourceConfigRequest{}
+
+	for _, sourceConfig := range req.SourceConfigs {
+		parsedSourceConfigs = append(parsedSourceConfigs, &types.CreateStackSourceConfigRequest{
+			Name:                   sourceConfig.Name,
+			ImageRepoURI:           sourceConfig.ImageRepoURI,
+			ImageTag:               sourceConfig.ImageTag,
+			StableSourceConfigID:   sourceConfig.StableSourceConfigID,
+			StackSourceConfigBuild: sourceConfig.StackSourceConfigBuild,
+		})
+	}
+
+	sourceConfigs, err := getSourceConfigModels(parsedSourceConfigs)
 
 	if err != nil {
 		p.HandleAPIError(w, r, apierrors.NewErrInternal(err))

+ 9 - 9
api/server/router/v1/stack.go

@@ -9,7 +9,7 @@ import (
 	"github.com/porter-dev/porter/api/types"
 )
 
-// swagger:parameters getStack deleteStack putStackSource rollbackStack listStackRevisions addApplication addEnvGroup updateStack
+// swagger:parameters getStack deleteStack patchStackSource rollbackStack listStackRevisions addApplication addEnvGroup updateStack
 type stackPathParams struct {
 	// The project id
 	// in: path
@@ -432,8 +432,8 @@ func getV1StackRoutes(
 		Router:   r,
 	})
 
-	// PUT /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/source -> stack.NewStackPutSourceConfig
-	// swagger:operation PUT /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/source putStackSource
+	// PATCH /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/source -> stack.NewStackPatchSourceConfig
+	// swagger:operation PATCH /api/v1/projects/{project_id}/clusters/{cluster_id}/namespaces/{namespace}/stacks/{stack_id}/source patchStackSource
 	//
 	// Updates a stack's source configuration
 	//
@@ -449,10 +449,10 @@ func getV1StackRoutes(
 	//   - name: namespace
 	//   - name: stack_id
 	//   - in: body
-	//     name: PutStackSourceConfigRequest
+	//     name: PatchStackSourceConfigRequest
 	//     description: The source configurations to update
 	//     schema:
-	//       $ref: '#/definitions/PutStackSourceConfigRequest'
+	//       $ref: '#/definitions/PatchStackSourceConfigRequest'
 	// responses:
 	//   '200':
 	//     description: Successfully updated the source configuration
@@ -460,7 +460,7 @@ func getV1StackRoutes(
 	//       $ref: '#/definitions/Stack'
 	//   '403':
 	//     description: Forbidden
-	putSourceEndpoint := factory.NewAPIEndpoint(
+	patchSourceEndpoint := factory.NewAPIEndpoint(
 		&types.APIRequestMetadata{
 			Verb:   types.APIVerbUpdate,
 			Method: types.HTTPVerbPut,
@@ -478,15 +478,15 @@ func getV1StackRoutes(
 		},
 	)
 
-	putSourceHandler := stack.NewStackPutSourceConfigHandler(
+	patchSourceHandler := stack.NewStackPatchSourceConfigHandler(
 		config,
 		factory.GetDecoderValidator(),
 		factory.GetResultWriter(),
 	)
 
 	routes = append(routes, &router.Route{
-		Endpoint: putSourceEndpoint,
-		Handler:  putSourceHandler,
+		Endpoint: patchSourceEndpoint,
+		Handler:  patchSourceHandler,
 		Router:   r,
 	})
 

+ 25 - 12
api/types/stacks.go

@@ -22,17 +22,6 @@ type CreateStackRequest struct {
 	EnvGroups []*CreateStackEnvGroupRequest `json:"env_groups,omitempty" form:"required,dive,required"`
 }
 
-type PutStackSourceConfigPaylod struct {
-	*CreateStackSourceConfigRequest
-
-	StableSourceConfigID string `json:"stable_source_config_id" form:"required"`
-}
-
-// swagger:model
-type PutStackSourceConfigRequest struct {
-	SourceConfigs []*CreateStackSourceConfigRequest `json:"source_configs,omitempty" form:"required,dive,required"`
-}
-
 const URLParamStackRevisionNumber URLParam = "stack_revision_number"
 
 // swagger:model
@@ -40,9 +29,16 @@ type StackRollbackRequest struct {
 	TargetRevision uint `json:"target_revision"`
 }
 
+type StackSourceConfigWithRequiredStableSourceConfigID struct {
+	*CreateStackSourceConfigRequest
+
+	// required: true
+	StableSourceConfigID string `json:"stable_source_config_id" form:"required"`
+}
+
 // swagger:model
 type PatchStackSourceConfigRequest struct {
-	SourceConfig *UpdateStackSourceConfigRequest `json:"source_config,omitempty" form:"required"`
+	SourceConfigs []*StackSourceConfigWithRequiredStableSourceConfigID `json:"source_configs,omitempty" form:"required,dive,required"`
 }
 
 type CreateStackAppResourceRequest struct {
@@ -333,3 +329,20 @@ type StackSourceConfigBuildPack struct {
 	// A list of buildpacks to use
 	Buildpacks []string `json:"buildpacks"`
 }
+
+type BaseStackSourceConfigRequest struct {
+	// required: true
+	Name string `json:"name" form:"required"`
+
+	// required: true
+	ImageRepoURI string `json:"image_repo_uri" form:"required"`
+
+	// required: true
+	ImageTag string `json:"image_tag" form:"required"`
+
+	// required: true
+	StableSourceConfigID string `json:"stable_source_config_id"`
+
+	// If this field is empty, the resource is deployed directly from the image repo uri
+	StackSourceConfigBuild *StackSourceConfigBuild `json:"build,omitempty"`
+}

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

@@ -2082,7 +2082,7 @@ const updateStackSourceConfig = baseApi<
     stack_id: string;
   }
 >(
-  "PUT",
+  "PATCH",
   ({ project_id, cluster_id, namespace, stack_id }) =>
     `/api/v1/projects/${project_id}/clusters/${cluster_id}/namespaces/${namespace}/stacks/${stack_id}/source`
 );