Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/cli/cmd/workspace/common/objectformats.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

// WorkspaceFormat returns a FormatterOptions object which contains a list of columns to be used for displaying
// workspace information such as name, kind, kubecontext and environment."
// workspace information such as name, kind, kubecontext, group, and environment.
func WorkspaceFormat() output.FormatterOptions {
return output.FormatterOptions{
Columns: []output.Column{
Expand All @@ -38,6 +38,11 @@ func WorkspaceFormat() output.FormatterOptions {
Heading: "KUBECONTEXT",
JSONPath: "{ .Connection.context }",
},
{
Heading: "GROUP",
JSONPath: "{ .Scope }",
Transformer: &objectformats.ResourceScopeToResourceGroupNameTransformer{},
},
{
Heading: "ENVIRONMENT",
JSONPath: "{ .Environment }",
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/cmd/workspace/common/objectformats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ func Test_WorkspaceFormat(t *testing.T) {
"kind": workspaces.KindKubernetes,
"context": "test-context",
},
Scope: "/planes/radius/local/resourceGroups/test-group",
Environment: "/planes/radius/local/resourceGroups/test-group/providers/Applications.Core/environments/test",
}

buffer := &bytes.Buffer{}
err := output.Write(output.FormatTable, obj, buffer, WorkspaceFormat())
require.NoError(t, err)

expected := "WORKSPACE KIND KUBECONTEXT ENVIRONMENT\ntest kubernetes test-context test\n"
expected := "WORKSPACE KIND KUBECONTEXT GROUP ENVIRONMENT\ntest kubernetes test-context test-group test\n"
require.Equal(t, expected, buffer.String())
}
4 changes: 4 additions & 0 deletions pkg/cli/cmd/workspace/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ func Test_Run(t *testing.T) {
// Intentionally NOT in alphabetical order
"workspace-b": {
Environment: "b",
Scope: "/planes/radius/local/resourceGroups/group-b",
Source: workspaces.SourceUserConfig,
Connection: map[string]any{},
},
"workspace-a": {
Environment: "a",
Scope: "/planes/radius/local/resourceGroups/group-a",
Source: workspaces.SourceUserConfig,
Connection: map[string]any{},
},
Expand All @@ -104,12 +106,14 @@ func Test_Run(t *testing.T) {
{
Name: "workspace-a",
Environment: "a",
Scope: "/planes/radius/local/resourceGroups/group-a",
Source: workspaces.SourceUserConfig,
Connection: map[string]any{},
},
{
Name: "workspace-b",
Environment: "b",
Scope: "/planes/radius/local/resourceGroups/group-b",
Source: workspaces.SourceUserConfig,
Connection: map[string]any{},
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/cli/cmd/workspace/show/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func Test_Run(t *testing.T) {
Source: workspaces.SourceUserConfig,
Name: "test-workspace",
Environment: "test-environment",
Scope: "/planes/radius/local/resourceGroups/test-group",
Connection: map[string]any{},
},
}
Expand All @@ -107,6 +108,7 @@ func Test_Run(t *testing.T) {
Source: workspaces.SourceUserConfig,
Name: "test-workspace",
Environment: "test-environment",
Scope: "/planes/radius/local/resourceGroups/test-group",
Connection: map[string]any{},
},
Options: common.WorkspaceFormat(),
Expand Down
20 changes: 20 additions & 0 deletions pkg/cli/objectformats/transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ func (t *ResourceIDToResourceGroupNameTransformer) Transform(input string) strin
return id.FindScope(radius.ScopeResourceGroups)
}

// ResourceScopeToResourceGroupNameTransformer is a transformer that takes a scope ID and returns the resource group name.
type ResourceScopeToResourceGroupNameTransformer struct {
}

// Transform takes a scope ID and returns the resource group name.
func (t *ResourceScopeToResourceGroupNameTransformer) Transform(input string) string {
if input == "" {
return ""
}

// NOTE: this is for display to human users in a table. It's not a great place
// for us to put a long explanation.
id, err := resources.ParseScope(input)
if err != nil {
return "<error>"
}

return id.FindScope(radius.ScopeResourceGroups)
}

// ResourceIDToResourceNameTransformer is a transformer that takes a resource ID and returns the resource name.
type ResourceIDToResourceNameTransformer struct {
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/cli/objectformats/transformers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,35 @@ func Test_ResourceIDToResourceGroupNameTransformer(t *testing.T) {
})
}
}

func Test_ResourceScopeToResourceGroupNameTransformer(t *testing.T) {
cases := []struct {
name string
input string
expected string
}{
{
name: "empty input",
input: "",
expected: "",
},
{
name: "invalid input",
input: "////",
expected: "<error>",
},
{
name: "valid input",
input: "/planes/radius/local/resourceGroups/test-group",
expected: "test-group",
},
}

for _, testcase := range cases {
t.Run(testcase.name, func(t *testing.T) {
transformer := &ResourceScopeToResourceGroupNameTransformer{}
actual := transformer.Transform(testcase.input)
require.Equal(t, testcase.expected, actual)
})
}
}
Loading