Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ListPolicy에 filledParameter 옵션 추가 #422

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading