Skip to content

Commit

Permalink
Removed sdk ref and added http get call to get shards
Browse files Browse the repository at this point in the history
  • Loading branch information
tirthct committed Dec 15, 2023
1 parent f282fd8 commit 8215614
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 32 deletions.
13 changes: 1 addition & 12 deletions cmd/ocm/list/rhRegion/cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rhRegion

import (
"context"
"fmt"

"github.com/openshift-online/ocm-cli/pkg/config"
Expand All @@ -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 {
Expand All @@ -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)
}
Expand Down
13 changes: 8 additions & 5 deletions cmd/ocm/login/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
46 changes: 31 additions & 15 deletions pkg/rhRegion/rhRegion.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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(), &regions)
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(&regions)
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
}

0 comments on commit 8215614

Please sign in to comment.