api: Remove SecretRequestOption type

This type is only used by CLI code. It duplicates SecretReference in the
types/swarm package. Change the CLI code to use that type instead.

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
This commit is contained in:
Aaron Lehmann 2017-03-16 10:54:18 -07:00 committed by Vincent Demeester
parent 2a4ce79981
commit 4cde08da8e
2 changed files with 35 additions and 39 deletions

View File

@ -8,12 +8,12 @@ import (
"strconv"
"strings"
"github.com/docker/docker/api/types"
swarmtypes "github.com/docker/docker/api/types/swarm"
)
// SecretOpt is a Value type for parsing secrets
type SecretOpt struct {
values []*types.SecretRequestOption
values []*swarmtypes.SecretReference
}
// Set a new secret value
@ -24,18 +24,18 @@ func (o *SecretOpt) Set(value string) error {
return err
}
options := &types.SecretRequestOption{
Source: "",
Target: "",
UID: "0",
GID: "0",
Mode: 0444,
options := &swarmtypes.SecretReference{
File: &swarmtypes.SecretReferenceFileTarget{
UID: "0",
GID: "0",
Mode: 0444,
},
}
// support a simple syntax of --secret foo
if len(fields) == 1 {
options.Source = fields[0]
options.Target = fields[0]
options.File.Name = fields[0]
options.SecretName = fields[0]
o.values = append(o.values, options)
return nil
}
@ -51,34 +51,30 @@ func (o *SecretOpt) Set(value string) error {
value := parts[1]
switch key {
case "source", "src":
options.Source = value
options.SecretName = value
case "target":
tDir, _ := filepath.Split(value)
if tDir != "" {
return fmt.Errorf("target must not be a path")
}
options.Target = value
options.File.Name = value
case "uid":
options.UID = value
options.File.UID = value
case "gid":
options.GID = value
options.File.GID = value
case "mode":
m, err := strconv.ParseUint(value, 0, 32)
if err != nil {
return fmt.Errorf("invalid mode specified: %v", err)
}
options.Mode = os.FileMode(m)
options.File.Mode = os.FileMode(m)
default:
if len(fields) == 1 && value == "" {
} else {
return fmt.Errorf("invalid field in secret request: %s", key)
}
return fmt.Errorf("invalid field in secret request: %s", key)
}
}
if options.Source == "" {
if options.SecretName == "" {
return fmt.Errorf("source is required")
}
@ -95,13 +91,13 @@ func (o *SecretOpt) Type() string {
func (o *SecretOpt) String() string {
secrets := []string{}
for _, secret := range o.values {
repr := fmt.Sprintf("%s -> %s", secret.Source, secret.Target)
repr := fmt.Sprintf("%s -> %s", secret.SecretName, secret.File.Name)
secrets = append(secrets, repr)
}
return strings.Join(secrets, ", ")
}
// Value returns the secret requests
func (o *SecretOpt) Value() []*types.SecretRequestOption {
func (o *SecretOpt) Value() []*swarmtypes.SecretReference {
return o.values
}

View File

@ -16,10 +16,10 @@ func TestSecretOptionsSimple(t *testing.T) {
reqs := opt.Value()
assert.Equal(t, len(reqs), 1)
req := reqs[0]
assert.Equal(t, req.Source, "app-secret")
assert.Equal(t, req.Target, "app-secret")
assert.Equal(t, req.UID, "0")
assert.Equal(t, req.GID, "0")
assert.Equal(t, req.SecretName, "app-secret")
assert.Equal(t, req.File.Name, "app-secret")
assert.Equal(t, req.File.UID, "0")
assert.Equal(t, req.File.GID, "0")
}
func TestSecretOptionsSourceTarget(t *testing.T) {
@ -31,8 +31,8 @@ func TestSecretOptionsSourceTarget(t *testing.T) {
reqs := opt.Value()
assert.Equal(t, len(reqs), 1)
req := reqs[0]
assert.Equal(t, req.Source, "foo")
assert.Equal(t, req.Target, "testing")
assert.Equal(t, req.SecretName, "foo")
assert.Equal(t, req.File.Name, "testing")
}
func TestSecretOptionsShorthand(t *testing.T) {
@ -44,7 +44,7 @@ func TestSecretOptionsShorthand(t *testing.T) {
reqs := opt.Value()
assert.Equal(t, len(reqs), 1)
req := reqs[0]
assert.Equal(t, req.Source, "foo")
assert.Equal(t, req.SecretName, "foo")
}
func TestSecretOptionsCustomUidGid(t *testing.T) {
@ -56,10 +56,10 @@ func TestSecretOptionsCustomUidGid(t *testing.T) {
reqs := opt.Value()
assert.Equal(t, len(reqs), 1)
req := reqs[0]
assert.Equal(t, req.Source, "foo")
assert.Equal(t, req.Target, "testing")
assert.Equal(t, req.UID, "1000")
assert.Equal(t, req.GID, "1001")
assert.Equal(t, req.SecretName, "foo")
assert.Equal(t, req.File.Name, "testing")
assert.Equal(t, req.File.UID, "1000")
assert.Equal(t, req.File.GID, "1001")
}
func TestSecretOptionsCustomMode(t *testing.T) {
@ -71,9 +71,9 @@ func TestSecretOptionsCustomMode(t *testing.T) {
reqs := opt.Value()
assert.Equal(t, len(reqs), 1)
req := reqs[0]
assert.Equal(t, req.Source, "foo")
assert.Equal(t, req.Target, "testing")
assert.Equal(t, req.UID, "1000")
assert.Equal(t, req.GID, "1001")
assert.Equal(t, req.Mode, os.FileMode(0444))
assert.Equal(t, req.SecretName, "foo")
assert.Equal(t, req.File.Name, "testing")
assert.Equal(t, req.File.UID, "1000")
assert.Equal(t, req.File.GID, "1001")
assert.Equal(t, req.File.Mode, os.FileMode(0444))
}