Browse Source

Context aci create : check context name does not exists before starting interactive aci create

Guillaume Tardif 5 years ago
parent
commit
2824a7a736
4 changed files with 38 additions and 7 deletions
  1. 5 0
      cli/cmd/context/create.go
  2. 14 5
      cli/cmd/context/createaci.go
  3. 13 2
      context/store/store.go
  4. 6 0
      tests/e2e/e2e_test.go

+ 5 - 0
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")
 }

+ 14 - 5
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 {

+ 13 - 2
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 {

+ 6 - 0
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"))