Skip to content

Commit

Permalink
Add API for getting the TAE context active resource
Browse files Browse the repository at this point in the history
- Added runtime API GetTAEContextActiveResource() to get the TAE active resource

Signed-off-by: Prem Kumar Kalle <[email protected]>
  • Loading branch information
prkalle committed Oct 13, 2023
1 parent 69513e7 commit 1d59a55
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
Expand Down
37 changes: 37 additions & 0 deletions tae/tae.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ const (
customCommandName string = "_custom_command"
)

// ResourceInfo resource information of the application engine
type ResourceInfo struct {
// OrgID ID of the Org
OrgID string
// ProjectName name of the project
ProjectName string
// SpaceName name of the space
SpaceName string
}

// cmdOptions specifies the command options
type cmdOptions struct {
outWriter io.Writer
Expand Down Expand Up @@ -196,3 +206,30 @@ func SetTAEContextActiveResource(contextName, projectName, spaceName string, opt
}
return nil
}

// GetTAEContextActiveResource returns the application engine active resource information for the given context
func GetTAEContextActiveResource(contextName string) (*ResourceInfo, error) {
ctx, err := config.GetContext(contextName)
if err != nil {
return nil, err
}
if ctx.ContextType != configtypes.ContextTypeTAE {
return nil, errors.Errorf("context must be of type: %s", configtypes.ContextTypeTAE)
}
if ctx.AdditionalMetadata == nil {
return nil, errors.New("context is missing the TAE metadata")
}
activeResourceInfo := &ResourceInfo{
OrgID: stringValue(ctx.AdditionalMetadata[OrgIDKey]),
ProjectName: stringValue(ctx.AdditionalMetadata[ProjectNameKey]),
SpaceName: stringValue(ctx.AdditionalMetadata[SpaceNameKey]),
}
return activeResourceInfo, nil
}

func stringValue(val interface{}) string {
if val == nil {
return ""
}
return val.(string)
}
44 changes: 44 additions & 0 deletions tae/tae_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,50 @@ func TestGetKubeconfigForContext(t *testing.T) {
assert.ErrorContains(t, err, "context must be of type: application-engine")
}

func TestGetTAEContextActiveResource(t *testing.T) {
setupForGetContext(t)
defer cleanupTestingDir(t)

c, err := config.GetContext("test-tae")
assert.NoError(t, err)

// Test getting the TAE active resource of a non-existent context
_, err = GetTAEContextActiveResource("non-existent-context")
assert.Error(t, err)
assert.ErrorContains(t, err, "context non-existent-context not found")

// Test getting the TAE active resource of a context that is not tae context
_, err = GetTAEContextActiveResource("test-mc")
assert.Error(t, err)
assert.ErrorContains(t, err, "context must be of type: application-engine")

// Test getting the TAE active resource of a context with active resource as Org only
activeResources, err := GetTAEContextActiveResource("test-tae")
assert.NoError(t, err)
assert.Equal(t, activeResources.OrgID, "fake-org-id")
assert.Empty(t, activeResources.ProjectName)
assert.Empty(t, activeResources.SpaceName)

// Test getting the TAE active resource of a context with active resource as space
c.AdditionalMetadata[ProjectNameKey] = "fake-project"
c.AdditionalMetadata[SpaceNameKey] = "fake-space"
err = config.SetContext(c, false)
assert.NoError(t, err)
activeResources, err = GetTAEContextActiveResource("test-tae")
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")

// If context activeMetadata is not set(error case)
c.AdditionalMetadata = nil
err = config.SetContext(c, false)
assert.NoError(t, err)
_, err = GetTAEContextActiveResource("test-tae")
assert.Error(t, err)
assert.ErrorContains(t, err, "context is missing the TAE metadata")
}

func setupFakeCLI(dir string, exitStatus string, newCommandExitStatus string, enableCustomCommand bool) (string, error) {
filePath := filepath.Join(dir, "tanzu")

Expand Down

0 comments on commit 1d59a55

Please sign in to comment.