diff --git a/cli/cmd/context/create.go b/cli/cmd/context/create.go index 960c12876..4c4565390 100644 --- a/cli/cmd/context/create.go +++ b/cli/cmd/context/create.go @@ -130,6 +130,11 @@ func createDockerContext(ctx context.Context, name string, contextType string, d return result } +func contextExists(ctx context.Context, name string) bool { + s := store.ContextStore(ctx) + return s.ContextExists(name) +} + func addDescriptionFlag(cmd *cobra.Command, descriptionOpt *string) { cmd.Flags().StringVar(descriptionOpt, "description", "", "Description of the context") } diff --git a/cli/cmd/context/createaci.go b/cli/cmd/context/createaci.go index 244056a0f..68c690dc1 100644 --- a/cli/cmd/context/createaci.go +++ b/cli/cmd/context/createaci.go @@ -24,6 +24,7 @@ import ( "github.com/docker/api/client" "github.com/docker/api/context/store" + "github.com/docker/api/errdefs" ) type aciCreateOpts struct { @@ -40,11 +41,7 @@ func createAciCommand() *cobra.Command { Short: "Create a context for Azure Container Instances", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - contextData, description, err := getAciContextData(cmd.Context(), opts) - if err != nil { - return err - } - return createDockerContext(cmd.Context(), args[0], store.AciContextType, description, contextData) + return runCreateAci(cmd.Context(), args[0], opts) }, } @@ -56,6 +53,18 @@ func createAciCommand() *cobra.Command { return cmd } +func runCreateAci(ctx context.Context, contextName string, opts aciCreateOpts) error { + if contextExists(ctx, contextName) { + return errors.Wrapf(errdefs.ErrAlreadyExists, "context %s", contextName) + } + contextData, description, err := getAciContextData(ctx, opts) + if err != nil { + return err + } + return createDockerContext(ctx, contextName, store.AciContextType, description, contextData) + +} + func getAciContextData(ctx context.Context, opts aciCreateOpts) (interface{}, string, error) { cs, err := client.GetCloudService(ctx, store.AciContextType) if err != nil { diff --git a/context/store/store.go b/context/store/store.go index b06888e5d..c2100e42e 100644 --- a/context/store/store.go +++ b/context/store/store.go @@ -83,6 +83,8 @@ type Store interface { List() ([]*DockerContext, error) // Remove removes a context by name from the context store Remove(name string) error + // ContextExists checks if a context already exists + ContextExists(name string) bool } // Endpoint holds the Docker or the Kubernetes endpoint, they both have the @@ -218,15 +220,24 @@ func toTypedEndpoints(endpoints map[string]interface{}) (map[string]interface{}, return result, nil } -func (s *store) Create(name string, contextType string, description string, data interface{}) error { +func (s *store) ContextExists(name string) bool { if name == DefaultContextName { - return errors.Wrap(errdefs.ErrAlreadyExists, objectName(name)) + return true } dir := contextDirOf(name) metaDir := filepath.Join(s.root, contextsDir, metadataDir, dir) if _, err := os.Stat(metaDir); !os.IsNotExist(err) { + return true + } + return false +} + +func (s *store) Create(name string, contextType string, description string, data interface{}) error { + if s.ContextExists(name) { return errors.Wrap(errdefs.ErrAlreadyExists, objectName(name)) } + dir := contextDirOf(name) + metaDir := filepath.Join(s.root, contextsDir, metadataDir, dir) err := os.Mkdir(metaDir, 0755) if err != nil { diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index 2519aa7dc..34a444c15 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -103,6 +103,12 @@ func (s *E2eSuite) TestCanForceRemoveCurrentContext() { Expect(out).To(ContainSubstring("default *")) } +func (s *E2eSuite) TestContextCreateAciChecksContextNameBeforeInteractivePart() { + s.NewDockerCommand("context", "create", "mycontext", "--from", "default").ExecOrDie() + _, err := s.NewDockerCommand("context", "create", "aci", "mycontext").Exec() + Expect(err.Error()).To(ContainSubstring("context mycontext: already exists")) +} + func (s *E2eSuite) TestClassicLoginWithparameters() { output, err := s.NewDockerCommand("login", "-u", "nouser", "-p", "wrongpasword").Exec() Expect(output).To(ContainSubstring("Get https://registry-1.docker.io/v2/: unauthorized: incorrect username or password"))