Skip to content

Commit 53c177a

Browse files
committed
inform user to login to OCM if the backplane failed due to that
1 parent 272df9a commit 53c177a

File tree

8 files changed

+76
-29
lines changed

8 files changed

+76
-29
lines changed

cmd/ocm-backplane/accessrequest/createAccessRequest.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import (
88

99
"github.com/openshift/backplane-cli/pkg/accessrequest"
1010

11-
ocmcli "github.com/openshift-online/ocm-cli/pkg/ocm"
12-
"github.com/openshift/backplane-cli/pkg/login"
13-
"github.com/openshift/backplane-cli/pkg/utils"
1411
logger "github.com/sirupsen/logrus"
1512
"github.com/spf13/cobra"
13+
14+
"github.com/openshift/backplane-cli/pkg/login"
15+
"github.com/openshift/backplane-cli/pkg/ocm"
16+
"github.com/openshift/backplane-cli/pkg/utils"
1617
)
1718

1819
var (
@@ -96,7 +97,7 @@ func runCreateAccessRequest(cmd *cobra.Command, args []string) error {
9697
return fmt.Errorf("failed to compute cluster ID: %v", err)
9798
}
9899

99-
ocmConnection, err := ocmcli.NewConnection().Build()
100+
ocmConnection, err := ocm.DefaultOCMInterface.SetupOCMConnection()
100101
if err != nil {
101102
return fmt.Errorf("failed to create OCM connection: %v", err)
102103
}

cmd/ocm-backplane/accessrequest/expireAccessRequest.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"fmt"
55

66
"github.com/openshift/backplane-cli/pkg/accessrequest"
7+
"github.com/openshift/backplane-cli/pkg/ocm"
78

8-
ocmcli "github.com/openshift-online/ocm-cli/pkg/ocm"
99
"github.com/spf13/cobra"
1010
)
1111

@@ -30,7 +30,7 @@ func runExpireAccessRequest(cmd *cobra.Command, args []string) error {
3030
return fmt.Errorf("failed to compute cluster ID: %v", err)
3131
}
3232

33-
ocmConnection, err := ocmcli.NewConnection().Build()
33+
ocmConnection, err := ocm.DefaultOCMInterface.SetupOCMConnection()
3434
if err != nil {
3535
return fmt.Errorf("failed to create OCM connection: %v", err)
3636
}

cmd/ocm-backplane/accessrequest/getAccessRequest.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"fmt"
55

66
"github.com/openshift/backplane-cli/pkg/accessrequest"
7+
"github.com/openshift/backplane-cli/pkg/ocm"
78

8-
ocmcli "github.com/openshift-online/ocm-cli/pkg/ocm"
99
logger "github.com/sirupsen/logrus"
1010
"github.com/spf13/cobra"
1111
)
@@ -31,7 +31,7 @@ func runGetAccessRequest(cmd *cobra.Command, args []string) error {
3131
return fmt.Errorf("failed to compute cluster ID: %v", err)
3232
}
3333

34-
ocmConnection, err := ocmcli.NewConnection().Build()
34+
ocmConnection, err := ocm.DefaultOCMInterface.SetupOCMConnection()
3535
if err != nil {
3636
return fmt.Errorf("failed to create OCM connection: %v", err)
3737
}

cmd/ocm-backplane/cloud/console.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"os"
77
"strconv"
88

9-
ocmsdk "github.com/openshift-online/ocm-cli/pkg/ocm"
10-
119
"github.com/openshift/backplane-cli/pkg/ocm"
1210

1311
"github.com/pkg/browser"
@@ -130,9 +128,9 @@ func runConsole(cmd *cobra.Command, argv []string) (err error) {
130128
logger.Infof("Using backplane URL: %s\n", backplaneConfiguration.URL)
131129

132130
// Initialize OCM connection
133-
ocmConnection, err := ocmsdk.NewConnection().Build()
131+
ocmConnection, err := ocm.DefaultOCMInterface.SetupOCMConnection()
134132
if err != nil {
135-
return fmt.Errorf("unable to build ocm sdk: %w", err)
133+
return fmt.Errorf("failed to create OCM connection: %w", err)
136134
}
137135
defer ocmConnection.Close()
138136

cmd/ocm-backplane/cloud/credentials.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66

7-
ocmsdk "github.com/openshift-online/ocm-cli/pkg/ocm"
87
logger "github.com/sirupsen/logrus"
98
"github.com/spf13/cobra"
109
"sigs.k8s.io/yaml"
@@ -98,9 +97,9 @@ func runCredentials(cmd *cobra.Command, argv []string) error {
9897
logger.Infof("Using backplane URL: %s\n", backplaneConfiguration.URL)
9998

10099
// Initialize OCM connection
101-
ocmConnection, err := ocmsdk.NewConnection().Build()
100+
ocmConnection, err := ocm.DefaultOCMInterface.SetupOCMConnection()
102101
if err != nil {
103-
return fmt.Errorf("unable to build ocm sdk: %w", err)
102+
return fmt.Errorf("failed to create OCM connection: %w", err)
104103
}
105104
defer ocmConnection.Close()
106105

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ require (
2525
github.com/spf13/cobra v1.8.1
2626
github.com/spf13/pflag v1.0.5
2727
github.com/spf13/viper v1.19.0
28+
github.com/trivago/tgo v1.0.7
2829
golang.org/x/term v0.22.0
2930
gopkg.in/AlecAivazis/survey.v1 v1.8.8
3031
k8s.io/api v0.28.3
@@ -123,7 +124,6 @@ require (
123124
github.com/spf13/afero v1.11.0 // indirect
124125
github.com/spf13/cast v1.6.0 // indirect
125126
github.com/subosito/gotenv v1.6.0 // indirect
126-
github.com/trivago/tgo v1.0.7 // indirect
127127
github.com/xlab/treeprint v1.2.0 // indirect
128128
github.com/zalando/go-keyring v0.2.3 // indirect
129129
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect

pkg/ocm/mocks/ocmWrapperMock.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/ocm/ocmWrapper.go

+47-13
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"errors"
55
"fmt"
66
"net/http"
7+
"os"
78
"strings"
89

9-
"github.com/openshift-online/ocm-cli/pkg/ocm"
10+
ocmocm "github.com/openshift-online/ocm-cli/pkg/ocm"
11+
ocmurls "github.com/openshift-online/ocm-cli/pkg/urls"
1012
ocmsdk "github.com/openshift-online/ocm-sdk-go"
1113
acctrspv1 "github.com/openshift-online/ocm-sdk-go/accesstransparency/v1"
1214
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
@@ -35,19 +37,51 @@ type OCMInterface interface {
3537
GetClusterActiveAccessRequest(ocmConnection *ocmsdk.Connection, clusterID string) (*acctrspv1.AccessRequest, error)
3638
CreateClusterAccessRequest(ocmConnection *ocmsdk.Connection, clusterID, reason, jiraIssueID, approvalDuration string) (*acctrspv1.AccessRequest, error)
3739
CreateAccessRequestDecision(ocmConnection *ocmsdk.Connection, accessRequest *acctrspv1.AccessRequest, decision acctrspv1.DecisionDecision, justification string) (*acctrspv1.Decision, error)
40+
SetupOCMConnection() (*ocmsdk.Connection, error)
3841
}
3942

4043
const (
41-
ClustersPageSize = 50
44+
ClustersPageSize = 50
45+
ocmNotLoggedInMessage = "Not logged in"
4246
)
4347

44-
type DefaultOCMInterfaceImpl struct{}
48+
type DefaultOCMInterfaceImpl struct {
49+
// connection *ocmsdk.Connection
50+
}
4551

4652
var DefaultOCMInterface OCMInterface = &DefaultOCMInterfaceImpl{}
4753

54+
// SetupOCMConnection setups the ocm connection for all the other ocm requests
55+
func (o *DefaultOCMInterfaceImpl) SetupOCMConnection() (*ocmsdk.Connection, error) {
56+
57+
envURL := os.Getenv("OCM_URL")
58+
if envURL != "" {
59+
// Fetch the real ocm url from the alias and set it back to the ENV
60+
ocmURL, err := ocmurls.ResolveGatewayURL(envURL, nil)
61+
if err != nil {
62+
return nil, err
63+
}
64+
os.Setenv("OCM_URL", ocmURL)
65+
logger.Debugf("reset the OCM_URL to %s", ocmURL)
66+
}
67+
68+
// Setup connection at the first try
69+
connection, err := ocmocm.NewConnection().Build()
70+
if err != nil {
71+
if strings.Contains(err.Error(), ocmNotLoggedInMessage) {
72+
return nil, fmt.Errorf("please ensure you are logged into OCM by using the command " +
73+
"\"ocm login --url $ENV\"")
74+
} else {
75+
return nil, err
76+
}
77+
}
78+
79+
return connection, nil
80+
}
81+
4882
// IsClusterHibernating returns a boolean to indicate whether the cluster is hibernating
4983
func (o *DefaultOCMInterfaceImpl) IsClusterHibernating(clusterID string) (bool, error) {
50-
connection, err := ocm.NewConnection().Build()
84+
connection, err := o.SetupOCMConnection()
5185
if err != nil {
5286
return false, fmt.Errorf("failed to create OCM connection: %v", err)
5387
}
@@ -64,7 +98,7 @@ func (o *DefaultOCMInterfaceImpl) IsClusterHibernating(clusterID string) (bool,
6498
// GetTargetCluster returns one single cluster based on the search key and survery.
6599
func (o *DefaultOCMInterfaceImpl) GetTargetCluster(clusterKey string) (clusterID, clusterName string, err error) {
66100
// Create the client for the OCM API:
67-
connection, err := ocm.NewConnection().Build()
101+
connection, err := o.SetupOCMConnection()
68102
if err != nil {
69103
return "", "", fmt.Errorf("failed to create OCM connection: %v", err)
70104
}
@@ -96,7 +130,7 @@ func (o *DefaultOCMInterfaceImpl) GetTargetCluster(clusterKey string) (clusterID
96130
// for the given clusterID
97131
func (o *DefaultOCMInterfaceImpl) GetManagingCluster(targetClusterID string) (clusterID, clusterName string, isHostedControlPlane bool, err error) {
98132
// Create the client for the OCM API:
99-
connection, err := ocm.NewConnection().Build()
133+
connection, err := o.SetupOCMConnection()
100134
if err != nil {
101135
return "", "", false, fmt.Errorf("failed to create OCM connection: %v", err)
102136
}
@@ -153,7 +187,7 @@ func (o *DefaultOCMInterfaceImpl) GetManagingCluster(targetClusterID string) (cl
153187
// GetServiceCluster gets the service cluster for a given hpyershift hosted cluster
154188
func (o *DefaultOCMInterfaceImpl) GetServiceCluster(targetClusterID string) (clusterID, clusterName string, err error) {
155189
// Create the client for the OCM API
156-
connection, err := ocm.NewConnection().Build()
190+
connection, err := o.SetupOCMConnection()
157191
if err != nil {
158192
return "", "", fmt.Errorf("failed to create OCM connection: %v", err)
159193
}
@@ -207,7 +241,7 @@ func (o *DefaultOCMInterfaceImpl) GetServiceCluster(targetClusterID string) (clu
207241
// GetOCMAccessToken initiates the OCM connection and returns the access token
208242
func (o *DefaultOCMInterfaceImpl) GetOCMAccessToken() (*string, error) {
209243
// Get ocm access token
210-
connection, err := ocm.NewConnection().Build()
244+
connection, err := o.SetupOCMConnection()
211245
if err != nil {
212246
return nil, fmt.Errorf("failed to create OCM connection: %v", err)
213247
}
@@ -236,7 +270,7 @@ func (o *DefaultOCMInterfaceImpl) GetPullSecret() (string, error) {
236270

237271
// Get ocm access token
238272
logger.Debugln("Finding ocm token")
239-
connection, err := ocm.NewConnection().Build()
273+
connection, err := o.SetupOCMConnection()
240274
if err != nil {
241275
return "", fmt.Errorf("failed to create OCM connection: %v", err)
242276
}
@@ -256,7 +290,7 @@ func (o *DefaultOCMInterfaceImpl) GetPullSecret() (string, error) {
256290
// for a given internal cluster id.
257291
func (o *DefaultOCMInterfaceImpl) GetClusterInfoByID(clusterID string) (*cmv1.Cluster, error) {
258292
// Create the client for the OCM API:
259-
connection, err := ocm.NewConnection().Build()
293+
connection, err := o.SetupOCMConnection()
260294
if err != nil {
261295
return nil, fmt.Errorf("failed to create OCM connection: %v", err)
262296
}
@@ -282,7 +316,7 @@ func (o *DefaultOCMInterfaceImpl) GetClusterInfoByIDWithConn(ocmConnection *ocms
282316
// IsProduction checks if OCM is currently in production env
283317
func (o *DefaultOCMInterfaceImpl) IsProduction() (bool, error) {
284318
// Create the client for the OCM API:
285-
connection, err := ocm.NewConnection().Build()
319+
connection, err := o.SetupOCMConnection()
286320
if err != nil {
287321
return false, fmt.Errorf("failed to create OCM connection: %v", err)
288322
}
@@ -299,10 +333,10 @@ func (o *DefaultOCMInterfaceImpl) GetStsSupportJumpRoleARN(ocmConnection *ocmsdk
299333
return response.Body().RoleArn(), nil
300334
}
301335

302-
// GetBackplaneURL returns the Backplane API URL based on the OCM env
336+
// GetOCMEnvironment returns the Backplane API URL based on the OCM env
303337
func (o *DefaultOCMInterfaceImpl) GetOCMEnvironment() (*cmv1.Environment, error) {
304338
// Create the client for the OCM API
305-
connection, err := ocm.NewConnection().Build()
339+
connection, err := o.SetupOCMConnection()
306340
if err != nil {
307341
return nil, fmt.Errorf("failed to create OCM connection: %v", err)
308342
}

0 commit comments

Comments
 (0)