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

StackPolicyStatistics API에 tks템플릿, org 템플릿으로 생성된 정책 수 추가 #365

Merged
merged 1 commit into from
Apr 9, 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
2 changes: 1 addition & 1 deletion internal/delivery/http/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ func (h *PolicyHandler) ListStackPolicyStatus(w http.ResponseWriter, r *http.Req
// @Param sortColumn query string false "sortColumn"
// @Param sortOrder query string false "sortOrder"
// @Param filters query []string false "filters"
// @Success 200 {object} domain.ListStackPolicyStatusResponse
// @Success 200 {object} domain.StackPolicyStatistics
// @Router /organizations/{organizationId}/stacks/{stackId}/statistics [get]
// @Security JWT
func (h *PolicyHandler) StackPolicyStatistics(w http.ResponseWriter, r *http.Request) {
Expand Down
29 changes: 17 additions & 12 deletions internal/repository/policy-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type IPolicyTemplateRepository interface {
GetLatestTemplateVersion(ctx context.Context, policyTemplateId uuid.UUID) (version string, err error)
CountTksTemplateByOrganization(ctx context.Context, organizationId string) (count int64, err error)
CountOrganizationTemplate(ctx context.Context, organizationId string) (count int64, err error)
CountPolicyFromOrganizationTemplate(ctx context.Context, organizationId string) (count int64, err error)
}

type PolicyTemplateRepository struct {
Expand Down Expand Up @@ -184,22 +185,26 @@ func (r *PolicyTemplateRepository) CountTksTemplateByOrganization(ctx context.Co
}

func (r *PolicyTemplateRepository) CountOrganizationTemplate(ctx context.Context, organizationId string) (count int64, err error) {
subQueryAloowedAll := r.db.Table("policy_template_permitted_organizations").Select("policy_template_id")
subQueryMatchId := r.db.Table("policy_template_permitted_organizations").Select("policy_template_id").
err = r.db.WithContext(ctx).
Model(&model.PolicyTemplate{}).
Where("type = ?", "organization").
Where("organization_id = ?", organizationId).
Count(&count).Error

return
}

func (r *PolicyTemplateRepository) CountPolicyFromOrganizationTemplate(ctx context.Context, organizationId string) (count int64, err error) {
subQuery := r.db.Table("policy_templates").Select("id").
Where("type = ?", "organization").
Where("organization_id = ?", organizationId)

err = r.db.WithContext(ctx).
Model(&model.PolicyTemplate{}).
Model(&model.Policy{}).
Where(
// tks 템플릿인 경우
r.db.Where("type = ?", "tks").
Where(
// permitted_organizations이 비어있거나
r.db.Where("id not in (?)", subQueryAloowedAll).
Or("id in (?)", subQueryMatchId),
// permitted_organization에 매칭되는 템플릿 아이디가 있거나
),
).Count(&count).Error
r.db.Where("template_id in (?)", subQuery),
).
Count(&count).Error

return
}
Expand Down
15 changes: 11 additions & 4 deletions internal/usecase/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,11 +752,18 @@ func (u *PolicyUsecase) GetPolicyStatistics(ctx context.Context, organizationId
Total: tksTemplateCount + orgTemplateCount,
}

policyFromOrgTemplates, err := u.templateRepo.CountPolicyFromOrganizationTemplate(ctx, organizationId)
if err != nil {
return nil, err
}

result.Policy = domain.PolicyCount{
Deny: deny,
Warn: warn,
Dryrun: dryrun,
Total: policyTotal,
Deny: deny,
Warn: warn,
Dryrun: dryrun,
FromOrgTemplate: policyFromOrgTemplates,
FromTksTemplate: policyTotal - policyFromOrgTemplates,
Total: policyTotal,
}

return &result, nil
Expand Down
10 changes: 6 additions & 4 deletions pkg/domain/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,12 @@ type TemplateCount struct {
}

type PolicyCount struct {
Deny int64 `json:"deny"`
Warn int64 `json:"warn"`
Dryrun int64 `json:"dryrun"`
Total int64 `json:"total"`
Deny int64 `json:"deny"`
Warn int64 `json:"warn"`
Dryrun int64 `json:"dryrun"`
FromTksTemplate int64 `json:"fromTksTemplate"`
FromOrgTemplate int64 `json:"fromOrgTemplate"`
Total int64 `json:"total"`
}

type PolicyStatisticsResponse struct {
Expand Down
Loading