Skip to content

Commit

Permalink
Add service and manager flags to backplane session
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Bargenquast committed Jul 8, 2023
1 parent 38d58b1 commit 158066c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
16 changes: 16 additions & 0 deletions cmd/ocm-backplane/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ func NewCmdSession() *cobra.Command {
return validEnvs, cobra.ShellCompDirectiveNoFileComp
},
}

// Add login cmd specific flags
sessionCmd.Flags().BoolVar(
&options.Manager,
"manager",
false,
"Login to management cluster instead of the cluster itself.",
)

sessionCmd.Flags().BoolVar(
&options.Service,
"service",
false,
"Login to service cluster for the given hosted cluster or mgmt cluster.",
)

sessionCmd.Flags().BoolVarP(
&options.DeleteSession,
"delete",
Expand Down
36 changes: 33 additions & 3 deletions pkg/cli/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"strings"

logger "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/openshift/backplane-cli/cmd/ocm-backplane/login"
Expand Down Expand Up @@ -41,6 +42,9 @@ type Options struct {

ClusterId string
ClusterName string

Manager bool
Service bool
}

var (
Expand Down Expand Up @@ -73,6 +77,32 @@ func (e *BackplaneSession) RunCommand(cmd *cobra.Command, args []string) error {
return fmt.Errorf("invalid cluster Id %s", clusterKey)
}

if e.Options.Manager {
logger.WithField("Cluster ID", clusterId).Debugln("Finding managing cluster")
clusterId, clusterName, err = utils.DefaultOCMInterface.GetManagingCluster(clusterId)
e.Options.Alias = clusterId
if err != nil {
return err
}

logger.WithFields(logger.Fields{
"ID": clusterId,
"Name": clusterName}).Infoln("Management cluster")
}

if e.Options.Service {
logger.WithField("Cluster ID", clusterId).Debugln("Finding service cluster")
clusterId, clusterName, err = utils.DefaultOCMInterface.GetServiceCluster(clusterId)
e.Options.Alias = clusterId
if err != nil {
return err
}

logger.WithFields(logger.Fields{
"ID": clusterId,
"Name": clusterName}).Infoln("Service cluster")
}

// set cluster options
e.Options.ClusterName = clusterName
e.Options.ClusterId = clusterId
Expand Down Expand Up @@ -373,17 +403,17 @@ func (e *BackplaneSession) initClusterLogin(cmd *cobra.Command) error {
// Setting up the flags
err := login.LoginCmd.Flags().Set("multi", "true")
if err != nil {
return fmt.Errorf("error occered when setting multi flag %v", err)
return fmt.Errorf("error occurred when setting multi flag %v", err)
}
err = login.LoginCmd.Flags().Set("kube-path", e.Path)
if err != nil {
return fmt.Errorf("error occered when kube-path flag %v", err)
return fmt.Errorf("error occurred when kube-path flag %v", err)
}

// Execute login command
err = login.LoginCmd.RunE(cmd, []string{e.Options.ClusterId})
if err != nil {
return fmt.Errorf("error occered when login to the cluster %v", err)
return fmt.Errorf("error occurred when login to the cluster %v", err)
}
}

Expand Down

0 comments on commit 158066c

Please sign in to comment.