Skip to content

Commit

Permalink
Merge pull request #288 from sangkenlee/policytemplate-refactoring
Browse files Browse the repository at this point in the history
정책 템플릿 코드 리팩토링 반영
  • Loading branch information
ktkfree authored Mar 19, 2024
2 parents f6ee6b2 + 572b4bb commit bdeca73
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 386 deletions.
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/servicequotas v1.20.1
github.com/aws/aws-sdk-go-v2/service/ses v1.21.0
github.com/aws/aws-sdk-go-v2/service/sts v1.27.0
github.com/deckarep/golang-set/v2 v2.6.0
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-playground/locales v0.14.1
github.com/go-playground/universal-translator v0.18.1
Expand All @@ -37,8 +38,10 @@ require (
github.com/thoas/go-funk v0.9.3
github.com/vmware-tanzu/cluster-api-provider-bringyourownhost v0.5.0
golang.org/x/crypto v0.21.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/net v0.22.0
golang.org/x/oauth2 v0.17.0
golang.org/x/text v0.14.0
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/yaml.v3 v3.0.1
gorm.io/datatypes v1.2.0
Expand Down Expand Up @@ -124,10 +127,8 @@ require (
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.15.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
Expand Down
29 changes: 23 additions & 6 deletions internal/delivery/http/policy-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func (h *PolicyTemplateHandler) CreatePolicyTemplate(w http.ResponseWriter, r *h
err := UnmarshalRequestInput(r, &input)

if err != nil {
log.Errorf(r.Context(), "error is :%s(%T)", err.Error(), err)
ErrorJSON(w, r, err)
return
}
Expand All @@ -80,7 +79,7 @@ func (h *PolicyTemplateHandler) CreatePolicyTemplate(w http.ResponseWriter, r *h
}

var out domain.CreatePolicyTemplateReponse
out.ID = domain.PolicyTemplateId(policyTemplateId)
out.ID = policyTemplateId.String()

ResponseJSON(w, r, http.StatusOK, out)
}
Expand Down Expand Up @@ -122,7 +121,7 @@ func (h *PolicyTemplateHandler) UpdatePolicyTemplate(w http.ResponseWriter, r *h
return
}

err = h.usecase.Update(r.Context(), id, input)
err = h.usecase.Update(r.Context(), id, input.TemplateName, input.Description, input.Severity, input.Deprecated, input.PermittedOrganizationIds)

if err != nil {
log.Errorf(r.Context(), "error is :%s(%T)", err.Error(), err)
Expand All @@ -135,7 +134,7 @@ func (h *PolicyTemplateHandler) UpdatePolicyTemplate(w http.ResponseWriter, r *h
return
}

ResponseJSON(w, r, http.StatusOK, "")
ResponseJSON(w, r, http.StatusOK, nil)
}

// DeletePolicyTemplate godoc
Expand Down Expand Up @@ -228,11 +227,20 @@ func (h *PolicyTemplateHandler) GetPolicyTemplate(w http.ResponseWriter, r *http
return
}

if policyTemplate == nil {
ResponseJSON(w, r, http.StatusNotFound, nil)
return
}

var out domain.GetPolicyTemplateResponse
if err = serializer.Map(r.Context(), *policyTemplate, &out.PolicyTemplate); err != nil {
log.Error(r.Context(), err)
}

if err = h.usecase.UpdatePermittedOrganizations(r.Context(), policyTemplate, &out.PolicyTemplate); err != nil {
log.Error(r.Context(), err)
}

ResponseJSON(w, r, http.StatusOK, out)
}

Expand Down Expand Up @@ -271,6 +279,10 @@ func (h *PolicyTemplateHandler) ListPolicyTemplate(w http.ResponseWriter, r *htt
}
}

if err = h.usecase.UpdatePermittedOrganizationsForList(r.Context(), &policyTemplates, &out.PolicyTemplates); err != nil {
log.Error(r.Context(), err)
}

if out.Pagination, err = pg.Response(r.Context()); err != nil {
log.Info(r.Context(), err)
}
Expand Down Expand Up @@ -415,13 +427,14 @@ func (h *PolicyTemplateHandler) GetPolicyTemplateVersion(w http.ResponseWriter,

policyTemplate, err := h.usecase.GetPolicyTemplateVersion(r.Context(), id, version)
if err != nil {
log.Errorf(r.Context(), "error is :%s(%T)", err.Error(), err)
if _, status := httpErrors.ErrorResponse(err); status == http.StatusNotFound {
ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "PT_NOT_FOUND_POLICY_TEMPLATE_VERSION", ""))
return
}
}

ErrorJSON(w, r, err)
if policyTemplate == nil {
ResponseJSON(w, r, http.StatusNotFound, nil)
return
}

Expand All @@ -430,6 +443,10 @@ func (h *PolicyTemplateHandler) GetPolicyTemplateVersion(w http.ResponseWriter,
log.Error(r.Context(), err)
}

if err = h.usecase.UpdatePermittedOrganizations(r.Context(), policyTemplate, &out.PolicyTemplate); err != nil {
log.Error(r.Context(), err)
}

ResponseJSON(w, r, http.StatusOK, out)
}

Expand Down
68 changes: 62 additions & 6 deletions internal/model/policy-template.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package model

import (
"encoding/json"
"strings"

"github.com/google/uuid"
"github.com/openinfradev/tks-api/pkg/domain"
"gorm.io/gorm"
)

const (
FILE_DELIMETER = "---\n"
)

type PolicyTemplateSupportedVersion struct {
gorm.Model

Expand All @@ -21,18 +28,17 @@ type PolicyTemplate struct {
gorm.Model

ID uuid.UUID `gorm:"primarykey;type:varchar(36);not null"`
Type string // Org or Tks
Name string
Version string
TemplateName string
Type string // Org or Tks
Version string `gorm:"-:all"` // 삭제 예정
SupportedVersions []PolicyTemplateSupportedVersion `gorm:"foreignKey:PolicyTemplateId"`
Description string
Kind string
Deprecated bool
Mandatory bool // Tks 인 경우에는 무시
Severity string
PermittedOrganizations []Organization `gorm:"many2many:policy_template_permitted_organiations;"`
TemplateName string `gorm:"-:all" json:"templateName"`
ParametersSchema []domain.ParameterDef `gorm:"-:all" json:"parametersSchema,omitempty"`
PermittedOrganizations []Organization `gorm:"many2many:policy_template_permitted_organiations"`
ParametersSchema []domain.ParameterDef `gorm:"-:all"`
Rego string `gorm:"-:all"`
Libs []string `gorm:"-:all"`
PermittedOrganizationIds []string `gorm:"-:all"`
Expand All @@ -41,3 +47,53 @@ type PolicyTemplate struct {
UpdatorId *uuid.UUID `gorm:"type:uuid"`
Updator User `gorm:"foreignKey:UpdatorId"`
}

func (pt *PolicyTemplate) BeforeCreate(tx *gorm.DB) (err error) {
pt.ID = uuid.New()

jsonByte, err := json.Marshal(pt.ParametersSchema)

if err != nil {
return err
}

libs := strings.Join(pt.Libs, FILE_DELIMETER)

pt.Version = "v1.0.0"

pt.SupportedVersions = []PolicyTemplateSupportedVersion{
{
Version: "v1.0.0",
ParameterSchema: string(jsonByte),
Rego: pt.Rego,
Libs: libs,
},
}

return nil
}

func (pt *PolicyTemplate) AfterFind(tx *gorm.DB) (err error) {
// 목록 조회 시 에러가 발생해서 전체 조회가 실패하는 것을 방지하기 위해서 에러는 무시
if len(pt.SupportedVersions) > 0 {
supportedVersion := pt.SupportedVersions[0]
pt.Version = supportedVersion.Version
pt.Rego = supportedVersion.Rego
pt.Libs = strings.Split(supportedVersion.ParameterSchema, FILE_DELIMETER)

// 마찬가지로 에러 무시
_ = json.Unmarshal([]byte(supportedVersion.ParameterSchema), &pt.ParametersSchema)
}

pt.PermittedOrganizationIds = make([]string, len(pt.PermittedOrganizations))
for i, org := range pt.PermittedOrganizations {
pt.PermittedOrganizationIds[i] = org.ID
}

return
}

type PolicyTemplatePermittedOrganization struct {
PolicyTemplateId uuid.UUID `gorm:"primarykey"`
OrganizationId string `gorm:"primarykey"`
}
Loading

0 comments on commit bdeca73

Please sign in to comment.