Skip to content

Commit

Permalink
Add GetContextsByType API
Browse files Browse the repository at this point in the history
This API return a lists of Contexts matching the provided context type

Signed-off-by: Vui Lam <[email protected]>
  • Loading branch information
vuil committed Oct 13, 2023
1 parent 2d49faf commit 69513e7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
21 changes: 21 additions & 0 deletions config/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,27 @@ func GetActiveContext(contextType configtypes.ContextType) (c *configtypes.Conte
return getActiveContext(node, contextType)
}

// GetContextsByType retrieves the contexts of a provided context type
func GetContextsByType(contextType configtypes.ContextType) ([]*configtypes.Context, error) {
var results []*configtypes.Context

node, err := getClientConfigNode()
if err != nil {
return nil, err
}
cfg, err := convertNodeToClientConfig(node)
if err != nil {
return nil, err
}

for _, ctx := range cfg.KnownContexts {
if ctx.ContextType == contextType {
results = append(results, ctx)
}
}
return results, nil
}

// GetAllCurrentContextsMap returns all current context per Target
//
// Deprecated: GetAllCurrentContextsMap is deprecated. Use GetAllActiveContextsMap instead
Expand Down
56 changes: 56 additions & 0 deletions config/contexts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,62 @@ func TestGetContext(t *testing.T) {
}
}

func TestGetContextsByType(t *testing.T) {
err := setupForGetContext()
assert.NoError(t, err)

defer func() {
cleanupDir(LocalDirName)
}()

tcs := []struct {
name string
contextType configtypes.ContextType
expectedNamesOfContexts []string
contextToDelete string
}{
{
name: "get k8s contexts",
expectedNamesOfContexts: []string{"test-mc", "test-mc-2"},
contextType: configtypes.ContextTypeK8s,
},
{
name: "get tmc contexts",
expectedNamesOfContexts: []string{"test-tmc"},
contextType: configtypes.ContextTypeTMC,
},
{
name: "get tae contexts",
expectedNamesOfContexts: []string{"test-tae"},
contextType: configtypes.ContextTypeTAE,
},
{
name: "get tae contexts",
contextToDelete: "test-tae",
expectedNamesOfContexts: []string(nil),
contextType: configtypes.ContextTypeTAE,
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
var ctxNames []string

if tc.contextToDelete != "" {
err := DeleteContext(tc.contextToDelete)
assert.Nil(t, err)
}
ctxs, err := GetContextsByType(tc.contextType)
assert.Nil(t, err)

for _, ctx := range ctxs {
ctxNames = append(ctxNames, ctx.Name)
}
assert.Equal(t, ctxNames, tc.expectedNamesOfContexts)
})
}
}

func TestContextExists(t *testing.T) {
err := setupForGetContext()
assert.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func SetContext(context Context, setCurrent bool) error
func DeleteContext(name string) error
func RemoveContext(name string) error
func ContextExists(name string) (bool, error)
func GetContextsByType(contextType ContextType) ([]*configtypes.Context, error)
func GetActiveContext(contextType ContextType) error
func GetAllActiveContextsMap() (map[configtypes.ContextType]*configtypes.Context, error)
func GetAllActiveContextsList() ([]string, error)
Expand Down

0 comments on commit 69513e7

Please sign in to comment.