Skip to content

Commit

Permalink
Merge pull request #88 from yjinjo/master
Browse files Browse the repository at this point in the history
Modify code such that verbs & resources for local env
  • Loading branch information
yjinjo authored Dec 18, 2024
2 parents 25134b4 + 9495094 commit 495e56d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.idea

dist/
test/yaml/
27 changes: 16 additions & 11 deletions cmd/common/fetchService.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,25 @@ func FetchService(serviceName string, verb string, resourceName string, options
}

// Get hostPort based on environment prefix
var envPrefix string
urlParts := strings.Split(config.Environments[config.Environment].URL, ".")
for i, part := range urlParts {
if part == "console" && i+1 < len(urlParts) {
envPrefix = urlParts[i+1] // Get the part after "console" (dev or stg)
break
var hostPort string
if strings.HasPrefix(config.Environment, "local-") {
hostPort = "localhost:50051"
} else {
var envPrefix string
urlParts := strings.Split(config.Environments[config.Environment].URL, ".")
for i, part := range urlParts {
if part == "console" && i+1 < len(urlParts) {
envPrefix = urlParts[i+1] // Get the part after "console" (dev or stg)
break
}
}
}

if envPrefix == "" {
return nil, fmt.Errorf("environment prefix not found in URL: %s", config.Environments[config.Environment].URL)
}
if envPrefix == "" {
return nil, fmt.Errorf("environment prefix not found in URL: %s", config.Environments[config.Environment].URL)
}

hostPort := fmt.Sprintf("%s.api.%s.spaceone.dev:443", convertServiceNameToEndpoint(serviceName), envPrefix)
hostPort = fmt.Sprintf("%s.api.%s.spaceone.dev:443", convertServiceNameToEndpoint(serviceName), envPrefix)
}

// Configure gRPC connection
var conn *grpc.ClientConn
Expand Down
41 changes: 41 additions & 0 deletions cmd/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,47 @@ func BuildVerbResourceMap(serviceName string) (map[string][]string, error) {
return nil, fmt.Errorf("failed to load config: %v", err)
}

if strings.HasPrefix(config.Environment, "local-") {
conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())
if err != nil {
return nil, fmt.Errorf("local gRPC server connection failed: %v", err)
}
defer conn.Close()

ctx := context.Background()
refClient := grpcreflect.NewClient(ctx, grpc_reflection_v1alpha.NewServerReflectionClient(conn))
defer refClient.Reset()

services, err := refClient.ListServices()
if err != nil {
return nil, fmt.Errorf("failed to list local services: %v", err)
}

verbResourceMap := make(map[string][]string)
for _, s := range services {
if !strings.Contains(s, fmt.Sprintf(".%s.", serviceName)) {
continue
}

serviceDesc, err := refClient.ResolveService(s)
if err != nil {
continue
}

resourceName := s[strings.LastIndex(s, ".")+1:]
for _, method := range serviceDesc.GetMethods() {
verb := method.GetName()
if resources, ok := verbResourceMap[verb]; ok {
verbResourceMap[verb] = append(resources, resourceName)
} else {
verbResourceMap[verb] = []string{resourceName}
}
}
}

return verbResourceMap, nil
}

cacheDir := filepath.Join(home, ".cfctl", "cache", config.Environment)
cacheFile := filepath.Join(cacheDir, fmt.Sprintf("%s_verbs.yaml", serviceName))

Expand Down

0 comments on commit 495e56d

Please sign in to comment.