diff --git a/go.work.sum b/go.work.sum index 3396347c2..cbe41c137 100644 --- a/go.work.sum +++ b/go.work.sum @@ -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= diff --git a/tae/tae.go b/tae/tae.go index 304fb0f58..6b3f27670 100644 --- a/tae/tae.go +++ b/tae/tae.go @@ -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 @@ -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) +} diff --git a/tae/tae_test.go b/tae/tae_test.go index 6e234d80e..02fab5768 100644 --- a/tae/tae_test.go +++ b/tae/tae_test.go @@ -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")