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

Modify code such that verbs & resources for local env #88

Merged
merged 1 commit into from
Dec 18, 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
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
Loading