From ed18996cfcc7df97b348c16e1b5b3166b5686125 Mon Sep 17 00:00:00 2001 From: sangkenlee Date: Tue, 30 Apr 2024 15:39:47 +0900 Subject: [PATCH] =?UTF-8?q?policy=20resource=20name=20=EC=A4=91=EB=B3=B5?= =?UTF-8?q?=20=EC=B2=B4=ED=81=AC=20api=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/delivery/api/endpoint.go | 1 + .../delivery/api/generated_endpoints.go.go | 8 ++++ internal/delivery/http/policy.go | 45 ++++++++++++++++++- internal/route/route.go | 1 + internal/usecase/policy.go | 7 ++- pkg/httpErrors/errorCode.go | 22 ++++----- 6 files changed, 70 insertions(+), 14 deletions(-) diff --git a/internal/delivery/api/endpoint.go b/internal/delivery/api/endpoint.go index c3cb77cf..c847c189 100644 --- a/internal/delivery/api/endpoint.go +++ b/internal/delivery/api/endpoint.go @@ -268,6 +268,7 @@ const ( UpdatePolicy UpdatePolicyTargetClusters ExistsPolicyName + ExistsPolicyResourceName GetPolicyEdit AddPoliciesForStack DeletePoliciesForStack diff --git a/internal/delivery/api/generated_endpoints.go.go b/internal/delivery/api/generated_endpoints.go.go index 498d16ff..937cb4d0 100644 --- a/internal/delivery/api/generated_endpoints.go.go +++ b/internal/delivery/api/generated_endpoints.go.go @@ -839,6 +839,10 @@ var ApiMap = map[Endpoint]EndpointInfo{ Name: "ExistsPolicyName", Group: "Policy", }, + ExistsPolicyResourceName: { + Name: "ExistsPolicyResourceName", + Group: "Policy", + }, GetPolicyEdit: { Name: "GetPolicyEdit", Group: "Policy", @@ -1352,6 +1356,8 @@ func (e Endpoint) String() string { return "UpdatePolicyTargetClusters" case ExistsPolicyName: return "ExistsPolicyName" + case ExistsPolicyResourceName: + return "ExistsPolicyResourceName" case GetPolicyEdit: return "GetPolicyEdit" case AddPoliciesForStack: @@ -1822,6 +1828,8 @@ func GetEndpoint(name string) Endpoint { return UpdatePolicyTargetClusters case "ExistsPolicyName": return ExistsPolicyName + case "ExistsPolicyResourceName": + return ExistsPolicyResourceName case "GetPolicyEdit": return GetPolicyEdit case "AddPoliciesForStack": diff --git a/internal/delivery/http/policy.go b/internal/delivery/http/policy.go index ab42458c..905a6db6 100644 --- a/internal/delivery/http/policy.go +++ b/internal/delivery/http/policy.go @@ -33,6 +33,7 @@ type IPolicyHandler interface { GetMandatoryPolicies(w http.ResponseWriter, r *http.Request) SetMandatoryPolicies(w http.ResponseWriter, r *http.Request) ExistsPolicyName(w http.ResponseWriter, r *http.Request) + ExistsPolicyResourceName(w http.ResponseWriter, r *http.Request) ListStackPolicyStatus(w http.ResponseWriter, r *http.Request) GetStackPolicyTemplateStatus(w http.ResponseWriter, r *http.Request) UpdateStackPolicyTemplateStatus(w http.ResponseWriter, r *http.Request) @@ -618,7 +619,7 @@ func (h *PolicyHandler) SetMandatoryPolicies(w http.ResponseWriter, r *http.Requ // ExistsPolicyName godoc // // @Tags Policy -// @Summary [ExistsPolicyName] 정책 아름 존재 여부 확인 +// @Summary [ExistsPolicyName] 정책 이름 존재 여부 확인 // @Description 해당 이름을 가진 정책이 이미 존재하는지 확인한다. // @Accept json // @Produce json @@ -638,7 +639,7 @@ func (h *PolicyHandler) ExistsPolicyName(w http.ResponseWriter, r *http.Request) policyName, ok := vars["policyName"] if !ok { - ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("policyTemplateName not found in path"), + ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("policyName not found in path"), "P_INVALID_POLICY_NAME", "")) return } @@ -655,6 +656,46 @@ func (h *PolicyHandler) ExistsPolicyName(w http.ResponseWriter, r *http.Request) ResponseJSON(w, r, http.StatusOK, out) } +// ExistsPolicyResourceName godoc +// +// @Tags Policy +// @Summary [ExistsPolicyResourceName] 정책 자원 이름 존재 여부 확인 +// @Description 해당 자원 이름을 가진 정책이 이미 존재하는지 확인한다. +// @Accept json +// @Produce json +// @Param organizationId path string true "조직 식별자(o로 시작)" +// @Param policyResourceName path string true "정책 자원 이름(쿠버네티스 배포 시 자원 이름)" +// @Success 200 {object} domain.CheckExistedResponse +// @Router /organizations/{organizationId}/policies/resource-name/{policyResourceName}/existence [get] +// @Security JWT +func (h *PolicyHandler) ExistsPolicyResourceName(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + organizationId, ok := vars["organizationId"] + if !ok { + ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("invalid organizationId"), + "C_INVALID_ORGANIZATION_ID", "")) + return + } + + policyResourceName, ok := vars["policyResourceName"] + if !ok { + ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("policyResourceName not found in path"), + "P_INVALID_POLICY_RESOURCE_NAME", "")) + return + } + + exist, err := h.usecase.IsPolicyResourceNameExist(r.Context(), organizationId, policyResourceName) + if err != nil { + ErrorJSON(w, r, err) + return + } + + var out domain.CheckExistedResponse + out.Existed = exist + + ResponseJSON(w, r, http.StatusOK, out) +} + // ListStackPolicyStatus godoc // // @Tags StackPolicyStatus diff --git a/internal/route/route.go b/internal/route/route.go index 09e8b1eb..56e2500e 100644 --- a/internal/route/route.go +++ b/internal/route/route.go @@ -364,6 +364,7 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/policies/{policyId}", customMiddleware.Handle(internalApi.DeletePolicy, http.HandlerFunc(policyHandler.DeletePolicy))).Methods(http.MethodDelete) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/policies/{policyId}", customMiddleware.Handle(internalApi.UpdatePolicy, http.HandlerFunc(policyHandler.UpdatePolicy))).Methods(http.MethodPatch) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/policies/name/{policyName}/existence", customMiddleware.Handle(internalApi.ExistsPolicyName, http.HandlerFunc(policyHandler.ExistsPolicyName))).Methods(http.MethodGet) + r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/policies/resource-name/{policyResourceName}/existence", customMiddleware.Handle(internalApi.ExistsPolicyResourceName, http.HandlerFunc(policyHandler.ExistsPolicyResourceName))).Methods(http.MethodGet) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/policies", customMiddleware.Handle(internalApi.AddPoliciesForStack, http.HandlerFunc(policyHandler.AddPoliciesForStack))).Methods(http.MethodPost) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/policies", customMiddleware.Handle(internalApi.DeletePoliciesForStack, http.HandlerFunc(policyHandler.DeletePoliciesForStack))).Methods(http.MethodPut) r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/stacks/{stackId}/statistics", customMiddleware.Handle(internalApi.StackPolicyStatistics, http.HandlerFunc(policyHandler.StackPolicyStatistics))).Methods(http.MethodGet) diff --git a/internal/usecase/policy.go b/internal/usecase/policy.go index 0ebbc371..73fafafd 100644 --- a/internal/usecase/policy.go +++ b/internal/usecase/policy.go @@ -32,6 +32,7 @@ type IPolicyUsecase interface { 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) + IsPolicyResourceNameExist(ctx context.Context, organizationId string, policyResourceName string) (exists bool, err error) UpdatePolicyTargetClusters(ctx context.Context, organizationId string, policyId uuid.UUID, currentClusterIds []string, targetClusterIds []string) (err error) SetMandatoryPolicies(ctx context.Context, organizationId string, mandatoryPolicyIds []uuid.UUID, nonMandatoryPolicyIds []uuid.UUID) (err error) GetMandatoryPolicies(ctx context.Context, organizationId string) (response *domain.GetMandatoryPoliciesResponse, err error) @@ -105,7 +106,7 @@ func (u *PolicyUsecase) Create(ctx context.Context, organizationId string, dto m } if exists { - return uuid.Nil, httpErrors.NewBadRequestError(httpErrors.DuplicateResource, "P_CREATE_ALREADY_EXISTED_RESOURCE_NAME", "policy resource name already exists") + return uuid.Nil, httpErrors.NewBadRequestError(httpErrors.DuplicateResource, "P_INVALID_POLICY_RESOURCE_NAME", "policy resource name already exists") } dto.TargetClusters = make([]model.Cluster, len(dto.TargetClusterIds)) @@ -438,6 +439,10 @@ func (u *PolicyUsecase) IsPolicyNameExist(ctx context.Context, organizationId st return u.repo.ExistByName(ctx, organizationId, policyName) } +func (u *PolicyUsecase) IsPolicyResourceNameExist(ctx context.Context, organizationId string, policyResoName string) (exists bool, err error) { + return u.repo.ExistByResourceName(ctx, organizationId, policyResoName) +} + func (u *PolicyUsecase) IsPolicyIdExist(ctx context.Context, organizationId string, policyId uuid.UUID) (exists bool, err error) { return u.repo.ExistByID(ctx, organizationId, policyId) } diff --git a/pkg/httpErrors/errorCode.go b/pkg/httpErrors/errorCode.go index 99e8a908..21fb3510 100644 --- a/pkg/httpErrors/errorCode.go +++ b/pkg/httpErrors/errorCode.go @@ -135,17 +135,17 @@ var errorMap = map[ErrorCode]string{ "PT_INVALID_PARAMETER_SCHEMA": "파라미터 스키마에 잘못된 타입이 지정되었습니다.", // Policy - "P_CREATE_ALREADY_EXISTED_NAME": "정첵에 이미 존재하는 이름입니다.", - "P_NOT_FOUND_POLICY": "정책이 존재하지 않습니다.", - "P_INVALID_POLICY_NAME": "유효하지 않은 정책 이름입니다. 정책 이름을 확인하세요.", - "P_CREATE_ALREADY_EXISTED_RESOURCE_NAME": "유효하지 않은 정책 자원 이름(k8s 자원 이름)입니다. 정책 자원 이름을 확인하세요.", - "P_INVALID_MATCH": "유효하지 않은 match 설정입니다. match 설정을 확인하세요.", - "P_FAILED_FETCH_POLICY": "정책 ID에 해당하는 정책을 가져오는데 실패했습니다.", - "P_FAILED_FETCH_CLUSTER": "정책의 클러스터 정보를 가져오는데 실패했습니다.", - "P_FAILED_FETCH_TEMPLATE": "정책의 템플릿 정보를 가져오는데 실패했습니다.", - "P_CALL_TO_APPLY_KUBERNETES": "쿠버네티스 클러스터 호출에 실패했습니다.", - "P_FAILED_TO_APPLY_KUBERNETES": "쿠버네티스 클러스터 변경사항 적용에 실패했습니다.", - "P_INVALID_POLICY_PARAMETER": "정책 파라미터가 템플릿의 파라미터 스키마에 유효하지 않습니다. 파라미터를 확인하세요.", + "P_CREATE_ALREADY_EXISTED_NAME": "정첵에 이미 존재하는 이름입니다.", + "P_NOT_FOUND_POLICY": "정책이 존재하지 않습니다.", + "P_INVALID_POLICY_NAME": "유효하지 않은 정책 이름입니다. 정책 이름을 확인하세요.", + "P_INVALID_POLICY_RESOURCE_NAME": "유효하지 않은 정책 자원 이름(k8s 자원 이름)입니다. 정책 자원 이름을 확인하세요.", + "P_INVALID_MATCH": "유효하지 않은 match 설정입니다. match 설정을 확인하세요.", + "P_FAILED_FETCH_POLICY": "정책 ID에 해당하는 정책을 가져오는데 실패했습니다.", + "P_FAILED_FETCH_CLUSTER": "정책의 클러스터 정보를 가져오는데 실패했습니다.", + "P_FAILED_FETCH_TEMPLATE": "정책의 템플릿 정보를 가져오는데 실패했습니다.", + "P_CALL_TO_APPLY_KUBERNETES": "쿠버네티스 클러스터 호출에 실패했습니다.", + "P_FAILED_TO_APPLY_KUBERNETES": "쿠버네티스 클러스터 변경사항 적용에 실패했습니다.", + "P_INVALID_POLICY_PARAMETER": "정책 파라미터가 템플릿의 파라미터 스키마에 유효하지 않습니다. 파라미터를 확인하세요.", } func (m ErrorCode) GetText() string {