Merge pull request #1838 from simonferquel/remove-context-in-function-names

Remove "context" from context store interfaces function names
This commit is contained in:
Sebastiaan van Stijn 2019-04-18 18:38:01 +02:00 committed by GitHub
commit d365225c32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 176 additions and 176 deletions

View File

@ -291,7 +291,7 @@ func newAPIClientFromEndpoint(ep docker.Endpoint, configFile *configfile.ConfigF
} }
func resolveDockerEndpoint(s store.Reader, contextName string) (docker.Endpoint, error) { func resolveDockerEndpoint(s store.Reader, contextName string) (docker.Endpoint, error) {
ctxMeta, err := s.GetContextMetadata(contextName) ctxMeta, err := s.GetMetadata(contextName)
if err != nil { if err != nil {
return docker.Endpoint{}, err return docker.Endpoint{}, err
} }
@ -399,7 +399,7 @@ func (cli *DockerCli) CurrentContext() string {
// StackOrchestrator resolves which stack orchestrator is in use // StackOrchestrator resolves which stack orchestrator is in use
func (cli *DockerCli) StackOrchestrator(flagValue string) (Orchestrator, error) { func (cli *DockerCli) StackOrchestrator(flagValue string) (Orchestrator, error) {
currentContext := cli.CurrentContext() currentContext := cli.CurrentContext()
ctxRaw, err := cli.ContextStore().GetContextMetadata(currentContext) ctxRaw, err := cli.ContextStore().GetMetadata(currentContext)
if store.IsErrContextDoesNotExist(err) { if store.IsErrContextDoesNotExist(err) {
// case where the currentContext has been removed (CLI behavior is to fallback to using DOCKER_HOST based resolution) // case where the currentContext has been removed (CLI behavior is to fallback to using DOCKER_HOST based resolution)
return GetStackOrchestrator(flagValue, "", cli.ConfigFile().StackOrchestrator, cli.Err()) return GetStackOrchestrator(flagValue, "", cli.ConfigFile().StackOrchestrator, cli.Err())
@ -517,7 +517,7 @@ func resolveContextName(opts *cliflags.CommonOptions, config *configfile.ConfigF
return ctxName, nil return ctxName, nil
} }
if config != nil && config.CurrentContext != "" { if config != nil && config.CurrentContext != "" {
_, err := contextstore.GetContextMetadata(config.CurrentContext) _, err := contextstore.GetMetadata(config.CurrentContext)
if store.IsErrContextDoesNotExist(err) { if store.IsErrContextDoesNotExist(err) {
return "", errors.Errorf("Current context %q is not found on the file system, please check your config file at %s", config.CurrentContext, config.Filename) return "", errors.Errorf("Current context %q is not found on the file system, please check your config file at %s", config.CurrentContext, config.Filename)
} }

View File

@ -13,7 +13,7 @@ type DockerContext struct {
} }
// GetDockerContext extracts metadata from stored context metadata // GetDockerContext extracts metadata from stored context metadata
func GetDockerContext(storeMetadata store.ContextMetadata) (DockerContext, error) { func GetDockerContext(storeMetadata store.Metadata) (DockerContext, error) {
if storeMetadata.Metadata == nil { if storeMetadata.Metadata == nil {
// can happen if we save endpoints before assigning a context metadata // can happen if we save endpoints before assigning a context metadata
// it is totally valid, and we should return a default initialized value // it is totally valid, and we should return a default initialized value

View File

@ -121,10 +121,10 @@ func createNewContext(o *CreateOptions, stackOrchestrator command.Orchestrator,
if err := validateEndpointsAndOrchestrator(contextMetadata); err != nil { if err := validateEndpointsAndOrchestrator(contextMetadata); err != nil {
return err return err
} }
if err := s.CreateOrUpdateContext(contextMetadata); err != nil { if err := s.CreateOrUpdate(contextMetadata); err != nil {
return err return err
} }
if err := s.ResetContextTLSMaterial(o.Name, &contextTLSData); err != nil { if err := s.ResetTLSMaterial(o.Name, &contextTLSData); err != nil {
return err return err
} }
fmt.Fprintln(cli.Out(), o.Name) fmt.Fprintln(cli.Out(), o.Name)
@ -136,7 +136,7 @@ func checkContextNameForCreation(s store.Reader, name string) error {
if err := validateContextName(name); err != nil { if err := validateContextName(name); err != nil {
return err return err
} }
if _, err := s.GetContextMetadata(name); !store.IsErrContextDoesNotExist(err) { if _, err := s.GetMetadata(name); !store.IsErrContextDoesNotExist(err) {
if err != nil { if err != nil {
return errors.Wrap(err, "error while getting existing contexts") return errors.Wrap(err, "error while getting existing contexts")
} }
@ -164,8 +164,8 @@ type descriptionAndOrchestratorStoreDecorator struct {
orchestrator command.Orchestrator orchestrator command.Orchestrator
} }
func (d *descriptionAndOrchestratorStoreDecorator) GetContextMetadata(name string) (store.ContextMetadata, error) { func (d *descriptionAndOrchestratorStoreDecorator) GetMetadata(name string) (store.Metadata, error) {
c, err := d.Reader.GetContextMetadata(name) c, err := d.Reader.GetMetadata(name)
if err != nil { if err != nil {
return c, err return c, err
} }
@ -183,8 +183,8 @@ func (d *descriptionAndOrchestratorStoreDecorator) GetContextMetadata(name strin
return c, nil return c, nil
} }
func newContextMetadata(stackOrchestrator command.Orchestrator, o *CreateOptions) store.ContextMetadata { func newContextMetadata(stackOrchestrator command.Orchestrator, o *CreateOptions) store.Metadata {
return store.ContextMetadata{ return store.Metadata{
Endpoints: make(map[string]interface{}), Endpoints: make(map[string]interface{}),
Metadata: command.DockerContext{ Metadata: command.DockerContext{
Description: o.Description, Description: o.Description,

View File

@ -27,7 +27,7 @@ func makeFakeCli(t *testing.T, opts ...func(*test.FakeCli)) (*test.FakeCli, func
Store: store.New(dir, storeConfig), Store: store.New(dir, storeConfig),
Resolver: func() (*command.DefaultContext, error) { Resolver: func() (*command.DefaultContext, error) {
return &command.DefaultContext{ return &command.DefaultContext{
Meta: store.ContextMetadata{ Meta: store.Metadata{
Endpoints: map[string]interface{}{ Endpoints: map[string]interface{}{
docker.DockerEndpoint: docker.EndpointMeta{ docker.DockerEndpoint: docker.EndpointMeta{
Host: "unix:///var/run/docker.sock", Host: "unix:///var/run/docker.sock",
@ -63,7 +63,7 @@ func withCliConfig(configFile *configfile.ConfigFile) func(*test.FakeCli) {
func TestCreateInvalids(t *testing.T) { func TestCreateInvalids(t *testing.T) {
cli, cleanup := makeFakeCli(t) cli, cleanup := makeFakeCli(t)
defer cleanup() defer cleanup()
assert.NilError(t, cli.ContextStore().CreateOrUpdateContext(store.ContextMetadata{Name: "existing-context"})) assert.NilError(t, cli.ContextStore().CreateOrUpdate(store.Metadata{Name: "existing-context"}))
tests := []struct { tests := []struct {
options CreateOptions options CreateOptions
expecterErr string expecterErr string
@ -158,7 +158,7 @@ func TestCreateOrchestratorEmpty(t *testing.T) {
func validateTestKubeEndpoint(t *testing.T, s store.Reader, name string) { func validateTestKubeEndpoint(t *testing.T, s store.Reader, name string) {
t.Helper() t.Helper()
ctxMetadata, err := s.GetContextMetadata(name) ctxMetadata, err := s.GetMetadata(name)
assert.NilError(t, err) assert.NilError(t, err)
kubeMeta := ctxMetadata.Endpoints[kubernetes.KubernetesEndpoint].(kubernetes.EndpointMeta) kubeMeta := ctxMetadata.Endpoints[kubernetes.KubernetesEndpoint].(kubernetes.EndpointMeta)
kubeEP, err := kubeMeta.WithTLSData(s, name) kubeEP, err := kubeMeta.WithTLSData(s, name)
@ -261,7 +261,7 @@ func TestCreateFromContext(t *testing.T) {
Kubernetes: c.kubernetes, Kubernetes: c.kubernetes,
}) })
assert.NilError(t, err) assert.NilError(t, err)
newContext, err := cli.ContextStore().GetContextMetadata(c.name) newContext, err := cli.ContextStore().GetMetadata(c.name)
assert.NilError(t, err) assert.NilError(t, err)
newContextTyped, err := command.GetDockerContext(newContext) newContextTyped, err := command.GetDockerContext(newContext)
assert.NilError(t, err) assert.NilError(t, err)
@ -330,7 +330,7 @@ func TestCreateFromCurrent(t *testing.T) {
DefaultStackOrchestrator: c.orchestrator, DefaultStackOrchestrator: c.orchestrator,
}) })
assert.NilError(t, err) assert.NilError(t, err)
newContext, err := cli.ContextStore().GetContextMetadata(c.name) newContext, err := cli.ContextStore().GetMetadata(c.name)
assert.NilError(t, err) assert.NilError(t, err)
newContextTyped, err := command.GetDockerContext(newContext) newContextTyped, err := command.GetDockerContext(newContext)
assert.NilError(t, err) assert.NilError(t, err)

View File

@ -29,9 +29,9 @@ func TestExportImportWithFile(t *testing.T) {
cli.OutBuffer().Reset() cli.OutBuffer().Reset()
cli.ErrBuffer().Reset() cli.ErrBuffer().Reset()
assert.NilError(t, RunImport(cli, "test2", contextFile)) assert.NilError(t, RunImport(cli, "test2", contextFile))
context1, err := cli.ContextStore().GetContextMetadata("test") context1, err := cli.ContextStore().GetMetadata("test")
assert.NilError(t, err) assert.NilError(t, err)
context2, err := cli.ContextStore().GetContextMetadata("test2") context2, err := cli.ContextStore().GetMetadata("test2")
assert.NilError(t, err) assert.NilError(t, err)
assert.DeepEqual(t, context1.Endpoints, context2.Endpoints) assert.DeepEqual(t, context1.Endpoints, context2.Endpoints)
assert.DeepEqual(t, context1.Metadata, context2.Metadata) assert.DeepEqual(t, context1.Metadata, context2.Metadata)
@ -57,9 +57,9 @@ func TestExportImportPipe(t *testing.T) {
cli.OutBuffer().Reset() cli.OutBuffer().Reset()
cli.ErrBuffer().Reset() cli.ErrBuffer().Reset()
assert.NilError(t, RunImport(cli, "test2", "-")) assert.NilError(t, RunImport(cli, "test2", "-"))
context1, err := cli.ContextStore().GetContextMetadata("test") context1, err := cli.ContextStore().GetMetadata("test")
assert.NilError(t, err) assert.NilError(t, err)
context2, err := cli.ContextStore().GetContextMetadata("test2") context2, err := cli.ContextStore().GetMetadata("test2")
assert.NilError(t, err) assert.NilError(t, err)
assert.DeepEqual(t, context1.Endpoints, context2.Endpoints) assert.DeepEqual(t, context1.Endpoints, context2.Endpoints)
assert.DeepEqual(t, context1.Metadata, context2.Metadata) assert.DeepEqual(t, context1.Metadata, context2.Metadata)

View File

@ -80,7 +80,7 @@ func RunExport(dockerCli command.Cli, opts *ExportOptions) error {
if err := validateContextName(opts.ContextName); err != nil && opts.ContextName != command.DefaultContextName { if err := validateContextName(opts.ContextName); err != nil && opts.ContextName != command.DefaultContextName {
return err return err
} }
ctxMeta, err := dockerCli.ContextStore().GetContextMetadata(opts.ContextName) ctxMeta, err := dockerCli.ContextStore().GetMetadata(opts.ContextName)
if err != nil { if err != nil {
return err return err
} }

View File

@ -40,25 +40,25 @@ func newInspectCommand(dockerCli command.Cli) *cobra.Command {
func runInspect(dockerCli command.Cli, opts inspectOptions) error { func runInspect(dockerCli command.Cli, opts inspectOptions) error {
getRefFunc := func(ref string) (interface{}, []byte, error) { getRefFunc := func(ref string) (interface{}, []byte, error) {
c, err := dockerCli.ContextStore().GetContextMetadata(ref) c, err := dockerCli.ContextStore().GetMetadata(ref)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
tlsListing, err := dockerCli.ContextStore().ListContextTLSFiles(ref) tlsListing, err := dockerCli.ContextStore().ListTLSFiles(ref)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
return contextWithTLSListing{ return contextWithTLSListing{
ContextMetadata: c, Metadata: c,
TLSMaterial: tlsListing, TLSMaterial: tlsListing,
Storage: dockerCli.ContextStore().GetContextStorageInfo(ref), Storage: dockerCli.ContextStore().GetStorageInfo(ref),
}, nil, nil }, nil, nil
} }
return inspect.Inspect(dockerCli.Out(), opts.refs, opts.format, getRefFunc) return inspect.Inspect(dockerCli.Out(), opts.refs, opts.format, getRefFunc)
} }
type contextWithTLSListing struct { type contextWithTLSListing struct {
store.ContextMetadata store.Metadata
TLSMaterial map[string]store.EndpointFiles TLSMaterial map[string]store.EndpointFiles
Storage store.ContextStorageInfo Storage store.StorageInfo
} }

View File

@ -17,7 +17,7 @@ func TestInspect(t *testing.T) {
refs: []string{"current"}, refs: []string{"current"},
})) }))
expected := string(golden.Get(t, "inspect.golden")) expected := string(golden.Get(t, "inspect.golden"))
si := cli.ContextStore().GetContextStorageInfo("current") si := cli.ContextStore().GetStorageInfo("current")
expected = strings.Replace(expected, "<METADATA_PATH>", strings.Replace(si.MetadataPath, `\`, `\\`, -1), 1) expected = strings.Replace(expected, "<METADATA_PATH>", strings.Replace(si.MetadataPath, `\`, `\\`, -1), 1)
expected = strings.Replace(expected, "<TLS_PATH>", strings.Replace(si.TLSPath, `\`, `\\`, -1), 1) expected = strings.Replace(expected, "<TLS_PATH>", strings.Replace(si.TLSPath, `\`, `\\`, -1), 1)
assert.Equal(t, cli.OutBuffer().String(), expected) assert.Equal(t, cli.OutBuffer().String(), expected)

View File

@ -42,7 +42,7 @@ func runList(dockerCli command.Cli, opts *listOptions) error {
opts.format = formatter.TableFormatKey opts.format = formatter.TableFormatKey
} }
curContext := dockerCli.CurrentContext() curContext := dockerCli.CurrentContext()
contextMap, err := dockerCli.ContextStore().ListContexts() contextMap, err := dockerCli.ContextStore().List()
if err != nil { if err != nil {
return err return err
} }

View File

@ -122,7 +122,7 @@ func getDockerEndpoint(dockerCli command.Cli, config map[string]string) (docker.
return docker.Endpoint{}, err return docker.Endpoint{}, err
} }
if contextName, ok := config[keyFrom]; ok { if contextName, ok := config[keyFrom]; ok {
metadata, err := dockerCli.ContextStore().GetContextMetadata(contextName) metadata, err := dockerCli.ContextStore().GetMetadata(contextName)
if err != nil { if err != nil {
return docker.Endpoint{}, err return docker.Endpoint{}, err
} }
@ -173,7 +173,7 @@ func getKubernetesEndpoint(dockerCli command.Cli, config map[string]string) (*ku
return nil, nil return nil, nil
} }
if contextName, ok := config[keyFrom]; ok { if contextName, ok := config[keyFrom]; ok {
ctxMeta, err := dockerCli.ContextStore().GetContextMetadata(contextName) ctxMeta, err := dockerCli.ContextStore().GetMetadata(contextName)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -50,7 +50,7 @@ func RunRemove(dockerCli command.Cli, opts RemoveOptions, names []string) error
} }
func doRemove(dockerCli command.Cli, name string, isCurrent, force bool) error { func doRemove(dockerCli command.Cli, name string, isCurrent, force bool) error {
if _, err := dockerCli.ContextStore().GetContextMetadata(name); err != nil { if _, err := dockerCli.ContextStore().GetMetadata(name); err != nil {
return err return err
} }
if isCurrent { if isCurrent {
@ -64,5 +64,5 @@ func doRemove(dockerCli command.Cli, name string, isCurrent, force bool) error {
return err return err
} }
} }
return dockerCli.ContextStore().RemoveContext(name) return dockerCli.ContextStore().Remove(name)
} }

View File

@ -18,9 +18,9 @@ func TestRemove(t *testing.T) {
createTestContextWithKubeAndSwarm(t, cli, "current", "all") createTestContextWithKubeAndSwarm(t, cli, "current", "all")
createTestContextWithKubeAndSwarm(t, cli, "other", "all") createTestContextWithKubeAndSwarm(t, cli, "other", "all")
assert.NilError(t, RunRemove(cli, RemoveOptions{}, []string{"other"})) assert.NilError(t, RunRemove(cli, RemoveOptions{}, []string{"other"}))
_, err := cli.ContextStore().GetContextMetadata("current") _, err := cli.ContextStore().GetMetadata("current")
assert.NilError(t, err) assert.NilError(t, err)
_, err = cli.ContextStore().GetContextMetadata("other") _, err = cli.ContextStore().GetMetadata("other")
assert.Check(t, store.IsErrContextDoesNotExist(err)) assert.Check(t, store.IsErrContextDoesNotExist(err))
} }

View File

@ -72,7 +72,7 @@ func RunUpdate(cli command.Cli, o *UpdateOptions) error {
return err return err
} }
s := cli.ContextStore() s := cli.ContextStore()
c, err := s.GetContextMetadata(o.Name) c, err := s.GetMetadata(o.Name)
if err != nil { if err != nil {
return err return err
} }
@ -118,11 +118,11 @@ func RunUpdate(cli command.Cli, o *UpdateOptions) error {
if err := validateEndpointsAndOrchestrator(c); err != nil { if err := validateEndpointsAndOrchestrator(c); err != nil {
return err return err
} }
if err := s.CreateOrUpdateContext(c); err != nil { if err := s.CreateOrUpdate(c); err != nil {
return err return err
} }
for ep, tlsData := range tlsDataToReset { for ep, tlsData := range tlsDataToReset {
if err := s.ResetContextEndpointTLSMaterial(o.Name, ep, tlsData); err != nil { if err := s.ResetEndpointTLSMaterial(o.Name, ep, tlsData); err != nil {
return err return err
} }
} }
@ -132,7 +132,7 @@ func RunUpdate(cli command.Cli, o *UpdateOptions) error {
return nil return nil
} }
func validateEndpointsAndOrchestrator(c store.ContextMetadata) error { func validateEndpointsAndOrchestrator(c store.Metadata) error {
dockerContext, err := command.GetDockerContext(c) dockerContext, err := command.GetDockerContext(c)
if err != nil { if err != nil {
return err return err

View File

@ -25,7 +25,7 @@ func TestUpdateDescriptionOnly(t *testing.T) {
Name: "test", Name: "test",
Description: "description", Description: "description",
})) }))
c, err := cli.ContextStore().GetContextMetadata("test") c, err := cli.ContextStore().GetMetadata("test")
assert.NilError(t, err) assert.NilError(t, err)
dc, err := command.GetDockerContext(c) dc, err := command.GetDockerContext(c)
assert.NilError(t, err) assert.NilError(t, err)
@ -46,7 +46,7 @@ func TestUpdateDockerOnly(t *testing.T) {
keyHost: "tcp://some-host", keyHost: "tcp://some-host",
}, },
})) }))
c, err := cli.ContextStore().GetContextMetadata("test") c, err := cli.ContextStore().GetMetadata("test")
assert.NilError(t, err) assert.NilError(t, err)
dc, err := command.GetDockerContext(c) dc, err := command.GetDockerContext(c)
assert.NilError(t, err) assert.NilError(t, err)

View File

@ -26,7 +26,7 @@ func RunUse(dockerCli command.Cli, name string) error {
if err := validateContextName(name); err != nil && name != "default" { if err := validateContextName(name); err != nil && name != "default" {
return err return err
} }
if _, err := dockerCli.ContextStore().GetContextMetadata(name); err != nil && name != "default" { if _, err := dockerCli.ContextStore().GetMetadata(name); err != nil && name != "default" {
return err return err
} }
configValue := name configValue := name

View File

@ -22,7 +22,7 @@ const (
// DefaultContext contains the default context data for all enpoints // DefaultContext contains the default context data for all enpoints
type DefaultContext struct { type DefaultContext struct {
Meta store.ContextMetadata Meta store.Metadata
TLS store.ContextTLSData TLS store.ContextTLSData
} }
@ -35,7 +35,7 @@ type ContextStoreWithDefault struct {
Resolver DefaultContextResolver Resolver DefaultContextResolver
} }
// resolveDefaultContext creates a ContextMetadata for the current CLI invocation parameters // resolveDefaultContext creates a Metadata for the current CLI invocation parameters
func resolveDefaultContext(opts *cliflags.CommonOptions, config *configfile.ConfigFile, stderr io.Writer) (*DefaultContext, error) { func resolveDefaultContext(opts *cliflags.CommonOptions, config *configfile.ConfigFile, stderr io.Writer) (*DefaultContext, error) {
stackOrchestrator, err := GetStackOrchestrator("", "", config.StackOrchestrator, stderr) stackOrchestrator, err := GetStackOrchestrator("", "", config.StackOrchestrator, stderr)
if err != nil { if err != nil {
@ -44,7 +44,7 @@ func resolveDefaultContext(opts *cliflags.CommonOptions, config *configfile.Conf
contextTLSData := store.ContextTLSData{ contextTLSData := store.ContextTLSData{
Endpoints: make(map[string]store.EndpointTLSData), Endpoints: make(map[string]store.EndpointTLSData),
} }
contextMetadata := store.ContextMetadata{ contextMetadata := store.Metadata{
Endpoints: make(map[string]interface{}), Endpoints: make(map[string]interface{}),
Metadata: DockerContext{ Metadata: DockerContext{
Description: "", Description: "",
@ -81,9 +81,9 @@ func resolveDefaultContext(opts *cliflags.CommonOptions, config *configfile.Conf
return &DefaultContext{Meta: contextMetadata, TLS: contextTLSData}, nil return &DefaultContext{Meta: contextMetadata, TLS: contextTLSData}, nil
} }
// ListContexts implements store.Store's ListContexts // List implements store.Store's List
func (s *ContextStoreWithDefault) ListContexts() ([]store.ContextMetadata, error) { func (s *ContextStoreWithDefault) List() ([]store.Metadata, error) {
contextList, err := s.Store.ListContexts() contextList, err := s.Store.List()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -94,52 +94,52 @@ func (s *ContextStoreWithDefault) ListContexts() ([]store.ContextMetadata, error
return append(contextList, defaultContext.Meta), nil return append(contextList, defaultContext.Meta), nil
} }
// CreateOrUpdateContext is not allowed for the default context and fails // CreateOrUpdate is not allowed for the default context and fails
func (s *ContextStoreWithDefault) CreateOrUpdateContext(meta store.ContextMetadata) error { func (s *ContextStoreWithDefault) CreateOrUpdate(meta store.Metadata) error {
if meta.Name == DefaultContextName { if meta.Name == DefaultContextName {
return errors.New("default context cannot be created nor updated") return errors.New("default context cannot be created nor updated")
} }
return s.Store.CreateOrUpdateContext(meta) return s.Store.CreateOrUpdate(meta)
} }
// RemoveContext is not allowed for the default context and fails // Remove is not allowed for the default context and fails
func (s *ContextStoreWithDefault) RemoveContext(name string) error { func (s *ContextStoreWithDefault) Remove(name string) error {
if name == DefaultContextName { if name == DefaultContextName {
return errors.New("default context cannot be removed") return errors.New("default context cannot be removed")
} }
return s.Store.RemoveContext(name) return s.Store.Remove(name)
} }
// GetContextMetadata implements store.Store's GetContextMetadata // GetMetadata implements store.Store's GetMetadata
func (s *ContextStoreWithDefault) GetContextMetadata(name string) (store.ContextMetadata, error) { func (s *ContextStoreWithDefault) GetMetadata(name string) (store.Metadata, error) {
if name == DefaultContextName { if name == DefaultContextName {
defaultContext, err := s.Resolver() defaultContext, err := s.Resolver()
if err != nil { if err != nil {
return store.ContextMetadata{}, err return store.Metadata{}, err
} }
return defaultContext.Meta, nil return defaultContext.Meta, nil
} }
return s.Store.GetContextMetadata(name) return s.Store.GetMetadata(name)
} }
// ResetContextTLSMaterial is not implemented for default context and fails // ResetTLSMaterial is not implemented for default context and fails
func (s *ContextStoreWithDefault) ResetContextTLSMaterial(name string, data *store.ContextTLSData) error { func (s *ContextStoreWithDefault) ResetTLSMaterial(name string, data *store.ContextTLSData) error {
if name == DefaultContextName { if name == DefaultContextName {
return errors.New("The default context store does not support ResetContextTLSMaterial") return errors.New("The default context store does not support ResetTLSMaterial")
} }
return s.Store.ResetContextTLSMaterial(name, data) return s.Store.ResetTLSMaterial(name, data)
} }
// ResetContextEndpointTLSMaterial is not implemented for default context and fails // ResetEndpointTLSMaterial is not implemented for default context and fails
func (s *ContextStoreWithDefault) ResetContextEndpointTLSMaterial(contextName string, endpointName string, data *store.EndpointTLSData) error { func (s *ContextStoreWithDefault) ResetEndpointTLSMaterial(contextName string, endpointName string, data *store.EndpointTLSData) error {
if contextName == DefaultContextName { if contextName == DefaultContextName {
return errors.New("The default context store does not support ResetContextEndpointTLSMaterial") return errors.New("The default context store does not support ResetEndpointTLSMaterial")
} }
return s.Store.ResetContextEndpointTLSMaterial(contextName, endpointName, data) return s.Store.ResetEndpointTLSMaterial(contextName, endpointName, data)
} }
// ListContextTLSFiles implements store.Store's ListContextTLSFiles // ListTLSFiles implements store.Store's ListTLSFiles
func (s *ContextStoreWithDefault) ListContextTLSFiles(name string) (map[string]store.EndpointFiles, error) { func (s *ContextStoreWithDefault) ListTLSFiles(name string) (map[string]store.EndpointFiles, error) {
if name == DefaultContextName { if name == DefaultContextName {
defaultContext, err := s.Resolver() defaultContext, err := s.Resolver()
if err != nil { if err != nil {
@ -155,11 +155,11 @@ func (s *ContextStoreWithDefault) ListContextTLSFiles(name string) (map[string]s
} }
return tlsfiles, nil return tlsfiles, nil
} }
return s.Store.ListContextTLSFiles(name) return s.Store.ListTLSFiles(name)
} }
// GetContextTLSData implements store.Store's GetContextTLSData // GetTLSData implements store.Store's GetTLSData
func (s *ContextStoreWithDefault) GetContextTLSData(contextName, endpointName, fileName string) ([]byte, error) { func (s *ContextStoreWithDefault) GetTLSData(contextName, endpointName, fileName string) ([]byte, error) {
if contextName == DefaultContextName { if contextName == DefaultContextName {
defaultContext, err := s.Resolver() defaultContext, err := s.Resolver()
if err != nil { if err != nil {
@ -171,7 +171,7 @@ func (s *ContextStoreWithDefault) GetContextTLSData(contextName, endpointName, f
return defaultContext.TLS.Endpoints[endpointName].Files[fileName], nil return defaultContext.TLS.Endpoints[endpointName].Files[fileName], nil
} }
return s.Store.GetContextTLSData(contextName, endpointName, fileName) return s.Store.GetTLSData(contextName, endpointName, fileName)
} }
type noDefaultTLSDataError struct { type noDefaultTLSDataError struct {
@ -189,10 +189,10 @@ func (e *noDefaultTLSDataError) NotFound() {}
// IsTLSDataDoesNotExist satisfies github.com/docker/cli/cli/context/store.tlsDataDoesNotExist // IsTLSDataDoesNotExist satisfies github.com/docker/cli/cli/context/store.tlsDataDoesNotExist
func (e *noDefaultTLSDataError) IsTLSDataDoesNotExist() {} func (e *noDefaultTLSDataError) IsTLSDataDoesNotExist() {}
// GetContextStorageInfo implements store.Store's GetContextStorageInfo // GetStorageInfo implements store.Store's GetStorageInfo
func (s *ContextStoreWithDefault) GetContextStorageInfo(contextName string) store.ContextStorageInfo { func (s *ContextStoreWithDefault) GetStorageInfo(contextName string) store.StorageInfo {
if contextName == DefaultContextName { if contextName == DefaultContextName {
return store.ContextStorageInfo{MetadataPath: "<IN MEMORY>", TLSPath: "<IN MEMORY>"} return store.StorageInfo{MetadataPath: "<IN MEMORY>", TLSPath: "<IN MEMORY>"}
} }
return s.Store.GetContextStorageInfo(contextName) return s.Store.GetStorageInfo(contextName)
} }

View File

@ -30,8 +30,8 @@ var testCfg = store.NewConfig(func() interface{} { return &testContext{} },
store.EndpointTypeGetter("ep2", func() interface{} { return &endpoint{} }), store.EndpointTypeGetter("ep2", func() interface{} { return &endpoint{} }),
) )
func testDefaultMetadata() store.ContextMetadata { func testDefaultMetadata() store.Metadata {
return store.ContextMetadata{ return store.Metadata{
Endpoints: map[string]interface{}{ Endpoints: map[string]interface{}{
"ep1": endpoint{Foo: "bar"}, "ep1": endpoint{Foo: "bar"},
}, },
@ -40,7 +40,7 @@ func testDefaultMetadata() store.ContextMetadata {
} }
} }
func testStore(t *testing.T, meta store.ContextMetadata, tls store.ContextTLSData) (store.Store, func()) { func testStore(t *testing.T, meta store.Metadata, tls store.ContextTLSData) (store.Store, func()) {
//meta := testDefaultMetadata() //meta := testDefaultMetadata()
testDir, err := ioutil.TempDir("", t.Name()) testDir, err := ioutil.TempDir("", t.Name())
assert.NilError(t, err) assert.NilError(t, err)
@ -102,33 +102,33 @@ func TestExportDefaultImport(t *testing.T) {
err := store.Import("dest", s, r) err := store.Import("dest", s, r)
assert.NilError(t, err) assert.NilError(t, err)
srcMeta, err := s.GetContextMetadata("default") srcMeta, err := s.GetMetadata("default")
assert.NilError(t, err) assert.NilError(t, err)
destMeta, err := s.GetContextMetadata("dest") destMeta, err := s.GetMetadata("dest")
assert.NilError(t, err) assert.NilError(t, err)
assert.DeepEqual(t, destMeta.Metadata, srcMeta.Metadata) assert.DeepEqual(t, destMeta.Metadata, srcMeta.Metadata)
assert.DeepEqual(t, destMeta.Endpoints, srcMeta.Endpoints) assert.DeepEqual(t, destMeta.Endpoints, srcMeta.Endpoints)
srcFileList, err := s.ListContextTLSFiles("default") srcFileList, err := s.ListTLSFiles("default")
assert.NilError(t, err) assert.NilError(t, err)
destFileList, err := s.ListContextTLSFiles("dest") destFileList, err := s.ListTLSFiles("dest")
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, 1, len(destFileList)) assert.Equal(t, 1, len(destFileList))
assert.Equal(t, 1, len(srcFileList)) assert.Equal(t, 1, len(srcFileList))
assert.Equal(t, 2, len(destFileList["ep2"])) assert.Equal(t, 2, len(destFileList["ep2"]))
assert.Equal(t, 2, len(srcFileList["ep2"])) assert.Equal(t, 2, len(srcFileList["ep2"]))
srcData1, err := s.GetContextTLSData("default", "ep2", "file1") srcData1, err := s.GetTLSData("default", "ep2", "file1")
assert.NilError(t, err) assert.NilError(t, err)
assert.DeepEqual(t, file1, srcData1) assert.DeepEqual(t, file1, srcData1)
srcData2, err := s.GetContextTLSData("default", "ep2", "file2") srcData2, err := s.GetTLSData("default", "ep2", "file2")
assert.NilError(t, err) assert.NilError(t, err)
assert.DeepEqual(t, file2, srcData2) assert.DeepEqual(t, file2, srcData2)
destData1, err := s.GetContextTLSData("dest", "ep2", "file1") destData1, err := s.GetTLSData("dest", "ep2", "file1")
assert.NilError(t, err) assert.NilError(t, err)
assert.DeepEqual(t, file1, destData1) assert.DeepEqual(t, file1, destData1)
destData2, err := s.GetContextTLSData("dest", "ep2", "file2") destData2, err := s.GetTLSData("dest", "ep2", "file2")
assert.NilError(t, err) assert.NilError(t, err)
assert.DeepEqual(t, file2, destData2) assert.DeepEqual(t, file2, destData2)
} }
@ -137,7 +137,7 @@ func TestListDefaultContext(t *testing.T) {
meta := testDefaultMetadata() meta := testDefaultMetadata()
s, cleanup := testStore(t, meta, store.ContextTLSData{}) s, cleanup := testStore(t, meta, store.ContextTLSData{})
defer cleanup() defer cleanup()
result, err := s.ListContexts() result, err := s.List()
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, 1, len(result)) assert.Equal(t, 1, len(result))
assert.DeepEqual(t, meta, result[0]) assert.DeepEqual(t, meta, result[0])
@ -146,7 +146,7 @@ func TestListDefaultContext(t *testing.T) {
func TestGetDefaultContextStorageInfo(t *testing.T) { func TestGetDefaultContextStorageInfo(t *testing.T) {
s, cleanup := testStore(t, testDefaultMetadata(), store.ContextTLSData{}) s, cleanup := testStore(t, testDefaultMetadata(), store.ContextTLSData{})
defer cleanup() defer cleanup()
result := s.GetContextStorageInfo(DefaultContextName) result := s.GetStorageInfo(DefaultContextName)
assert.Equal(t, "<IN MEMORY>", result.MetadataPath) assert.Equal(t, "<IN MEMORY>", result.MetadataPath)
assert.Equal(t, "<IN MEMORY>", result.TLSPath) assert.Equal(t, "<IN MEMORY>", result.TLSPath)
} }
@ -155,7 +155,7 @@ func TestGetDefaultContextMetadata(t *testing.T) {
meta := testDefaultMetadata() meta := testDefaultMetadata()
s, cleanup := testStore(t, meta, store.ContextTLSData{}) s, cleanup := testStore(t, meta, store.ContextTLSData{})
defer cleanup() defer cleanup()
result, err := s.GetContextMetadata(DefaultContextName) result, err := s.GetMetadata(DefaultContextName)
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, DefaultContextName, result.Name) assert.Equal(t, DefaultContextName, result.Name)
assert.DeepEqual(t, meta.Metadata, result.Metadata) assert.DeepEqual(t, meta.Metadata, result.Metadata)
@ -166,7 +166,7 @@ func TestErrCreateDefault(t *testing.T) {
meta := testDefaultMetadata() meta := testDefaultMetadata()
s, cleanup := testStore(t, meta, store.ContextTLSData{}) s, cleanup := testStore(t, meta, store.ContextTLSData{})
defer cleanup() defer cleanup()
err := s.CreateOrUpdateContext(store.ContextMetadata{ err := s.CreateOrUpdate(store.Metadata{
Endpoints: map[string]interface{}{ Endpoints: map[string]interface{}{
"ep1": endpoint{Foo: "bar"}, "ep1": endpoint{Foo: "bar"},
}, },
@ -180,7 +180,7 @@ func TestErrRemoveDefault(t *testing.T) {
meta := testDefaultMetadata() meta := testDefaultMetadata()
s, cleanup := testStore(t, meta, store.ContextTLSData{}) s, cleanup := testStore(t, meta, store.ContextTLSData{})
defer cleanup() defer cleanup()
err := s.RemoveContext("default") err := s.Remove("default")
assert.Error(t, err, "default context cannot be removed") assert.Error(t, err, "default context cannot be removed")
} }
@ -188,6 +188,6 @@ func TestErrTLSDataError(t *testing.T) {
meta := testDefaultMetadata() meta := testDefaultMetadata()
s, cleanup := testStore(t, meta, store.ContextTLSData{}) s, cleanup := testStore(t, meta, store.ContextTLSData{})
defer cleanup() defer cleanup()
_, err := s.GetContextTLSData("default", "noop", "noop") _, err := s.GetTLSData("default", "noop", "noop")
assert.Check(t, store.IsErrTLSDataDoesNotExist(err)) assert.Check(t, store.IsErrTLSDataDoesNotExist(err))
} }

View File

@ -153,7 +153,7 @@ func withHTTPClient(tlsConfig *tls.Config) func(*client.Client) error {
} }
// EndpointFromContext parses a context docker endpoint metadata into a typed EndpointMeta structure // EndpointFromContext parses a context docker endpoint metadata into a typed EndpointMeta structure
func EndpointFromContext(metadata store.ContextMetadata) (EndpointMeta, error) { func EndpointFromContext(metadata store.Metadata) (EndpointMeta, error) {
ep, ok := metadata.Endpoints[DockerEndpoint] ep, ok := metadata.Endpoints[DockerEndpoint]
if !ok { if !ok {
return EndpointMeta{}, errors.New("cannot find docker endpoint in context") return EndpointMeta{}, errors.New("cannot find docker endpoint in context")

View File

@ -85,15 +85,15 @@ func TestSaveLoadContexts(t *testing.T) {
assert.NilError(t, save(store, epDefault, "embed-default-context")) assert.NilError(t, save(store, epDefault, "embed-default-context"))
assert.NilError(t, save(store, epContext2, "embed-context2")) assert.NilError(t, save(store, epContext2, "embed-context2"))
rawNoTLSMeta, err := store.GetContextMetadata("raw-notls") rawNoTLSMeta, err := store.GetMetadata("raw-notls")
assert.NilError(t, err) assert.NilError(t, err)
rawNoTLSSkipMeta, err := store.GetContextMetadata("raw-notls-skip") rawNoTLSSkipMeta, err := store.GetMetadata("raw-notls-skip")
assert.NilError(t, err) assert.NilError(t, err)
rawTLSMeta, err := store.GetContextMetadata("raw-tls") rawTLSMeta, err := store.GetMetadata("raw-tls")
assert.NilError(t, err) assert.NilError(t, err)
embededDefaultMeta, err := store.GetContextMetadata("embed-default-context") embededDefaultMeta, err := store.GetMetadata("embed-default-context")
assert.NilError(t, err) assert.NilError(t, err)
embededContext2Meta, err := store.GetContextMetadata("embed-context2") embededContext2Meta, err := store.GetMetadata("embed-context2")
assert.NilError(t, err) assert.NilError(t, err)
rawNoTLS := EndpointFromContext(rawNoTLSMeta) rawNoTLS := EndpointFromContext(rawNoTLSMeta)
@ -133,16 +133,16 @@ func checkClientConfig(t *testing.T, ep Endpoint, server, namespace string, ca,
} }
func save(s store.Writer, ep Endpoint, name string) error { func save(s store.Writer, ep Endpoint, name string) error {
meta := store.ContextMetadata{ meta := store.Metadata{
Endpoints: map[string]interface{}{ Endpoints: map[string]interface{}{
KubernetesEndpoint: ep.EndpointMeta, KubernetesEndpoint: ep.EndpointMeta,
}, },
Name: name, Name: name,
} }
if err := s.CreateOrUpdateContext(meta); err != nil { if err := s.CreateOrUpdate(meta); err != nil {
return err return err
} }
return s.ResetContextEndpointTLSMaterial(name, KubernetesEndpoint, ep.TLSData.ToStoreTLSData()) return s.ResetEndpointTLSMaterial(name, KubernetesEndpoint, ep.TLSData.ToStoreTLSData())
} }
func TestSaveLoadGKEConfig(t *testing.T) { func TestSaveLoadGKEConfig(t *testing.T) {
@ -158,7 +158,7 @@ func TestSaveLoadGKEConfig(t *testing.T) {
ep, err := FromKubeConfig("testdata/gke-kubeconfig", "", "") ep, err := FromKubeConfig("testdata/gke-kubeconfig", "", "")
assert.NilError(t, err) assert.NilError(t, err)
assert.NilError(t, save(store, ep, "gke-context")) assert.NilError(t, save(store, ep, "gke-context"))
persistedMetadata, err := store.GetContextMetadata("gke-context") persistedMetadata, err := store.GetMetadata("gke-context")
assert.NilError(t, err) assert.NilError(t, err)
persistedEPMeta := EndpointFromContext(persistedMetadata) persistedEPMeta := EndpointFromContext(persistedMetadata)
assert.Check(t, persistedEPMeta != nil) assert.Check(t, persistedEPMeta != nil)
@ -183,7 +183,7 @@ func TestSaveLoadEKSConfig(t *testing.T) {
ep, err := FromKubeConfig("testdata/eks-kubeconfig", "", "") ep, err := FromKubeConfig("testdata/eks-kubeconfig", "", "")
assert.NilError(t, err) assert.NilError(t, err)
assert.NilError(t, save(store, ep, "eks-context")) assert.NilError(t, save(store, ep, "eks-context"))
persistedMetadata, err := store.GetContextMetadata("eks-context") persistedMetadata, err := store.GetMetadata("eks-context")
assert.NilError(t, err) assert.NilError(t, err)
persistedEPMeta := EndpointFromContext(persistedMetadata) persistedEPMeta := EndpointFromContext(persistedMetadata)
assert.Check(t, persistedEPMeta != nil) assert.Check(t, persistedEPMeta != nil)

View File

@ -62,7 +62,7 @@ func (c *Endpoint) KubernetesConfig() clientcmd.ClientConfig {
} }
// EndpointFromContext extracts kubernetes endpoint info from current context // EndpointFromContext extracts kubernetes endpoint info from current context
func EndpointFromContext(metadata store.ContextMetadata) *EndpointMeta { func EndpointFromContext(metadata store.Metadata) *EndpointMeta {
ep, ok := metadata.Endpoints[KubernetesEndpoint] ep, ok := metadata.Endpoints[KubernetesEndpoint]
if !ok { if !ok {
return nil return nil
@ -78,7 +78,7 @@ func EndpointFromContext(metadata store.ContextMetadata) *EndpointMeta {
// If kubeconfigOverride is specified, use this config file instead of the context defaults.ConfigFromContext // If kubeconfigOverride is specified, use this config file instead of the context defaults.ConfigFromContext
// if command.ContextDockerHost is specified as the context name, fallsback to the default user's kubeconfig file // if command.ContextDockerHost is specified as the context name, fallsback to the default user's kubeconfig file
func ConfigFromContext(name string, s store.Reader) (clientcmd.ClientConfig, error) { func ConfigFromContext(name string, s store.Reader) (clientcmd.ClientConfig, error) {
ctxMeta, err := s.GetContextMetadata(name) ctxMeta, err := s.GetMetadata(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -10,8 +10,8 @@ import (
"gotest.tools/assert/cmp" "gotest.tools/assert/cmp"
) )
func testMetadata(name string) ContextMetadata { func testMetadata(name string) Metadata {
return ContextMetadata{ return Metadata{
Endpoints: map[string]interface{}{ Endpoints: map[string]interface{}{
"ep1": endpoint{Foo: "bar"}, "ep1": endpoint{Foo: "bar"},
}, },
@ -34,7 +34,7 @@ func TestMetadataCreateGetRemove(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
defer os.RemoveAll(testDir) defer os.RemoveAll(testDir)
testee := metadataStore{root: testDir, config: testCfg} testee := metadataStore{root: testDir, config: testCfg}
expected2 := ContextMetadata{ expected2 := Metadata{
Endpoints: map[string]interface{}{ Endpoints: map[string]interface{}{
"ep1": endpoint{Foo: "baz"}, "ep1": endpoint{Foo: "baz"},
"ep2": endpoint{Foo: "bee"}, "ep2": endpoint{Foo: "bee"},
@ -82,7 +82,7 @@ func TestMetadataList(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
defer os.RemoveAll(testDir) defer os.RemoveAll(testDir)
testee := metadataStore{root: testDir, config: testCfg} testee := metadataStore{root: testDir, config: testCfg}
wholeData := []ContextMetadata{ wholeData := []Metadata{
testMetadata("context1"), testMetadata("context1"),
testMetadata("context2"), testMetadata("context2"),
testMetadata("context3"), testMetadata("context3"),
@ -103,7 +103,7 @@ func TestEmptyConfig(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
defer os.RemoveAll(testDir) defer os.RemoveAll(testDir)
testee := metadataStore{root: testDir} testee := metadataStore{root: testDir}
wholeData := []ContextMetadata{ wholeData := []Metadata{
testMetadata("context1"), testMetadata("context1"),
testMetadata("context2"), testMetadata("context2"),
testMetadata("context3"), testMetadata("context3"),
@ -136,7 +136,7 @@ func TestWithEmbedding(t *testing.T) {
Val: "Hello", Val: "Hello",
}, },
} }
assert.NilError(t, testee.createOrUpdate(ContextMetadata{Metadata: testCtxMeta, Name: "test"})) assert.NilError(t, testee.createOrUpdate(Metadata{Metadata: testCtxMeta, Name: "test"}))
res, err := testee.get(contextdirOf("test")) res, err := testee.get(contextdirOf("test"))
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, testCtxMeta, res.Metadata) assert.Equal(t, testCtxMeta, res.Metadata)

View File

@ -26,7 +26,7 @@ func (s *metadataStore) contextDir(id contextdir) string {
return filepath.Join(s.root, string(id)) return filepath.Join(s.root, string(id))
} }
func (s *metadataStore) createOrUpdate(meta ContextMetadata) error { func (s *metadataStore) createOrUpdate(meta Metadata) error {
contextDir := s.contextDir(contextdirOf(meta.Name)) contextDir := s.contextDir(contextdirOf(meta.Name))
if err := os.MkdirAll(contextDir, 0755); err != nil { if err := os.MkdirAll(contextDir, 0755); err != nil {
return err return err
@ -56,26 +56,26 @@ func parseTypedOrMap(payload []byte, getter TypeGetter) (interface{}, error) {
return reflect.ValueOf(typed).Elem().Interface(), nil return reflect.ValueOf(typed).Elem().Interface(), nil
} }
func (s *metadataStore) get(id contextdir) (ContextMetadata, error) { func (s *metadataStore) get(id contextdir) (Metadata, error) {
contextDir := s.contextDir(id) contextDir := s.contextDir(id)
bytes, err := ioutil.ReadFile(filepath.Join(contextDir, metaFile)) bytes, err := ioutil.ReadFile(filepath.Join(contextDir, metaFile))
if err != nil { if err != nil {
return ContextMetadata{}, convertContextDoesNotExist(err) return Metadata{}, convertContextDoesNotExist(err)
} }
var untyped untypedContextMetadata var untyped untypedContextMetadata
r := ContextMetadata{ r := Metadata{
Endpoints: make(map[string]interface{}), Endpoints: make(map[string]interface{}),
} }
if err := json.Unmarshal(bytes, &untyped); err != nil { if err := json.Unmarshal(bytes, &untyped); err != nil {
return ContextMetadata{}, err return Metadata{}, err
} }
r.Name = untyped.Name r.Name = untyped.Name
if r.Metadata, err = parseTypedOrMap(untyped.Metadata, s.config.contextType); err != nil { if r.Metadata, err = parseTypedOrMap(untyped.Metadata, s.config.contextType); err != nil {
return ContextMetadata{}, err return Metadata{}, err
} }
for k, v := range untyped.Endpoints { for k, v := range untyped.Endpoints {
if r.Endpoints[k], err = parseTypedOrMap(v, s.config.endpointTypes[k]); err != nil { if r.Endpoints[k], err = parseTypedOrMap(v, s.config.endpointTypes[k]); err != nil {
return ContextMetadata{}, err return Metadata{}, err
} }
} }
return r, err return r, err
@ -86,7 +86,7 @@ func (s *metadataStore) remove(id contextdir) error {
return os.RemoveAll(contextDir) return os.RemoveAll(contextDir)
} }
func (s *metadataStore) list() ([]ContextMetadata, error) { func (s *metadataStore) list() ([]Metadata, error) {
ctxDirs, err := listRecursivelyMetadataDirs(s.root) ctxDirs, err := listRecursivelyMetadataDirs(s.root)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
@ -94,7 +94,7 @@ func (s *metadataStore) list() ([]ContextMetadata, error) {
} }
return nil, err return nil, err
} }
var res []ContextMetadata var res []Metadata
for _, dir := range ctxDirs { for _, dir := range ctxDirs {
c, err := s.get(contextdir(dir)) c, err := s.get(contextdir(dir))
if err != nil { if err != nil {

View File

@ -21,19 +21,19 @@ type Store interface {
Reader Reader
Lister Lister
Writer Writer
StorageInfo StorageInfoProvider
} }
// Reader provides read-only (without list) access to context data // Reader provides read-only (without list) access to context data
type Reader interface { type Reader interface {
GetContextMetadata(name string) (ContextMetadata, error) GetMetadata(name string) (Metadata, error)
ListContextTLSFiles(name string) (map[string]EndpointFiles, error) ListTLSFiles(name string) (map[string]EndpointFiles, error)
GetContextTLSData(contextName, endpointName, fileName string) ([]byte, error) GetTLSData(contextName, endpointName, fileName string) ([]byte, error)
} }
// Lister provides listing of contexts // Lister provides listing of contexts
type Lister interface { type Lister interface {
ListContexts() ([]ContextMetadata, error) List() ([]Metadata, error)
} }
// ReaderLister combines Reader and Lister interfaces // ReaderLister combines Reader and Lister interfaces
@ -42,17 +42,17 @@ type ReaderLister interface {
Lister Lister
} }
// StorageInfo provides more information about storage details of contexts // StorageInfoProvider provides more information about storage details of contexts
type StorageInfo interface { type StorageInfoProvider interface {
GetContextStorageInfo(contextName string) ContextStorageInfo GetStorageInfo(contextName string) StorageInfo
} }
// Writer provides write access to context data // Writer provides write access to context data
type Writer interface { type Writer interface {
CreateOrUpdateContext(meta ContextMetadata) error CreateOrUpdate(meta Metadata) error
RemoveContext(name string) error Remove(name string) error
ResetContextTLSMaterial(name string, data *ContextTLSData) error ResetTLSMaterial(name string, data *ContextTLSData) error
ResetContextEndpointTLSMaterial(contextName string, endpointName string, data *EndpointTLSData) error ResetEndpointTLSMaterial(contextName string, endpointName string, data *EndpointTLSData) error
} }
// ReaderWriter combines Reader and Writer interfaces // ReaderWriter combines Reader and Writer interfaces
@ -61,15 +61,15 @@ type ReaderWriter interface {
Writer Writer
} }
// ContextMetadata contains metadata about a context and its endpoints // Metadata contains metadata about a context and its endpoints
type ContextMetadata struct { type Metadata struct {
Name string `json:",omitempty"` Name string `json:",omitempty"`
Metadata interface{} `json:",omitempty"` Metadata interface{} `json:",omitempty"`
Endpoints map[string]interface{} `json:",omitempty"` Endpoints map[string]interface{} `json:",omitempty"`
} }
// ContextStorageInfo contains data about where a given context is stored // StorageInfo contains data about where a given context is stored
type ContextStorageInfo struct { type StorageInfo struct {
MetadataPath string MetadataPath string
TLSPath string TLSPath string
} }
@ -106,15 +106,15 @@ type store struct {
tls *tlsStore tls *tlsStore
} }
func (s *store) ListContexts() ([]ContextMetadata, error) { func (s *store) List() ([]Metadata, error) {
return s.meta.list() return s.meta.list()
} }
func (s *store) CreateOrUpdateContext(meta ContextMetadata) error { func (s *store) CreateOrUpdate(meta Metadata) error {
return s.meta.createOrUpdate(meta) return s.meta.createOrUpdate(meta)
} }
func (s *store) RemoveContext(name string) error { func (s *store) Remove(name string) error {
id := contextdirOf(name) id := contextdirOf(name)
if err := s.meta.remove(id); err != nil { if err := s.meta.remove(id); err != nil {
return patchErrContextName(err, name) return patchErrContextName(err, name)
@ -122,13 +122,13 @@ func (s *store) RemoveContext(name string) error {
return patchErrContextName(s.tls.removeAllContextData(id), name) return patchErrContextName(s.tls.removeAllContextData(id), name)
} }
func (s *store) GetContextMetadata(name string) (ContextMetadata, error) { func (s *store) GetMetadata(name string) (Metadata, error) {
res, err := s.meta.get(contextdirOf(name)) res, err := s.meta.get(contextdirOf(name))
patchErrContextName(err, name) patchErrContextName(err, name)
return res, err return res, err
} }
func (s *store) ResetContextTLSMaterial(name string, data *ContextTLSData) error { func (s *store) ResetTLSMaterial(name string, data *ContextTLSData) error {
id := contextdirOf(name) id := contextdirOf(name)
if err := s.tls.removeAllContextData(id); err != nil { if err := s.tls.removeAllContextData(id); err != nil {
return patchErrContextName(err, name) return patchErrContextName(err, name)
@ -146,7 +146,7 @@ func (s *store) ResetContextTLSMaterial(name string, data *ContextTLSData) error
return nil return nil
} }
func (s *store) ResetContextEndpointTLSMaterial(contextName string, endpointName string, data *EndpointTLSData) error { func (s *store) ResetEndpointTLSMaterial(contextName string, endpointName string, data *EndpointTLSData) error {
id := contextdirOf(contextName) id := contextdirOf(contextName)
if err := s.tls.removeAllEndpointData(id, endpointName); err != nil { if err := s.tls.removeAllEndpointData(id, endpointName); err != nil {
return patchErrContextName(err, contextName) return patchErrContextName(err, contextName)
@ -162,19 +162,19 @@ func (s *store) ResetContextEndpointTLSMaterial(contextName string, endpointName
return nil return nil
} }
func (s *store) ListContextTLSFiles(name string) (map[string]EndpointFiles, error) { func (s *store) ListTLSFiles(name string) (map[string]EndpointFiles, error) {
res, err := s.tls.listContextData(contextdirOf(name)) res, err := s.tls.listContextData(contextdirOf(name))
return res, patchErrContextName(err, name) return res, patchErrContextName(err, name)
} }
func (s *store) GetContextTLSData(contextName, endpointName, fileName string) ([]byte, error) { func (s *store) GetTLSData(contextName, endpointName, fileName string) ([]byte, error) {
res, err := s.tls.getData(contextdirOf(contextName), endpointName, fileName) res, err := s.tls.getData(contextdirOf(contextName), endpointName, fileName)
return res, patchErrContextName(err, contextName) return res, patchErrContextName(err, contextName)
} }
func (s *store) GetContextStorageInfo(contextName string) ContextStorageInfo { func (s *store) GetStorageInfo(contextName string) StorageInfo {
dir := contextdirOf(contextName) dir := contextdirOf(contextName)
return ContextStorageInfo{ return StorageInfo{
MetadataPath: s.meta.contextDir(dir), MetadataPath: s.meta.contextDir(dir),
TLSPath: s.tls.contextDir(dir), TLSPath: s.tls.contextDir(dir),
} }
@ -189,7 +189,7 @@ func Export(name string, s Reader) io.ReadCloser {
tw := tar.NewWriter(writer) tw := tar.NewWriter(writer)
defer tw.Close() defer tw.Close()
defer writer.Close() defer writer.Close()
meta, err := s.GetContextMetadata(name) meta, err := s.GetMetadata(name)
if err != nil { if err != nil {
writer.CloseWithError(err) writer.CloseWithError(err)
return return
@ -211,7 +211,7 @@ func Export(name string, s Reader) io.ReadCloser {
writer.CloseWithError(err) writer.CloseWithError(err)
return return
} }
tlsFiles, err := s.ListContextTLSFiles(name) tlsFiles, err := s.ListTLSFiles(name)
if err != nil { if err != nil {
writer.CloseWithError(err) writer.CloseWithError(err)
return return
@ -236,7 +236,7 @@ func Export(name string, s Reader) io.ReadCloser {
return return
} }
for _, fileName := range endpointFiles { for _, fileName := range endpointFiles {
data, err := s.GetContextTLSData(name, endpointName, fileName) data, err := s.GetTLSData(name, endpointName, fileName)
if err != nil { if err != nil {
writer.CloseWithError(err) writer.CloseWithError(err)
return return
@ -282,12 +282,12 @@ func Import(name string, s Writer, reader io.Reader) error {
if err != nil { if err != nil {
return err return err
} }
var meta ContextMetadata var meta Metadata
if err := json.Unmarshal(data, &meta); err != nil { if err := json.Unmarshal(data, &meta); err != nil {
return err return err
} }
meta.Name = name meta.Name = name
if err := s.CreateOrUpdateContext(meta); err != nil { if err := s.CreateOrUpdate(meta); err != nil {
return err return err
} }
} else if strings.HasPrefix(hdr.Name, "tls/") { } else if strings.HasPrefix(hdr.Name, "tls/") {
@ -310,7 +310,7 @@ func Import(name string, s Writer, reader io.Reader) error {
tlsData.Endpoints[endpointName].Files[fileName] = data tlsData.Endpoints[endpointName].Files[fileName] = data
} }
} }
return s.ResetContextTLSMaterial(name, &tlsData) return s.ResetTLSMaterial(name, &tlsData)
} }
type setContextName interface { type setContextName interface {

View File

@ -27,8 +27,8 @@ func TestExportImport(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
defer os.RemoveAll(testDir) defer os.RemoveAll(testDir)
s := New(testDir, testCfg) s := New(testDir, testCfg)
err = s.CreateOrUpdateContext( err = s.CreateOrUpdate(
ContextMetadata{ Metadata{
Endpoints: map[string]interface{}{ Endpoints: map[string]interface{}{
"ep1": endpoint{Foo: "bar"}, "ep1": endpoint{Foo: "bar"},
}, },
@ -40,7 +40,7 @@ func TestExportImport(t *testing.T) {
rand.Read(file1) rand.Read(file1)
file2 := make([]byte, 3700) file2 := make([]byte, 3700)
rand.Read(file2) rand.Read(file2)
err = s.ResetContextEndpointTLSMaterial("source", "ep1", &EndpointTLSData{ err = s.ResetEndpointTLSMaterial("source", "ep1", &EndpointTLSData{
Files: map[string][]byte{ Files: map[string][]byte{
"file1": file1, "file1": file1,
"file2": file2, "file2": file2,
@ -51,30 +51,30 @@ func TestExportImport(t *testing.T) {
defer r.Close() defer r.Close()
err = Import("dest", s, r) err = Import("dest", s, r)
assert.NilError(t, err) assert.NilError(t, err)
srcMeta, err := s.GetContextMetadata("source") srcMeta, err := s.GetMetadata("source")
assert.NilError(t, err) assert.NilError(t, err)
destMeta, err := s.GetContextMetadata("dest") destMeta, err := s.GetMetadata("dest")
assert.NilError(t, err) assert.NilError(t, err)
assert.DeepEqual(t, destMeta.Metadata, srcMeta.Metadata) assert.DeepEqual(t, destMeta.Metadata, srcMeta.Metadata)
assert.DeepEqual(t, destMeta.Endpoints, srcMeta.Endpoints) assert.DeepEqual(t, destMeta.Endpoints, srcMeta.Endpoints)
srcFileList, err := s.ListContextTLSFiles("source") srcFileList, err := s.ListTLSFiles("source")
assert.NilError(t, err) assert.NilError(t, err)
destFileList, err := s.ListContextTLSFiles("dest") destFileList, err := s.ListTLSFiles("dest")
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, 1, len(destFileList)) assert.Equal(t, 1, len(destFileList))
assert.Equal(t, 1, len(srcFileList)) assert.Equal(t, 1, len(srcFileList))
assert.Equal(t, 2, len(destFileList["ep1"])) assert.Equal(t, 2, len(destFileList["ep1"]))
assert.Equal(t, 2, len(srcFileList["ep1"])) assert.Equal(t, 2, len(srcFileList["ep1"]))
srcData1, err := s.GetContextTLSData("source", "ep1", "file1") srcData1, err := s.GetTLSData("source", "ep1", "file1")
assert.NilError(t, err) assert.NilError(t, err)
assert.DeepEqual(t, file1, srcData1) assert.DeepEqual(t, file1, srcData1)
srcData2, err := s.GetContextTLSData("source", "ep1", "file2") srcData2, err := s.GetTLSData("source", "ep1", "file2")
assert.NilError(t, err) assert.NilError(t, err)
assert.DeepEqual(t, file2, srcData2) assert.DeepEqual(t, file2, srcData2)
destData1, err := s.GetContextTLSData("dest", "ep1", "file1") destData1, err := s.GetTLSData("dest", "ep1", "file1")
assert.NilError(t, err) assert.NilError(t, err)
assert.DeepEqual(t, file1, destData1) assert.DeepEqual(t, file1, destData1)
destData2, err := s.GetContextTLSData("dest", "ep1", "file2") destData2, err := s.GetTLSData("dest", "ep1", "file2")
assert.NilError(t, err) assert.NilError(t, err)
assert.DeepEqual(t, file2, destData2) assert.DeepEqual(t, file2, destData2)
} }
@ -84,8 +84,8 @@ func TestRemove(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
defer os.RemoveAll(testDir) defer os.RemoveAll(testDir)
s := New(testDir, testCfg) s := New(testDir, testCfg)
err = s.CreateOrUpdateContext( err = s.CreateOrUpdate(
ContextMetadata{ Metadata{
Endpoints: map[string]interface{}{ Endpoints: map[string]interface{}{
"ep1": endpoint{Foo: "bar"}, "ep1": endpoint{Foo: "bar"},
}, },
@ -93,15 +93,15 @@ func TestRemove(t *testing.T) {
Name: "source", Name: "source",
}) })
assert.NilError(t, err) assert.NilError(t, err)
assert.NilError(t, s.ResetContextEndpointTLSMaterial("source", "ep1", &EndpointTLSData{ assert.NilError(t, s.ResetEndpointTLSMaterial("source", "ep1", &EndpointTLSData{
Files: map[string][]byte{ Files: map[string][]byte{
"file1": []byte("test-data"), "file1": []byte("test-data"),
}, },
})) }))
assert.NilError(t, s.RemoveContext("source")) assert.NilError(t, s.Remove("source"))
_, err = s.GetContextMetadata("source") _, err = s.GetMetadata("source")
assert.Check(t, IsErrContextDoesNotExist(err)) assert.Check(t, IsErrContextDoesNotExist(err))
f, err := s.ListContextTLSFiles("source") f, err := s.ListTLSFiles("source")
assert.NilError(t, err) assert.NilError(t, err)
assert.Equal(t, 0, len(f)) assert.Equal(t, 0, len(f))
} }
@ -111,7 +111,7 @@ func TestListEmptyStore(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
defer os.RemoveAll(testDir) defer os.RemoveAll(testDir)
store := New(testDir, testCfg) store := New(testDir, testCfg)
result, err := store.ListContexts() result, err := store.List()
assert.NilError(t, err) assert.NilError(t, err)
assert.Check(t, len(result) == 0) assert.Check(t, len(result) == 0)
} }
@ -121,7 +121,7 @@ func TestErrHasCorrectContext(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
defer os.RemoveAll(testDir) defer os.RemoveAll(testDir)
store := New(testDir, testCfg) store := New(testDir, testCfg)
_, err = store.GetContextMetadata("no-exists") _, err = store.GetMetadata("no-exists")
assert.ErrorContains(t, err, "no-exists") assert.ErrorContains(t, err, "no-exists")
assert.Check(t, IsErrContextDoesNotExist(err)) assert.Check(t, IsErrContextDoesNotExist(err))
} }

View File

@ -43,14 +43,14 @@ func (data *TLSData) ToStoreTLSData() *store.EndpointTLSData {
// LoadTLSData loads TLS data from the store // LoadTLSData loads TLS data from the store
func LoadTLSData(s store.Reader, contextName, endpointName string) (*TLSData, error) { func LoadTLSData(s store.Reader, contextName, endpointName string) (*TLSData, error) {
tlsFiles, err := s.ListContextTLSFiles(contextName) tlsFiles, err := s.ListTLSFiles(contextName)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to retrieve context tls files for context %q", contextName) return nil, errors.Wrapf(err, "failed to retrieve context tls files for context %q", contextName)
} }
if epTLSFiles, ok := tlsFiles[endpointName]; ok { if epTLSFiles, ok := tlsFiles[endpointName]; ok {
var tlsData TLSData var tlsData TLSData
for _, f := range epTLSFiles { for _, f := range epTLSFiles {
data, err := s.GetContextTLSData(contextName, endpointName, f) data, err := s.GetTLSData(contextName, endpointName, f)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to retrieve context tls data for file %q of context %q", f, contextName) return nil, errors.Wrapf(err, "failed to retrieve context tls data for file %q of context %q", f, contextName)
} }

View File

@ -233,7 +233,7 @@ func (c *FakeCli) StackOrchestrator(flagValue string) (command.Orchestrator, err
} }
ctxOrchestrator := "" ctxOrchestrator := ""
if c.currentContext != "" && c.contextStore != nil { if c.currentContext != "" && c.contextStore != nil {
meta, err := c.contextStore.GetContextMetadata(c.currentContext) meta, err := c.contextStore.GetMetadata(c.currentContext)
if err != nil { if err != nil {
return "", err return "", err
} }