Skip to content

Commit 70d2cef

Browse files
committed
update secret inspect to support IDs
This updates secret inspect to support inspect by ID in addition to name as well as inspecting multiple secrets. This also cleans up the help text for consistency. Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
1 parent 48c3df0 commit 70d2cef

File tree

4 files changed

+107
-40
lines changed

4 files changed

+107
-40
lines changed

cli/command/secret/inspect.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ import (
99
)
1010

1111
type inspectOptions struct {
12-
name string
12+
names []string
1313
format string
1414
}
1515

1616
func newSecretInspectCommand(dockerCli *command.DockerCli) *cobra.Command {
1717
opts := inspectOptions{}
1818
cmd := &cobra.Command{
19-
Use: "inspect [name]",
19+
Use: "inspect SECRET [SECRET]",
2020
Short: "Inspect a secret",
21-
Args: cli.ExactArgs(1),
21+
Args: cli.RequiresMinArgs(1),
2222
RunE: func(cmd *cobra.Command, args []string) error {
23-
opts.name = args[0]
23+
opts.names = args
2424
return runSecretInspect(dockerCli, opts)
2525
},
2626
}
@@ -33,23 +33,13 @@ func runSecretInspect(dockerCli *command.DockerCli, opts inspectOptions) error {
3333
client := dockerCli.Client()
3434
ctx := context.Background()
3535

36-
// attempt to lookup secret by name
37-
secrets, err := getSecretsByName(ctx, client, []string{opts.name})
36+
ids, err := getCliRequestedSecretIDs(ctx, client, opts.names)
3837
if err != nil {
3938
return err
4039
}
41-
42-
id := opts.name
43-
for _, s := range secrets {
44-
if s.Spec.Annotations.Name == opts.name {
45-
id = s.ID
46-
break
47-
}
48-
}
49-
50-
getRef := func(name string) (interface{}, []byte, error) {
40+
getRef := func(id string) (interface{}, []byte, error) {
5141
return client.SecretInspectWithRaw(ctx, id)
5242
}
5343

54-
return inspect.Inspect(dockerCli.Out(), []string{id}, opts.format, getRef)
44+
return inspect.Inspect(dockerCli.Out(), ids, opts.format, getRef)
5545
}

cli/command/secret/remove.go

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ import (
1010
)
1111

1212
type removeOptions struct {
13-
ids []string
13+
names []string
1414
}
1515

1616
func newSecretRemoveCommand(dockerCli *command.DockerCli) *cobra.Command {
1717
return &cobra.Command{
18-
Use: "rm [id]",
18+
Use: "rm SECRET [SECRET]",
1919
Short: "Remove a secret",
2020
Args: cli.RequiresMinArgs(1),
2121
RunE: func(cmd *cobra.Command, args []string) error {
2222
opts := removeOptions{
23-
ids: args,
23+
names: args,
2424
}
2525
return runSecretRemove(dockerCli, opts)
2626
},
@@ -31,32 +31,14 @@ func runSecretRemove(dockerCli *command.DockerCli, opts removeOptions) error {
3131
client := dockerCli.Client()
3232
ctx := context.Background()
3333

34-
// attempt to lookup secret by name
35-
secrets, err := getSecretsByName(ctx, client, opts.ids)
34+
ids, err := getCliRequestedSecretIDs(ctx, client, opts.names)
3635
if err != nil {
3736
return err
3837
}
3938

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-
5739
for _, id := range ids {
5840
if err := client.SecretRemove(ctx, id); err != nil {
59-
return err
41+
fmt.Fprintf(dockerCli.Out(), "WARN: %s\n", err)
6042
}
6143

6244
fmt.Fprintln(dockerCli.Out(), id)

cli/command/secret/utils.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,30 @@ func getSecretsByName(ctx context.Context, client client.APIClient, names []stri
1818
Filters: args,
1919
})
2020
}
21+
22+
func getCliRequestedSecretIDs(ctx context.Context, client client.APIClient, names []string) ([]string, error) {
23+
ids := names
24+
25+
// attempt to lookup secret by name
26+
secrets, err := getSecretsByName(ctx, client, ids)
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
lookup := make(map[string]struct{})
32+
for _, id := range ids {
33+
lookup[id] = struct{}{}
34+
}
35+
36+
if len(secrets) > 0 {
37+
ids = []string{}
38+
39+
for _, s := range secrets {
40+
if _, ok := lookup[s.Spec.Annotations.Name]; ok {
41+
ids = append(ids, s.ID)
42+
}
43+
}
44+
}
45+
46+
return ids, nil
47+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// +build !windows
2+
3+
package main
4+
5+
import (
6+
"encoding/json"
7+
8+
"github.com/docker/docker/api/types/swarm"
9+
"github.com/docker/docker/pkg/integration/checker"
10+
"github.com/go-check/check"
11+
)
12+
13+
func (s *DockerSwarmSuite) TestSecretInspect(c *check.C) {
14+
d := s.AddDaemon(c, true, true)
15+
16+
testName := "test_secret"
17+
id := d.createSecret(c, swarm.SecretSpec{
18+
swarm.Annotations{
19+
Name: testName,
20+
},
21+
[]byte("TESTINGDATA"),
22+
})
23+
c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("secrets: %s", id))
24+
25+
secret := d.getSecret(c, id)
26+
c.Assert(secret.Spec.Name, checker.Equals, testName)
27+
28+
out, err := d.Cmd("secret", "inspect", testName)
29+
c.Assert(err, checker.IsNil, check.Commentf(out))
30+
31+
var secrets []swarm.Secret
32+
c.Assert(json.Unmarshal([]byte(out), &secrets), checker.IsNil)
33+
c.Assert(secrets, checker.HasLen, 1)
34+
}
35+
36+
func (s *DockerSwarmSuite) TestSecretInspectMultiple(c *check.C) {
37+
d := s.AddDaemon(c, true, true)
38+
39+
testNames := []string{
40+
"test0",
41+
"test1",
42+
}
43+
for _, n := range testNames {
44+
id := d.createSecret(c, swarm.SecretSpec{
45+
swarm.Annotations{
46+
Name: n,
47+
},
48+
[]byte("TESTINGDATA"),
49+
})
50+
c.Assert(id, checker.Not(checker.Equals), "", check.Commentf("secrets: %s", id))
51+
52+
secret := d.getSecret(c, id)
53+
c.Assert(secret.Spec.Name, checker.Equals, n)
54+
55+
}
56+
57+
args := []string{
58+
"secret",
59+
"inspect",
60+
}
61+
args = append(args, testNames...)
62+
out, err := d.Cmd(args...)
63+
c.Assert(err, checker.IsNil, check.Commentf(out))
64+
65+
var secrets []swarm.Secret
66+
c.Assert(json.Unmarshal([]byte(out), &secrets), checker.IsNil)
67+
c.Assert(secrets, checker.HasLen, 2)
68+
}

0 commit comments

Comments
 (0)