Skip to content

Commit

Permalink
Removed RhRegion ref, fixed list re-regions method to remove armed ch…
Browse files Browse the repository at this point in the history
…eck and used %w instead of %v
  • Loading branch information
tirthct committed Dec 18, 2023
1 parent 8c91c17 commit 80c1121
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 35 deletions.
20 changes: 3 additions & 17 deletions cmd/ocm/list/rhRegion/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

"github.com/openshift-online/ocm-cli/pkg/config"
"github.com/openshift-online/ocm-cli/pkg/ocm"
"github.com/openshift-online/ocm-cli/pkg/rhRegion"
"github.com/spf13/cobra"
)
Expand All @@ -27,27 +26,14 @@ func run(cmd *cobra.Command, argv []string) error {
return fmt.Errorf("Not logged in, run the 'login' command")
}

connection, err := ocm.NewConnection().Build()
regions, err := rhRegion.GetRhRegions(cfg.URL)
if err != nil {
return err
return fmt.Errorf("Failed to get OCM regions: %w", err)
}
defer connection.Close()

// Check that the configuration has credentials or tokens that don't have expired:
armed, reason, err := cfg.Armed()
if err != nil {
return err
}
if !armed {
return fmt.Errorf("Not logged in, %s, run the 'login' command", reason)
}

regions, err := rhRegion.GetRhRegions(connection.URL())
if err != nil {
return fmt.Errorf("Failed to get OCM regions: %v", err)
}
for regionName := range regions {
fmt.Println(regionName)
}
return nil

}
5 changes: 2 additions & 3 deletions cmd/ocm/login/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ func run(cmd *cobra.Command, argv []string) error {

// Update the configuration with the values given in the command line:
cfg.TokenURL = tokenURL
cfg.RhRegion = args.rhRegion
cfg.ClientID = clientID
cfg.ClientSecret = args.clientSecret
cfg.Scopes = args.scopes
Expand Down Expand Up @@ -276,10 +275,10 @@ 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 != "" {
if args.rhRegion != "" {
regValue, err := rhRegion.GetRhRegion(connection.URL(), args.rhRegion)
if err != nil {
return fmt.Errorf("Can't find region: %v", err)
return fmt.Errorf("Can't find region: %w", err)
}
cfg.URL = regValue.URL
}
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
gitlab.com/c0b/go-ordered-json v0.0.0-20201030195603-febf46534d5a
golang.org/x/net v0.10.0
golang.org/x/term v0.10.0
golang.org/x/text v0.11.0
gopkg.in/yaml.v3 v3.0.1
Expand Down Expand Up @@ -73,10 +72,9 @@ require (
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/zgalor/weberr v0.7.0 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/tools v0.9.3 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

replace github.com/openshift-online/ocm-sdk-go v0.1.388 => ../ocm-sdk-go
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU
github.com/onsi/ginkgo/v2 v2.11.0/go.mod h1:ZhrRA5XmEE3x3rhlzamx/JJvujdZoJ2uvgI7kR0iZvM=
github.com/onsi/gomega v1.27.8 h1:gegWiwZjBsf2DgiSbf5hpokZ98JVDMcWkUiigk6/KXc=
github.com/onsi/gomega v1.27.8/go.mod h1:2J8vzI/s+2shY9XHRApDkdgPo1TKT7P2u6fXeJKFnNQ=
github.com/openshift-online/ocm-sdk-go v0.1.388 h1:c8yPCUQwJm3QhcVmnyMPFpeDtxPBaPeYh5hLv1vg9YQ=
github.com/openshift-online/ocm-sdk-go v0.1.388/go.mod h1:/+VFIw1iW2H0jEkFH4GnbL/liWareyzsL0w7mDIudB4=
github.com/openshift/rosa v1.2.24 h1:vv0yYnWHx6CCPEAau/0rS54P2ksaf+uWXb1TQPWxiYE=
github.com/openshift/rosa v1.2.24/go.mod h1:MVXB27O3PF8WoOic23I03mmq6/9kVxpFx6FKyLMCyrQ=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
Expand Down
11 changes: 3 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ type Config struct {
RefreshToken string `json:"refresh_token,omitempty" doc:"Offline or refresh token."`
Scopes []string `json:"scopes,omitempty" doc:"OpenID scope. If this option is used it will replace completely the default scopes. Can be repeated multiple times to specify multiple scopes."`
TokenURL string `json:"token_url,omitempty" doc:"OpenID token URL."`
RhRegion string
URL string `json:"url,omitempty" doc:"URL of the API gateway. The value can be the complete URL or an alias. The valid aliases are 'production', 'staging' and 'integration'."`
User string `json:"user,omitempty" doc:"User name."`
Pager string `json:"pager,omitempty" doc:"Pager command, for example 'less'. If empty no pager will be used."`
URL string `json:"url,omitempty" doc:"URL of the API gateway. The value can be the complete URL or an alias. The valid aliases are 'production', 'staging' and 'integration'."`
User string `json:"user,omitempty" doc:"User name."`
Pager string `json:"pager,omitempty" doc:"Pager command, for example 'less'. If empty no pager will be used."`
}

// Load loads the configuration from the configuration file. If the configuration file doesn't exist
Expand Down Expand Up @@ -271,10 +270,6 @@ func (c *Config) Connection() (connection *sdk.Connection, err error) {
}
builder.Insecure(c.Insecure)

if c.RhRegion != "" {
builder.RhRegion(c.RhRegion)
}

// Create the connection:
connection, err = builder.Build()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/rhRegion/rhRegion.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ func GetRhRegions(ocmServiceUrl string) (map[string]Region, error) {
// Adding nolint here in order to prevent linter from failing due to variable http get
resp, err := http.Get(url) //nolint
if err != nil {
return regions, fmt.Errorf("Can't retrieve shards: %s\n", err)
return regions, fmt.Errorf("Can't retrieve shards: %s", err)
}
err = json2.NewDecoder(resp.Body).Decode(&regions)
if err != nil {
return regions, fmt.Errorf("Can't decode shards: %s\n", err)
return regions, fmt.Errorf("Can't decode shards: %s", err)
}
return regions, nil
}
Expand All @@ -40,7 +40,7 @@ func GetRhRegion(ocmServiceUrl string, regionName string) (Region, error) {
regionName = fmt.Sprintf("rh-%s", regionName)
regVal, ok := regions[regionName]
if !ok {
return Region{}, fmt.Errorf("Can't find region %s\n", regionName)
return Region{}, fmt.Errorf("Can't find region %s", regionName)
}
return regVal, nil
}
Expand All @@ -54,7 +54,7 @@ func DetermineRegionDiscoveryUrl(ocmServiceUrl string) (string, error) {
//TODO: Remove the OR condition from this if statement before the MR merge
if strings.HasSuffix(baseUrl.Hostname(), "integration.openshift.com") || true {
regionDiscoveryHost = "api.integration.openshift.com"
} else if strings.HasSuffix(baseUrl.Hostname(), "integration.openshift.com") {
} else if strings.HasSuffix(baseUrl.Hostname(), "stage.openshift.com") {
regionDiscoveryHost = "api.stage.openshift.com"
}
return fmt.Sprintf("https://%s/static/ocm-shards.json", regionDiscoveryHost), nil
Expand Down

0 comments on commit 80c1121

Please sign in to comment.