diff --git a/cmd/common/fetchService.go b/cmd/common/fetchService.go index a5bd64c..b8d43bc 100644 --- a/cmd/common/fetchService.go +++ b/cmd/common/fetchService.go @@ -177,6 +177,7 @@ func FetchService(serviceName string, verb string, resourceName string, options os.Exit(1) } + // TODO: Remove this once all services are migrated to new endpoint format if !hasIdentityService { hostPort = fmt.Sprintf("%s.kr1.api.spaceone.megazone.io:443", convertServiceNameToEndpoint(serviceName)) } else { diff --git a/cmd/common/helpers.go b/cmd/common/helpers.go index 6b8a5cf..2ef21e1 100644 --- a/cmd/common/helpers.go +++ b/cmd/common/helpers.go @@ -180,24 +180,52 @@ func fetchVerbResourceMap(serviceName string, config *Config) (map[string][]stri InsecureSkipVerify: false, } apiEndpoint, _ := other.GetAPIEndpoint(envConfig.Endpoint) - identityEndpoint, _, err := other.GetIdentityEndpoint(apiEndpoint) + identityEndpoint, hasIdentityService, err := other.GetIdentityEndpoint(apiEndpoint) - trimmedEndpoint := strings.TrimPrefix(identityEndpoint, "grpc+ssl://") - parts := strings.Split(trimmedEndpoint, ".") - if len(parts) < 4 { - return nil, fmt.Errorf("invalid endpoint format: %s", trimmedEndpoint) - } + if !hasIdentityService { + // Get endpoints map first + endpointsMap, err := other.FetchEndpointsMap(apiEndpoint) + if err != nil { + return nil, fmt.Errorf("failed to fetch endpoints map: %v", err) + } - // Replace 'identity' with the converted service name - parts[0] = convertServiceNameToEndpoint(serviceName) - serviceEndpoint := strings.Join(parts, ".") + // Find the endpoint for the current service + endpoint, exists := endpointsMap[serviceName] + if !exists { + return nil, fmt.Errorf("endpoint not found for service: %s", serviceName) + } - creds := credentials.NewTLS(tlsConfig) - conn, err = grpc.Dial(serviceEndpoint, grpc.WithTransportCredentials(creds)) - if err != nil { - return nil, fmt.Errorf("connection failed: %v", err) - } + // Parse the endpoint + parts := strings.Split(endpoint, "://") + if len(parts) != 2 { + return nil, fmt.Errorf("invalid endpoint format: %s", endpoint) + } + + // Extract hostPort (remove the /v1 suffix if present) + hostPort := strings.Split(parts[1], "/")[0] + creds := credentials.NewTLS(tlsConfig) + conn, err = grpc.Dial(hostPort, grpc.WithTransportCredentials(creds)) + if err != nil { + return nil, fmt.Errorf("connection failed: %v", err) + } + } else { + trimmedEndpoint := strings.TrimPrefix(identityEndpoint, "grpc+ssl://") + parts := strings.Split(trimmedEndpoint, ".") + if len(parts) < 4 { + return nil, fmt.Errorf("invalid endpoint format: %s", trimmedEndpoint) + } + + // Replace 'identity' with the converted service name + parts[0] = convertServiceNameToEndpoint(serviceName) + serviceEndpoint := strings.Join(parts, ".") + + creds := credentials.NewTLS(tlsConfig) + conn, err = grpc.Dial(serviceEndpoint, grpc.WithTransportCredentials(creds)) + if err != nil { + return nil, fmt.Errorf("connection failed: %v", err) + } + } } defer conn.Close()