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

policy resource name 중복 체크 api 추가 #459

Merged
merged 1 commit into from
Apr 30, 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
1 change: 1 addition & 0 deletions internal/delivery/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ const (
UpdatePolicy
UpdatePolicyTargetClusters
ExistsPolicyName
ExistsPolicyResourceName
GetPolicyEdit
AddPoliciesForStack
DeletePoliciesForStack
Expand Down
8 changes: 8 additions & 0 deletions internal/delivery/api/generated_endpoints.go.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,10 @@ var ApiMap = map[Endpoint]EndpointInfo{
Name: "ExistsPolicyName",
Group: "Policy",
},
ExistsPolicyResourceName: {
Name: "ExistsPolicyResourceName",
Group: "Policy",
},
GetPolicyEdit: {
Name: "GetPolicyEdit",
Group: "Policy",
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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":
Expand Down
45 changes: 43 additions & 2 deletions internal/delivery/http/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion internal/usecase/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
}
Expand Down
22 changes: 11 additions & 11 deletions pkg/httpErrors/errorCode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading