Skip to content

Commit

Permalink
inform user to login to OCM if the backplane failed due to that
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeng committed Jul 18, 2024
1 parent 272df9a commit d516bb6
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 25 deletions.
9 changes: 5 additions & 4 deletions cmd/ocm-backplane/accessrequest/createAccessRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (

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

ocmcli "github.com/openshift-online/ocm-cli/pkg/ocm"
"github.com/openshift/backplane-cli/pkg/login"
"github.com/openshift/backplane-cli/pkg/utils"
logger "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/openshift/backplane-cli/pkg/login"
"github.com/openshift/backplane-cli/pkg/ocm"
"github.com/openshift/backplane-cli/pkg/utils"
)

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

ocmConnection, err := ocmcli.NewConnection().Build()
ocmConnection, err := ocm.DefaultOCMInterface.SetupOCMConnection()
if err != nil {
return fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/ocm-backplane/accessrequest/expireAccessRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"

"github.com/openshift/backplane-cli/pkg/accessrequest"
"github.com/openshift/backplane-cli/pkg/ocm"

ocmcli "github.com/openshift-online/ocm-cli/pkg/ocm"
"github.com/spf13/cobra"
)

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

ocmConnection, err := ocmcli.NewConnection().Build()
ocmConnection, err := ocm.DefaultOCMInterface.SetupOCMConnection()
if err != nil {
return fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/ocm-backplane/accessrequest/getAccessRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"

"github.com/openshift/backplane-cli/pkg/accessrequest"
"github.com/openshift/backplane-cli/pkg/ocm"

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

ocmConnection, err := ocmcli.NewConnection().Build()
ocmConnection, err := ocm.DefaultOCMInterface.SetupOCMConnection()
if err != nil {
return fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/ocm-backplane/cloud/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"os"
"strconv"

ocmsdk "github.com/openshift-online/ocm-cli/pkg/ocm"

"github.com/openshift/backplane-cli/pkg/ocm"

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

// Initialize OCM connection
ocmConnection, err := ocmsdk.NewConnection().Build()
ocmConnection, err := ocm.DefaultOCMInterface.SetupOCMConnection()
if err != nil {
return fmt.Errorf("unable to build ocm sdk: %w", err)
return fmt.Errorf("failed to create OCM connection: %w", err)
}
defer ocmConnection.Close()

Expand Down
5 changes: 2 additions & 3 deletions cmd/ocm-backplane/cloud/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"

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

// Initialize OCM connection
ocmConnection, err := ocmsdk.NewConnection().Build()
ocmConnection, err := ocm.DefaultOCMInterface.SetupOCMConnection()
if err != nil {
return fmt.Errorf("unable to build ocm sdk: %w", err)
return fmt.Errorf("failed to create OCM connection: %w", err)
}
defer ocmConnection.Close()

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
github.com/trivago/tgo v1.0.7
golang.org/x/term v0.22.0
gopkg.in/AlecAivazis/survey.v1 v1.8.8
k8s.io/api v0.28.3
Expand Down Expand Up @@ -123,7 +124,6 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/trivago/tgo v1.0.7 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
github.com/zalando/go-keyring v0.2.3 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
Expand Down
15 changes: 15 additions & 0 deletions pkg/ocm/mocks/ocmWrapperMock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 27 additions & 9 deletions pkg/ocm/ocmWrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type OCMInterface interface {
GetClusterActiveAccessRequest(ocmConnection *ocmsdk.Connection, clusterID string) (*acctrspv1.AccessRequest, error)
CreateClusterAccessRequest(ocmConnection *ocmsdk.Connection, clusterID, reason, jiraIssueID, approvalDuration string) (*acctrspv1.AccessRequest, error)
CreateAccessRequestDecision(ocmConnection *ocmsdk.Connection, accessRequest *acctrspv1.AccessRequest, decision acctrspv1.DecisionDecision, justification string) (*acctrspv1.Decision, error)
SetupOCMConnection() (*ocmsdk.Connection, error)
}

const (
Expand All @@ -45,9 +46,26 @@ type DefaultOCMInterfaceImpl struct{}

var DefaultOCMInterface OCMInterface = &DefaultOCMInterfaceImpl{}

func (o *DefaultOCMInterfaceImpl) SetupOCMConnection() (*ocmsdk.Connection, error) {
ocmNotLoggedInMessage := "Not logged in"

// Setup connection at the first try
connection, err := ocm.NewConnection().Build()
if err != nil {
if strings.Contains(err.Error(), ocmNotLoggedInMessage) {
return nil, fmt.Errorf("please make sure you have logged into OCM, " +
"use \"ocm login --use-auth-code --url $ENV\" to login ")
} else {
return nil, err
}
}

return connection, nil
}

// IsClusterHibernating returns a boolean to indicate whether the cluster is hibernating
func (o *DefaultOCMInterfaceImpl) IsClusterHibernating(clusterID string) (bool, error) {
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return false, fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand All @@ -64,7 +82,7 @@ func (o *DefaultOCMInterfaceImpl) IsClusterHibernating(clusterID string) (bool,
// GetTargetCluster returns one single cluster based on the search key and survery.
func (o *DefaultOCMInterfaceImpl) GetTargetCluster(clusterKey string) (clusterID, clusterName string, err error) {
// Create the client for the OCM API:
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return "", "", fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand Down Expand Up @@ -96,7 +114,7 @@ func (o *DefaultOCMInterfaceImpl) GetTargetCluster(clusterKey string) (clusterID
// for the given clusterID
func (o *DefaultOCMInterfaceImpl) GetManagingCluster(targetClusterID string) (clusterID, clusterName string, isHostedControlPlane bool, err error) {
// Create the client for the OCM API:
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return "", "", false, fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand Down Expand Up @@ -153,7 +171,7 @@ func (o *DefaultOCMInterfaceImpl) GetManagingCluster(targetClusterID string) (cl
// GetServiceCluster gets the service cluster for a given hpyershift hosted cluster
func (o *DefaultOCMInterfaceImpl) GetServiceCluster(targetClusterID string) (clusterID, clusterName string, err error) {
// Create the client for the OCM API
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return "", "", fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand Down Expand Up @@ -207,7 +225,7 @@ func (o *DefaultOCMInterfaceImpl) GetServiceCluster(targetClusterID string) (clu
// GetOCMAccessToken initiates the OCM connection and returns the access token
func (o *DefaultOCMInterfaceImpl) GetOCMAccessToken() (*string, error) {
// Get ocm access token
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return nil, fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand Down Expand Up @@ -236,7 +254,7 @@ func (o *DefaultOCMInterfaceImpl) GetPullSecret() (string, error) {

// Get ocm access token
logger.Debugln("Finding ocm token")
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return "", fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand All @@ -256,7 +274,7 @@ func (o *DefaultOCMInterfaceImpl) GetPullSecret() (string, error) {
// for a given internal cluster id.
func (o *DefaultOCMInterfaceImpl) GetClusterInfoByID(clusterID string) (*cmv1.Cluster, error) {
// Create the client for the OCM API:
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return nil, fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand All @@ -282,7 +300,7 @@ func (o *DefaultOCMInterfaceImpl) GetClusterInfoByIDWithConn(ocmConnection *ocms
// IsProduction checks if OCM is currently in production env
func (o *DefaultOCMInterfaceImpl) IsProduction() (bool, error) {
// Create the client for the OCM API:
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return false, fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand All @@ -302,7 +320,7 @@ func (o *DefaultOCMInterfaceImpl) GetStsSupportJumpRoleARN(ocmConnection *ocmsdk
// GetBackplaneURL returns the Backplane API URL based on the OCM env
func (o *DefaultOCMInterfaceImpl) GetOCMEnvironment() (*cmv1.Environment, error) {
// Create the client for the OCM API
connection, err := ocm.NewConnection().Build()
connection, err := o.SetupOCMConnection()
if err != nil {
return nil, fmt.Errorf("failed to create OCM connection: %v", err)
}
Expand Down

0 comments on commit d516bb6

Please sign in to comment.