Browse code

secrets: enable secret inspect and rm by secret name

Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>

Evan Hazlett authored on 2016/11/02 12:32:21
Showing 3 changed files
... ...
@@ -34,9 +34,23 @@ func runSecretInspect(dockerCli *command.DockerCli, opts inspectOptions) error {
34 34
 	client := dockerCli.Client()
35 35
 	ctx := context.Background()
36 36
 
37
+	// attempt to lookup secret by name
38
+	secrets, err := getSecrets(client, ctx, []string{opts.name})
39
+	if err != nil {
40
+		return err
41
+	}
42
+
43
+	id := opts.name
44
+	for _, s := range secrets {
45
+		if s.Spec.Annotations.Name == opts.name {
46
+			id = s.ID
47
+			break
48
+		}
49
+	}
50
+
37 51
 	getRef := func(name string) (interface{}, []byte, error) {
38
-		return client.SecretInspectWithRaw(ctx, name)
52
+		return client.SecretInspectWithRaw(ctx, id)
39 53
 	}
40 54
 
41
-	return inspect.Inspect(dockerCli.Out(), []string{opts.name}, opts.format, getRef)
55
+	return inspect.Inspect(dockerCli.Out(), []string{id}, opts.format, getRef)
42 56
 }
... ...
@@ -31,7 +31,30 @@ func runSecretRemove(dockerCli *command.DockerCli, opts removeOptions) error {
31 31
 	client := dockerCli.Client()
32 32
 	ctx := context.Background()
33 33
 
34
-	for _, id := range opts.ids {
34
+	// attempt to lookup secret by name
35
+	secrets, err := getSecrets(client, ctx, opts.ids)
36
+	if err != nil {
37
+		return err
38
+	}
39
+
40
+	ids := opts.ids
41
+
42
+	names := make(map[string]int)
43
+	for _, id := range ids {
44
+		names[id] = 1
45
+	}
46
+
47
+	if len(secrets) > 0 {
48
+		ids = []string{}
49
+
50
+		for _, s := range secrets {
51
+			if _, ok := names[s.Spec.Annotations.Name]; ok {
52
+				ids = append(ids, s.ID)
53
+			}
54
+		}
55
+	}
56
+
57
+	for _, id := range ids {
35 58
 		if err := client.SecretRemove(ctx, id); err != nil {
36 59
 			return err
37 60
 		}
38 61
new file mode 100644
... ...
@@ -0,0 +1,21 @@
0
+package secret
1
+
2
+import (
3
+	"context"
4
+
5
+	"github.com/docker/docker/api/types"
6
+	"github.com/docker/docker/api/types/filters"
7
+	"github.com/docker/docker/api/types/swarm"
8
+	"github.com/docker/docker/client"
9
+)
10
+
11
+func getSecrets(client client.APIClient, ctx context.Context, names []string) ([]swarm.Secret, error) {
12
+	args := filters.NewArgs()
13
+	for _, n := range names {
14
+		args.Add("names", n)
15
+	}
16
+
17
+	return client.SecretList(ctx, types.SecretListOptions{
18
+		Filter: args,
19
+	})
20
+}