Skip to content

Commit 5e4c99b

Browse files
authored
Adds ability to change api url via env var (#621)
* Adds ability to change api url via env var Adds the ability to change the OCM API URL based on the environment variable `OCM_URL` allowing a user to change the OCM context without changing global state on their machine. * Migrates all cmds to use ConnectionBuilder These commands were missing the ConnectionBuilder abstraction and were still building the connection manually, so these were migrated so that they will work correctly. * Adds warning message to ocm login command Adds an explicit warning message in the login command that the OCM_URL environment variable is explicitly NOT used for login. * Adds tech preview warning to override url Allows override URL but when running with the debug flag this will inform users that this functionality is tech preview and may have issues while we build and test this. This should allow SRE to be able to use this feature immediately following the next release while setting the expectation with customers who may find this that it may still have some unexpected quirks, and to fall back to supported methodology for using this tool. * Removes note about overridden URL * refactors hardcoded env key to variable * fix lint
1 parent edd560c commit 5e4c99b

File tree

8 files changed

+38
-86
lines changed

8 files changed

+38
-86
lines changed

cmd/ocm/account/quota/cmd.go

+3-20
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323

2424
"github.com/spf13/cobra"
2525

26-
"github.com/openshift-online/ocm-cli/pkg/config"
2726
"github.com/openshift-online/ocm-cli/pkg/dump"
27+
"github.com/openshift-online/ocm-cli/pkg/ocm"
2828
amv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
2929
)
3030

@@ -60,29 +60,12 @@ func init() {
6060
}
6161

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

72-
// Check that the configuration has credentials or tokens that haven't have expired:
73-
armed, reason, err := cfg.Armed()
64+
// Create the client for the OCM API:
65+
connection, err := ocm.NewConnection().Build()
7466
if err != nil {
7567
return err
7668
}
77-
if !armed {
78-
return fmt.Errorf("Not logged in, %s, run the 'login' command", reason)
79-
}
80-
81-
// Create the connection, and remember to close it:
82-
connection, err := cfg.Connection()
83-
if err != nil {
84-
return fmt.Errorf("Can't create connection: %v", err)
85-
}
8669
defer connection.Close()
8770

8871
orgID := args.org

cmd/ocm/account/roles/cmd.go

+3-21
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222

2323
"github.com/spf13/cobra"
2424

25-
"github.com/openshift-online/ocm-cli/pkg/config"
2625
"github.com/openshift-online/ocm-cli/pkg/dump"
26+
"github.com/openshift-online/ocm-cli/pkg/ocm"
2727
amv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
2828
)
2929

@@ -57,29 +57,11 @@ func init() {
5757

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

60-
// Load the configuration file:
61-
cfg, err := config.Load()
62-
if err != nil {
63-
return fmt.Errorf("Can't load config file: %v", err)
64-
}
65-
if cfg == nil {
66-
return fmt.Errorf("Not logged in, run the 'login' command")
67-
}
68-
69-
// Check that the configuration has credentials or tokens that haven't have expired:
70-
armed, reason, err := cfg.Armed()
60+
// Create the client for the OCM API:
61+
connection, err := ocm.NewConnection().Build()
7162
if err != nil {
7263
return err
7364
}
74-
if !armed {
75-
return fmt.Errorf("Not logged in, %s, run the 'login' command", reason)
76-
}
77-
78-
// Create the connection, and remember to close it:
79-
connection, err := cfg.Connection()
80-
if err != nil {
81-
return fmt.Errorf("Can't create connection: %v", err)
82-
}
8365
defer connection.Close()
8466

8567
// No role name was provided; Print all roles.

cmd/ocm/account/status/cmd.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
acc_util "github.com/openshift-online/ocm-cli/pkg/account"
2525
"github.com/openshift-online/ocm-cli/pkg/config"
26+
"github.com/openshift-online/ocm-cli/pkg/ocm"
2627
amv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
2728
)
2829

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

64-
// Check that the configuration has credentials or tokens that haven't have expired:
65-
armed, reason, err := cfg.Armed()
65+
// Create the client for the OCM API:
66+
connection, err := ocm.NewConnection().Build()
6667
if err != nil {
6768
return err
6869
}
69-
if !armed {
70-
return fmt.Errorf("Not logged in, %s, run the 'login' command", reason)
71-
}
72-
73-
// Create the connection, and remember to close it:
74-
connection, err := cfg.Connection()
75-
if err != nil {
76-
return fmt.Errorf("Can't create connection: %v", err)
77-
}
7870
defer connection.Close()
7971

8072
// Send the request:
@@ -87,7 +79,7 @@ func run(cmd *cobra.Command, argv []string) error {
8779
// Display user and which server they are logged into
8880
currAccount := response.Body()
8981
currOrg := currAccount.Organization()
90-
fmt.Printf("User %s on %s in org '%s' %s (external_id: %s)",
82+
fmt.Printf("User %s on %s in org '%s' %s (external_id: %s) ",
9183
currAccount.Username(), cfg.URL, currOrg.Name(), currOrg.ID(), currOrg.ExternalID())
9284

9385
// Display roles currently assigned to the user

cmd/ocm/account/users/cmd.go

+3-21
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/spf13/cobra"
2525

2626
acc_util "github.com/openshift-online/ocm-cli/pkg/account"
27-
"github.com/openshift-online/ocm-cli/pkg/config"
27+
"github.com/openshift-online/ocm-cli/pkg/ocm"
2828
amv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
2929
)
3030

@@ -74,29 +74,11 @@ func init() {
7474

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

77-
// Load the configuration file:
78-
cfg, err := config.Load()
79-
if err != nil {
80-
return fmt.Errorf("Can't load config file: %v", err)
81-
}
82-
if cfg == nil {
83-
return fmt.Errorf("Not logged in, run the 'login' command")
84-
}
85-
86-
// Check that the configuration has credentials or tokens that haven't have expired:
87-
armed, reason, err := cfg.Armed()
77+
// Create the client for the OCM API:
78+
connection, err := ocm.NewConnection().Build()
8879
if err != nil {
8980
return err
9081
}
91-
if !armed {
92-
return fmt.Errorf("Not logged in, %s, run the 'login' command", reason)
93-
}
94-
95-
// Create the connection, and remember to close it:
96-
connection, err := cfg.Connection()
97-
if err != nil {
98-
return fmt.Errorf("Can't create connection: %v", err)
99-
}
10082
defer connection.Close()
10183

10284
// needed variables:

cmd/ocm/get/cmd.go

+4-11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/openshift-online/ocm-cli/pkg/arguments"
2626
"github.com/openshift-online/ocm-cli/pkg/config"
2727
"github.com/openshift-online/ocm-cli/pkg/dump"
28+
"github.com/openshift-online/ocm-cli/pkg/ocm"
2829
"github.com/openshift-online/ocm-cli/pkg/urls"
2930
)
3031

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

72-
// Check that the configuration has credentials or tokens that don't have expired:
73-
armed, reason, err := cfg.Armed()
73+
// Create the client for the OCM API:
74+
connection, err := ocm.NewConnection().Build()
7475
if err != nil {
7576
return err
7677
}
77-
if !armed {
78-
return fmt.Errorf("Not logged in, %s, run the 'login' command", reason)
79-
}
80-
81-
// Create the connection:
82-
connection, err := cfg.Connection()
83-
if err != nil {
84-
return fmt.Errorf("Can't create connection: %v", err)
85-
}
78+
defer connection.Close()
8679

8780
// Create and populate the request:
8881
request := connection.Get()

cmd/ocm/login/cmd.go

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"time"
2626

2727
"github.com/openshift-online/ocm-cli/pkg/config"
28+
"github.com/openshift-online/ocm-cli/pkg/properties"
2829
"github.com/openshift-online/ocm-cli/pkg/urls"
2930
sdk "github.com/openshift-online/ocm-sdk-go"
3031
"github.com/openshift-online/ocm-sdk-go/authentication"
@@ -313,6 +314,10 @@ func run(cmd *cobra.Command, argv []string) error {
313314
gatewayURL = fmt.Sprintf("https://%s", regValue.URL)
314315
}
315316

317+
if overrideUrl := os.Getenv(properties.URLEnvKey); overrideUrl != "" {
318+
fmt.Fprintf(os.Stderr, "WARNING: the `%s` 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", properties.URLEnvKey, gatewayURL) //nolint:lll
319+
}
320+
316321
// Update the configuration with the values given in the command line:
317322
cfg.TokenURL = tokenURL
318323
cfg.ClientID = clientID

pkg/ocm/connection.go

+12
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ package ocm
1515

1616
import (
1717
"fmt"
18+
"os"
1819

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

2122
"github.com/openshift-online/ocm-cli/pkg/config"
23+
"github.com/openshift-online/ocm-cli/pkg/debug"
24+
"github.com/openshift-online/ocm-cli/pkg/properties"
2225
)
2326

2427
// ConnectionBuilder contains the information and logic needed to build a connection to OCM. Don't
@@ -63,6 +66,15 @@ func (b *ConnectionBuilder) Build() (result *sdk.Connection, err error) {
6366
return
6467
}
6568

69+
// overwrite the config URL if the environment variable is set
70+
if overrideUrl := os.Getenv(properties.URLEnvKey); overrideUrl != "" {
71+
if debug.Enabled() {
72+
fmt.Fprintf(os.Stderr, "INFO: %s is overridden via environment variable. This functionality is considered tech preview and may cause unexpected issues.\n", properties.URLEnvKey) //nolint:lll
73+
fmt.Fprintf(os.Stderr, " If you experience issues while %s is set, unset the %s environment variable and attempt to log in directly to the desired OCM environment.\n\n", properties.URLEnvKey, properties.URLEnvKey) //nolint:lll
74+
}
75+
b.cfg.URL = overrideUrl
76+
}
77+
6678
result, err = b.cfg.Connection()
6779
if err != nil {
6880
return

pkg/properties/properties.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ limitations under the License.
1616

1717
package properties
1818

19-
const KeyringEnvKey = "OCM_KEYRING"
19+
const (
20+
KeyringEnvKey = "OCM_KEYRING"
21+
URLEnvKey = "OCM_URL"
22+
)

0 commit comments

Comments
 (0)