1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
package secret
import (
"context"
"fmt"
"github.com/docker/swarmkit/api"
)
func getSecret(ctx context.Context, c api.ControlClient, input string) (*api.Secret, error) {
// not sure what it is, match by name or id prefix
resp, err := c.ListSecrets(ctx,
&api.ListSecretsRequest{
Filters: &api.ListSecretsRequest_Filters{
Names: []string{input},
IDPrefixes: []string{input},
},
},
)
if err != nil {
return nil, err
}
switch len(resp.Secrets) {
case 0:
return nil, fmt.Errorf("secret %s not found", input)
case 1:
return resp.Secrets[0], nil
default:
// ok, multiple matches. Prefer exact ID over exact name. If no exact matches, return an error
for _, s := range resp.Secrets {
if s.ID == input {
return s, nil
}
}
for _, s := range resp.Secrets {
if s.Spec.Annotations.Name == input {
return s, nil
}
}
return nil, fmt.Errorf("secret %s is ambiguous (%d matches found)", input, len(resp.Secrets))
}
}
|