Skip to content

Commit 55718f5

Browse files
Merge pull request #344 from clcollins/incorporate_ocm_login
Default to OCM login via environment
2 parents 115f5f5 + 3ed19af commit 55718f5

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

pkg/utils/ocm.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,33 @@ import (
44
"encoding/json"
55
"fmt"
66
"log"
7+
"os"
78
"strings"
89

910
"github.com/aws/aws-sdk-go/aws/arn"
10-
"github.com/openshift-online/ocm-cli/pkg/ocm"
1111
sdk "github.com/openshift-online/ocm-sdk-go"
1212
v1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
1313
)
1414

1515
const ClusterServiceClusterSearch = "id = '%s' or name = '%s' or external_id = '%s'"
1616

17+
const (
18+
productionURL = "https://api.openshift.com"
19+
stagingURL = "https://api.stage.openshift.com"
20+
integrationURL = "https://api.integration.openshift.com"
21+
)
22+
23+
var urlAliases = map[string]string{
24+
"production": productionURL,
25+
"prod": productionURL,
26+
"prd": productionURL,
27+
"staging": stagingURL,
28+
"stage": stagingURL,
29+
"stg": stagingURL,
30+
"integration": integrationURL,
31+
"int": integrationURL,
32+
}
33+
1734
// GetClusterAnyStatus returns an OCM cluster object given an OCM connection and cluster id
1835
// (internal and external ids both supported).
1936
func GetClusterAnyStatus(conn *sdk.Connection, clusterId string) (*v1.Cluster, error) {
@@ -104,7 +121,26 @@ func GenerateQuery(clusterIdentifier string) string {
104121
}
105122

106123
func CreateConnection() *sdk.Connection {
107-
connection, err := ocm.NewConnection().Build()
124+
token := os.Getenv("OCM_TOKEN")
125+
url := os.Getenv("OCM_URL")
126+
127+
connectionBuilder := sdk.NewConnectionBuilder()
128+
129+
if token != "" {
130+
connectionBuilder.Tokens(token)
131+
}
132+
133+
if url != "" {
134+
gatewayURL, ok := urlAliases[url]
135+
if !ok {
136+
fmt.Println("Invalid OCM_URL found: ", url)
137+
fmt.Println("Valid URL aliases are: 'production', 'staging', 'integration'")
138+
}
139+
connectionBuilder.URL(gatewayURL)
140+
}
141+
142+
connection, err := connectionBuilder.Build()
143+
108144
if err != nil {
109145
if strings.Contains(err.Error(), "Not logged in, run the") {
110146
log.Fatalf("Failed to create OCM connection: Authentication error, run the 'ocm login' command first.")

0 commit comments

Comments
 (0)