Skip to content

Commit

Permalink
Merge pull request #422 from sangkenlee/list-fill-parameter
Browse files Browse the repository at this point in the history
ListPolicy에 filledParameter 옵션 추가
  • Loading branch information
ktkfree authored Apr 24, 2024
2 parents 49ec92d + a36c248 commit 3349205
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
28 changes: 27 additions & 1 deletion internal/delivery/http/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package http
import (
"fmt"
"net/http"
"strconv"

"github.com/google/uuid"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -352,6 +353,7 @@ func (h *PolicyHandler) GetPolicy(w http.ResponseWriter, r *http.Request) {
// @Accept json
// @Produce json
// @Param organizationId path string true "조직 식별자(o로 시작)"
// @Param filledParameter query string false "filledParameter"
// @Param pageSize query string false "pageSize"
// @Param pageNumber query string false "pageNumber"
// @Param sortColumn query string false "sortColumn"
Expand All @@ -373,7 +375,19 @@ func (h *PolicyHandler) ListPolicy(w http.ResponseWriter, r *http.Request) {

pg := pagination.NewPagination(&urlParams)

policies, err := h.usecase.Fetch(r.Context(), organizationId, pg)
filledParameter := false

parse := urlParams.Get("filledParameter")
if len(parse) > 0 {
parsedBool, err := strconv.ParseBool(parse)
if err != nil {
ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("invalid fillParameter: '%s'", parse), "PT_INVALID_FILLPARAMETER", ""))
return
}
filledParameter = parsedBool
}

policies, err := h.usecase.Fetch(r.Context(), organizationId, pg, filledParameter)
if err != nil {
ErrorJSON(w, r, err)
return
Expand All @@ -394,6 +408,18 @@ func (h *PolicyHandler) ListPolicy(w http.ResponseWriter, r *http.Request) {
log.Error(r.Context(), err)
}
}

if filledParameter {
parameterSchema := policy.PolicyTemplate.ParametersSchema
parameters := policy.Parameters

err = policytemplate.FillParamDefFromJsonStr(parameterSchema, parameters)
if err != nil {
log.Error(r.Context(), err)
} else {
out.Policies[i].FilledParameters = parameterSchema
}
}
}

if out.Pagination, err = pg.Response(r.Context()); err != nil {
Expand Down
24 changes: 21 additions & 3 deletions internal/usecase/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type IPolicyUsecase interface {
Delete(ctx context.Context, organizationId string, policyId uuid.UUID) (err error)
Get(ctx context.Context, organizationId string, policyId uuid.UUID) (policy *model.Policy, err error)
GetForEdit(ctx context.Context, organizationId string, policyId uuid.UUID) (policy *model.Policy, err error)
Fetch(ctx context.Context, organizationId string, pg *pagination.Pagination) (*[]model.Policy, error)
Fetch(ctx context.Context, organizationId string, pg *pagination.Pagination, filledParameter bool) (*[]model.Policy, error)
IsPolicyIdExist(ctx context.Context, organizationId string, policyId uuid.UUID) (exists bool, err error)
IsPolicyNameExist(ctx context.Context, organizationId string, policyName string) (exists bool, err error)
UpdatePolicyTargetClusters(ctx context.Context, organizationId string, policyId uuid.UUID, currentClusterIds []string, targetClusterIds []string) (err error)
Expand Down Expand Up @@ -412,8 +412,26 @@ func (u *PolicyUsecase) GetForEdit(ctx context.Context, organizationId string, p
return policy, err
}

func (u *PolicyUsecase) Fetch(ctx context.Context, organizationId string, pg *pagination.Pagination) (*[]model.Policy, error) {
return u.repo.Fetch(ctx, organizationId, pg)
func (u *PolicyUsecase) Fetch(ctx context.Context, organizationId string, pg *pagination.Pagination, filledParameter bool) (*[]model.Policy, error) {
policies, err := u.repo.Fetch(ctx, organizationId, pg)

if err != nil {
return nil, err
}

// 단순 Fetch인 경우에는 Policy에 해당하는 Template만 Join해 줌
// PolicyTemplate의 최신 버전을 조회해서 파라미터 스키마 등을 조회해서 넣어줘야 함
if filledParameter {
for i, policy := range *policies {
policyTemplate, err := u.templateRepo.GetByID(ctx, policy.TemplateId)

if err == nil && policyTemplate != nil {
(*policies)[i].PolicyTemplate = *policyTemplate
}
}
}

return policies, err
}

func (u *PolicyUsecase) IsPolicyNameExist(ctx context.Context, organizationId string, policyName string) (exists bool, err error) {
Expand Down

0 comments on commit 3349205

Please sign in to comment.