From a36c2481d8ff66f67e707a151deb8dea10db41c2 Mon Sep 17 00:00:00 2001 From: sangkenlee Date: Wed, 24 Apr 2024 14:34:26 +0900 Subject: [PATCH] =?UTF-8?q?ListPolicy=EC=97=90=20filledParameter=20?= =?UTF-8?q?=EC=98=B5=EC=85=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/delivery/http/policy.go | 28 +++++++++++++++++++++++++++- internal/usecase/policy.go | 24 +++++++++++++++++++++--- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/internal/delivery/http/policy.go b/internal/delivery/http/policy.go index 9a45cd31..a06144a9 100644 --- a/internal/delivery/http/policy.go +++ b/internal/delivery/http/policy.go @@ -3,6 +3,7 @@ package http import ( "fmt" "net/http" + "strconv" "github.com/google/uuid" "github.com/gorilla/mux" @@ -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" @@ -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 @@ -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 { diff --git a/internal/usecase/policy.go b/internal/usecase/policy.go index 5f28f28e..0ebbc371 100644 --- a/internal/usecase/policy.go +++ b/internal/usecase/policy.go @@ -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) @@ -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) {