Skip to content

Commit

Permalink
feat: add prod level connection
Browse files Browse the repository at this point in the history
Signed-off-by: Youngjin Jo <[email protected]>
  • Loading branch information
yjinjo committed Dec 18, 2024
1 parent 22b5a98 commit 4093c20
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
53 changes: 31 additions & 22 deletions cmd/common/fetchService.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,24 @@ func FetchService(serviceName string, verb string, resourceName string, options
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 strings.Contains(config.Environments[config.Environment].URL, "megazone.io") {
hostPort = fmt.Sprintf("%s.kr1.api.spaceone.megazone.io:443", convertServiceNameToEndpoint(serviceName))
} 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]
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
Expand Down Expand Up @@ -375,6 +379,7 @@ func loadConfig() (*Config, error) {
func fetchJSONResponse(config *Config, serviceName string, verb string, resourceName string, options *FetchOptions) ([]byte, error) {
var conn *grpc.ClientConn
var err error
var hostPort string

if strings.HasPrefix(config.Environment, "local-") {
conn, err = grpc.Dial("localhost:50051", grpc.WithInsecure(),
Expand All @@ -386,20 +391,24 @@ func fetchJSONResponse(config *Config, serviceName string, verb string, resource
return nil, fmt.Errorf("connection failed: unable to connect to local server: %v", err)
}
} 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]
break
if strings.Contains(config.Environments[config.Environment].URL, "megazone.io") {
hostPort = fmt.Sprintf("%s.kr1.api.spaceone.megazone.io:443", convertServiceNameToEndpoint(serviceName))
} 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]
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)
}

tlsConfig := &tls.Config{
InsecureSkipVerify: false,
Expand Down
28 changes: 17 additions & 11 deletions cmd/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func handleLocalEnvironment(serviceName string) (map[string][]string, error) {
return nil, fmt.Errorf("only plugin service is supported in local environment")
}

// local 환경의 plugin 서비스 endpoint 설정
conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())
if err != nil {
return nil, fmt.Errorf("failed to connect to local plugin service: %v", err)
Expand Down Expand Up @@ -143,19 +142,26 @@ func fetchVerbResourceMap(serviceName string, config *Config) (map[string][]stri
// Parse URL to get environment
urlParts := strings.Split(envConfig.URL, ".")
var envPrefix string
for i, part := range urlParts {
if part == "console" && i+1 < len(urlParts) {
envPrefix = urlParts[i+1]
break
var hostPort string

if strings.Contains(envConfig.URL, "megazone.io") {
endpointServiceName := convertServiceNameToEndpoint(serviceName)
hostPort = fmt.Sprintf("%s.kr1.api.spaceone.megazone.io:443", endpointServiceName)
} else {
for i, part := range urlParts {
if part == "console" && i+1 < len(urlParts) {
envPrefix = urlParts[i+1]
break
}
}
}

if envPrefix == "" {
return nil, fmt.Errorf("environment prefix not found in URL: %s", envConfig.URL)
}
if envPrefix == "" {
return nil, fmt.Errorf("environment prefix not found in URL: %s", envConfig.URL)
}

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

tlsConfig := &tls.Config{
InsecureSkipVerify: false,
Expand Down

0 comments on commit 4093c20

Please sign in to comment.