Skip to content

Commit

Permalink
Update SetTanzuContextActiveResource API to accept Project ID along w…
Browse files Browse the repository at this point in the history
…ith Project Name

Signed-off-by: Prem Kumar Kalle <[email protected]>
  • Loading branch information
prkalle authored and vuil committed Mar 28, 2024
1 parent eebb4d6 commit 1b2ce73
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
22 changes: 16 additions & 6 deletions config/tanzu_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
const (
OrgIDKey = "tanzuOrgID"
ProjectNameKey = "tanzuProjectName"
ProjectIDKey = "tanzuProjectID"
SpaceNameKey = "tanzuSpaceName"
ClusterGroupNameKey = "tanzuClusterGroupName"
)
Expand All @@ -39,6 +40,8 @@ type ResourceInfo struct {
OrgID string
// ProjectName name of the Project
ProjectName string
// ProjectID ID of the Project.
ProjectID string
// SpaceName name of the Space
SpaceName string
// ClusterGroupName name of the ClusterGroup
Expand Down Expand Up @@ -249,11 +252,11 @@ func updateKubeconfigServerURL(kc *kubeconfig.Config, cliContext *configtypes.Co
// Pre-reqs: project and space/clustergroup names should be valid
//
// Note: To set
// - a space as active resource, both project and space names are required
// - a clustergroup as active resource, both project and clustergroup names are required
// - a project as active resource, only project name is required (space should be empty string)
// - org as active resource, project, space and clustergroup names should be empty strings
func SetTanzuContextActiveResource(contextName string, resourceInfo ResourceInfo, opts ...CommandOptions) error {
// - a space as active resource, both project,projectID and space names are required
// - a clustergroup as active resource, both project,projectID and clustergroup names are required
// - a project as active resource, only project name and project ID are required (space should be empty string)
// - org as active resource, project name, project ID, space and clustergroup names should be empty strings
func SetTanzuContextActiveResource(contextName string, resourceInfo ResourceInfo, opts ...CommandOptions) error { //nolint:gocritic
// For now, the implementation expects env var TANZU_BIN to be set and
// pointing to the core CLI binary used to invoke setting the active Tanzu resource.

Expand All @@ -268,7 +271,13 @@ func SetTanzuContextActiveResource(contextName string, resourceInfo ResourceInfo
}

altCommandArgs := []string{customCommandName}
args := []string{"context", "update", "tanzu-active-resource", contextName, "--project", resourceInfo.ProjectName}
args := []string{"context", "update", "tanzu-active-resource", contextName}
if resourceInfo.ProjectName != "" {
args = append(args, "--project", resourceInfo.ProjectName)
}
if resourceInfo.ProjectID != "" {
args = append(args, "--project-id", resourceInfo.ProjectID)
}
if resourceInfo.SpaceName != "" {
args = append(args, "--space", resourceInfo.SpaceName)
}
Expand Down Expand Up @@ -308,6 +317,7 @@ func GetTanzuContextActiveResource(contextName string) (*ResourceInfo, error) {
activeResourceInfo := &ResourceInfo{
OrgID: stringValue(ctx.AdditionalMetadata[OrgIDKey]),
ProjectName: stringValue(ctx.AdditionalMetadata[ProjectNameKey]),
ProjectID: stringValue(ctx.AdditionalMetadata[ProjectIDKey]),
SpaceName: stringValue(ctx.AdditionalMetadata[SpaceNameKey]),
ClusterGroupName: stringValue(ctx.AdditionalMetadata[ClusterGroupNameKey]),
}
Expand Down
46 changes: 28 additions & 18 deletions config/tanzu_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ import (
)

const (
fakeOrgID = "fake-org-id"
fakeProjectName = "fake-project"
fakeProjectID = "fake-project-id"
fakeSpace = "fake-space"
fakeClusterGroupName = "fake-clustergroup"

fakePluginScriptFmtString string = `#!/bin/bash
# Fake tanzu core binary
Expand Down Expand Up @@ -188,7 +194,6 @@ func TestGetKubeconfigForContext(t *testing.T) {
func TestGetTanzuContextActiveResource(t *testing.T) {
err := setupForGetContext()
assert.NoError(t, err)

defer cleanupTestingDir(t)

c, err := GetContext("test-tanzu")
Expand All @@ -207,33 +212,38 @@ func TestGetTanzuContextActiveResource(t *testing.T) {
// Test getting the Tanzu active resource of a context with active resource as Org only
activeResources, err := GetTanzuContextActiveResource("test-tanzu")
assert.NoError(t, err)
assert.Equal(t, activeResources.OrgID, "fake-org-id")
assert.Equal(t, activeResources.OrgID, fakeOrgID)
assert.Empty(t, activeResources.ProjectName)
assert.Empty(t, activeResources.ProjectID)
assert.Empty(t, activeResources.SpaceName)

// Test getting the Tanzu active resource of a context with active resource as space
c.AdditionalMetadata[ProjectNameKey] = "fake-project" //nolint:goconst
c.AdditionalMetadata[SpaceNameKey] = "fake-space"
c.AdditionalMetadata[ProjectNameKey] = fakeProjectName
c.AdditionalMetadata[ProjectIDKey] = fakeProjectID
c.AdditionalMetadata[SpaceNameKey] = fakeSpace
err = SetContext(c, false)
assert.NoError(t, err)
activeResources, err = GetTanzuContextActiveResource("test-tanzu")
assert.NoError(t, err)
assert.Equal(t, activeResources.OrgID, "fake-org-id")
assert.Equal(t, activeResources.ProjectName, "fake-project")
assert.Equal(t, activeResources.SpaceName, "fake-space")
assert.Equal(t, activeResources.OrgID, fakeOrgID)
assert.Equal(t, activeResources.ProjectName, fakeProjectName)
assert.Equal(t, activeResources.ProjectID, fakeProjectID)
assert.Equal(t, activeResources.SpaceName, fakeSpace)
assert.Equal(t, activeResources.ClusterGroupName, "")

// Test getting the Tanzu active resource of a context with active resource as clustergroup
c.AdditionalMetadata[ProjectNameKey] = "fake-project"
c.AdditionalMetadata[ProjectNameKey] = fakeProjectName
c.AdditionalMetadata[ProjectIDKey] = fakeProjectID
c.AdditionalMetadata[SpaceNameKey] = ""
c.AdditionalMetadata[ClusterGroupNameKey] = "fake-clustergroup"
c.AdditionalMetadata[ClusterGroupNameKey] = fakeClusterGroupName
err = SetContext(c, false)
assert.NoError(t, err)
activeResources, err = GetTanzuContextActiveResource("test-tanzu")
assert.NoError(t, err)
assert.Equal(t, activeResources.OrgID, "fake-org-id")
assert.Equal(t, activeResources.ProjectName, "fake-project")
assert.Equal(t, activeResources.ClusterGroupName, "fake-clustergroup")
assert.Equal(t, activeResources.OrgID, fakeOrgID)
assert.Equal(t, activeResources.ProjectName, fakeProjectName)
assert.Equal(t, activeResources.ProjectID, fakeProjectID)
assert.Equal(t, activeResources.ClusterGroupName, fakeClusterGroupName)
assert.Equal(t, activeResources.SpaceName, "")

// If context activeMetadata is not set(error case)
Expand Down Expand Up @@ -278,26 +288,26 @@ func TestSetTanzuContextActiveResource(t *testing.T) {
{
test: "with no alternate command and Tanzu active resource update successfully",
exitStatus: "0",
expectedOutput: "context update tanzu-active-resource test-context --project projectA --space spaceA succeeded\n",
expectedOutput: "context update tanzu-active-resource test-context --project projectA --project-id projectA-ID --space spaceA succeeded\n",
expectedFailure: false,
},
{
test: "with no alternate command and Tanzu active resource update unsuccessfully",
exitStatus: "1",
expectedOutput: "context update tanzu-active-resource test-context --project projectA --space spaceA failed\n",
expectedOutput: "context update tanzu-active-resource test-context --project projectA --project-id projectA-ID --space spaceA failed\n",
expectedFailure: true,
},
{
test: "with alternate command and Tanzu active resource update successfully",
newCommandExitStatus: "0",
expectedOutput: "newcommand update tanzu-active-resource test-context --project projectA --space spaceA succeeded\n",
expectedOutput: "newcommand update tanzu-active-resource test-context --project projectA --project-id projectA-ID --space spaceA succeeded\n",
expectedFailure: false,
enableCustomCommand: true,
},
{
test: "with alternate command and Tanzu active resource update unsuccessfully",
newCommandExitStatus: "1",
expectedOutput: "newcommand update tanzu-active-resource test-context --project projectA --space spaceA failed\n",
expectedOutput: "newcommand update tanzu-active-resource test-context --project projectA --project-id projectA-ID --space spaceA failed\n",
expectedFailure: true,
enableCustomCommand: true,
},
Expand Down Expand Up @@ -332,7 +342,7 @@ func TestSetTanzuContextActiveResource(t *testing.T) {

// Test-1:
// - verify correct string gets printed to default stdout and stderr
err = SetTanzuContextActiveResource("test-context", ResourceInfo{ProjectName: "projectA", SpaceName: "spaceA"})
err = SetTanzuContextActiveResource("test-context", ResourceInfo{ProjectName: "projectA", ProjectID: "projectA-ID", SpaceName: "spaceA"})
w.Close()
stdoutRecieved := <-c

Expand All @@ -347,7 +357,7 @@ func TestSetTanzuContextActiveResource(t *testing.T) {
// Test-2: when external stdout and stderr are provided with WithStdout, WithStderr options,
// verify correct string gets printed to provided custom stdout/stderr
var combinedOutputBuff bytes.Buffer
err = SetTanzuContextActiveResource("test-context", ResourceInfo{ProjectName: "projectA", SpaceName: "spaceA"}, WithOutputWriter(&combinedOutputBuff), WithErrorWriter(&combinedOutputBuff))
err = SetTanzuContextActiveResource("test-context", ResourceInfo{ProjectName: "projectA", ProjectID: "projectA-ID", SpaceName: "spaceA"}, WithOutputWriter(&combinedOutputBuff), WithErrorWriter(&combinedOutputBuff))
if spec.expectedFailure {
assert.NotNil(err)
} else {
Expand Down

0 comments on commit 1b2ce73

Please sign in to comment.