Skip to content

Commit

Permalink
Merge pull request #465 from openinfradev/revert-462-authorization_en…
Browse files Browse the repository at this point in the history
…dpoint

Revert "Permission-API Endpoint mapping 개선"
  • Loading branch information
ktkfree authored May 2, 2024
2 parents 704c3f4 + ae3fa78 commit ec7cc9d
Show file tree
Hide file tree
Showing 26 changed files with 2,006 additions and 1,586 deletions.
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

0 comments on commit ec7cc9d

Please sign in to comment.