From 51b37a7983435edcd2331571bcb85b2686cbd006 Mon Sep 17 00:00:00 2001 From: allencloud Date: Sun, 29 Jan 2017 01:04:10 +0800 Subject: [PATCH] remove cli/command/secrets/utils.go Signed-off-by: allencloud --- cli/command/secret/utils.go | 76 ------------------------------------- cli/command/stack/deploy.go | 30 +++++++-------- 2 files changed, 13 insertions(+), 93 deletions(-) delete mode 100644 cli/command/secret/utils.go diff --git a/cli/command/secret/utils.go b/cli/command/secret/utils.go deleted file mode 100644 index 11d31ffd16..0000000000 --- a/cli/command/secret/utils.go +++ /dev/null @@ -1,76 +0,0 @@ -package secret - -import ( - "fmt" - "strings" - - "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" - "golang.org/x/net/context" -) - -// GetSecretsByNameOrIDPrefixes returns secrets given a list of ids or names -func GetSecretsByNameOrIDPrefixes(ctx context.Context, client client.APIClient, terms []string) ([]swarm.Secret, error) { - args := filters.NewArgs() - for _, n := range terms { - args.Add("names", n) - args.Add("id", n) - } - - return client.SecretList(ctx, types.SecretListOptions{ - Filters: args, - }) -} - -func getCliRequestedSecretIDs(ctx context.Context, client client.APIClient, terms []string) ([]string, error) { - secrets, err := GetSecretsByNameOrIDPrefixes(ctx, client, terms) - if err != nil { - return nil, err - } - - if len(secrets) > 0 { - found := make(map[string]struct{}) - next: - for _, term := range terms { - // attempt to lookup secret by full ID - for _, s := range secrets { - if s.ID == term { - found[s.ID] = struct{}{} - continue next - } - } - // attempt to lookup secret by full name - for _, s := range secrets { - if s.Spec.Annotations.Name == term { - found[s.ID] = struct{}{} - continue next - } - } - // attempt to lookup secret by partial ID (prefix) - // return error if more than one matches found (ambiguous) - n := 0 - for _, s := range secrets { - if strings.HasPrefix(s.ID, term) { - found[s.ID] = struct{}{} - n++ - } - } - if n > 1 { - return nil, fmt.Errorf("secret %s is ambiguous (%d matches found)", term, n) - } - } - - // We already collected all the IDs found. - // Now we will remove duplicates by converting the map to slice - ids := []string{} - for id := range found { - ids = append(ids, id) - } - - return ids, nil - } - - return terms, nil -} diff --git a/cli/command/stack/deploy.go b/cli/command/stack/deploy.go index 203ae6d39c..753b1503b1 100644 --- a/cli/command/stack/deploy.go +++ b/cli/command/stack/deploy.go @@ -11,10 +11,10 @@ import ( "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/cli" "github.com/docker/docker/cli/command" - secretcli "github.com/docker/docker/cli/command/secret" "github.com/docker/docker/cli/compose/convert" "github.com/docker/docker/cli/compose/loader" composetypes "github.com/docker/docker/cli/compose/types" + apiclient "github.com/docker/docker/client" dockerclient "github.com/docker/docker/client" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -229,22 +229,18 @@ func createSecrets( client := dockerCli.Client() for _, secretSpec := range secrets { - // TODO: fix this after https://github.com/docker/docker/pull/29218 - secrets, err := secretcli.GetSecretsByNameOrIDPrefixes(ctx, client, []string{secretSpec.Name}) - switch { - case err != nil: - return err - case len(secrets) > 1: - return errors.Errorf("ambiguous secret name: %s", secretSpec.Name) - case len(secrets) == 0: - fmt.Fprintf(dockerCli.Out(), "Creating secret %s\n", secretSpec.Name) - _, err = client.SecretCreate(ctx, secretSpec) - default: - secret := secrets[0] - // Update secret to ensure that the local data hasn't changed - err = client.SecretUpdate(ctx, secret.ID, secret.Meta.Version, secretSpec) - } - if err != nil { + secret, _, err := client.SecretInspectWithRaw(ctx, secretSpec.Name) + if err == nil { + // secret already exists, then we update that + if err := client.SecretUpdate(ctx, secret.ID, secret.Meta.Version, secretSpec); err != nil { + return err + } + } else if apiclient.IsErrSecretNotFound(err) { + // secret does not exist, then we create a new one. + if _, err := client.SecretCreate(ctx, secretSpec); err != nil { + return err + } + } else { return err } }