secrets: enable secret inspect and rm by secret name

Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
Evan Hazlett 2016-11-01 23:32:21 -04:00
parent 2adbdcdf5a
commit e0e65b9a3b
3 changed files with 62 additions and 4 deletions

View File

@ -34,9 +34,23 @@ func runSecretInspect(dockerCli *command.DockerCli, opts inspectOptions) error {
client := dockerCli.Client()
ctx := context.Background()
getRef := func(name string) (interface{}, []byte, error) {
return client.SecretInspectWithRaw(ctx, name)
// attempt to lookup secret by name
secrets, err := getSecrets(client, ctx, []string{opts.name})
if err != nil {
return err
}
return inspect.Inspect(dockerCli.Out(), []string{opts.name}, opts.format, getRef)
id := opts.name
for _, s := range secrets {
if s.Spec.Annotations.Name == opts.name {
id = s.ID
break
}
}
getRef := func(name string) (interface{}, []byte, error) {
return client.SecretInspectWithRaw(ctx, id)
}
return inspect.Inspect(dockerCli.Out(), []string{id}, opts.format, getRef)
}

View File

@ -31,7 +31,30 @@ func runSecretRemove(dockerCli *command.DockerCli, opts removeOptions) error {
client := dockerCli.Client()
ctx := context.Background()
for _, id := range opts.ids {
// attempt to lookup secret by name
secrets, err := getSecrets(client, ctx, opts.ids)
if err != nil {
return err
}
ids := opts.ids
names := make(map[string]int)
for _, id := range ids {
names[id] = 1
}
if len(secrets) > 0 {
ids = []string{}
for _, s := range secrets {
if _, ok := names[s.Spec.Annotations.Name]; ok {
ids = append(ids, s.ID)
}
}
}
for _, id := range ids {
if err := client.SecretRemove(ctx, id); err != nil {
return err
}

View File

@ -0,0 +1,21 @@
package secret
import (
"context"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
)
func getSecrets(client client.APIClient, ctx context.Context, names []string) ([]swarm.Secret, error) {
args := filters.NewArgs()
for _, n := range names {
args.Add("names", n)
}
return client.SecretList(ctx, types.SecretListOptions{
Filter: args,
})
}