From 74446e61a8d4748407587286112871b0d149a148 Mon Sep 17 00:00:00 2001 From: Prem Kumar Kalle Date: Mon, 3 Feb 2025 15:16:54 -0800 Subject: [PATCH] Show scope path of the tanzu resource for context type tanzu Signed-off-by: Prem Kumar Kalle --- pkg/command/context.go | 24 ++++++++++++++++++++++++ pkg/command/context_test.go | 4 ++++ 2 files changed, 28 insertions(+) diff --git a/pkg/command/context.go b/pkg/command/context.go index 86318ee36..82ce879e1 100644 --- a/pkg/command/context.go +++ b/pkg/command/context.go @@ -1497,6 +1497,9 @@ func printContext(writer io.Writer, ctx *configtypes.Context) { columns = append(columns, "Cluster Group") row = append(row, resources.ClusterGroupName) } + scopePath := getTanzuResourceScopePath(resources) + columns = append(columns, "Scope Path") + row = append(row, scopePath) } } @@ -1517,6 +1520,27 @@ func printContext(writer io.Writer, ctx *configtypes.Context) { outputWriter.Render() } +// getTanzuResourceScopePath returns the scope path based on the ResourceInfo +// TODO: Update this function when CLI context provides support for cluster/CF resources(CF Foundation/CF Org/CF Space) +func getTanzuResourceScopePath(resources *config.ResourceInfo) string { + scopePath := "" + if resources.OrgID == "" { + return scopePath + } + // set org scope path as global scope "/" and update the path based on resource hierarchy + scopePath += "/" + if resources.ProjectName != "" { + scopePath += "projects/" + resources.ProjectName + if resources.SpaceName != "" { + scopePath += "/spaces/" + resources.SpaceName + } else if resources.ClusterGroupName != "" { + scopePath += "/clustergroups/" + resources.ClusterGroupName + } + } + + return scopePath +} + func newDeleteCtxCmd() *cobra.Command { var deleteCtxCmd = &cobra.Command{ Use: "delete CONTEXT_NAME", diff --git a/pkg/command/context_test.go b/pkg/command/context_test.go index 58cc51cec..39ab8881b 100644 --- a/pkg/command/context_test.go +++ b/pkg/command/context_test.go @@ -1860,6 +1860,7 @@ func TestContextCurrentCmd(t *testing.T) { Type: tanzu Organization: org-name (org-id) Project: none set + Scope Path: / Kube Config: /home/user/.kube/config Kube Context: kube-context-name `, @@ -1877,6 +1878,7 @@ func TestContextCurrentCmd(t *testing.T) { Type: tanzu Organization: org-name (org-id) Project: project-name (project-id) + Scope Path: /projects/project-name Kube Config: /home/user/.kube/config Kube Context: kube-context-name `, @@ -1895,6 +1897,7 @@ func TestContextCurrentCmd(t *testing.T) { Organization: org-name (org-id) Project: project-name (project-id) Space: space-name + Scope Path: /projects/project-name/spaces/space-name Kube Config: /home/user/.kube/config Kube Context: kube-context-name `, @@ -1913,6 +1916,7 @@ func TestContextCurrentCmd(t *testing.T) { Organization: org-name (org-id) Project: project-name (project-id) Cluster Group: clustergroup-name + Scope Path: /projects/project-name/clustergroups/clustergroup-name Kube Config: /home/user/.kube/config Kube Context: kube-context-name `,