From 82156141ddab1d81a4f8e33b9d872ed602334f31 Mon Sep 17 00:00:00 2001 From: tithakka Date: Fri, 15 Dec 2023 14:04:26 -0500 Subject: [PATCH] Removed sdk ref and added http get call to get shards --- cmd/ocm/list/rhRegion/cmd.go | 13 +--------- cmd/ocm/login/cmd.go | 13 ++++++---- pkg/config/config.go | 1 + pkg/rhRegion/rhRegion.go | 46 ++++++++++++++++++++++++------------ 4 files changed, 41 insertions(+), 32 deletions(-) diff --git a/cmd/ocm/list/rhRegion/cmd.go b/cmd/ocm/list/rhRegion/cmd.go index 1c145313..7216042b 100644 --- a/cmd/ocm/list/rhRegion/cmd.go +++ b/cmd/ocm/list/rhRegion/cmd.go @@ -1,7 +1,6 @@ package rhRegion import ( - "context" "fmt" "github.com/openshift-online/ocm-cli/pkg/config" @@ -19,8 +18,6 @@ var Cmd = &cobra.Command{ } func run(cmd *cobra.Command, argv []string) error { - ctx := context.Background() - // Load the configuration file: cfg, err := config.Load() if err != nil { @@ -44,16 +41,8 @@ func run(cmd *cobra.Command, argv []string) error { if !armed { return fmt.Errorf("Not logged in, %s, run the 'login' command", reason) } - // var token string - // if cfg.RefreshToken != "" { - // token = cfg.RefreshToken - // } else if cfg.AccessToken != "" { - // token = cfg.AccessToken - // } else { - // return fmt.Errorf("Cannot find the token, run the 'login' command") - // } - regions, err := rhRegion.GetRhRegions(connection, ctx) + regions, err := rhRegion.GetRhRegions(connection.URL()) if err != nil { return fmt.Errorf("Failed to get OCM regions: %v", err) } diff --git a/cmd/ocm/login/cmd.go b/cmd/ocm/login/cmd.go index 87e1fe1e..f5c8c28f 100644 --- a/cmd/ocm/login/cmd.go +++ b/cmd/ocm/login/cmd.go @@ -20,13 +20,11 @@ import ( "fmt" "os" + "github.com/openshift-online/ocm-cli/pkg/config" "github.com/openshift-online/ocm-cli/pkg/rhRegion" + "github.com/openshift-online/ocm-cli/pkg/urls" sdk "github.com/openshift-online/ocm-sdk-go" "github.com/spf13/cobra" - "golang.org/x/net/context" - - "github.com/openshift-online/ocm-cli/pkg/config" - "github.com/openshift-online/ocm-cli/pkg/urls" ) const ( @@ -279,12 +277,17 @@ func run(cmd *cobra.Command, argv []string) error { // If an OCM region is provided, update the config URL with the SDK generated URL if cfg.RhRegion != "" { - regValue, err := rhRegion.GetRhRegion(connection, context.Background(), args.rhRegion) + regValue, err := rhRegion.GetRhRegion(connection.URL(), args.rhRegion) if err != nil { return fmt.Errorf("Can't find region: %v", err) } cfg.URL = regValue.URL } + err = config.Save(cfg) + if err != nil { + return fmt.Errorf("Can't save config file: %v", err) + } + return nil } diff --git a/pkg/config/config.go b/pkg/config/config.go index 09dbda2d..54809a3d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -108,6 +108,7 @@ func Save(cfg *Config) error { if err != nil { return fmt.Errorf("can't write file '%s': %v", file, err) } + fmt.Println("successfully logged in to", cfg.URL) return nil } diff --git a/pkg/rhRegion/rhRegion.go b/pkg/rhRegion/rhRegion.go index 73499c37..674b6913 100644 --- a/pkg/rhRegion/rhRegion.go +++ b/pkg/rhRegion/rhRegion.go @@ -1,12 +1,11 @@ package rhRegion import ( - "context" - "encoding/json" + json2 "encoding/json" "fmt" - "os" - - sdk "github.com/openshift-online/ocm-sdk-go" + "net/http" + "net/url" + "strings" ) type Region struct { @@ -15,30 +14,47 @@ type Region struct { GCP []string } -func GetRhRegions(connection *sdk.Connection, ctx context.Context) (map[string]Region, error) { +func GetRhRegions(ocmServiceUrl string) (map[string]Region, error) { var regions map[string]Region - resp, err := connection.Get().Path("/static/ocm-shards.json").SendContext(ctx) + url, err := DetermineRegionDiscoveryUrl(ocmServiceUrl) if err != nil { - fmt.Fprintf(os.Stderr, "Can't retrieve shards: %s\n", err) + return regions, fmt.Errorf("Can't determine region discovery URL: %s\n", err) } - fmt.Println(connection.URL()) - err = json.Unmarshal(resp.Bytes(), ®ions) + resp, err := http.Get(url) if err != nil { - fmt.Fprintf(os.Stderr, "Can't unmarshal shards: %s\n", err) + return regions, fmt.Errorf("Can't retrieve shards: %s\n", err) + } + err = json2.NewDecoder(resp.Body).Decode(®ions) + if err != nil { + return regions, fmt.Errorf("Can't decode shards: %s\n", err) } - return regions, nil } -func GetRhRegion(connection *sdk.Connection, ctx context.Context, regionName string) (Region, error) { - regions, err := GetRhRegions(connection, ctx) +func GetRhRegion(ocmServiceUrl string, regionName string) (Region, error) { + regions, err := GetRhRegions(ocmServiceUrl) if err != nil { return Region{}, err } regionName = fmt.Sprintf("rh-%s", regionName) regVal, ok := regions[regionName] if !ok { - return Region{}, fmt.Errorf("Cannot find region %s", regionName) + return Region{}, fmt.Errorf("Can't find region %s\n", regionName) } return regVal, nil } + +func DetermineRegionDiscoveryUrl(ocmServiceUrl string) (string, error) { + baseUrl, err := url.Parse(ocmServiceUrl) + if err != nil { + return "", err + } + regionDiscoveryHost := "api.openshift.com" + if strings.HasSuffix(baseUrl.Hostname(), "integration.openshift.com") { + regionDiscoveryHost = "api.integration.openshift.com" + } else if strings.HasSuffix(baseUrl.Hostname(), "integration.openshift.com") { + regionDiscoveryHost = "api.stage.openshift.com" + } + regionDiscoveryHost = "api.integration.openshift.com" + return fmt.Sprintf("https://%s/static/ocm-shards.json", regionDiscoveryHost), nil +}