From 949509459736c1d4526e66e05758f9e63de24600 Mon Sep 17 00:00:00 2001 From: Youngjin Jo Date: Wed, 18 Dec 2024 21:50:26 +0900 Subject: [PATCH] refactor: complete verb&resource for local env Signed-off-by: Youngjin Jo --- .gitignore | 2 +- cmd/common/fetchService.go | 27 +++++++++++++++---------- cmd/common/helpers.go | 41 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 1d51fec..a103f6a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ .idea - dist/ +test/yaml/ diff --git a/cmd/common/fetchService.go b/cmd/common/fetchService.go index 8089cf3..07ff4aa 100644 --- a/cmd/common/fetchService.go +++ b/cmd/common/fetchService.go @@ -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 diff --git a/cmd/common/helpers.go b/cmd/common/helpers.go index 1969427..1473b74 100644 --- a/cmd/common/helpers.go +++ b/cmd/common/helpers.go @@ -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))