Skip to content

Commit

Permalink
Update the UX for "context use" and "context list" commands to show P…
Browse files Browse the repository at this point in the history
…rojectID details

Signed-off-by: Prem Kumar Kalle <prem.kalle@broadcom.com>
  • Loading branch information
prkalle committed Apr 2, 2024
1 parent 50befed commit 1f49693
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
31 changes: 25 additions & 6 deletions pkg/command/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"reflect"
"regexp"
libslices "slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -1151,7 +1152,7 @@ var useCtxCmd = &cobra.Command{
RunE: useCtx,
}

func useCtx(cmd *cobra.Command, args []string) error {
func useCtx(cmd *cobra.Command, args []string) error { //nolint:gocyclo
var ctx *configtypes.Context
var err error

Expand Down Expand Up @@ -1182,10 +1183,13 @@ func useCtx(cmd *cobra.Command, args []string) error {
return err
}

suffixString := ""
suffixString := fmt.Sprintf("Type: %s", ctx.ContextType)
if ctx.ContextType == configtypes.ContextTypeTanzu {
if project, exists := ctx.AdditionalMetadata[config.ProjectNameKey]; exists && project != "" {
suffixString += fmt.Sprintf("Project: %s", project)
suffixString += fmt.Sprintf(", Project: %s", project)
}
if projectID, exists := ctx.AdditionalMetadata[config.ProjectIDKey]; exists && projectID != "" {
suffixString += fmt.Sprintf(", ProjectID: %s", projectID)
}
if space, exists := ctx.AdditionalMetadata[config.SpaceNameKey]; exists && space != "" {
suffixString += fmt.Sprintf(", Space: %s", space)
Expand All @@ -1195,10 +1199,10 @@ func useCtx(cmd *cobra.Command, args []string) error {
}
}
if suffixString != "" {
suffixString = " (" + suffixString + ")"
suffixString = "(" + suffixString + ")"
}

log.Infof("Successfully activated context '%s' of type '%s'%s.", ctxName, ctx.ContextType, suffixString)
log.Infof("Activated context '%s' %s successfully ", ctxName, suffixString)

// Sync all required plugins
_ = syncContextPlugins(cmd, ctx.ContextType, ctxName, true)
Expand Down Expand Up @@ -1366,6 +1370,7 @@ type ContextListOutputRow struct {
IsActive string
Type string
Project string
ProjectID string
Space string
ClusterGroup string
Endpoint string
Expand All @@ -1388,6 +1393,7 @@ func displayContextListOutputWithDynamicColumns(cfg *configtypes.ClientConfig, w
path := NA
context := NA
project := NA
projectID := NA
space := NA
clustergroup := NA

Expand All @@ -1401,6 +1407,7 @@ func displayContextListOutputWithDynamicColumns(cfg *configtypes.ClientConfig, w
case configtypes.ContextTypeTanzu:
tanzuContextExists = true
project = ""
projectID = ""
space = ""
clustergroup = ""
ep = ""
Expand All @@ -1414,6 +1421,9 @@ func displayContextListOutputWithDynamicColumns(cfg *configtypes.ClientConfig, w
if ctx.AdditionalMetadata[config.ProjectNameKey] != nil {
project = ctx.AdditionalMetadata[config.ProjectNameKey].(string)
}
if ctx.AdditionalMetadata[config.ProjectIDKey] != nil {
projectID = ctx.AdditionalMetadata[config.ProjectIDKey].(string)
}
if ctx.AdditionalMetadata[config.SpaceNameKey] != nil {
space = ctx.AdditionalMetadata[config.SpaceNameKey].(string)
}
Expand All @@ -1427,7 +1437,7 @@ func displayContextListOutputWithDynamicColumns(cfg *configtypes.ClientConfig, w
context = ctx.ClusterOpts.Context
}
}
row := ContextListOutputRow{ctx.Name, strconv.FormatBool(isCurrent), string(ctx.ContextType), project, space, clustergroup, ep, path, context}
row := ContextListOutputRow{ctx.Name, strconv.FormatBool(isCurrent), string(ctx.ContextType), project, projectID, space, clustergroup, ep, path, context}
rows = append(rows, row)
}

Expand All @@ -1438,6 +1448,9 @@ func displayContextListOutputWithDynamicColumns(cfg *configtypes.ClientConfig, w
dynamicColumns = append(dynamicColumns, "ClusterGroup")
}
if showAllColumns {
if tanzuContextExists {
requiredColumns = addProjectIDColumn(requiredColumns)
}
requiredColumns = append(requiredColumns, "Endpoint", "KubeconfigPath", "KubeContext")
requiredColumns = append(requiredColumns, dynamicColumns...)
}
Expand All @@ -1449,6 +1462,12 @@ func displayContextListOutputWithDynamicColumns(cfg *configtypes.ClientConfig, w
}
}

func addProjectIDColumn(requiredColumns []string) []string {
// add "ProjectID" column after "Project"
projectIndex := libslices.Index(requiredColumns, "Project")
return libslices.Insert(requiredColumns, projectIndex+1, "ProjectID")
}

var getCtxTokenCmd = &cobra.Command{
Use: "get-token CONTEXT_NAME",
Short: "Get the valid CSP token for the given tanzu context",
Expand Down
2 changes: 1 addition & 1 deletion pkg/command/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ var _ = Describe("Test tanzu context command", func() {
columnsString := strings.Join(strings.Fields(lines[0]), " ")

Expect(err).To(BeNil())
Expect(columnsString).To(Equal("NAME ISACTIVE TYPE PROJECT SPACE CLUSTERGROUP ENDPOINT KUBECONFIGPATH KUBECONTEXT"))
Expect(columnsString).To(Equal("NAME ISACTIVE TYPE PROJECT PROJECTID SPACE CLUSTERGROUP ENDPOINT KUBECONFIGPATH KUBECONTEXT"))
})

It("should not return tanzu related columns when not listing tanzu contexts without --wide", func() {
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/plugin_sync/k8s/plugin_sync_k8s_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-sync-lifecycle]", func() {
})

const (
ContextActivated = "Successfully activated context '%s'"
ContextActivated = "Activated context '%s' (Type: %s) successfully"
PluginWillBeInstalled = "The following plugins will be installed for context '%s' of contextType '%s':"
PluginsTableHeaderRegExp = "NAME\\s+TARGET\\s+VERSION"
PluginsRow = "%s\\s+%s\\s+%s"
Expand All @@ -312,7 +312,7 @@ var _ = f.CLICoreDescribe("[Tests:E2E][Feature:Plugin-sync-lifecycle]", func() {
Expect(err).To(BeNil(), "use context should set context without any error")
pluginsList, err = tf.PluginCmd.ListPluginsForGivenContext(contextName, true)
Expect(err).To(BeNil(), "should not get any error for plugin list")
Expect(stdErr).To(ContainSubstring(fmt.Sprintf(ContextActivated, contextName)))
Expect(stdErr).To(ContainSubstring(fmt.Sprintf(ContextActivated, contextName, types.TargetK8s)))
Expect(stdErr).To(ContainSubstring(fmt.Sprintf(PluginWillBeInstalled, contextName, types.TargetK8s)))
Expect(stdErr).To(MatchRegexp(PluginsTableHeaderRegExp))
for i := range pluginsList {
Expand Down

0 comments on commit 1f49693

Please sign in to comment.