Browse code

remove cli/command/secrets/utils.go

Signed-off-by: allencloud <allen.sun@daocloud.io>

allencloud authored on 2017/01/29 02:04:10
Showing 2 changed files
1 1
deleted file mode 100644
... ...
@@ -1,76 +0,0 @@
1
-package secret
2
-
3
-import (
4
-	"fmt"
5
-	"strings"
6
-
7
-	"github.com/docker/docker/api/types"
8
-	"github.com/docker/docker/api/types/filters"
9
-	"github.com/docker/docker/api/types/swarm"
10
-	"github.com/docker/docker/client"
11
-	"golang.org/x/net/context"
12
-)
13
-
14
-// GetSecretsByNameOrIDPrefixes returns secrets given a list of ids or names
15
-func GetSecretsByNameOrIDPrefixes(ctx context.Context, client client.APIClient, terms []string) ([]swarm.Secret, error) {
16
-	args := filters.NewArgs()
17
-	for _, n := range terms {
18
-		args.Add("names", n)
19
-		args.Add("id", n)
20
-	}
21
-
22
-	return client.SecretList(ctx, types.SecretListOptions{
23
-		Filters: args,
24
-	})
25
-}
26
-
27
-func getCliRequestedSecretIDs(ctx context.Context, client client.APIClient, terms []string) ([]string, error) {
28
-	secrets, err := GetSecretsByNameOrIDPrefixes(ctx, client, terms)
29
-	if err != nil {
30
-		return nil, err
31
-	}
32
-
33
-	if len(secrets) > 0 {
34
-		found := make(map[string]struct{})
35
-	next:
36
-		for _, term := range terms {
37
-			// attempt to lookup secret by full ID
38
-			for _, s := range secrets {
39
-				if s.ID == term {
40
-					found[s.ID] = struct{}{}
41
-					continue next
42
-				}
43
-			}
44
-			// attempt to lookup secret by full name
45
-			for _, s := range secrets {
46
-				if s.Spec.Annotations.Name == term {
47
-					found[s.ID] = struct{}{}
48
-					continue next
49
-				}
50
-			}
51
-			// attempt to lookup secret by partial ID (prefix)
52
-			// return error if more than one matches found (ambiguous)
53
-			n := 0
54
-			for _, s := range secrets {
55
-				if strings.HasPrefix(s.ID, term) {
56
-					found[s.ID] = struct{}{}
57
-					n++
58
-				}
59
-			}
60
-			if n > 1 {
61
-				return nil, fmt.Errorf("secret %s is ambiguous (%d matches found)", term, n)
62
-			}
63
-		}
64
-
65
-		// We already collected all the IDs found.
66
-		// Now we will remove duplicates by converting the map to slice
67
-		ids := []string{}
68
-		for id := range found {
69
-			ids = append(ids, id)
70
-		}
71
-
72
-		return ids, nil
73
-	}
74
-
75
-	return terms, nil
76
-}
... ...
@@ -11,10 +11,10 @@ import (
11 11
 	"github.com/docker/docker/api/types/swarm"
12 12
 	"github.com/docker/docker/cli"
13 13
 	"github.com/docker/docker/cli/command"
14
-	secretcli "github.com/docker/docker/cli/command/secret"
15 14
 	"github.com/docker/docker/cli/compose/convert"
16 15
 	"github.com/docker/docker/cli/compose/loader"
17 16
 	composetypes "github.com/docker/docker/cli/compose/types"
17
+	apiclient "github.com/docker/docker/client"
18 18
 	dockerclient "github.com/docker/docker/client"
19 19
 	"github.com/pkg/errors"
20 20
 	"github.com/spf13/cobra"
... ...
@@ -229,22 +229,18 @@ func createSecrets(
229 229
 	client := dockerCli.Client()
230 230
 
231 231
 	for _, secretSpec := range secrets {
232
-		// TODO: fix this after https://github.com/docker/docker/pull/29218
233
-		secrets, err := secretcli.GetSecretsByNameOrIDPrefixes(ctx, client, []string{secretSpec.Name})
234
-		switch {
235
-		case err != nil:
236
-			return err
237
-		case len(secrets) > 1:
238
-			return errors.Errorf("ambiguous secret name: %s", secretSpec.Name)
239
-		case len(secrets) == 0:
240
-			fmt.Fprintf(dockerCli.Out(), "Creating secret %s\n", secretSpec.Name)
241
-			_, err = client.SecretCreate(ctx, secretSpec)
242
-		default:
243
-			secret := secrets[0]
244
-			// Update secret to ensure that the local data hasn't changed
245
-			err = client.SecretUpdate(ctx, secret.ID, secret.Meta.Version, secretSpec)
246
-		}
247
-		if err != nil {
232
+		secret, _, err := client.SecretInspectWithRaw(ctx, secretSpec.Name)
233
+		if err == nil {
234
+			// secret already exists, then we update that
235
+			if err := client.SecretUpdate(ctx, secret.ID, secret.Meta.Version, secretSpec); err != nil {
236
+				return err
237
+			}
238
+		} else if apiclient.IsErrSecretNotFound(err) {
239
+			// secret does not exist, then we create a new one.
240
+			if _, err := client.SecretCreate(ctx, secretSpec); err != nil {
241
+				return err
242
+			}
243
+		} else {
248 244
 			return err
249 245
 		}
250 246
 	}