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

Revert "Permission-API Endpoint mapping 개선" #465

Merged
merged 1 commit into from
May 2, 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
104 changes: 36 additions & 68 deletions hack/endpoint-codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ package api
//)
//`

const apiMapTemplateStr = `var MapWithEndpoint = map[Endpoint]EndpointInfo{
const apiMapTemplateStr = `var ApiMap = map[Endpoint]EndpointInfo{
{{- range .}}
{{.Name}}: {
Name: "{{.Name}}",
Expand All @@ -46,51 +46,30 @@ const apiMapTemplateStr = `var MapWithEndpoint = map[Endpoint]EndpointInfo{
}
`

const restCodeTemplateStr = `var MapWithName = reverseApiMap()

func reverseApiMap() map[string]Endpoint {
m := make(map[string]Endpoint)
for k, v := range MapWithEndpoint {
m[v.Name] = k
const stringFunctionTemplateStr = `func (e Endpoint) String() string {
switch e {
{{- range .}}
case {{.Name}}:
return "{{.Name}}"
{{- end}}
default:
return ""
}
return m
}

func (e Endpoint) String() string {
return MapWithEndpoint[e].Name
}
`

func GetEndpoint(name string) Endpoint {
return MapWithName[name]
const getEndpointFunctionTemplateStr = `func GetEndpoint(name string) Endpoint {
switch name {
{{- range .}}
case "{{.Name}}":
return {{.Name}}
{{- end}}
default:
return -1
}
}

`

//
//const stringFunctionTemplateStr = `func (e Endpoint) String() string {
// switch e {
//{{- range .}}
// case {{.Name}}:
// return "{{.Name}}"
//{{- end}}
// default:
// return ""
// }
//}
//`
//
//const getEndpointFunctionTemplateStr = `func GetEndpoint(name string) Endpoint {
// switch name {
//{{- range .}}
// case "{{.Name}}":
// return {{.Name}}
//{{- end}}
// default:
// return -1
// }
//}
//`

func main() {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, endpointFilePath, nil, parser.ParseComments)
Expand Down Expand Up @@ -178,42 +157,31 @@ func main() {
log.Fatalf("failed to execute template: %v", err)
}

restCodeTemplate := template.New("restCode")
restCodeTemplate, err = restCodeTemplate.Parse(restCodeTemplateStr)
// contents for stringFunction
stringFunctionTemplate := template.New("stringFunction")
stringFunctionTemplate, err = stringFunctionTemplate.Parse(stringFunctionTemplateStr)
if err != nil {
log.Fatalf("failed to parse template: %v", err)
}
var restCode bytes.Buffer
if err := restCodeTemplate.Execute(&restCode, nil); err != nil {
var stringFunctionCode bytes.Buffer
if err := stringFunctionTemplate.Execute(&stringFunctionCode, endpoints); err != nil {
log.Fatalf("failed to execute template: %v", err)
}

// contents for getEndpointFunction
getEndpointFunctionTemplate := template.New("getEndpointFunction")
getEndpointFunctionTemplate, err = getEndpointFunctionTemplate.Parse(getEndpointFunctionTemplateStr)
if err != nil {
log.Fatalf("failed to parse template: %v", err)
}
var getEndpointFunctionCode bytes.Buffer
if err := getEndpointFunctionTemplate.Execute(&getEndpointFunctionCode, endpoints); err != nil {
log.Fatalf("failed to execute template: %v", err)
}
//
//// contents for stringFunction
//stringFunctionTemplate := template.New("stringFunction")
//stringFunctionTemplate, err = stringFunctionTemplate.Parse(stringFunctionTemplateStr)
//if err != nil {
// log.Fatalf("failed to parse template: %v", err)
//}
//var stringFunctionCode bytes.Buffer
//if err := stringFunctionTemplate.Execute(&stringFunctionCode, endpoints); err != nil {
// log.Fatalf("failed to execute template: %v", err)
//}
//
//// contents for getEndpointFunction
//getEndpointFunctionTemplate := template.New("getEndpointFunction")
//getEndpointFunctionTemplate, err = getEndpointFunctionTemplate.Parse(getEndpointFunctionTemplateStr)
//if err != nil {
// log.Fatalf("failed to parse template: %v", err)
//}
//var getEndpointFunctionCode bytes.Buffer
//if err := getEndpointFunctionTemplate.Execute(&getEndpointFunctionCode, endpoints); err != nil {
// log.Fatalf("failed to execute template: %v", err)
//}

// replace original file(endpointFilePath) with new contents
//contents := indexCode.String() + endpointCode.String() + apiMapCode.String() + stringFunctionCode.String() + getEndpointFunctionCode.String()
//contents := indexCode.String() + apiMapCode.String() + stringFunctionCode.String() + getEndpointFunctionCode.String()
contents := indexCode.String() + apiMapCode.String() + restCode.String()
contents := indexCode.String() + apiMapCode.String() + stringFunctionCode.String() + getEndpointFunctionCode.String()
newFilePath := strings.Replace(endpointFilePath, "endpoint", "generated_endpoints.go", 1)

if err := ioutil.WriteFile(newFilePath, []byte(contents), 0644); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func migrateSchema(db *gorm.DB) error {
if err := db.AutoMigrate(&model.CacheEmailCode{},
&model.ExpiredTokenTime{},
&model.Role{},
&model.PermissionEndpoint{},
&model.CloudAccount{},
&model.StackTemplate{},
&model.Organization{},
Expand Down Expand Up @@ -132,7 +131,7 @@ func EnsureDefaultRows(db *gorm.DB) error {
for _, ep := range eps {
storedEps[ep.Name] = struct{}{}
}
for _, ep := range api.MapWithEndpoint {
for _, ep := range api.ApiMap {
if _, ok := storedEps[ep.Name]; !ok {
if err := repoFactory.Endpoint.Create(ctx, &model.Endpoint{
Name: ep.Name,
Expand Down
10 changes: 3 additions & 7 deletions internal/delivery/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ const (
GetPolicyNotification

// Stack
CreateStack // 스택관리/생성
GetStacks // 스택관리/조회
CreateStack // 스택관리/생성
CheckStackName // 스택관리/조회
GetStack // 스택관리/조회
UpdateStack // 스택관리/수정
DeleteStack // 스택관리/삭제
CheckStackName // 스택관리/조회
GetStackStatus // 스택관리/조회
GetStackKubeConfig // 스택관리/조회
GetStackStatus // 스택관리/조회
SetFavoriteStack // 스택관리/조회
DeleteFavoriteStack // 스택관리/조회
InstallStack // 스택관리 / 조회
Expand Down Expand Up @@ -219,10 +219,6 @@ const (

// Permission
GetPermissionTemplates
GetEndpointsByPermissionId

// Endpoint
Admin_GetEndpoints

// Admin_User
Admin_CreateUser
Expand Down
3 changes: 2 additions & 1 deletion internal/delivery/api/endpoints_permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestEndpointsUsage(t *testing.T) {
var allEndpoints []string
for _, v := range api.MapWithEndpoint {
for _, v := range api.ApiMap {
allEndpoints = append(allEndpoints, v.Name)
}
//allEndpoints := []Endpoint{
Expand All @@ -26,6 +26,7 @@ func TestEndpointsUsage(t *testing.T) {
ps.Stack,
ps.Policy,
ps.Common,
ps.Admin,
}

leafPermissions := make([]*model.Permission, 0)
Expand Down
Loading
Loading