From 69513e7a8eff706fa5e7bbf9e91e0ea7ae70761c Mon Sep 17 00:00:00 2001 From: Vui Lam Date: Thu, 12 Oct 2023 09:56:15 -0700 Subject: [PATCH] Add GetContextsByType API This API return a lists of Contexts matching the provided context type Signed-off-by: Vui Lam --- config/contexts.go | 21 ++++++++++++++++ config/contexts_test.go | 56 +++++++++++++++++++++++++++++++++++++++++ docs/config.md | 1 + 3 files changed, 78 insertions(+) diff --git a/config/contexts.go b/config/contexts.go index 9e0fc3cf8..e387e7c62 100644 --- a/config/contexts.go +++ b/config/contexts.go @@ -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 diff --git a/config/contexts_test.go b/config/contexts_test.go index 3fe2954b0..a915f323e 100644 --- a/config/contexts_test.go +++ b/config/contexts_test.go @@ -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) diff --git a/docs/config.md b/docs/config.md index 69d27611e..826c15d2b 100644 --- a/docs/config.md +++ b/docs/config.md @@ -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)