Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to change api url via env var #621

Merged
merged 7 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions cmd/ocm/account/quota/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (

"github.com/spf13/cobra"

"github.com/openshift-online/ocm-cli/pkg/config"
"github.com/openshift-online/ocm-cli/pkg/dump"
"github.com/openshift-online/ocm-cli/pkg/ocm"
amv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
)

Expand Down Expand Up @@ -60,29 +60,12 @@ func init() {
}

func run(cmd *cobra.Command, argv []string) error {
// Load the configuration file:
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("Can't load config file: %v", err)
}
if cfg == nil {
return fmt.Errorf("Not logged in, run the 'login' command")
}

// Check that the configuration has credentials or tokens that haven't have expired:
armed, reason, err := cfg.Armed()
// Create the client for the OCM API:
connection, err := ocm.NewConnection().Build()
if err != nil {
return err
}
if !armed {
return fmt.Errorf("Not logged in, %s, run the 'login' command", reason)
}

// Create the connection, and remember to close it:
connection, err := cfg.Connection()
if err != nil {
return fmt.Errorf("Can't create connection: %v", err)
}
defer connection.Close()

orgID := args.org
Expand Down
24 changes: 3 additions & 21 deletions cmd/ocm/account/roles/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (

"github.com/spf13/cobra"

"github.com/openshift-online/ocm-cli/pkg/config"
"github.com/openshift-online/ocm-cli/pkg/dump"
"github.com/openshift-online/ocm-cli/pkg/ocm"
amv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
)

Expand Down Expand Up @@ -57,29 +57,11 @@ func init() {

func run(cmd *cobra.Command, argv []string) error {

// Load the configuration file:
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("Can't load config file: %v", err)
}
if cfg == nil {
return fmt.Errorf("Not logged in, run the 'login' command")
}

// Check that the configuration has credentials or tokens that haven't have expired:
armed, reason, err := cfg.Armed()
// Create the client for the OCM API:
connection, err := ocm.NewConnection().Build()
if err != nil {
return err
}
if !armed {
return fmt.Errorf("Not logged in, %s, run the 'login' command", reason)
}

// Create the connection, and remember to close it:
connection, err := cfg.Connection()
if err != nil {
return fmt.Errorf("Can't create connection: %v", err)
}
defer connection.Close()

// No role name was provided; Print all roles.
Expand Down
21 changes: 9 additions & 12 deletions cmd/ocm/account/status/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package status

import (
"fmt"
"os"

"github.com/spf13/cobra"

acc_util "github.com/openshift-online/ocm-cli/pkg/account"
"github.com/openshift-online/ocm-cli/pkg/config"
"github.com/openshift-online/ocm-cli/pkg/ocm"
amv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
)

Expand Down Expand Up @@ -61,20 +63,11 @@ func run(cmd *cobra.Command, argv []string) error {
return fmt.Errorf("Not logged in, run the 'login' command")
}

// Check that the configuration has credentials or tokens that haven't have expired:
armed, reason, err := cfg.Armed()
// Create the client for the OCM API:
connection, err := ocm.NewConnection().Build()
if err != nil {
return err
}
if !armed {
return fmt.Errorf("Not logged in, %s, run the 'login' command", reason)
}

// Create the connection, and remember to close it:
connection, err := cfg.Connection()
if err != nil {
return fmt.Errorf("Can't create connection: %v", err)
}
defer connection.Close()

// Send the request:
Expand All @@ -87,9 +80,13 @@ func run(cmd *cobra.Command, argv []string) error {
// Display user and which server they are logged into
currAccount := response.Body()
currOrg := currAccount.Organization()
fmt.Printf("User %s on %s in org '%s' %s (external_id: %s)",
fmt.Printf("User %s on %s in org '%s' %s (external_id: %s) ",
currAccount.Username(), cfg.URL, currOrg.Name(), currOrg.ID(), currOrg.ExternalID())

if urlOverride := os.Getenv("OCM_URL"); urlOverride != "" {
fmt.Printf("- OCM_URL overridden via env to %s - ", urlOverride)
}
iamkirkbater marked this conversation as resolved.
Show resolved Hide resolved

// Display roles currently assigned to the user
roleSlice, err := acc_util.GetRolesFromUsers([]*amv1.Account{currAccount}, connection)
if err != nil {
Expand Down
24 changes: 3 additions & 21 deletions cmd/ocm/account/users/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/spf13/cobra"

acc_util "github.com/openshift-online/ocm-cli/pkg/account"
"github.com/openshift-online/ocm-cli/pkg/config"
"github.com/openshift-online/ocm-cli/pkg/ocm"
amv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
)

Expand Down Expand Up @@ -74,29 +74,11 @@ func init() {

func run(cmd *cobra.Command, argv []string) error {

// Load the configuration file:
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("Can't load config file: %v", err)
}
if cfg == nil {
return fmt.Errorf("Not logged in, run the 'login' command")
}

// Check that the configuration has credentials or tokens that haven't have expired:
armed, reason, err := cfg.Armed()
// Create the client for the OCM API:
connection, err := ocm.NewConnection().Build()
if err != nil {
return err
}
if !armed {
return fmt.Errorf("Not logged in, %s, run the 'login' command", reason)
}

// Create the connection, and remember to close it:
connection, err := cfg.Connection()
if err != nil {
return fmt.Errorf("Can't create connection: %v", err)
}
defer connection.Close()

// needed variables:
Expand Down
15 changes: 4 additions & 11 deletions cmd/ocm/get/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/openshift-online/ocm-cli/pkg/arguments"
"github.com/openshift-online/ocm-cli/pkg/config"
"github.com/openshift-online/ocm-cli/pkg/dump"
"github.com/openshift-online/ocm-cli/pkg/ocm"
"github.com/openshift-online/ocm-cli/pkg/urls"
)

Expand Down Expand Up @@ -69,20 +70,12 @@ func run(cmd *cobra.Command, argv []string) error {
return fmt.Errorf("Not logged in, run the 'login' command")
}

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

// Create the connection:
connection, err := cfg.Connection()
if err != nil {
return fmt.Errorf("Can't create connection: %v", err)
}
defer connection.Close()

// Create and populate the request:
request := connection.Get()
Expand Down
4 changes: 4 additions & 0 deletions cmd/ocm/login/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ func run(cmd *cobra.Command, argv []string) error {
gatewayURL = fmt.Sprintf("https://%s", regValue.URL)
}

if overrideUrl := os.Getenv("OCM_URL"); overrideUrl != "" {
iamkirkbater marked this conversation as resolved.
Show resolved Hide resolved
fmt.Fprintf(os.Stderr, "WARNING: the `OCM_URL` environment variable is set, but is not used for the login command. The `ocm login` command will only use the explicitly set flag's url, which is set as %s\n", gatewayURL)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> OCM_URL=https://api.stage.openshift.com ./ocm login --token=$OCM_ACCESS_TOKEN --url integration
WARNING: the `OCM_URL` environment variable is set, but is not used for the login command. The `ocm login` command will only use the explicitly set flag's url, which is set as https://api.integration.openshift.com

> ocm config get url
https://api.integration.openshift.com

// Update the configuration with the values given in the command line:
cfg.TokenURL = tokenURL
cfg.ClientID = clientID
Expand Down
12 changes: 12 additions & 0 deletions pkg/ocm/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ package ocm

import (
"fmt"
"os"

sdk "github.com/openshift-online/ocm-sdk-go"

"github.com/openshift-online/ocm-cli/pkg/config"
"github.com/openshift-online/ocm-cli/pkg/debug"
)

// ConnectionBuilder contains the information and logic needed to build a connection to OCM. Don't
Expand Down Expand Up @@ -63,6 +65,16 @@ func (b *ConnectionBuilder) Build() (result *sdk.Connection, err error) {
return
}

// overwrite the config URL if the environment variable is set
if overrideUrl := os.Getenv("OCM_URL"); overrideUrl != "" {
if debug.Enabled() {
fmt.Fprintln(os.Stderr, "INFO: OCM_URL is overridden via environment variable. This functionality is considered tech preview and may cause unexpected issues.")
fmt.Fprintln(os.Stderr, " If you experience issues while OCM_URL is set, unset the OCM_URL environment variable and attempt to log in directly to the desired OCM environment.")
fmt.Fprintln(os.Stderr, "") // add a newline on purpose to separate from ocm sdk debug output
}
b.cfg.URL = overrideUrl
iamkirkbater marked this conversation as resolved.
Show resolved Hide resolved
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When run with the debug flag, output will end up looking like this:

> OCM_URL=https://api.regional.openshift.com ./ocm --debug get cluster $CLUSTER_ID
INFO: OCM_URL is overridden via environment variable. This functionality is considered tech preview and may cause unexpected issues.
      If you experience issues while OCM_URL is set, unset the OCM_URL environment variable and attempt to log in directly to the desired OCM environment.
      
I0604 10:23:47.595646   83670 connection.go:876] Added URL with prefix '', regular expression '^(/.*)?$' and URL 'https://api.regional.openshift.com'
...

result, err = b.cfg.Connection()
if err != nil {
return
Expand Down
Loading