Skip to content

Commit

Permalink
Merge pull request #344 from clcollins/incorporate_ocm_login
Browse files Browse the repository at this point in the history
Default to OCM login via environment
  • Loading branch information
openshift-merge-robot authored Mar 20, 2023
2 parents 115f5f5 + 3ed19af commit 55718f5
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions pkg/utils/ocm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,33 @@ import (
"encoding/json"
"fmt"
"log"
"os"
"strings"

"github.com/aws/aws-sdk-go/aws/arn"
"github.com/openshift-online/ocm-cli/pkg/ocm"
sdk "github.com/openshift-online/ocm-sdk-go"
v1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
)

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

const (
productionURL = "https://api.openshift.com"
stagingURL = "https://api.stage.openshift.com"
integrationURL = "https://api.integration.openshift.com"
)

var urlAliases = map[string]string{
"production": productionURL,
"prod": productionURL,
"prd": productionURL,
"staging": stagingURL,
"stage": stagingURL,
"stg": stagingURL,
"integration": integrationURL,
"int": integrationURL,
}

// GetClusterAnyStatus returns an OCM cluster object given an OCM connection and cluster id
// (internal and external ids both supported).
func GetClusterAnyStatus(conn *sdk.Connection, clusterId string) (*v1.Cluster, error) {
Expand Down Expand Up @@ -104,7 +121,26 @@ func GenerateQuery(clusterIdentifier string) string {
}

func CreateConnection() *sdk.Connection {
connection, err := ocm.NewConnection().Build()
token := os.Getenv("OCM_TOKEN")
url := os.Getenv("OCM_URL")

connectionBuilder := sdk.NewConnectionBuilder()

if token != "" {
connectionBuilder.Tokens(token)
}

if url != "" {
gatewayURL, ok := urlAliases[url]
if !ok {
fmt.Println("Invalid OCM_URL found: ", url)
fmt.Println("Valid URL aliases are: 'production', 'staging', 'integration'")
}
connectionBuilder.URL(gatewayURL)
}

connection, err := connectionBuilder.Build()

if err != nil {
if strings.Contains(err.Error(), "Not logged in, run the") {
log.Fatalf("Failed to create OCM connection: Authentication error, run the 'ocm login' command first.")
Expand Down

0 comments on commit 55718f5

Please sign in to comment.